Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ndreynolds/flathead
A toy JavaScript interpreter written in C
https://github.com/ndreynolds/flathead
Last synced: about 2 months ago
JSON representation
A toy JavaScript interpreter written in C
- Host: GitHub
- URL: https://github.com/ndreynolds/flathead
- Owner: ndreynolds
- License: mit
- Created: 2012-08-15T05:51:18.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2023-12-12T02:33:16.000Z (about 1 year ago)
- Last Synced: 2024-08-03T18:13:27.828Z (5 months ago)
- Language: C
- Size: 915 KB
- Stars: 90
- Watchers: 11
- Forks: 16
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- AwesomeInterpreter - flathead
README
Flathead
========
Flathead is a tiny, portable JavaScript (ECMA-262) interpreter written in C.[![Build Status](https://travis-ci.org/ndreynolds/flathead.png?branch=master)](https://travis-ci.org/ndreynolds/flathead)
![Flathead's REPL](doc/screenshot.png)
The interpreter does a direct evaluation of the parse tree—it does not
currently build any further IR or perform any optimizations. As a result, it
starts up very quickly, and performs well on code that wouldn't benefit much
from optimization, and less well on code that would (e.g. loops).Flathead builds on Linux, OSX and \*BSD, on x86, x86_64 and ARM architectures.
Flathead comes with the full EcmaScript runtime (i.e. the Date, Math, Array
and other global objects) as well as a console object.Most of the language is now implemented, you can see the remaining
work to be done on [the Docket](#the-docket).Installing
----------
Download or clone the source and run `make` within.git clone https://github.com/ndreynolds/flathead.git
cd flathead
makeThis creates an executable at `bin/flat` within the source directory. (You can
optionally run `make install` to copy this to `/usr/local/bin/`.)#### Dependencies
If you received any errors, you may just be missing dependencies.
`flex` and `bison` are required to generate the lexer and parser.
The default build depends on GNU Readline (`lreadline`) for the REPL, and PCRE
(`lpcre`) for the regular expression implementation. They're not required,
however, for a minimal build. Run `make readline=off regexp=off` to compile
without them.If you're still having trouble building, please create an issue.
Running
-------
Once built, you can run `bin/flat` without arguments to start a REPL:$ bin/flat
> 2 + 2
4
>Or with a script as argument:
$ bin/flat say_hello.js
Hello!View the parse tree with `-n`:
$ bin/flat -n
> 2 + 2
source list
expression statement
expression (binary)
+
number (2.000000)
number (2.000000)
4
>See all the options with `-h`:
$ bin/flat -h
Usage: flat [options] [script.js]Options:
-v, --version print version info
-h, --help print this help text
-i, --interactive force REPL
-n, --nodes print the AST
-t, --tokens print tokensRunning the tests
-----------------
Flathead's test suite can be run against the `bin/flat` executable, as well as
other EcmaScript implementations (assuming you have them installed).The test runner itself requires Node.js. It locates files with names beginning
in `test_` and executes them with the configured implementation and options.
There are a few Node module dependencies, so you'll need to run `npm install`
before you can run the tests.The Makefile has a few shortcuts:
`make test` to run with Flathead's `bin/flat` executable.
`make test-v8` to run using `v8`.
`make test-node` to run using `node`.
`make test-sm` to run using `js` (SpiderMonkey).
`make test-rhino` to run using `rhino`.There's also:
`make test-grammar` to verify parsing and AST formation
The Docket
----------
- With statement (`with`)
- Labels (e.g. `loop1: ...; continue loop1;`)
- Automatic Semicolon Insertion
- Unicode
- The JSON object ([JSON-js][1] can be used as polyfill)
- URI functions
- `String#replace`: replacement function
- `String#split`: RegExp separators[1]: http://github.com/douglascrockford/JSON-js