Parser is a small interpreter written in C#. It tokenizes source code, builds an abstract syntax tree, and evaluates the resulting program. The project is intended as a compact, readable implementation of a simple expression language.
The project targets .NET 10. Install the .NET 10 SDK, then build the solution from the repository root:
dotnet buildUse the interpret command with the path to a source file:
dotnet run --project TheParser -- interpret examples/hello.parserAdd --debug to print the lexer tokens and abstract syntax tree before execution:
dotnet run --project TheParser -- interpret examples/arithmetic.parser --debugFor the command-line help:
dotnet run --project TheParser -- help
dotnet run --project TheParser -- help interpretEach statement ends with a semicolon. Whitespace can appear between tokens.
- Numbers are non-negative integers, such as
42. - Strings use double quotes.
\ninserts a newline inside a string. - Arithmetic supports
+,-,*, and/; multiplication and division bind more tightly than addition and subtraction. - Unary
+and-are supported, as are parenthesized expressions. +also concatenates strings and printable values.
Declare a variable with @, optionally supplying an initializer:
@total = 21 * 2;
@empty;
Variable names currently contain letters only.
Print(value)writes one value to standard output.Ask()reads one line from standard input and returns it as a string.ConvertToNumber(string)converts a numeric string to a number.
For example:
@name = Ask();
Print("Hello, " + name + "!\n");
The examples directory contains runnable programs that demonstrate the language features:
dotnet run --project TheParser -- interpret examples/hello.parser
dotnet run --project TheParser -- interpret examples/arithmetic.parser
dotnet run --project TheParser -- interpret examples/variables.parser
dotnet run --project TheParser -- interpret examples/input.parserinput.parser waits for a line of input. The other examples run without interaction.