Skip to content
/ lox Public

Lox programming language interpreters in Haxe and C

Notifications You must be signed in to change notification settings

VPagani/lox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lox Programming Language

A dynamic toy programming language implemented twice from scratch in Haxe (./src/lox-hx) and C (./src/lox-c) based on the book Crafting Interpreters by Robert Nystrom

Examples

Hello World

print "hello world";

Variables and Expressions

var a = 1;
print a = 2; // Prints "2"

a = 5;
var b = 6;
print a = a + b; // Prints "11"

var b = " foo";
print a = 4 + a + b; // "15 foo"

Functions

fun factorial(n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

print factorial(8); // Prints "40320"

Classes

class CoffeeMaker {
  init(coffee) {
    this.coffee = coffee;
  }

  brew() {
    print "Enjoy your cup of " + this.coffee;

    // No reusing the grounds!
    this.coffee = nil;
  }
}

var maker = CoffeeMaker("coffee and chicory");
maker.brew(); // Prints "Enjoy your cup of coffee and chicory"

Inheritance

class Doughnut {
  cook() {
    print "Fry until golden brown.";
  }
}

class BostonCream < Doughnut {
  cook() {
    super.cook();
    print "Pipe full of custard and coat with chocolate.";
  }
}

BostonCream().cook();

About

Lox programming language interpreters in Haxe and C

Resources

Stars

Watchers

Forks