This project is a rewrite of the code from Writing An Interpreter In Go in Rust.
Writing an interpreter is fun and challenging, at the same time I thought it would be a good project to practice Rust.
And many people have already implemented monkeylang in other languages. And I got a lot of help from their code. https://monkeylang.org/
- Library
- Lexer
- Parser
- Evaluator
- Console
- Web
The library is composed of Lexer, Parser, and Evaluator.
Lexical element of input text
Read a source code and extract tokens from the source code. Another name of lexer is tokenizer
Convert tokens into Abstract Syntax Tree. You will see that the methods in the parser object return Statements or Expressions.
Scan through AST and generate values from it.
Programs in Monkey are a series of statements.
In monkey language expressions produce values, statements don’t.
let <identifier> = <expression>;
A let statement in Monkey consists of two changing parts: an identifier and an expression.
Exampe:
let foobar = 5
return <expression>;
Example:
return add(x+y);
it’s a statement that consists solely of one expression
Example:
x + 10;
Block statements are a series of statements enclosed by an opening { and a closing }.
Example:
{ true }
foo * bar / foobar
foo and bar are identifier
<number>
Example:
5
Another example integer literal as the parameters of a function
add(5, 10);
<boolean>
boolan literal as the expression in a let statement
let foobar = true;
-, !
Examples:
-5
!true
+, -, /, %, *, ==, !=, <=, >=
Example:
5 + 5
if (<condition>) <consequence> else <alternative>
Example:
if (10 > 5) { true } else { false };
fn <parameters> <block statement>
For example, here is a function literal as the expression in a let statement:
``let add = fn(x, y) { return x + y };``
And here is a function literal as the expression in a return statement inside another function literal:
fn() {
return fn(x, y) { return x > y; };
}
Using a function literal as an argument when calling another function is also possible:
myFunc(x, y, fn(x, y) { return x > y; });
add(2, 3)
<sequence of characters>
"any string"
[comma seperated list of expressions]
Example:
[1,2,"3",add(4,5)]
{comma seperated list of key-value pairs}
Example:
{"name": "Jimmy", "age": 72, "band": "Led Zeppelin"};
let arr = [1,2,3,4];
first(arr);
output: 1
let arr = [1,2,3,4];
last(arr);
output: 4
let arr = [1,2,3,4];
rest(arr);
output: [2,3,4]
let arr = [1,2,3,4];
len(arr);
output: 4
let arr = [1,2,3,4];
push(arr,5);
output: [1,2,3,4,5]
let arr = [1,2,3,4];
puts(arr);
output: [1,2,3,4]