GithubHelp home page GithubHelp logo

luke-gru / clox Goto Github PK

View Code? Open in Web Editor NEW
33.0 5.0 3.0 1.35 MB

Scripting language developed alongside reading of craftinginterpreters

Makefile 0.44% C 94.12% C++ 5.19% Ruby 0.25%
language compiler virtual-machine scripting-language

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

clox's Issues

Classes allow addition / redefinition of properties outside the class. Intentional?

c lox seems to allow code to adding properties to an instance on the fly, and even overwrite existing functions.

here's an example code that shows some of the issues. Is this behavior intentional?

class Person {
	init() {
	   this.name = "John";
	   this.lname = "Smith";
	}
	fullname {
	   return this.name + " " + this.lname;
	}
}

p1 = Person();

print("a: " + p1.name);
print("b: " + p1.lname);
print("c: " + p1.fullname);

//seemingly overriding fullname method / property
//this should fail (Person does not implment fullname=(_))
//instead, it seem to just overwrite the method definition for fullname
p1.fullname = "Lemar Wilson";
print("d: " +p1.fullname);

//but if we call the method as a function, then it returns the expected string
print("e: " + p1.fullname());

//creating a new property outside of the class
p1.haircolor = "red";
print("f: " + p1.haircolor);

output:

% ./clox -f pers.lox
a: John
b: Smith
c: John Smith
d: Lemar Wilson
e: John Smith
f: red

Terminate all non-joined threads in freeVM()

Currently non-joined threads are ignored in freeVM(), and we just don't free the GVL if there's more than 1 non-joined thread. That's not a good solution, all threads should be terminated through interrupts, and waited for to exit by the main thread. Also, all mutexes held by those threads, if any, should be unlocked before the interrupt is given.

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.