Giter VIP home page Giter VIP logo

clox's Introduction

Clox

Build Status

Lox (clox) is an object-oriented scripting language developed alongside the reading of craftinginterpreters (craftinginterpreters.com) by Bob Nystrom.

I've followed along, but added my own features as well.

Added features

  • Multi-threading with libpthread and a global VM lock. Threads can run concurrently for IO and other blocking operations, but cannot run concurrently in the VM itself (like cpython and cruby).
  • try/catch exception handling
  • Splatted (rest) parameters and arguments
  • Map (hash/dict) literals
  • Regular expressions
  • Default arguments
  • Keyword arguments
  • Mutable strings
  • Modules similar to Ruby's
  • Blocks similar to Ruby's
  • Iterators and foreach() statement
  • Integrated REPL
  • String interpolation
  • Multi-process support using fork() syscall
  • Signal handling: registering signal handlers, sending signals
  • Small standard library
  • Object finalizers

Some internal differences with the book

  • creation of AST before compilation phase (separate parser/compiler)
  • bytecode optimization passes (including constant folding)
  • Generational M&S garbage collector with managed heaps

Future features

  • Standard library networking support
  • Support string methods that work on utf8 codepoints
  • Add constants (no redefinitions, will given compiler or runtime error)
  • See TODO for more info
  • Add JIT compiler, either method or tracing

OS/compiler support

  • Only tested on linux (Ubuntu 16.04) and mac OS, uses fork() and libpthread, as well as some C99 features
  • Tested with gcc and clang
  • Almost C++ compliant (g++ compiles, clang++ doesn't)

Examples

Here's an example from the standard library:

class Benchmark {
  class start(name: "Benchmark", iterations: 10) {
    var times = [];
    print "Running ${iterations} iterations";
    for (var i = 0; i < iterations; i+=1) {
      var t1 = Timer();
      yield();
      var t2 = Timer();
      times << (t2 - t1);
    }
    this.report(name, iterations, times);
  }

  class report(name, iters, times) {
    print "============";
    print name;
    print "============";
    print "Iterations: ${iters}";
    var sum = 0;
    for (var i = 0; i < times.size(); i+=1) {
      sum += times[i].seconds();
    }
    var avg = sum/times.size();
    print "Average iteration (s): ${avg}";
  }
}

// Usage:

fun bench() {
  for (var i = 0; i < 100000; i+=1) {
    [1,2,3,4,5] << 1;
  }
}

Benchmark.start(name: "array", iterations: 1, &bench);

See the examples folder for more examples, and the lib folder for the standard library. The files in the examples folder are also tests run by the ./bin/test_examples binary (see Makefile for more details).

Thanks

Thanks to Bob Nystrom for the book and github repo!

clox's People

Contributors

luke-gru avatar

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.