-
Notifications
You must be signed in to change notification settings - Fork 5
LRM Expressions
Everything in JSJS is an expression. Accordingly, an entire JSJS program can be defined as a list of expressions.
program:
| expr_list EOF { $1 }
expr_list:
| exprs = nonempty_list(delimited_expr) { exprs }
All these expressions are composed of identifiers, operators, literals and function calls. Following is an exhaustive list of different types of expressions in JSJS:
type expr =
| Binop of expr * op * expr
| Unop of op * expr
| NumLit of float
| BoolLit of bool
| StrLit of string
| MapLit of (expr * expr) list
| ListLit of expr list
| Assign of string * primitiveType * expr
| Val of string
| Block of expr list
| If of expr * expr * expr
| Call of string * expr list
| FunLit of func_decl
| ModuleLit of string * expr
and
func_decl = {
formals : (string * primitiveType) list;
return_type : primitiveType;
body : expr;
};;
A block is a list of several expressions enclosed in curly braces. The entire block is executed in the order in which expressions appear and the value of the last expression is returned. In JSJS, blocks are encountered inside of if-then-else statements and function definitions.
block:
| LBRACE expr_list RBRACE { $2 }
expr_list:
| exprs = nonempty_list(delimited_expr) { exprs }
delimited_expr:
| expr SEMICOLON { $1 }
if-then-else behaves similarly to the standard control flow constructs of programming languages. Following is the grammar definition of an if-then-else statement.
Grammar:
| if expr then block else block { If($2, Block($4), Block($6)) }
One important point to keep in mind is that the return type of then and else blocks should be same. Otherwise the compiler will throw a type mismatch error.
Example:
if x > y
then { print(x); x; }
else { print(y); y; }
The curly braces in then and else blocks are required only if the blocks consist of more than one expression but are optional otherwise. They are accordingly handled in the grammar as well.
JSJS © 2016