This is a Java implementation of Thorsten Ball's Monkey Programming Language from his great book Writing An Interpreter in Go.
- Tokenizer
- Lexer
- Parser
- AST
- Builtin Functions
- if/else
- Arrays & Maps
- Function Literals
- Closures
- REPL ([Read Eval Print Loop](Read Eval Print Loop))
5 * 5 + 10;
3 + 4 * 5 == 3 * 1 + 4 * 5;
5 * 10 > 40 + 5;
((10 + 2) * 30) == (300 + 20 * 3);
(10 + 2) * 30 == 300 + 20 * 3;
500 / 2 != 250;
puts(true == true);
puts(false == true);
if (5 * 5 + 10 > 34) { puts(99) } else { puts(100) }
let a = 5;
let b = a > 3;
let c = a * 99;
if (b) { 10 } else { 1 };
let d = if (c > a) { 99 } else { 100 };
d * c * a;
You can also see how function literals can be used.
let hello = "Hello there, fellow Monkey users and fans!"
let giveMeHello = fn() { "Hello!" }
giveMeHello();
let myArray = ["one", "two", "three"];
len(myArray);
first(myArray);
rest(myArray);
last(myArray);
push(myArray, "four")
let myHash = {"name": "Jimmy", "age": 72, "band": "Led Zeppelin"};
myHash["name"];
myHash["age"];
myHash["band"];
let two = "two";
let h = { "one": 10 - 9, two: 1 + 1, "thr" + "ee": 6 / 2, 4: 4, true: 5, false: 6};
h["one"];
let addTwo = fn(x) { x + 2; };
addTwo(2);
let multiply = fn(x, y) { x * y };
multiply(50 / 2, 1 * 2);
fn(x) { x == 10 }(5);
false;
fn(x) { x == 10 }(10);
true;
- Java, this project uses the JDK 16.
- Maven.
To build the .jar file, just type:
$ make
or you could just run:
$ mvn clean package
To run the REPL (Read Evaluate Print Loop):
make run
or:
java --enable-preview -jar target/monkeyj-jar-with-dependencies.jar
There are a few code examples of the language in the examples directory. To run them, you can use the following command:
$ java --enable-preview -jar target/monkeyj-jar-with-dependencies.jar < examples/strings/concat1.mnk
This code was brought to you by Leo Gutiérrez in his free time. If you want to thank me and support the development of this project, please make a small donation on PayPal. In case you also like my other open source contributions and articles, please consider motivating me by becoming a sponsor/patron on Patreon. Thank you! ❤️