GithubHelp home page GithubHelp logo

lox's Introduction

Lox

Another implementation of Lox programming language from the awesome book craftinginterpreters by Bob Nystrom.

Lox is a dynamically typed programming language, its syntax is a member of the C family.

Data types

  • boolean true; false;
  • numbers 19 // integer 3.1415 // decimal
  • strings "hello"
  • nil

Expressions

Arithmetic

  • a + b
  • a - b
  • a * b
  • a / b
  • - negateMe

Comparison and equality

  • a < b
  • a <= b
  • a > b
  • a >= b
  • a == b
  • 123 == 123 // true
  • "123" == 123 // false

Logical operator

  • and
  • or
  • !

Statements

  • print "hakuna matata"
  • { print "a block of statements"; print "hakuna matata btw"; }

Variables

  • var name = "Clark";
  • var lastname; // nil

Control Flow

if (somVariable == 5) {
  print "it's five!";
} else {
  print "it's not five.";
}
var a = 0;
while (a < 5) {
  print a;
  a = a + 1;
}
for (var i=0; i < 5; i = i +1) {
  print i;
}

Functions

fun makeBreakfastFor(name) {
  print "Breakfast for " + name; 
} // it returns nil

fun sumPlease(a, b) {
  return a + b;
}
// functions are first-class in Lox
fun doSomeMath(a, b, op) {
  return op(a, b);
}
doSomeMath(sumPlease, 5, 6);

Lox also supports closures, local functions and block scope.

Classes

class Breakfast {
  init(name) {
    this.name = name;
  }
  cook() {
    print "Enjoy your breakfast " + this.name;
  }
}

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

var johnBr = Breakfast("John");
johnBr.cook(); // "Enjoy your breakfast John"

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.