# SPL

### TL;DR

SPL is a very simple structured programming language designed to be used in exercises for the compiler construction course at the University of Bern (Spring 2023). It is also used as a case study for PetitParser. See PetitParser SPL case study.

## Language features

SPL is a minimal language supporting only simple simple expressions, variable declarations, `if` and `while` control statements, blocks, and print statements.

There is no input to programs, aside from variable declarations, and no functions, so no run-time stack.

SPL supports as basic data types Booleans, strings and numbers (integers and floats).

Expressions include basic arithmetic operators, comparison, logical and and or, and Boolean and integer negation.

## Example program

In this example we can see comments, variable declarations, initialization, arithmetic and Boolean expressions, a `while` statement, and a `print` statement.

```        ```
// Factorial in SPL -- computes the factorial of arg
var arg=5;
var x=arg;
var fact=1;
while (x>0) {
fact = fact * x;
x = x - 1;
}
print fact;
```
```

## Grammar

What follows is a complete specification of the grammar of SPL (but without the specification of comments and tokens).

```        ```
program        := declaration* EOF ;
declaration    := varDecl
| statement ;
varDecl        := "var" IDENTIFIER ( "=" expression )? ";" ;
statement      := exprStmt
| ifStmt
| printStmt
| whileStmt
| block ;
exprStmt       := expression ";" ;
ifStmt         := "if" "(" expression ")" statement ( "else" statement )? ;
printStmt      := "print" expression ";" ;
whileStmt      := "while" "(" expression ")" statement ;
block          := "{" declaration* "}" ;
expression     := assignment ;
assignment     := IDENTIFIER "=" assignment
| logic_or ;
logic_or       := logic_and ( "or" logic_and )* ;
logic_and      := equality ( "and" equality )* ;
equality       := comparison ( ( "!=" | "==" ) comparison )* ;
comparison     := term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term           := factor ( ( "-" | "+" ) factor )* ;
factor         := unary ( ( "/" | "*" ) unary )* ;
unary          := ( "!" | "-" ) unary
| primary ;
primary        := "true" | "false" | NUMBER | STRING
| "(" expression ")"
| IDENTIFIER ;
```
```

## Semantics

SPL programs take no input aside from the program source, and produce only the output from the use of `print` statements.

Some aspects are left unspecified, for example:

- Is there a flat environment of variables, or do blocks introduce nested environments?

- What is the value of a variable that is declared, but not initialized or assigned?

- What should happen on error? (Access to undeclared or uninitialized variable. Type rrors in expressions. Non-terminating programs.)

- Should erroneous programs be rejected, aborted at run time, or continue executing?

For the interpreter, we will assume (1) a flat environment of variables, (2) declared variables initially have a `nil` value, (3) errors are detected at run time, and raise a run-timer error in the interpreter.