added lox test files

This commit is contained in:
Moritz Gmeiner 2024-08-03 02:44:12 +02:00
commit 0f3d0a15f0
268 changed files with 7497 additions and 3 deletions

View file

@ -0,0 +1,27 @@
var f;
var g;
{
var local = "local";
fun f_() {
print local;
local = "after f";
print local;
}
f = f_;
fun g_() {
print local;
local = "after g";
print local;
}
g = g_;
}
f();
// expect: local
// expect: after f
g();
// expect: after f
// expect: after g

View file

@ -0,0 +1,13 @@
var a = "global";
{
fun assign() {
a = "assigned";
}
var a = "inner";
assign();
print a; // expect: inner
}
print a; // expect: assigned

View file

@ -0,0 +1,11 @@
var f;
fun foo(param) {
fun f_() {
print param;
}
f = f_;
}
foo("param");
f(); // expect: param

View file

@ -0,0 +1,15 @@
// This is a regression test. There was a bug where if an upvalue for an
// earlier local (here "a") was captured *after* a later one ("b"), then it
// would crash because it walked to the end of the upvalue list (correct), but
// then didn't handle not finding the variable.
fun f() {
var a = "a";
var b = "b";
fun g() {
print b; // expect: b
print a; // expect: a
}
g();
}
f();

View file

@ -0,0 +1,13 @@
var f;
class Foo {
method(param) {
fun f_() {
print param;
}
f = f_;
}
}
Foo().method("param");
f(); // expect: param

View file

@ -0,0 +1,11 @@
var f;
{
var local = "local";
fun f_() {
print local;
}
f = f_;
}
f(); // expect: local

View file

@ -0,0 +1,25 @@
var f;
fun f1() {
var a = "a";
fun f2() {
var b = "b";
fun f3() {
var c = "c";
fun f4() {
print a;
print b;
print c;
}
f = f4;
}
f3();
}
f2();
}
f1();
f();
// expect: a
// expect: b
// expect: c

View file

@ -0,0 +1,7 @@
{
var local = "local";
fun f() {
print local; // expect: local
}
f();
}

View file

@ -0,0 +1,14 @@
var f;
{
var a = "a";
fun f_() {
print a;
print a;
}
f = f_;
}
f();
// expect: a
// expect: a

View file

@ -0,0 +1,16 @@
{
var f;
{
var a = "a";
fun f_() { print a; }
f = f_;
}
{
// Since a is out of scope, the local slot will be reused by b. Make sure
// that f still closes over a.
var b = "b";
f(); // expect: a
}
}

View file

@ -0,0 +1,12 @@
{
var foo = "closure";
fun f() {
{
print foo; // expect: closure
var foo = "shadow";
print foo; // expect: shadow
}
print foo; // expect: closure
}
f();
}

View file

@ -0,0 +1,13 @@
// This is a regression test. There was a bug where the VM would try to close
// an upvalue even if the upvalue was never created because the codepath for
// the closure was not executed.
{
var a = "a";
if (false) {
fun foo() { a; }
}
}
// If we get here, we didn't segfault when a went out of scope.
print "ok"; // expect: ok

View file

@ -0,0 +1,28 @@
// This is a regression test. When closing upvalues for discarded locals, it
// wouldn't make sure it discarded the upvalue for the correct stack slot.
//
// Here we create two locals that can be closed over, but only the first one
// actually is. When "b" goes out of scope, we need to make sure we don't
// prematurely close "a".
var closure;
{
var a = "a";
{
var b = "b";
fun returnA() {
return a;
}
closure = returnA;
if (false) {
fun returnB() {
return b;
}
}
}
print closure(); // expect: a
}