Skip to content

Commit

Permalink
fix: error reporting for inbuilt funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhgray committed Oct 26, 2024
1 parent 72a4111 commit aa417e5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
3 changes: 3 additions & 0 deletions tahini/app/src/main/java/com/tahini/lang/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ public Object visitCallExpr(Expr.Call expr) {
try {
result = function.call(this, arguments);
} catch (RuntimeError error) {
if (error.token == null) {
throw new RuntimeError(expr.paren, error.getMessage(), new ArrayList<>(callStack));
}
throw new RuntimeError(error.token, error.getMessage(), new ArrayList<>(callStack));
}

Expand Down
24 changes: 24 additions & 0 deletions tahini/tests/arrays7.tah
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fun isEven(n) {
while (n >= 2) {
n = n - 2;
}
return n == 0;
}

fun filterEven(arr) {
var oddArr = [];
for (var i = 0; i < len(arr); i = i + 1) {
if (isEven(arr[i])) {
oddArr = oddArr + [arr[i]];
}
}
return oddArr;
}

var arr = [1, 2, 3, 4, 5];
print filterEven(arr);
print len(arr, 0);

// [2.0, 4.0]
// RuntimeError: Expected 1 arguments but got 2.
// [line 20]
32 changes: 17 additions & 15 deletions test.tah
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// function which returns a list
fun testList() {
return [1, 2, 3, 4, 5];
fun isEven(n) {
while (n >= 2) {
n = n - 2;
}
return n == 0;
}

print [];

print len(testList())+3;

print testList()[1:5];
fun filterEven(arr) {
var oddArr = [];
for (var i = 0; i < len(arr); i = i + 1) {
if (isEven(arr[i])) {
oddArr = oddArr + [arr[i]];
}
}
return oddArr;
}

print testList() + [100];
print filterEven([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

// function which takes a list as an argument
fun testList2(list) {
return list[2];
}

var ddd = 4.4;
print ddd;
Expand All @@ -30,11 +32,11 @@ precondition: n >= 0
}

// list of functions
var list = [testList, fib];
var list = [fib];

// call the function in the list
print "T";
print list[0]()[2];
print list[0](7);
print list[0];

// are funcitons first class citizens?
Expand Down

0 comments on commit aa417e5

Please sign in to comment.