Skip to content

Commit

Permalink
Updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexito4 committed Sep 10, 2017
1 parent 67f6e2b commit 4176723
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 42 deletions.
8 changes: 2 additions & 6 deletions Sources/LoxCore/Interpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ final class Interpreter: ExprVisitor, StmtVisitor {
func visitCallExpr(_ expr: Expr.Call) -> ExprVisitorReturn {
let calleeResult = evaluate(expr: expr.callee)

guard let callee = calleeResult?.value else {
return calleeResult
guard let callee = calleeResult?.value, let function = callee as? Callable else {
return .failure(InterpreterError.runtime(expr.paren, "Can only call functions and classes."))
}

var arguments: Array<Any> = []
Expand All @@ -266,10 +266,6 @@ final class Interpreter: ExprVisitor, StmtVisitor {
arguments.append(arg)
}

guard let function = callee as? Callable else {
return .failure(InterpreterError.runtime(expr.paren, "Can only call functions and classes."))
}

guard arguments.count == function.arity else {
return .failure(InterpreterError.runtime(expr.paren, "Expected \(function.arity) arguments but got \(arguments.count)."))
}
Expand Down
12 changes: 10 additions & 2 deletions Sources/LoxCore/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ final class Parser {

private func declaration() -> Stmt? {
do {
if match(.fun) {
if check(.fun) && checkNext(.identifier) {
try consume(.fun, message: "")
return try function(kind: "function")
}
if match(.Var) {
Expand Down Expand Up @@ -374,7 +375,8 @@ final class Parser {
return Expr.Grouping(expression: expr)
}

if match(.fun) {
if check(.fun) && checkNext(.leftParen) {
try consume(.fun, message: "")
return try functionBody(kind: "function")
}

Expand Down Expand Up @@ -422,6 +424,12 @@ extension Parser {
return peek().type == tokenType
}

func checkNext(_ tokenType: TokenType) -> Bool {
if isAtEnd() { return false }
if tokens[current + 1].type == .eof { return false }
return tokens[current + 1].type == tokenType
}

func advance() -> Token {
if !isAtEnd() {
current += 1
Expand Down
22 changes: 12 additions & 10 deletions temptest.lox
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
fun repeat(times, fn) {
for (var i = 0; i < times; i = i + 1) {
fn();
}
}

var times = 10;
repeat(times, fun () {
print("Repeating " + times + " times.");
});
fun () {};

// fun repeat(times, fn) {
// for (var i = 0; i < times; i = i + 1) {
// fn();
// }
// }

// var times = 10;
// repeat(times, fun () {
// print("Repeating " + times + " times.");
// });

// fun sayHi(first, last) {
// print "Hi, " + first + " " + last + "!";
Expand Down
3 changes: 1 addition & 2 deletions test/constructor/default_arguments.lox
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class Foo {}

var foo = Foo(1, 2, 3);
print foo; // expect: Foo instance
var foo = Foo(1, 2, 3); // expect runtime error: Expected 0 arguments but got 3.
5 changes: 1 addition & 4 deletions test/constructor/extra_arguments.lox
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
class Foo {
init(a, b) {
print "init"; // expect: init
this.a = a;
this.b = b;
}
}

var foo = Foo(1, 2, 3, 4);
print foo.a; // expect: 1
print foo.b; // expect: 2
var foo = Foo(1, 2, 3, 4); // expect runtime error: Expected 2 arguments but got 4.
2 changes: 1 addition & 1 deletion test/constructor/missing_arguments.lox
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ class Foo {
init(a, b) {}
}

var foo = Foo(1); // expect runtime error: Not enough arguments.
var foo = Foo(1); // expect runtime error: Expected 2 arguments but got 1.
6 changes: 4 additions & 2 deletions test/function/body_must_be_block.lox
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// [line 3] Error at '123': Expect '{' before block.
// [c line 4] Error at end: Expect '}' after block.
// original was -> [line 3] Error at '123': Expect '{' before block.
// but because we support lambdas the error is slightly different:
// [line 5] Error at '123': Expect '{' before function body.
// ?? [c line 4] Error at end: Expect '}' after block.
fun f() 123;
6 changes: 3 additions & 3 deletions test/function/extra_arguments.lox
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fun f(a, b) {
print a; // expect: 1
print b; // expect: 2
print a;
print b;
}

f(1, 2, 3, 4);
f(1, 2, 3, 4); // expect runtime error: Expected 2 arguments but got 4.
2 changes: 1 addition & 1 deletion test/function/missing_arguments.lox
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fun f(a, b) {}

f(1); // expect runtime error: Not enough arguments.
f(1); // expect runtime error: Expected 2 arguments but got 1.
2 changes: 2 additions & 0 deletions test/function/print.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fun foo() {}
print foo; // expect: <fn foo>
6 changes: 3 additions & 3 deletions test/method/extra_arguments.lox
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Foo {
method(a, b) {
print a; // expect: 1
print b; // expect: 2
print a;
print b;
}
}

Foo().method(1, 2, 3, 4);
Foo().method(1, 2, 3, 4); // expect runtime error: Expected 2 arguments but got 4.
2 changes: 1 addition & 1 deletion test/method/missing_arguments.lox
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ class Foo {
method(a, b) {}
}

Foo().method(1); // expect runtime error: Not enough arguments.
Foo().method(1); // expect runtime error: Expected 2 arguments but got 1.
3 changes: 2 additions & 1 deletion test/operator/add_bool_string.lox
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
true + "s"; // expect runtime error: Operands must be two numbers or two strings.
// No error here, Chapter 7 challenge 2 implemented.
// true + "s"; expect runtime error: Operands must be two numbers or two strings.
6 changes: 2 additions & 4 deletions test/super/extra_arguments.lox
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ class Base {

class Derived < Base {
foo() {
print "Derived.foo()";
super.foo("a", "b", "c", "d");
print "Derived.foo()"; // expect: Derived.foo()
super.foo("a", "b", "c", "d"); // expect runtime error: Expected 2 arguments but got 4.
}
}

Derived().foo();
// expect: Derived.foo()
// expect: Base.foo(a, b)
2 changes: 1 addition & 1 deletion test/super/missing_arguments.lox
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Base {

class Derived < Base {
foo() {
super.foo(1); // expect runtime error: Not enough arguments.
super.foo(1); // expect runtime error: Expected 2 arguments but got 1.
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/unexpected_character.lox
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// [line 3] Error: Unexpected character.
// [line 3] Error: Unexpected character. '|'
// [java line 3] Error at 'b': Expect ')' after arguments.
foo(a | b);
30 changes: 30 additions & 0 deletions tools/test_chap10challenge2_and_chap7challenge2patch.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
diff --git a/test/function/body_must_be_block.lox b/test/function/body_must_be_block.lox
index 745a8d0..16f0337 100755
--- a/test/function/body_must_be_block.lox
+++ b/test/function/body_must_be_block.lox
@@ -1,3 +1,5 @@
-// [line 3] Error at '123': Expect '{' before block.
-// [c line 4] Error at end: Expect '}' after block.
+// original was -> [line 3] Error at '123': Expect '{' before block.
+// but because we support lambdas the error is slightly different:
+// [line 5] Error at '123': Expect '{' before function body.
+// ?? [c line 4] Error at end: Expect '}' after block.
fun f() 123;
diff --git a/test/operator/add_bool_string.lox b/test/operator/add_bool_string.lox
index 04739d5..0c8c87a 100755
--- a/test/operator/add_bool_string.lox
+++ b/test/operator/add_bool_string.lox
@@ -1 +1,2 @@
-true + "s"; // expect runtime error: Operands must be two numbers or two strings.
+// No error here, Chapter 7 challenge 2 implemented.
+// true + "s"; expect runtime error: Operands must be two numbers or two strings.
\ No newline at end of file
diff --git a/test/unexpected_character.lox b/test/unexpected_character.lox
index 5e51396..9ca1244 100755
--- a/test/unexpected_character.lox
+++ b/test/unexpected_character.lox
@@ -1,3 +1,3 @@
-// [line 3] Error: Unexpected character.
+// [line 3] Error: Unexpected character. '|'
// [java line 3] Error at 'b': Expect ')' after arguments.
foo(a | b);

0 comments on commit 4176723

Please sign in to comment.