Code Monkey home page Code Monkey logo

glox's Introduction

Glox

A Go implementaion of the tree walk interpreter for the Lox language. I wrote this version while reading the book Crafting Interpreters by Robert Nystrom.

Documentation

Installation

Download the latest binary from the Releases page as per your platform. Unzip the package, you'll have the glox binary. Running only ./glox will give you the interactive terminal or you can pass in the location of a glox script file to run a script e.g. ./glox hello.glox where hello.glox contains the glox script in the same directory as the glox binary.

Examples

Hello world:

// Your very first lox program.
print "Hello, world";

Variable declaration

var a = 5;
var b = 6;

var c = a + b;
print c; // prints 11

Loops

for (var i = 0; i < 5; i = i+1) {
  print i;
}

// prints 
// 0
// 1
// 2
// 3
// 4

Conditionals

var a = 5;
if (a >= 4) {
  print "a is greater than or equal to 4";
} else {
  print "a is less than 4";
}

Functions

fun printSum(a, b) {
  print a + b;
}

printSum(5, 6); // prints 11

// Closure
fun returnFunction() {
  var outside = "outside";

  fun inner() {
    print outside;
  }

  return inner;
}

var fn = returnFunction();
fn(); // prints outside

Classes

class Breakfast {
  // constructor
  init(meat, bread) {
    this.meat = meat;
    this.bread = bread;
  }

  cook() {
    print "Eggs a-fryin'!";
  }

  serve(who) {
    print "Enjoy your " + this.meat + " and " +
        this.bread + ", " + who + ".";
  }
}

// instantiate
var breakfast = Breakfast("sausage", "toast");
breakfast.serve("Sayantan"); // prints "Enjoy your sausage and toast, Sayantan".

Inheritence

class Brunch < Breakfast {
  init(meat, bread, drink) {
    super.init(meat, bread);
    this.drink = drink;
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.