https://github.com/davidedc/lw-toy-imperative-language
an interpreter (in Java) for the LW didactic imperative language, sometimes used to teach the fundamentals of parsing, static checking, execution of imperative languages.
https://github.com/davidedc/lw-toy-imperative-language
Last synced: about 2 months ago
JSON representation
an interpreter (in Java) for the LW didactic imperative language, sometimes used to teach the fundamentals of parsing, static checking, execution of imperative languages.
- Host: GitHub
- URL: https://github.com/davidedc/lw-toy-imperative-language
- Owner: davidedc
- Created: 2013-04-01T21:05:42.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2013-05-06T20:20:18.000Z (about 13 years ago)
- Last Synced: 2025-03-05T23:45:07.387Z (over 1 year ago)
- Language: Java
- Size: 117 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
A small interpreter for the didactic (toy) LW imperative language.
======
LW is a minimal imperative language used in some universities to teach the basics of imperative languages, and the concepts of static analysis, basic parsing, handling scope, what a state is, the difference between an expression and a command, and how to walk an AST tree to do static checks and to run the program.
I did this compact LW (and its older brother, LWPlus) interpreter at uni in 1998. Funnily enough, I couldn't find recent references to LW, so actually I had to reverse engineer the semantics of the language (and some examples) reading my very old undocumented code.
The language supports one type only (ints), it supports C-like block scoping rules and while loops. Programs read data from a file, and print out the calculated output. There is no support function definition / invocation.
There are two neat things about this: how compact the whole thing is (4 pages of code), and that the grammar for the language is actually not baked in the interpreter. Rather, it's read at runtime from a grammar file which is almost in BNF form, so one could tweak that a little, I thought that was quite cool, at the time :-). The semantics of the static checks and execution are baked in the interpreter though.
To build the interpreter:
```
javac LWInterpreter.java
```
Usage:
```
java LW grammarFileName progSourceName maxMem dataFile
```
To run the examples program:
```
java LW LW.grammar examples/LW/minimal.lw 20 examples/data/nodata.data
java LW LW.grammar examples/LW/basic.lw 20 examples/data/zero.data
java LW LW.grammar examples/LW/factorial.lw 20 examples/data/one.data
java LW LW.grammar examples/LW/factorial.lw 20 examples/data/two.data
java LW LW.grammar examples/LW/factorial.lw 20 examples/data/three.data
java LW LW.grammar examples/LW/factorial.lw 20 examples/data/four.data
java LW LWPlus.grammar examples/LWPlus/minimal.lwplus 20 examples/data/nodata.data
java LW LWPlus.grammar examples/LWPlus/basic.lwplus 20 examples/data/nodata.data
java LW LWPlus.grammar examples/LWPlus/plus.lwplus 20 examples/data/one-one.data
java LW LWPlus.grammar examples/LWPlus/factorial.lwplus 20 examples/data/one.data
java LW LWPlus.grammar examples/LWPlus/factorial.lwplus 20 examples/data/two.data
java LW LWPlus.grammar examples/LWPlus/factorial.lwplus 20 examples/data/three.data
java LW LWPlus.grammar examples/LWPlus/factorial.lwplus 20 examples/data/four.data
```
Example program (in LWPlus) to calculate factorial:
```
do
with PARTIAL begin
with COUNTER begin
read COUNTER ;
PARTIAL := 1 ;
while COUNTER <> 1 do
PARTIAL := prod PARTIAL COUNTER ;
COUNTER := sum COUNTER -1
endw ;
write PARTIAL
end
end
endp
```