I have wrote in C# .NET 6.0 my version of “TinyCompile” after reading
https://github.com/jamiebuilds/the-super-tiny-compiler,
This sample is written in JS and contains the following parts:
- Tokeniser (written clear & gracefully)
- Parser
- Transformer - (creation new ast)
- Code Generator
The basic idea here is that to create a “visitor” object that has methods that will accept different node types.
My version
https://github.com/okogosov/PPL/blob/main/TinyCompiler.zip
contains the following:
1.Tokenizer (same the-super-tiny-compiler (STC), I saved with STC commentary even)
2. Parser, I create ast with Composer pattern
3. Traversal
The goal of my version is to calculate result and pass it to other operation.
I do not use as usual visitor pattern, I create keywords dictionary, with delegates per each keyword. So I can add to this dictionary entries during run-time.
I do not use also the node CallExpression from STC version. It is possible to set CallExpressionFlag = false | true in call constructor,
PrefixNotation(bool CallExpressionFlag).
Without this node Traversal works faster.
The note about my Traversal – each node gets data in children_results stack from its children, so at the end it is possible to pass result from ast.children_results to other operation.
As well I added operators ‘+’ (as add) and ‘-’(as subtract), each pair with same delegate.
When I run Traversal not problem to create new ast, as in STC.
I hope my code is understable enough and it is easy to support it.
My PPL interpreter https:
https://github.com/okogosov/PPL
uses similar mechanism, added with recognition commentaries, etc. PPL component class includes additional services –
saving tree in format .ppl and .json,
reading tree in .ppl,
cloning nodes, etc…