Git Product home page Git Product logo

toy's Introduction

toy

This is a tiny interpreter (with a bytecode compiler and a stack-based virtual machine) for a lilliputian subset of JavaScript, written in C and... JavaScript itself. It fits in less than 2500 lines of code.

The parser and the bytecode generator (in compile.js) are written in Toy-compatible JavaScript. It was just too painful to write those in C. Since you can run them with Node.js or Toy itself, they self-compile (into compiler_code.c) and are magically bundled inside the toy executable.

Interesting features

  • Closures
  • Compiles itself (see above)
  • Little mark-and-sweep garbage collector

Unsupported JavaScript features

  • ES201{5,6,7}
  • Functions with many arguments (use lists, dictionnaries or curryfication instead)
  • Prototypes
  • Exceptions
  • undefined (seriously, who needs undefined? null is sufficient)
  • Optional semicolons
  • Booleans (use 0 and 1, or anything else truthy or falsy)
  • for, switch, do
  • else (just write if statements. Thousands of if statements.)
  • Regexps

Compile and run

You need Node.js, a C compiler and GNU make. Just run make and try the examples.

Various observations

First of all, because I hate naming things Γ  la JavaScript:

  • an object, a hash map or a hash table is dictionnary
  • an array is a list

For the sake of simplicity, the whole thing is really slow.

The dictionnary implementation is a shame.

Lists are implemented with those dictionnaries. This is really straightforward since we need dictionnaries and JavaScript lists behaves mostly the same. It’s obviously slow.

Since the garbage collector does not visit the stack, each object has a reference counter which prevents it from being collected if that counter is nonzero. Moreover, the GC must not run at any time, but only between two instructions (see the call to request_garbage_collection(); in vm.c).

Variables are compiled as-is. The VM interprets scoping rules and catches variable definition errors at run-time. The compiler is really straightforward.

I just hope you are not crazy enough to use this hack in production.

Why?

It was fun. Enjoy πŸ˜‰

toy's People

Contributors

motet-a avatar pierre-emmanuelj 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.