Skip to content

Commit 3f5eb14

Browse files
committed
interpreting functions
1 parent b49b525 commit 3f5eb14

10 files changed

Lines changed: 48 additions & 9 deletions

File tree

bun.lockb

0 Bytes
Binary file not shown.

examples/test.bbee

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const log = func (i) => {
2+
output (stringify i + ", ")
3+
},
4+
5+
mut i = 0,
6+
7+
for(i = i + 1, i < 10) { log@(i) }, 0

examples/test.math

Lines changed: 0 additions & 3 deletions
This file was deleted.

language/interpreter.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ export default function interpret(
4646
};
4747
return new_fn;
4848

49+
case ExpressionKind.FunctionCall: {
50+
const fn: Value = interpretHere(expression.left);
51+
if (typeof fn != "string" && typeof fn != "number") {
52+
const values: Record<identifier, Variable> = {};
53+
if(fn.inputs.length != expression.inputs.length) throw new Error("number of inputs to function call does not match number in declaration")
54+
for (let [index, identifier] of Object.entries(fn.inputs)) {
55+
values[identifier] = {
56+
value: interpretHere(expression.inputs[Number(index)]),
57+
mutable: true,
58+
};
59+
}
60+
const childMemory: MemoryBlock = {
61+
parent: memory,
62+
values: values,
63+
};
64+
return interpret(fn.body, childMemory);
65+
} else throw new Error("trying to call non callable");
66+
}
67+
4968
case ExpressionKind.NumericLiteral:
5069
return expression.value;
5170

@@ -200,9 +219,6 @@ export default function interpret(
200219
// comma calculates left and discards it but it may create side effects like assignment
201220
return right;
202221
}
203-
case "@":
204-
console.log(expression)
205-
return interpretHere(expression.right);
206222
default:
207223
throw new Error("Unknown binary operator: " + expression.operator);
208224
}

language/parser.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export class Parser {
359359
let left = this.parseUnaryOperation();
360360

361361
while (this.top() && this.top().kind == TokenKind.call) {
362-
const operator: string = this.pop().value;
362+
this.pop(); // @
363363

364364
if (PrimitiveTokens.includes(this.top().kind)) {
365365
const right: Expression = this.parseUnaryOperation();
@@ -379,11 +379,30 @@ export class Parser {
379379

380380
this.pop(); // (
381381

382-
const inputs = [];
382+
const inputs: Expression[] = [];
383383

384384
while (this.top().kind !== TokenKind.closePar) {
385-
385+
// this feels illegal
386+
const input = this.parseAssignmentOperation();
387+
inputs.push(input);
388+
389+
if (this.top().kind !== TokenKind.comma && this.top().kind !== TokenKind.closePar)
390+
throw new Error(
391+
"Expected comma operator or closing parentheses in function inputs"
392+
);
393+
394+
if (this.top().kind === TokenKind.comma) this.pop(); // ,
386395
}
396+
397+
this.pop(); // )
398+
399+
const expression: FunctionCallExpression = {
400+
kind: ExpressionKind.FunctionCall,
401+
left: left,
402+
inputs: inputs,
403+
};
404+
405+
left = expression;
387406
}
388407

389408
return left;

0 commit comments

Comments
 (0)