Classes and Instances 27

This commit is contained in:
nazrin 2025-06-13 02:29:46 +00:00
parent 6d5dff6e3d
commit d9dc02b92f
13 changed files with 271 additions and 63 deletions

View file

@ -16,6 +16,7 @@ void main(){
"./test/for.lox".match("1 2 1 2 3 ".replace(' ', '\n'));
"./test/ifelse.lox".match("a1 b2 c3 ".replace(' ', '\n'));
"./test/while.lox".match("1 2 3 2 1 0 1 1 2 3 2 1 0 1 ".replace(' ', '\n'));
"./test/fields.lox".match("0 10 ".replace(' ', '\n'));
"./test/ops.lox".match("1\n2\n3\n4\n5\n6\n7\ntrue\nfalse\ntrue\ntrue\nhello, world\n");
"./test/shortcircuit.lox".match("true\nAAAA!\nAAAA!\nAAAA?\n");

48
test/fields.lox Normal file
View file

@ -0,0 +1,48 @@
class Obj{}
var p = Obj();
p.length = 0;
p.next = nil;
fun append(p, item){
p.length = p.length + 1;
var n = Obj();
n.next = p.next;
p.next = n;
}
fun walk(p){
class Res{}
var n = p;
var d = 0;
while(n.next){
n = n.next;
d = d + 1;
}
var r = Res();
r.item = n;
r.depth = d;
return r;
}
fun max(a, b){
if(a > b)
return a;
return b;
}
var sum = 0;
var maxLength = 0;
for(var i = 0; i < 10; i = i + 1){
append(p, Obj());
sum = sum + p.length;
var res = walk(p);
sum = sum - res.depth;
maxLength = max(maxLength, p.length);
}
print sum;
print maxLength;