This document contains the technical specification of the Buttercup programming language.
In the following sections, a letter is any character from the English alphabet from lowercase to uppercase A
to Z
. A digit is any character from 0
to 9
. In general, spaces, tabulators, and newlines are used as delimiters between lexical units, but are otherwise ignored.
Buttercup only has line comments: they start with the character ;
(semicolon) and conclude at the end of the line.
An identifier is a sequence of one or more letters. Uppercase and lowercase letters are considered different. Identifiers can be of any length.
An integer literal is a sequence of one or more digits. Literals are in base ten. Their range is from 0 to 2^{31} – 1.
Literal booleans are #t
and #f
.
The monospace
elements represent terminal symbols (tokens).
‹program›  →  ‹declaration›* ‹statement›* 
‹declaration›  →  ‹type› ‹identifier› 
‹type›  →  int  bool 
‹statement›  →  ‹assignment›  ‹print›  ‹condition› 
‹assignment›  →  ‹identifier› = ‹expression› 
‹print›  →  print ‹expression› 
‹condition›  →  if ‹expression› then ‹statement›* end 
‹expression›  →  ‹expression› ‹operator› ‹simple_expression›
 ‹simple_expression› 
‹operator›  →  &  <  +  * 
‹simple_expression›  →  ‹identifier›  ‹integer›  ‹boolean›  ( ‹expression› )   ‹simple_expression› 
Statements are executed sequentially starting from the first one.
An integer literal is of type int
.
#t
and #f
literals are of type bool
.
Statement  Description 

var = exp

Place in var the result of evaluating exp. var must be previously declared. var and exp must have the same type. 
print exp

Evaluates exp and prints the result to the standard output. 
if exp then statements... end

Evaluates exp, which must be of type bool . If the result is #t , executes
statements..., otherwise has no further effect.

Operator  Name  Type  Notes  

op_{1}  op_{2}  result  
op_{1} & op_{2}

And 
bool

bool

bool

Returns the logical conjunction of op_{1} and
op_{2}. That is, returns #t only if op_{1} and op_{2} are #t , otherwise returns #f .

op_{1} < op_{2}

Less than 
int

int

bool

Returns #t if op_{1} is less than op_{2}, otherwise returns #f .

op_{1} + op_{2}

Add 
int

int

int

Returns the addition of op_{1} plus op_{2}. Generates an exception if the result is out of bounds. 
op_{1} * op_{2}

Multiply 
int

int

int

Returns the multiplication of op_{1} times op_{2}. Generates an exception if the result is out of bounds. 
 op_{1}

Negation 
int

N/A 
int

Returns the negation (reversing the sign) of op_{1}. Generates an exception if the result is out of bounds. 