summaryrefslogtreecommitdiff
path: root/src/lang/js-lang
diff options
context:
space:
mode:
Diffstat (limited to 'src/lang/js-lang')
-rw-r--r--src/lang/js-lang/builtin/builtin.ts17
-rw-r--r--src/lang/js-lang/builtin/sbuiltin.ts19
-rw-r--r--src/lang/js-lang/core/eval.ts45
3 files changed, 56 insertions, 25 deletions
diff --git a/src/lang/js-lang/builtin/builtin.ts b/src/lang/js-lang/builtin/builtin.ts
index ed279f6..bc40794 100644
--- a/src/lang/js-lang/builtin/builtin.ts
+++ b/src/lang/js-lang/builtin/builtin.ts
@@ -61,10 +61,12 @@ export const V_BUILTIN_Eq: BUILTIN = (args) => {
}
if (last === firstLast) {
+ last = arg;
continue;
}
if (arg === last) {
+ last = arg;
continue;
}
@@ -74,20 +76,6 @@ export const V_BUILTIN_Eq: BUILTIN = (args) => {
return true;
};
-export const V_BUILTIN_IfElse: BUILTIN = (args) => {
- if (args.length !== 3) {
- throw new Error(`Invalid args for "if": ${JSON.stringify(args)}`);
- }
-
- const [cond, trueVal, falseVal] = args;
-
- if (typeof cond !== "boolean") {
- throw new Error(`Condition value ${JSON.stringify(cond)} is not a boolean`);
- }
-
- return cond ? trueVal : falseVal;
-};
-
export const nameToBUILTIN: Record<string, BUILTIN> = {
arr: V_BUILTIN_Arr,
tostring: V_BUILTIN_ToString,
@@ -95,5 +83,4 @@ export const nameToBUILTIN: Record<string, BUILTIN> = {
sub: V_BUILTIN_Sub,
mul: V_BUILTIN_Mul,
eq: V_BUILTIN_Eq,
- "?": V_BUILTIN_IfElse,
};
diff --git a/src/lang/js-lang/builtin/sbuiltin.ts b/src/lang/js-lang/builtin/sbuiltin.ts
index e5bc6ab..663c2f9 100644
--- a/src/lang/js-lang/builtin/sbuiltin.ts
+++ b/src/lang/js-lang/builtin/sbuiltin.ts
@@ -1,4 +1,4 @@
-import { callFn, getEvaluatedChildren } from "../core/eval";
+import { _evaluate, callFn, getEvaluatedChildren } from "../core/eval";
import { ASTNode, FnPrim, StackFrame } from "../../ts-lang";
type SBUILTIN = (node: ASTNode, frame: StackFrame) => any;
@@ -40,7 +40,24 @@ export const V_SBUILTIN_Map: SBUILTIN = (node, frame) => {
return values.map((v, i) => callFn(fn, [v, i], frame));
};
+export const V_SBUILTIN_IfElse: SBUILTIN = (node, frame) => {
+ const children = node.children;
+
+ if (children.length !== 3) {
+ throw new Error(`Invalid args for "if": ${JSON.stringify(children)}`);
+ }
+
+ const cond = _evaluate(children[0], frame);
+
+ if (typeof cond !== "boolean") {
+ throw new Error(`Condition value ${JSON.stringify(cond)} is not a boolean`);
+ }
+
+ return cond ? _evaluate(children[1], frame) : _evaluate(children[2], frame);
+};
+
export const nameToSBUILTIN: Record<string, SBUILTIN> = {
call: V_SBUILTIN_Call,
map: V_SBUILTIN_Map,
+ "?": V_SBUILTIN_IfElse,
};
diff --git a/src/lang/js-lang/core/eval.ts b/src/lang/js-lang/core/eval.ts
index 60a2059..1a9e292 100644
--- a/src/lang/js-lang/core/eval.ts
+++ b/src/lang/js-lang/core/eval.ts
@@ -6,6 +6,7 @@ import {
NodeType,
FnPrim,
SENTINEL_NO_BUILTIN,
+ NamedFnPrim,
} from "../../ts-lang";
import { nameToBUILTIN, nameToSBUILTIN, V_SBUILTIN_Map } from "../builtin";
@@ -27,11 +28,24 @@ const findInStack = (frame: StackFrame, nameToFind: string) => {
return frame.bindings[nameToFind];
}
- if (!frame.parent) {
- throw new Error(`Can't find name ${nameToFind} on the stack`);
+ throw new Error(`Can't find name ${nameToFind} on the stack`);
+};
+
+const handleBind = (node: ASTNode, frame: StackFrame) => {
+ const inner = _evaluate(node.children[1], frame);
+
+ if (inner.fn) {
+ const named: NamedFnPrim<any, any, any, any> = {
+ args: inner.args,
+ fn: inner.fn,
+ name: node.children[0].name,
+ frame,
+ };
+
+ return named;
}
- return findInStack(frame.parent, nameToFind);
+ return inner;
};
const handleFn = (node: ASTNode): FnPrim => {
@@ -46,13 +60,22 @@ const handleFn = (node: ASTNode): FnPrim => {
const mapZip = (args: ASTNode[], values: any[]) =>
Object.fromEntries(args.map(({ name }, i) => [name, values[i]]));
-export const callFn = (fn: FnPrim, values: any[], frame: StackFrame) =>
- _evaluate(fn.fn, {
- bindings: mapZip(fn.args as ASTNode[], values),
- parent: frame,
+export const callFn = (fn: FnPrim, values: any[], frame: StackFrame) => {
+ if ((fn as NamedFnPrim<any, any, any, any>).frame) {
+ return _evaluate(fn.fn, {
+ bindings: {
+ ...mapZip(fn.args as ASTNode[], values),
+ ...frame.bindings,
+ ...(fn as NamedFnPrim<any, any, any, any>).frame.bindings,
+ },
+ });
+ }
+ return _evaluate(fn.fn, {
+ bindings: { ...mapZip(fn.args as ASTNode[], values), ...frame.bindings },
});
+};
-export const _evaluate = (node: ASTNode, frame: StackFrame) => {
+export const _evaluate = (node: ASTNode, frame: StackFrame): any => {
if (node.type === NodeType.INT) {
return node.value;
}
@@ -62,6 +85,10 @@ export const _evaluate = (node: ASTNode, frame: StackFrame) => {
return handleFn(node);
}
+ if (node.name === "bind") {
+ return handleBind(node, frame);
+ }
+
const builtinResult = mapBuiltins(node, frame);
if (builtinResult !== V_SENTINEL_NO_BUILTIN) {
return builtinResult;
@@ -76,7 +103,7 @@ export const _evaluate = (node: ASTNode, frame: StackFrame) => {
export const getEvaluatedChildren = (node: ASTNode, frame: StackFrame) =>
node.children.map((child) => _evaluate(child, frame));
-export const emptyStackFrame: EmptyStackFrame = { bindings: {}, parent: null };
+export const emptyStackFrame: EmptyStackFrame = { bindings: {} };
export const evaluate = <const Node extends ASTNode>(
node: Node