An open API service indexing awesome lists of open source software.

https://github.com/georgesittas/ipl-interpreter

Interpreter for a simple imperative language called IPL
https://github.com/georgesittas/ipl-interpreter

c crafting-interpreters imperative interpreter language

Last synced: about 1 year ago
JSON representation

Interpreter for a simple imperative language called IPL

Awesome Lists containing this project

README

          

# IPL Interpreter

IPL is a simple imperative language created for educational purposes in the [Introduction to Programming course](http://cgi.di.uoa.gr/~ip/). This
implementation uses some of the techniques described in the (amazing!) book [Crafting Interpreters](https://craftinginterpreters.com/).

## Usage

```Bash
# Compile the project
make

# Cleanup
make clean
```

## Specification

### Types

The language only supports integers and arrays of integers.

### Variables

Variables don't need to be declared before their use and they are implicitly initialized to `0` on their first use. Variable names
can contain at most 100 characters and they must start with a letter, followed by any number of letters, numbers or underscores.

### Constants

Only non-negative constants are allowed; one can produce negative values by subtracting from 0.

### Input

The built-in command `read ` reads an integer value into ``, which can be either a variable or an array element.

### Output

The built-in commands `write ` and `writeln ` output the integer value ``: a constant, variable or array element.
The former outputs a trailing space, while the latter a newline. In case these are used without an argument, a single space or newline
character will be printed.

### Arithmetic Expressions

An arithmetic expression can contain at most two operands that can be either constants, variables or array elements.
The supported operators are `+`, `-`, `/`, `*` and `%`, having the same semantics as in C.

### Assignment

Integer values can be assigned to a variable or to an array element using the assignment statement: ` = `.
Here, ` can be either a constant, a variable, an array element or an arithmetic expression.

### Conditions

Conditions follow the same format as the arithmetic expressions, but they can only be used in control-flow constructs like
if-else and while statements. The supported operators are `==`, `!=`, `<=`, `<`, `>=` and `>`, having the same semantics as in C.

### While loop

This is the only statement that can be used for creating loops in IPL:

```c
while


....
```

The language is indentation-sensitive, so tabs define blocks.

### Branching

Similar to the while loop statement, IPL provides an if-else statement (else clause is optional):

```c
if


....
else


....
```

### Random Numbers

The built-in command `random ` generates a random integer and stores it in ``.

### Comments

A comment in IPL starts with the # character and ends when a newline character is found.

### Command Line Arguments

The built-in command `argument size ` stores the number of command line arguments in ``. The input
file is not counted as an argument. The built-in command `argument ` stores an integer argument in
``, where `` is either a constant, a variable or an array element and represents the argument index.
If the index is out-of-bounds, a runtime error is raised.

### Break and Continue

The built-in commands `break ` and `continue ` have the same semantics as break and continue in C when ` = 1`.
In all other cases, they jump `` loops, where `` is a positive integer. Below is an example:

```c
while a < 5
a = a + 1
write a
if a == 3
break
while b < 20
b = b + 1
write b
c = b % 5
if c == 0
continue 2
writeln
```

`Output: 1 1 2 3 4 5 2 6 7 8 9 10 3`

### Arrays

Integer arrays can be created as `new []`, where `` is a non zero constant, variable or array
element representing the new array's dimension. Arrays and variables must have different names. All uninitialized
array elements are implicitly initialized to 0. The array's memory can be collected with the free statement, using
`free `. An array element reference works just like in C: `[]`, where `` can be either a
constant, a variable or an array element and an out-of-bounds index raises a runtime error. The built-in command
`size ` stores the size of the array referred to by `` in ``.