An implementation of Bob Nystrom's Lox programming language from Crafting Interpreters.
Lox is a dynamic, high-level scripting language.
The examples directory has a few basic example programs in Lox with equivalent Python programs for comparison.
Run go build
to build golox
.
Run ./golox
without any arguments to enter the REPL.
Run ./golox <filename>.lox
to run a lox file.
- Pretty-print parsed program
- Pretty-print local variables after a command
- Support raw keyboard mode
- up and down arrow keys to go to previous commands
- allow newlines for multiline REPL programs
- Comments:
// this is a line comment
- Data Types
- Numbers (represented by float64):
1.535
,32
, etc. - Booleans:
true
,false
- Strings:
"this is a string"
- Nil:
nil
- (Extension) Lists
- Numbers (represented by float64):
- Expressions
- Arithmetic
- Addition:
18.9 + 16.3
- Subtraction:
18.9 - 16.3
- Multiplication:
18.9 * 16.3
- Division:
18.9 / 16.3
- Negation:
-18.9
,-(10 * 2)
- Addition:
- Comparison & Equality
- Less Than:
18.9 < 16.3
isfalse
- Less Than or Equal:
18.9 <= 16.3
isfalse
- Greater Than:
18.9 > 16.3
istrue
- Greater Than or Equal:
18.9 >= 16.3
istrue
- Less Than:
- Logical Operators
- Not:
!false
- And:
true and false
isfalse
- Or:
true or false
istrue
- Not:
- Precedence and Grouping:
(2 + 3 * 4) / 2
is7
- String Concatenation:
"hey" + " " + "there"
is"hey there"
- (Extension) Lists Concatenation
- Arithmetic
- Statements
- Print Statements:
print "hello"; print 1.84; print x;
- Expression Statements:
"hello"; 1.84; x;
- If/Else Statements:
if (10 > 5) { x = 10; } if (x > 12) { x = 12; } else { x = x + 1; }
- While Loops:
while (x < 12) { x = x + 1; }
- Variable Declarations:
var x = 103; var foo123 = "hello";
- Variable Assignments:
x = 103; foo123 = "hello";
- Block Statements:
var y = 11; { var x = 10 + y; print x; } // x doesn't exist outside of the block scope
- Function Declaration & Calls:
- Close over outer-scoped variables
- Recursion
- Return Statements exit scope and return value
var foo = 15; fun myFunc(x, y, z) { if (myFunc(x+1, y-1, z) > 10) { return 10; } return foo + x * y * z; } print myFunc(1, 4, 2);
- Classes
- Class Declaration & Instantiation:
- Class Methods and Properties
class BaseClass { sayHi() { print "Hi!"; } } class Foo < BaseClass { init(meat) { this.meat = meat; } cook() { print "Eggs and " + this.meat + " cooking!"; } serve(customer) {print "Here's your order, " + customer;} } // Instantiate and call method var foo = Foo("bacon"); foo.serve("Billy"); // Set arbitrary property value foo.whateverProperty = "Look, a new property!"; // Call inherited method from base class foo.sayHi();
- Print Statements:
- Standard Library
- Lists
- Custom Garbage Collector (currently piggybacking on Go's GC)
- Compile to bytecode or machine code instead of interpreting AST
- Nothing done yet, but I'm interested in learning to use LLVMjit to make the language fast and efficient.