CKTIS / C#
Program architecture: a DSL parser, AST, and interpreter
Separate a small domain-specific language's syntax from its execution. A parser constructs an abstract syntax tree, and an AST interpreter computes the result from its nodes. This separation lets you test text processing, program representation, and execution rules independently.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
DSL text → parser → abstract syntax tree (AST) → AST interpreter → resultProgram architecture defines the responsibilities of components and the data flow between them. For a small domain-specific language (DSL) describing a narrow set of operations, use a pipeline: text → parser → abstract syntax tree (AST) → AST interpreter → result. This is a design pattern, not a special C# feature.
The parser checks syntax and constructs AST nodes rather than executing the command while reading it. A separate lexer can extract tokens first; splitting on spaces is sufficient here. An AST preserves the structure of operations, not necessarily the complete source text. Do not equate it with Roslyn's full-fidelity tree, which also preserves comments and whitespace.
The example recognizes commands of the form add <int> <int>. Parse splits on space characters, discards empty tokens, and expects exactly three remaining tokens. The first must be lowercase add, and the other two must parse as Int32 values using Int32.TryParse with NumberStyles.AllowLeadingSign and CultureInfo.InvariantCulture. A leading sign is optional, and Int32.TryParse ignores trailing NUL characters (U+0000) in numeric tokens, so a digit followed by a NUL is also accepted. Parse constructs an Add containing two Number nodes. Evaluate, the AST interpreter, recursively computes node values; it does not generate machine code or IL. The input add 2 3 produces 5.
Separating these stages allows independent parser and interpreter tests. Parse throws FormatException if there are not exactly three tokens, the first token is not add, or either numeric token fails to parse. Addition outside the Int32 range causes OverflowException because of checked. This is deliberately a minimal language without nested expressions. When extending it, add diagnostics with source positions and limits on input length and tree depth; separating stages does not itself make untrusted programs safe to execute.
Examples
using System;
using System.Globalization;
Expr ast = Parse("add 2 3");
Console.WriteLine(Evaluate(ast).ToString(CultureInfo.InvariantCulture));
static Expr Parse(string text)
{
string[] tokens = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length != 3 || tokens[0] != "add" ||
!int.TryParse(tokens[1], NumberStyles.AllowLeadingSign,
CultureInfo.InvariantCulture, out int left) ||
!int.TryParse(tokens[2], NumberStyles.AllowLeadingSign,
CultureInfo.InvariantCulture, out int right))
throw new FormatException("Expected: add <int> <int>");
return new Add(new Number(left), new Number(right));
}
static int Evaluate(Expr expr) => expr switch
{
Number n => n.Value,
Add a => checked(Evaluate(a.Left) + Evaluate(a.Right)),
_ => throw new InvalidOperationException("Unknown AST node.")
};
abstract record Expr;
sealed record Number(int Value) : Expr;
sealed record Add(Expr Left, Expr Right) : Expr;Result
5
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.