Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
uvxwx committed Jan 23, 2023
1 parent 0f013de commit a3508f4
Show file tree
Hide file tree
Showing 90 changed files with 1,880 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/if/class_in_else.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (true) "ok"; else class Foo {}
// expect: line 1: location: at 'class': expected expression
2 changes: 2 additions & 0 deletions test/if/class_in_then.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (true) class Foo {}
// expect: line 1: location: at 'class': expected expression
3 changes: 3 additions & 0 deletions test/if/dangling_else.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// A dangling else binds to the right-most if.
if (true) if (false) print "bad"; else print "good"; // expect: good
if (false) if (true) print "bad"; else print "bad";
6 changes: 6 additions & 0 deletions test/if/else.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Evaluate the 'else' expression if the condition is false.
if (true) print "good"; else print "bad"; // expect: good
if (false) print "bad"; else print "good"; // expect: good

// Allow block body.
if (false) nil; else { print "block"; } // expect: block
2 changes: 2 additions & 0 deletions test/if/fun_in_else.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (true) "ok"; else fun foo() {}
// expect: line 1: location: at 'foo': expected '(' for function expression
2 changes: 2 additions & 0 deletions test/if/fun_in_then.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (true) fun foo() {}
// expect: line 1: location: at 'foo': expected '(' for function expression
10 changes: 10 additions & 0 deletions test/if/if.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Evaluate the 'then' expression if the condition is true.
if (true) print "good"; // expect: good
if (false) print "bad";

// Allow block body.
if (true) { print "block"; } // expect: block

// Assignment in if condition.
var a = false;
if (a = true) print a; // expect: true
8 changes: 8 additions & 0 deletions test/if/truth.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// False and nil are false.
if (false) print "bad"; else print "false"; // expect: false
if (nil) print "bad"; else print "nil"; // expect: nil

// Everything else is true.
if (true) print true; // expect: true
if (0) print 0; // expect: 0
if ("") print "empty"; // expect: empty
2 changes: 2 additions & 0 deletions test/if/var_in_else.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (true) "ok"; else var foo;
// expect: line 1: location: at 'var': expected expression
2 changes: 2 additions & 0 deletions test/if/var_in_then.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (true) var foo;
// expect: line 1: location: at 'var': expected expression
15 changes: 15 additions & 0 deletions test/inheritance/constructor.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A {
fun A(param) {
this.field = param;
}

fun test() {
print this.field;
}
}

class B < A {}

var b = B("value");
b.test();
// expect: line 13: location: at ')': expected 0 arguments, got 1
4 changes: 4 additions & 0 deletions test/inheritance/inherit_from_function.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fun foo() {}

class Subclass < foo {}
// expect: line 3: location: at 'foo': expected class, got function
3 changes: 3 additions & 0 deletions test/inheritance/inherit_from_nil.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var Nil = nil;
class Foo < Nil {}
// expect: line 2: location: at 'Nil': expected class, got nil
3 changes: 3 additions & 0 deletions test/inheritance/inherit_from_number.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var Number = 123;
class Foo < Number {}
// expect: line 2: location: at 'Number': expected class, got number
14 changes: 14 additions & 0 deletions test/inheritance/inherit_methods.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Foo {
fun methodOnFoo() { print "foo"; }
fun override() { print "foo"; }
}

class Bar < Foo {
fun methodOnBar() { print "bar"; }
fun override() { print "bar"; }
}

var bar = Bar();
bar.methodOnFoo(); // expect: foo
bar.methodOnBar(); // expect: bar
bar.override(); // expect: bar
4 changes: 4 additions & 0 deletions test/inheritance/parenthesized_superclass.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Foo {}

class Bar < (Foo) {}
// expect: line 3: location: at '(': expected class name after '<'
38 changes: 38 additions & 0 deletions test/inheritance/set_fields_from_base_class.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Foo {
fun foo(a, b) {
this.field1 = a;
this.field2 = b;
}

fun fooPrint() {
print this.field1;
print this.field2;
}
}

class Bar < Foo {
fun bar(a, b) {
this.field1 = a;
this.field2 = b;
}

fun barPrint() {
print this.field1;
print this.field2;
}
}

var bar = Bar();
bar.foo("foo 1", "foo 2");
bar.fooPrint();
// expect: foo 1
// expect: foo 2

bar.bar("bar 1", "bar 2");
bar.barPrint();
// expect: bar 1
// expect: bar 2

bar.fooPrint();
// expect: bar 1
// expect: bar 2
7 changes: 7 additions & 0 deletions test/list/empty.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{} // By itself.

// In a statement.
if (true) {}
if (false) {} else {}

print "ok"; // expect: ok
8 changes: 8 additions & 0 deletions test/list/scope.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var a = "outer";

{
var a = "inner";
print a; // expect: inner
}

print a; // expect: outer
19 changes: 19 additions & 0 deletions test/logical_operator/and.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Note: These tests implicitly depend on ints being truthy.

// Return the first non-true argument.
print false and 1; // expect: false
print true and 1; // expect: 1
print 1 and 2 and false; // expect: false

// Return the last argument if all are true.
print 1 and true; // expect: true
print 1 and 2 and 3; // expect: 3

// Short-circuit at the first false argument.
var a = "before";
var b = "before";
(a = true) and
(b = false) and
(a = "bad");
print a; // expect: true
print b; // expect: false
8 changes: 8 additions & 0 deletions test/logical_operator/and_truth.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// False and nil are false.
print false and "bad"; // expect: false
print nil and "bad"; // expect: nil

// Everything else is true.
print true and "ok"; // expect: ok
print 0 and "ok"; // expect: ok
print "" and "ok"; // expect: ok
19 changes: 19 additions & 0 deletions test/logical_operator/or.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Note: These tests implicitly depend on ints being truthy.

// Return the first true argument.
print 1 or true; // expect: 1
print false or 1; // expect: 1
print false or false or true; // expect: true

// Return the last argument if all are false.
print false or false; // expect: false
print false or false or false; // expect: false

// Short-circuit at the first true argument.
var a = "before";
var b = "before";
(a = false) or
(b = true) or
(a = "bad");
print a; // expect: false
print b; // expect: true
8 changes: 8 additions & 0 deletions test/logical_operator/or_truth.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// False and nil are false.
print false or "ok"; // expect: ok
print nil or "ok"; // expect: ok

// Everything else is true.
print true or "ok"; // expect: true
print 0 or "ok"; // expect: 0
print "s" or "ok"; // expect: s
22 changes: 22 additions & 0 deletions test/method/arity.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Foo {
fun method0() { return "no args"; }
fun method1(a) { return a; }
fun method2(a, b) { return a + b; }
fun method3(a, b, c) { return a + b + c; }
fun method4(a, b, c, d) { return a + b + c + d; }
fun method5(a, b, c, d, e) { return a + b + c + d + e; }
fun method6(a, b, c, d, e, f) { return a + b + c + d + e + f; }
fun method7(a, b, c, d, e, f, g) { return a + b + c + d + e + f + g; }
fun method8(a, b, c, d, e, f, g, h) { return a + b + c + d + e + f + g + h; }
}

var foo = Foo();
print foo.method0(); // expect: no args
print foo.method1(1); // expect: 1
print foo.method2(1, 2); // expect: 3
print foo.method3(1, 2, 3); // expect: 6
print foo.method4(1, 2, 3, 4); // expect: 10
print foo.method5(1, 2, 3, 4, 5); // expect: 15
print foo.method6(1, 2, 3, 4, 5, 6); // expect: 21
print foo.method7(1, 2, 3, 4, 5, 6, 7); // expect: 28
print foo.method8(1, 2, 3, 4, 5, 6, 7, 8); // expect: 36
5 changes: 5 additions & 0 deletions test/method/empty_block.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Foo {
fun bar() {}
}

print Foo().bar(); // expect: nil
9 changes: 9 additions & 0 deletions test/method/extra_arguments.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo {
fun method(a, b) {
print a;
print b;
}
}

Foo().method(1, 2, 3, 4);
// expect: line 8: location: at ')': expected 2 arguments, got 4
7 changes: 7 additions & 0 deletions test/method/missing_arguments.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Foo {
fun method(a, b)
{a; b;} // suppress resolver
}

Foo().method(1);
// expect: line 6: location: at ')': expected 2 arguments, got 1
4 changes: 4 additions & 0 deletions test/method/not_found.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Foo {}

Foo().unknown();
// expect: line 3: location: at 'unknown': undefined property
5 changes: 5 additions & 0 deletions test/method/print_bound_method.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Foo {
fun method() { }
}
var foo = Foo();
print foo.method; // expect: function
9 changes: 9 additions & 0 deletions test/method/refer_to_name.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo {
fun method() {
// no 'this'
print method;
}
}

Foo().method();
// expect: line 4: location: at 'method': undeclared variable
Loading

0 comments on commit a3508f4

Please sign in to comment.