Skip to content

candrewlee14/golox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoLox

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.

Usage

Run go build to build golox.

Run ./golox without any arguments to enter the REPL.

Run ./golox <filename>.lox to run a lox file.

Features

REPL

  • 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

Interpreter

  • 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
  • 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)
    • Comparison & Equality
      • Less Than: 18.9 < 16.3 is false
      • Less Than or Equal: 18.9 <= 16.3 is false
      • Greater Than: 18.9 > 16.3 is true
      • Greater Than or Equal: 18.9 >= 16.3 is true
    • Logical Operators
      • Not: !false
      • And: true and false is false
      • Or: true or false is true
    • Precedence and Grouping: (2 + 3 * 4) / 2 is 7
    • String Concatenation: "hey" + " " + "there" is "hey there"
    • (Extension) Lists Concatenation
  • 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();
      

Extensions

  • 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.

Useful Resources

  1. Crafting Interpreters
  2. Writing An Interpreter In Go
  3. LLVM Usage For Creating a Programming Language
  4. LLVM C API Tutorial
  5. LLVM Library for Go

About

An implementation of the lox language in Go

Resources

Stars

Watchers

Forks

Packages

No packages published