Ask your beloved programming literate anything.
>>51045113>What are you working on /g/?A small lisp implementation written in Crystal to explore the language. for now, it's composed of
- A scanner that splits the string into words
eg:
source = "(define a b (+ 4 22 bar 7.9)"
scan(souce)
returns
['(', 'define', 'a', 'b', '(', '+', '4', '22', 'bar', '7.9', ')']
- A tokenizer that recognizes valid words and converts them into values ("44" → 44).
eg:
tokenize(scan(source))
returns
[Token(ParenthesisOpen),
Token(Symbol, define),
Token(Symbol, a),
Token(Symbol, b),
Token(ParenthesisOpen),
Token(Symbol, +),
Token(Integer, 4),
Token(Integer, 22),
Token(Symbol, bar),
Token(Real, 7.9),
Token(ParenthesisClose)]
Note that "define" should have its own token type because it's a special form. I am doing scanning and tokenization in the same pass for practical reasons.
- A parser that converts a valid sequence of tokens into an expression. All symbols are replaced with an unique ID for faster comparison and look up in a values table.
eg:
parse(tokenize(scan(source)))
returns
//main scope
[ // start of an expression
LispSymbol(@id=0), // define
LispSymbol(@id=1), // a
LispSymbol(@id=2), // b
[ // start of an expression
LispSymbol(@id=3), // +
4,
22,
LispSymbol(@id=4), // bar
7.9
]
]
I will now write the evaluator, nothing magic if you have read sicp (chapter 4). Gambatte to myself.