Moby: the Moby Scheme Compiler

1What is Moby?

Moby is a project from the PLT Scheme team. The Moby compiler
consumes Advanced Student Language (ASL) programs that use World
primitives, and produces applications for mobile platforms. The
current prototype supports desktop browsers and smartphones.
Our long-term goal is to make Scheme the premiere reactive scripting
language for mobile phones.

will invoke a web browser, which should show the
running program on a web page. Because on-tick is used,
as every second passes, the runtime sends a tick stimulus to the program.
The page should also provide links to download packages
of the compiled program.

These programs run on the user’s web browser; they can also dynamically
generate DOM trees and style them with CSS, as in the examples below.

The following example renders the world as a paragraph of text, styled
with a font-size of 30. It uses draw-html and draw-css
to draw the web page.

The next example shows an image and an input text field. As with the
previous example, it uses draw-html and draw-css to
construct the web page, and every time the world changes, the runtime environment
reacts by re-drawing the web page.

One of the main handlers to js-big-bang is on-draw, which controls how
the world is rendered on screen. The first argument computes a
rendering of the world as a DOM tree, and the second argument computes
that tree’s styling.

Each of the dom-elements can take in an optional attribute list to
assign to the new dom element; the common useful attribute is a key-value binding for an "id",
which can be used to identify an element in the css-drawing function.

3.3Stimulus Handlers

Stimulus handlers are provided as additional arguments to a js-big-bang.

Each stimulus has an effect-less and an effect-full version; the effect-full
version allows you to provide an effect-generating function as well as a world-updater. When
the given stimulus emits, the old world is used to compute both
the new world and the optional effect. Afterwards, each effect in the
effect group is applied.

Maps a function f across all the key/value pairs in a-hash. The
order of the traversal is not defined.

4Developer details

The compiler takes a ASL program and translates it to Javascript code.
Moby reimplements the ASL primitives in a Javascript runtime library
that’s included with the compiled application. (See
doc/moby-developer-api.txt for more details.)

To support smartphones, Moby uses a bridge library called Phonegap, which
provides access to the native facilities of several cell phones. In
this way, Moby should be able to support multiple platforms with a lot
of code reuse. Moby handles the other libraries (tilt, location, sms,
music), though with support only for the Android platforms for now.

4.1Dependencies

Moby is mostly written in PLT Scheme, and the project sources are
hosted by github.com. To develop with Moby, you will need the
following:

Also, do a setup-plt -l moby so that PLT Scheme compiles the
Moby source code.

2. If you’re going to do Android development, make sure that
ant and the android binary are in your path; Moby will use
your PATH variable to find Apache Ant and the Android SDK.

You can verify that android and ant can be found with the following:

$ which android

/usr/local/android/tools/android

$ which ant

/usr/bin/ant

The path values you get back may differ according to your
system’s configuration.

4.3Running Moby from the command line

You can run the Moby command line utility ("moby/src/moby.ss") on a Moby
program to produce a compiled version of your application. Moby
supports two output backends, both which rely on Javascript:

js: compiles to a web page application, which can be deployed on
any web server.

js+android-phonegap: compiles to an Android .apk application
package; can also use features of the mobile platform.

By default, the command line utility will use the js backend.

Let’s run moby on the falling-ball.ss example in
"moby/examples/falling-ball.ss". We can first generate a web program.

$ cd moby/examples

$ mred ../src/moby.ss falling-ball.ss

$ cd FallingBall/

$ ls

index.htmlmain.jsruntimetest

If you open up index.html from your web browser, you should see a red
ball falling down the screen.

Furthermore, we may want to generate an Android application.

$ cd moby/examples

$ mred ../src/moby.ss -t js+android-phonegap falling-ball.ss

$ cd FallingBall

$ ls

AndroidManifest.xmlbuild.propertiesgenres

assetsbuild.xmllibssrc

bindefault.propertieslocal.propertiestests

$ ls bin

classesclasses.dexDroidGap.ap_DroidGap-debug.apk

"DroidGap-debug.apk" is the compiled Android binary. The Ant "build.xml"
build-script in the "FallingBall" directory can install, uninstall, and
reinstall the application if the Android emulator is online.

$ ant install

Buildfile: build.xml

[some output cut]

install:

[echo] Installing bin/DroidGap-debug.apk onto default emulator...

[exec] 1594 KB/s (120997 bytes in 0.074s)

03:38 I/ddms: Created: [Debugger 8610-->1641 inactive]

03:38 I/ddms: Good handshake from client, sending HELO to 1641

[exec]pkg: /data/local/tmp/DroidGap-debug.apk

[exec] Success

03:39 I/ddms: Closing [Client pid: 1641]

BUILD SUCCESSFUL

Total time: 6 seconds

After this, you can look at the Android emulator, which should now
have the "FallingBall" application installed.

4.4Compiler

"src/compiler/env.ss": maintains the environment structures that map
identifiers to bindings.

"src/compiler/permission.ss": defines a list of capabilities that a function
can be tagged with.

"src/compiler/toplevel.ss": maps the primitive toplevel names available in
the base language.

"src/compiler/modules.ss": adds a few extensions to the toplevel language,
including the reactive world primitives.

"src/compiler/pinfo.ss": maintains program information used to analyze a
program and figure out what functions are used and what
capabilities are needed.

"src/compiler/desugar.ss": applies syntactic transformations on programs
to a core form.

"src/compiler/helpers.ss": auxillary helper functions.

The compiler is intentionally written in a small superset of the language
("src/compiler/lang.ss"). As a consequence, it is self hosting, and we take
advantage of this to produce a running compiler on the browser.
("support/js/test/test-repl.html")

If we want to embed the evaluation of this program in a web page, we
can use the two strings above to do so. For convenience, we provide a
helper function compiled-program-main that ties both the definitions
and expression evaluation together.

4.5Runtime

The Javascript program that’s emitted depends on a runtime kernel
that’s currently implemented in Javascript. See the files in
"support/js/runtime".