mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
added lox test files
This commit is contained in:
parent
821f5c62bc
commit
0f3d0a15f0
268 changed files with 7497 additions and 3 deletions
27
lox.t/closure/assign_to_closure.lox
Normal file
27
lox.t/closure/assign_to_closure.lox
Normal 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
|
||||
13
lox.t/closure/assign_to_shadowed_later.lox
Normal file
13
lox.t/closure/assign_to_shadowed_later.lox
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
var a = "global";
|
||||
|
||||
{
|
||||
fun assign() {
|
||||
a = "assigned";
|
||||
}
|
||||
|
||||
var a = "inner";
|
||||
assign();
|
||||
print a; // expect: inner
|
||||
}
|
||||
|
||||
print a; // expect: assigned
|
||||
11
lox.t/closure/close_over_function_parameter.lox
Normal file
11
lox.t/closure/close_over_function_parameter.lox
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var f;
|
||||
|
||||
fun foo(param) {
|
||||
fun f_() {
|
||||
print param;
|
||||
}
|
||||
f = f_;
|
||||
}
|
||||
foo("param");
|
||||
|
||||
f(); // expect: param
|
||||
15
lox.t/closure/close_over_later_variable.lox
Normal file
15
lox.t/closure/close_over_later_variable.lox
Normal 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();
|
||||
13
lox.t/closure/close_over_method_parameter.lox
Normal file
13
lox.t/closure/close_over_method_parameter.lox
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
var f;
|
||||
|
||||
class Foo {
|
||||
method(param) {
|
||||
fun f_() {
|
||||
print param;
|
||||
}
|
||||
f = f_;
|
||||
}
|
||||
}
|
||||
|
||||
Foo().method("param");
|
||||
f(); // expect: param
|
||||
11
lox.t/closure/closed_closure_in_function.lox
Normal file
11
lox.t/closure/closed_closure_in_function.lox
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var f;
|
||||
|
||||
{
|
||||
var local = "local";
|
||||
fun f_() {
|
||||
print local;
|
||||
}
|
||||
f = f_;
|
||||
}
|
||||
|
||||
f(); // expect: local
|
||||
25
lox.t/closure/nested_closure.lox
Normal file
25
lox.t/closure/nested_closure.lox
Normal 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
|
||||
7
lox.t/closure/open_closure_in_function.lox
Normal file
7
lox.t/closure/open_closure_in_function.lox
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
var local = "local";
|
||||
fun f() {
|
||||
print local; // expect: local
|
||||
}
|
||||
f();
|
||||
}
|
||||
14
lox.t/closure/reference_closure_multiple_times.lox
Normal file
14
lox.t/closure/reference_closure_multiple_times.lox
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
var f;
|
||||
|
||||
{
|
||||
var a = "a";
|
||||
fun f_() {
|
||||
print a;
|
||||
print a;
|
||||
}
|
||||
f = f_;
|
||||
}
|
||||
|
||||
f();
|
||||
// expect: a
|
||||
// expect: a
|
||||
16
lox.t/closure/reuse_closure_slot.lox
Normal file
16
lox.t/closure/reuse_closure_slot.lox
Normal 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
|
||||
}
|
||||
}
|
||||
12
lox.t/closure/shadow_closure_with_local.lox
Normal file
12
lox.t/closure/shadow_closure_with_local.lox
Normal 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();
|
||||
}
|
||||
13
lox.t/closure/unused_closure.lox
Normal file
13
lox.t/closure/unused_closure.lox
Normal 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
|
||||
28
lox.t/closure/unused_later_closure.lox
Normal file
28
lox.t/closure/unused_later_closure.lox
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue