Garden is a small, garden-variety language to demonstrate how to implement variables and functions. (Note, this is only one way of implementing these language features. Another good resource is the book Language Implementation Patterns).
Garden supports expressions, printing, sequencing, conditionals, variable declarations, assignment, function definitions, and function calls.
The abstract syntax for Garden is:
n ∈ ℤ x,f ∈ Name
s ∈ Stmt ::= print e | s ; s
| if0 (e) then {s} else {s}
| var x := e | x := e
| def f(x) := {s} | f(e)
e ∈ Expr ::= n | x | e op e | ( e )
op ∈ Operator ::= + | - | * | /
The language includes a formal semantics, specified as comments in a Prolog file. The file also includes a Prolog implementation of the semantics.
The bulk of this repository contains a Scala implementation of Garden. You can start a read-eval-print loop for Garden by executing
sbt run
from the top-level directory (i.e., the one that contains this file).
You can run the included test suite by executing
sbt test
from the top-level directory (i.e., the one that contains this file).
The Scala implementation adds some syntactic sugar to the language:
- It supports blocks of statements (i.e., sequencing of an arbitrary number of statements).
- It supports functions with multiple parameters
The repository has several tags, which correspond to different versions of Garden, as it grows from the expression language to its full syntax. The tags are
Expressions: numbers and arithmetic operationsNames: adds named constantsStatements1: addsprintand support for statement sequencing (blocks)Statements2: adds conditionalsVariables: adds variables, i.e., mutable stateFunctions1: adds function definitions and function calls, but the implementation doesn't provide scopesFunctions2: adds support for scopes, but they're dynamicFunctions3: adds support for static (lexical) scopes