Translate SMoL to other programming languages. Translation to JavaScript is ready. See the SMoL vs JavaScript section for limitations and known incompatibilities between the languages. Translation to Python is WIP. See the SMoL vs JavaScript section for limitations and known incompatibilities between the languages.
Most people translate full programs. If this is your case, you can say
import { toJS } from SMoLTranslator;
toJS("program", smolSourceCode)
Replace JS
with PY
if you want Python-like syntax.
The translator supports more than program-level translation. Shown below is a description of all supported context:
program
: accept any number of terms; every term ends with;
for some languages; expressions are wrapped inconsole.log(_)
orprint(_)
, except for assignment expressions.function-body
: accept one or more terms, last of which must be an expression; every term ends with;
for some languages; the last expression is wrapped inreturn _
.one-term
: accept exactly one term; no;
is added.many-terms
: accept any number of terms; no;
is added.
This translator has been tested with more than 80% programs from the SMoL Tutor. 154 were tested. 31 were skipped for various reasons:
- (8 skipped) Programs from the heap tutorial. This tutorial is all about heap structure, so expected answers are NOT program outputs.
- (20 skipped) Programs from the local tutorial. This tutorial is all about local binding forms, which doesn't apply to many languages.
- (2 skipped) Programs where the expected output involve
@
. These programs are, again, testing heap structures. - (1 skipped) Programs where the expected output involve
=
. These programs output circular data structures. It is difficult to translate the outputs.
Every SMoL program can be translated to JavaScript. The translation is mostly straightforward,
except that let
expressions (in generally) must be turned into Immediately Invoked Function Expressions.
Program outputs might differ slightly after the translation due to the following language differences:
- In JavaScript, division by zero produces
Infinity
or a number rather than an error. - Variable mutation (e.g.,
x = 2
) produces the new value (in this case,2
) rather than a none/void/unit value. - Indexing an array (known as "vector" in SMoL) outside its index range (e.g.,
[1, 2][99]
) produces theundefined
value rather than an error.
All 24 failure (out of 154 tests) are due to the aforementioned reasons.
Many SMoL program can be translated to idiomatic Python. However, we find it difficult to translate the following language constructs:
- multi-term
lambda
expressions: Pythonlambda
must contain exactly one term; and the term must be an expression. let
expressions: Python provides nothing similar enough tolet
expressions; we also can't translatelet
to function application as we did for JavaScript because the aforementioned limitation of Pythonlambda
.