Skip to content

Commit b9842b0

Browse files
authored
Refactor process for updating references (#2)
* Add new ROOPL programs * Fix backend => frontend * Add new tests to target * Fix naming * Fix update of variable referencing * Add new ROOPL programs to examples list
1 parent e9d83d8 commit b9842b0

29 files changed

+279
-149
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ rplpp-test: build
1717
stack exec rooplpp test/BinaryTree.rplpp
1818
stack exec rooplpp test/BinaryTreeArrayUsage.rplpp
1919
stack exec rooplpp test/LinkedList.rplpp
20+
stack exec rooplpp test/LinkedListReversal.rplpp
2021
stack exec rooplpp test/DoublyLinkedList.rplpp
2122
stack exec rooplpp test/Fibonacci.rplpp
23+
stack exec rooplpp test/FindMinMax.rplpp
2224
stack exec rooplpp test/SimpleArithmetic.rplpp
2325
stack exec rooplpp test/SimpleConditional.rplpp
2426
stack exec rooplpp test/SimpleLoop.rplpp

ROOPLPP.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ synopsis: ROOPL++ suite. Supports compilation, interpretation and pr
55
license: MIT
66
license-file: LICENSE
77
homepage: https://github.com/haysch/ROOPLPP
8-
author: Lasse Hay-Schmidt (interpreter/inverter) & Martin Holm Cservenka (backend)
8+
author: Lasse Hay-Schmidt (interpreter/inverter) & Martin Holm Cservenka (frontend)
99
maintainer: [email protected]
1010
category: Language
1111
-- copyright:

src/AST.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ type Store = Map Location IExpression
143143

144144
type ObjectScope = [Env]
145145

146-
type Ref = Map SIdentifier SIdentifier
147-
type ReferenceScope = [Ref]
146+
type References = Map (SIdentifier, Maybe SIdentifier) (SIdentifier, Maybe SIdentifier)
147+
type ReferenceScope = [References]
148148

149149
data IExpression = Const Integer
150150
| Object TypeName Env

src/Interpreter.hs

+121-127
Large diffs are not rendered by default.

src/Stringify.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ stringifyDataType IntegerType = "int"
2222
stringifyDataType (ObjectType tn) = tn
2323
stringifyDataType (ObjectArrayType tn) = tn ++ "[]"
2424
stringifyDataType IntegerArrayType = "int[]"
25-
stringifyDataType NilType = "nil"
25+
stringifyDataType NilType = "null"
2626
stringifyDataType dt = error $ "Unsupported datatype: " ++ show dt
2727

2828
stringifyBinOp :: BinOp -> String
@@ -55,7 +55,7 @@ stringifySExpression (ArrayElement (n, e)) st =
5555
let n' = lookupId n st
5656
e' = stringifySExpression e st
5757
in printf "%s[%s]" n' e'
58-
stringifySExpression Nil _ = "nil"
58+
stringifySExpression Nil _ = "null"
5959
stringifySExpression (Binary binop e1 e2) st =
6060
let binop' = stringifyBinOp binop
6161
e1' = stringifySExpression e1 st
@@ -147,4 +147,4 @@ stringifyIExpression (Const _) = "int"
147147
stringifyIExpression (IntegerArray _) = "int[]"
148148
stringifyIExpression (Object tn _) = tn
149149
stringifyIExpression (ObjectArray tn _) = printf "%s[]" tn
150-
stringifyIExpression Null = "nil"
150+
stringifyIExpression Null = "null"

test/Fibonacci.rplpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,34 @@ class Program
66
// A reversible programming language and its invertible self-interpreter, 2007.
77
// https://dl.acm.org/doi/10.1145/1244381.1244404
88

9-
int n
10-
int x1
11-
int x2
9+
int outx1
10+
int outx2
1211

1312
method main()
14-
n += 4
15-
call fib()
13+
local int n = 4
14+
local int x1 = 0
15+
local int x2 = 0
16+
call fib(n, x1, x2)
17+
outx1 ^= x1
18+
outx2 ^= x2
19+
uncall fib(n, x1, x2)
20+
delocal int x2 = 0
21+
delocal int x1 = 0
22+
delocal int n = 4
23+
1624

1725
// method main_bwd()
1826
// x1 += 5
1927
// x2 += 8
2028
// uncall fib()
2129

22-
method fib()
30+
method fib(int n, int x1, int x2)
2331
if n = 0 then
2432
x1 += 1
2533
x2 += 1
2634
else
2735
n -= 1
28-
call fib()
36+
call fib(n, x1, x2)
2937
x1 += x2
3038
x1 <=> x2
3139
fi x1 = x2

test/FindMinMax.rplpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Foo
2+
int y
3+
4+
method set(int x)
5+
y ^= x
6+
7+
method swap(int x)
8+
x <=> y
9+
10+
class Program
11+
int min
12+
int max
13+
int[] xs
14+
15+
method main()
16+
local int length = 4
17+
local int m = 0
18+
new int[length] xs
19+
20+
xs[0] += 4
21+
xs[1] += 2
22+
xs[2] += 8
23+
xs[3] += 6
24+
25+
call findMin(m, length)
26+
min ^= m
27+
uncall findMin(m, length)
28+
29+
call findMax(m, length)
30+
max ^= m
31+
uncall findMax(m, length)
32+
33+
delocal int m = 0
34+
delocal int length = 4
35+
36+
method findMin(int m, int n)
37+
m ^= xs[0]
38+
local int i = 1
39+
from i = 1 do
40+
if xs[i] < m then
41+
xs[i] -= m
42+
m += xs[i]
43+
else skip
44+
fi xs[i] < 0
45+
i += 1
46+
loop skip
47+
until i = n
48+
delocal int i = n
49+
50+
method findMax(int m, int n)
51+
local int i = 0
52+
from i = 0 do
53+
if xs[i] > m then
54+
m -= xs[i]
55+
m <=> xs[i]
56+
else skip
57+
fi xs[i] < 0
58+
i += 1
59+
loop skip
60+
until i = n
61+
delocal int i = n

test/LinkedListReversal.rplpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Cell
2+
int data
3+
Cell next
4+
5+
method addToData(int value)
6+
data += value
7+
8+
method addFromData(int value)
9+
value += data
10+
11+
method swapNext(Cell cell)
12+
next <=> cell
13+
14+
//////////////////////////////////////////
15+
// Summation of a list of natural numbers
16+
// R.Glueck, 210330
17+
//////////////////////////////////////////
18+
class Program
19+
Cell right
20+
Cell left
21+
int sum
22+
23+
method main()
24+
////////////////////////////////////////
25+
// begin: Create list right -> [1, ..., n]
26+
local int n = 5 // n >= 0
27+
from n != 0 do skip
28+
loop
29+
local Cell cell = nil
30+
new Cell cell
31+
call cell::swapNext(right)
32+
call cell::addToData(n)
33+
right <=> cell
34+
n -= 1
35+
delocal Cell cell = nil
36+
until n = 0
37+
delocal int n = 0
38+
// end: Create list right -> [1, ..., n]
39+
////////////////////////////////////////
40+
41+
////////////////////////////////////////
42+
// begin: Summation of list values
43+
// left -> nil, right -> [1,...,n], sum = 0
44+
from left = nil do skip // walk forward through list
45+
loop
46+
call right::addFromData(sum) // payload operation: summation
47+
call right::swapNext(left) // swap pointers: cell points left
48+
left <=> right // left->[i,..,1], right->[i+1,...,n]
49+
until right = nil
50+
// left->[n,..,1], right->nil, sum = 1 + ... + n
51+
from right = nil do skip // walk backward through list
52+
loop
53+
left <=> right
54+
call right::swapNext(left) // swap pointers: cell points right
55+
skip // payload operation: none
56+
until left = nil
57+
// left->nil, right->[1,...,n], sum = 1 + ... + n
58+
// end: Summation of list values
59+
////////////////////////////////////////

test/SimpleMethodCall.rplpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ class Foo
88

99
class Program
1010
int z
11-
Foo f
1211

1312
method main()
13+
local Foo f = nil
1414
new Foo f
1515
local int x = 0
1616
call f::addTwo(x)
1717
z += x
1818
uncall f::addTwo(x)
1919
delocal int x = 0
20+
delete Foo f
21+
delocal Foo f = nil

test/result/ArrayUsageExample.out

-1
This file was deleted.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "foo": { "value": 5 }, "bar": { "value": 5, "value2": 10 }, "fooList": [{ "value": 0 }, { "value": 0 }, { "value": 0, "value2": 0 }, null, null], "barList": [null, null, null, null, null], "intList": [10, 10, 5, 0, 0], "singleton": null, "length": 3735943886 }

test/result/BinaryTree.out

-1
This file was deleted.

test/result/BinaryTree.out.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "sumResult": 3, "tree": { "root": { "left": { "left": { "left": null, "right": null, "value": 2 }, "right": null, "value": 1 }, "right": null, "value": 0 } }, "nodeCount": 3 }

test/result/BinaryTreeArrayUsage.out

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "tree": { "root": { "value": 3, "left": { "value": 2, "left": { "value": 1, "left": null, "right": null }, "right": null }, "right": { "value": 4, "left": null, "right": null } } }, "nodeCount": 4, "nodeVals": [3, 2, 4, 1] }

test/result/DoublyLinkedList.out

-1
This file was deleted.

test/result/DoublyLinkedList.out.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "list": { "head": { "data": 0, "index": 0, "left": null, "right": { "data": 1, "index": 1, "left": { "data": 0, "index": 0, "left": null, "right": null, "self": null }, "right": { "data": 2, "index": 2, "left": { "data": 1, "index": 1, "left": null, "right": null, "self": null }, "right": { "data": 3, "index": 3, "left": { "data": 2, "index": 2, "left": null, "right": null, "self": null }, "right": { "data": 4, "index": 4, "left": { "data": 3, "index": 3, "left": null, "right": null, "self": null }, "right": { "data": 5, "index": 5, "left": { "data": 4, "index": 4, "left": null, "right": null, "self": null }, "right": { "data": 6, "index": 6, "left": { "data": 5, "index": 5, "left": null, "right": null, "self": null }, "right": { "data": 7, "index": 7, "left": { "data": 6, "index": 6, "left": null, "right": null, "self": null }, "right": { "data": 8, "index": 8, "left": { "data": 7, "index": 7, "left": null, "right": null, "self": null }, "right": { "data": 9, "index": 9, "left": { "data": 8, "index": 8, "left": null, "right": null, "self": null }, "right": null, "self": { "data": 9, "index": 9, "left": null, "right": null, "self": null } }, "self": null }, "self": null }, "self": null }, "self": null }, "self": null }, "self": null }, "self": null }, "self": null }, "self": null }, "length": 10 }, "listLength": 10 }

test/result/Fibonacci.out

-1
This file was deleted.

test/result/Fibonacci.out.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "outx1": 5, "outx2": 8 }

test/result/FindMinMax.out.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "min": 2, "max": 8, "xs": [4, 2, 8, 6] }

test/result/LinkedList.out

-1
This file was deleted.

test/result/LinkedList.out.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "linkedList": { "head": { "next": { "next": { "next": { "next": { "next": { "next": { "next": { "next": { "next": { "next": null, "data": 9 }, "data": 8 }, "data": 7 }, "data": 6 }, "data": 5 }, "data": 4 }, "data": 3 }, "data": 2 }, "data": 1 }, "data": 0 }, "listLength": 10 }, "sumResult": 0, "listLength": 10 }
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "right": { "data": 1, "next": { "data": 2, "next": { "data": 3, "next": { "data": 4, "next": { "data": 5, "next": null } } } } }, "left": null, "sum": 15 }
File renamed without changes.

test/result/SimpleMethodCall.out

-1
This file was deleted.

test/result/SimpleMethodCall.out.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "z": 2 }

web/index.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
<li><a href="#examples/SimpleConditional">Conditional</a></li>
3434
<li><a href="#examples/SimpleLoop">Loop</a></li>
3535
<li><a href="#examples/SimpleMethodCall">Method Calling</a></li>
36-
<li><p class="li-section">Complex</p></li>
36+
<li><p class="li-section">Advanced</p></li>
3737
<li><a href="#examples/ArrayUsageExample">Array Usage</a></li>
3838
<li><a href="#examples/BinaryTree">Binary Tree</a></li>
3939
<li><a href="#examples/BinaryTreeArrayUsage">Binary Tree using Array</a></li>
4040
<li><a href="#examples/DoublyLinkedList">Doubly Linked List</a></li>
4141
<li><a href="#examples/Fibonacci">Fibonacci</a></li>
42+
<li><a href="#examples/FindMinMax">Find Min & Max</a></li>
4243
<li><a href="#examples/LinkedList">LinkedList</a></li>
44+
<li><a href="#examples/LinkedListReversal">LinkedList Reversal</a></li>
4345
</ul>
4446
</li>
4547
<li><a href="#about" data-toggle="modal">About</a></li>

0 commit comments

Comments
 (0)