资源说明:Programming Environment for Simplified JavaScript
.. image:: http://turtlescript.github.cscott.net/images/hello-world.png
:align: right
Project Goals
-------------
The TurtleScript experiment attempts to provide a simple logo-like
programming environment which is based on a "real" programming
language. It draws heavy inspiration from Etoys_, Scratch_, Snap_,
Elements_, TileScript_, `Turtle Art`_, and `Open Blocks`_. As a
browser-based programming environment, it builds on ideas from the
`Lively Kernel`_ and `Lively Qt`_. The `TileScript paper`_ describes
our own motivations well, although we've chosen a slightly different
path.
The ultimate goal is to construct a Sugar_-like activity environment with
a pervasive and scalable `View Source`_ capability. Any visible item
can be interrogated and almost all source code viewed, modified,
and saved for later use in an interactive fashion. The choice of
JavaScript is intended to leverage modern browser technology, which
already provides an advanced runtime environment and sandbox. It is
also a commercially-important programming language, which is (sadly)
important to many vocationally-minded educators.
I've also been recently (re)inspired by the Smalltalk community, and
in particular by Ian Piumarta's work on the small self-describing
systems cola_ and maru_. The SELF bytecode format described in
Chambers et al's `OOPSLA 89`_ paper was a further incentive to
investigate minimal executable representations. The hope is that
a few powerful optimizations can be employed against a tiny set of
fundamental operations to create a compact and high-performance
environment which is truly "`turtles all the way down`_".
.. _Etoys: http://wiki.laptop.org/go/Etoys
.. _Scratch: http://scratch.mit.edu/
.. _Snap: http://snap.berkeley.edu/
.. _Elements: https://web.archive.org/web/20140127205214/http://www.chirp.scratchr.org/blog/?p=24
.. _TileScript: http://tinlizzie.org/jstile/
.. _TileScript paper: http://tinlizzie.org/jstile/#TileScript
.. _Turtle Art: http://wiki.laptop.org/go/Turtle_Art
.. _Open Blocks: https://web.archive.org/web/20130823011046/http://education.mit.edu/openblocks
.. _Lively Kernel: http://www.lively-kernel.org/index.html
.. _Lively Qt: http://lively.cs.tut.fi/qt/
.. _Sugar: http://wiki.laptop.org/go/Sugar
.. _View Source: http://wiki.laptop.org/go/View_Source
.. _cola: https://web.archive.org/web/20160612023705/http://piumarta.com/software/cola/
.. _maru: https://web.archive.org/web/20190207024931/http://piumarta.com/software/maru/
.. _OOPSLA 89: https://web.archive.org/web/20130807083351/http://selflanguage.org/documentation/published/implementation.html
.. _turtles all the way down: http://en.wikipedia.org/wiki/Turtles_all_the_way_down
.. image:: https://travis-ci.org/cscott/TurtleScript.png
:target: https://travis-ci.org/cscott/TurtleScript
:align: right
:alt:
Warnings and Caveats
--------------------
This README is a description of a work-in-progress.
It is likely to get increasingly outdated over time. I'll attempt to
periodically overhaul it to match reality, but it may always contain
historical fragments, false starts, and loose ends.
State of the world: 2020-02-12
------------------------------
I dusted off this project and wrote TurtleScript interpreters for
PHP (`php-turtle`_) and Lua (`lua-turtle`_). I also added a new
``push_local_frame`` bytecode operation to allow "register allocation"
of local variables which do not escape their scope, and improved
the standard library implementation.
I expect to use these runtimes to explore two (interrelated) projects:
`Multilingual JavaScript`_ and the use of JavaScript within
the MediaWiki Scribunto_ extension.
.. _php-turtle: http://github.com/cscott/php-turtle
.. _lua-turtle: http://github.com/cscott/lua-turtle
.. _Multilingual JavaScript: https://phabricator.wikimedia.org/T230665
.. _Scribunto: https://www.mediawiki.org/wiki/Extension:Scribunto
State of the world: 2013-06-03
------------------------------
I recently wrote a "native" interpreter for TurtleScript in Mozilla's
Rust_ programming language. The source code
for that project can be found at
http://github.com/cscott/rusty-turtle.
In the process I improved the REPL_ for TurtleScript, built out the
standard library a bit, fixed bugs, and added node_ compatibility
and a test suite.
More recently I've begun to think about a
TurtleScript-to-`asm.js`_-to-`LLVM`_
compiler, which would let me investigate object layout and performance
optimizations without resorting to Rust, C, or another low-level
language. (Unfortunately ``asm.js`` is not a proper subset of
TurtleScript at present, since TurtleScript doesn't have the ``switch``
statements or bitwise operators. This gap can be mended.)
Accordingly, `asm-llvm.js`_ is the beginnings of an ``asm.js``-to-LLVM
compiler, written in TurtleScript.
I've used this codebase to write an `asm.js verification service`_.
There's also a set of parser speed `benchmarks`_.
.. _Rust: http://www.rust-lang.org
.. _REPL: http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop
.. _node: http://nodejs.org
.. _asm.js: http://asmjs.org
.. _LLVM: http://llvm.org/
.. _asm-llvm.js: http://turtlescript.github.cscott.net/docco/asm-llvm.html
.. _asm.js verification service: http://turtlescript.github.cscott.net/asmjs.html
.. _benchmarks: http://turtlescript.github.cscott.net/benchmark/
State of the world: 2011-05-19
------------------------------
The source code for the project is hosted at
http://github.com/cscott/TurtleScript.
It begins with a parser for the "Simplified JavaScript" of
`Douglas Crockford`_ in `parse.js`_. I've extended the language very
slightly: it now supports block comments and '$' in identifiers, and
represents blocks in the parse tree in a more uniform fashion. I've
also hoisted all variable declarations to the top of a block, to more
accurately reflect their scope. Some further improvements are
discussed in `Interesting Parser Tasks`_, but the base language is not
expected to change much more.
.. figure:: http://turtlescript.github.cscott.net/images/compile.png
:alt: Bytecode output
:align: right
:target: tdop.html_
Simple bytecode compiler/interpreter, 2011
There are a few backends which process the parsed text. The first to
be implemented (`jcompile.js`_, July 2010) simply emitted JavaScript
from the parse tree which can be ``eval``'ed by the browser's standard
JavaScript environment. At the moment, this isn't very interesting
|---| but it allows us to modify the parsed language in various ways
and still emit ECMA-standard JavaScript which can take advantage of
browsers' highly-tuned JavaScript implementations. Some possible
extensions are described in the `Interesting Compiler Tasks`_ section.
In May 2011 I wrote a simple bytecode compiler/interpreter for the
language, inspired by Piumarta's maru_ system. You can test the
parser and the bytecode compiler and interpreter using `tdop.html`_,
which is a highly modified version of Douglas Crockford's original `parser
demonstration`_. The bytecode instruction set is simple, but not
simple enough; `Simplifying the Environment`_ discusses improvements.
.. figure:: http://turtlescript.github.cscott.net/images/tiles1.png
:alt: 2010 graphical tiles
:align: right
:target: tile2.html_
Original CSS experiments, 2010
Back in 2010 I implemented some simple tile-based renderers for the parse
tree. These used jQuery_ to render the tree as CSS-styled HTML.
A CSS-styling demo is at `tile2.html`_ and `tiles.html`_ displays
"editable" interactive source using `jQuery UI`_. I was dissatisfied
with these results. The markup process was very slow, and rendering
into HTML/CSS added a lot of additional difficulty and complexity.
The promise of clean semantic HTML for the program source was not
fulfilled: the actual generated HTML needed to be exceedingly crufty
in order to get the rendering close to what I wanted. The interaction
model also failed to satisfy: jQuery UI had a lot of problems with
horizontal layouts, and the real time re-layout during drag operations
made the display stutter unacceptably.
.. figure:: http://turtlescript.github.cscott.net/images/tiles2.png
:alt: 2011 graphical tiles
:align: right
:target: ctiles.html_
Tile Rendering using