1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.parser.Parser;
import org.junit.Test;
import static org.junit.Assert.*;
public class MethodFieldsTest {
@Test public void nestedFunctionStillRejectsLocalCapture() {
String source = """
struct Game {
int count;
val input = { int amount ->
val action = { -> count += amount; };
action();
};
};
return 0;
""";
try {
new CodeGen(source).driver(CodeGen.Phase.Parse);
fail("Captured a nonconstant local parameter");
} catch( Parser.ParseException e ) {
assertEquals("Variable 'amount' is out of function scope and must be a final constant",e.getMessage());
}
}
@Test public void nestedFunctionReceiver() throws Exception {
String source = """
struct Game {
bool held, paused;
int count;
val debounce = { bool pressed, bool wasHeld, { bool } action ->
if( pressed && !wasHeld ) action();
return pressed;
};
val bump = { -> count++; return false; };
val input = { bool pressed ->
if( true ) {
held = debounce(pressed,held,{ ->
paused = !paused;
bump();
val again = { -> count += 2; return false; };
again();
return false;
});
}
};
val named = { -> debounce(true,false,bump); };
};
val a = new Game, b = new Game;
a.input(true); a.input(true);
if( a.count != 3 || !a.paused || b.count != 0 || b.paused ) return 1;
a.input(false); a.input(true);
if( a.count != 6 || a.paused ) return 2;
b.input(true); b.named();
if( b.count != 4 || !b.paused || a.count != 6 ) return 3;
return 0;
""";
assertEquals("0",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.LocalSched),0));
TestC.runSF("val main = { -> "+source+" };","nestedFunctionReceiver","",-1);
}
@Test public void laterSiblingParameterType() throws Exception {
String source = """
struct Game {
val input = { !Controls keys -> keys.presses++; return keys.presses; };
val read = { Controls keys -> keys.presses; };
};
struct Controls { int presses = 4; };
val game = new Game, keys = new Controls;
if( game.input(keys) != 5 || game.read(keys) != 5 ) return 1;
return 0;
""";
assertEquals("0",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.LocalSched),0));
TestC.runSF("val main = { -> "+source+" };","forwardMethodType","",-1);
}
private static final String FIELDS = """
int count = 100;
struct Counter {
val step = { int n ->
count = count+n;
++count;
count += 2;
speed *= 2.0;
return read();
};
val local = { int count -> count+1; };
val read = { -> count; };
int count = 3;
f64 speed = 1.5;
};
val a = new Counter, b = new Counter;
if( a.step(4) != 10 || b.read() != 3 || a.speed != 3.0 ) return 1;
if( a.local(20) != 21 || a.read() != 10 || count != 100 ) return 2;
if( b.step(1) != 7 || a.read() != 10 ) return 3;
return 0;
""";
@Test public void laterFieldsAndMethods() throws Exception {
assertEquals("0",Eval2.eval(new CodeGen(FIELDS).driver(CodeGen.Phase.LocalSched),0));
TestC.runSF("val main = { -> "+FIELDS+" };","methodFields","",-1);
}
@Test public void laterGlobalAndRecursiveMethod() {
String source = """
struct Counter {
val step = { i64 n ->
if( n == 0 ) return value;
value += increment;
return step(n-1);
};
int value = 1;
};
val increment = 2;
return new Counter.step(3);
""";
assertEquals("7",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.LocalSched),0));
}
@Test public void laterFinalFieldStillRejectsWrites() {
String source = "struct C { val change = { -> x = 2; }; int ~x = 1; }; return new C.change();";
try {
new CodeGen(source).driver(CodeGen.Phase.TypeCheck);
fail("Method wrote a final field");
} catch( Parser.ParseException e ) {
assertEquals("Cannot reassign final 'x'",e.getMessage());
}
}
}
|