Skip to content
/ cs-lox Public

A C# implementation of a Lox interpreter (adapted from jlox, credit to "Crafting Interpreters" by Robert Nystrom)

License

Notifications You must be signed in to change notification settings

zxubian/cs-lox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cs-lox

A C# implementation of a Lox interpreter (adapted from jlox, credit to "Crafting Interpreters" (book, repo) by Robert Nystrom)

Language Tweaks

  • Language supports nill equality comparison
var a = nil;
var b = nil;
print a == b;
>> True
var c = "not nil";
print a == c;
>> False
  • Static methods are supported:
class Greeter{
  class SayHello(){
      print "Hello, I am a static method."
  }
}
Greeter.SayHello();
>> Hello, I am a static method.
var a = Greeter();
a.SayHello();
>> Hello, I am a static method.
  • "Getter" propeties are supported:
class Person{
  init(name, surname){
      this.name = name;
      this.surname = surname;
  }
  fullname{
      return this.name + " " + this.surname;
  }
}

var john = Person("John", "Smith");
print john.fullname;
>> John Smith

Furthermore, a compile-error is thrown if the getter never returns a value.

  • Other features: ternary conditional (a ? b : c), comma operator support, breaking out of loops, C-style block comments ( /*...*/ )

Implementation Notes:

Statements and State

  • jlox uses the Void type for its Statement Visitor:
class Interpreter implements Expr.Visitor<Object>,
                            Stmt.Visitor<Void> {

 void interpret(Expr expression) { 

C#, however, does not allow this, so I instead opted to use a Unit return type (I used a handy one I saw in UniRx).

Functions

About

A C# implementation of a Lox interpreter (adapted from jlox, credit to "Crafting Interpreters" by Robert Nystrom)

Topics

Resources

License

Stars

Watchers

Forks

Languages