From 4c38a3152fbecc363ea992ad0ef49200cc2c32a7 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Wed, 29 Oct 2025 21:27:13 -0700 Subject: start eval --- src/lib/core/eval.ts | 54 +++++++++++++++++++++++++++++++++++++++++++++++--- src/lib/core/parser.ts | 10 ++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/lib/core/eval.ts b/src/lib/core/eval.ts index 779bfd9..7c2c91b 100644 --- a/src/lib/core/eval.ts +++ b/src/lib/core/eval.ts @@ -1,7 +1,55 @@ -import { ASTNode } from "./common"; +import { ASTNode, NodeType } from "./common"; import { Lex } from "./lexer"; import { Parse } from "./parser"; -export type Evaluate = "Not implemented"; +export type FnError = `Function execution error: ${T}`; -const test_result = null as unknown as Evaluate>>; +export type ToStringInner = T extends + | string + | number + ? `${T}` + : T extends readonly any[] + ? T extends [infer Head, ...infer Tail] + ? `${ToStringInner< + Tail, + `${Carry extends "" ? "" : `${Carry}, `}${ToStringInner}` + >}` + : `[${Carry}]` + : FnError<`Can't stringify`>; + +export type BUILTIN_ToString = { + [Idx in Exclude]: Args[Idx] extends ASTNode + ? ToStringInner> + : never; +}; +// export type BUILTIN_Print + +export type SENTINEL_NO_BUILTIN = "__NO_BUILTIN__"; +export type MapBuiltins = Node["name"] extends "tostring" + ? BUILTIN_ToString + : SENTINEL_NO_BUILTIN; + +export type EvalError = `Eval error: ${T}`; + +export type Evaluate = Node["type"] extends NodeType.INT + ? Node["value"] + : Node["type"] extends NodeType.ROOT + ? { + // FIXME render as array?? + [Idx in Exclude< + keyof Node["children"], + keyof any[] + >]: Node["children"][Idx] extends ASTNode + ? Evaluate + : never; + // indexing for now to remove object syntax + // pls fix + }[Exclude] + : Node["type"] extends NodeType.EXT + ? MapBuiltins + : EvalError<`Unhandled node type ${Node["type"]}`>; + +const input = `tostring(5, 3)` as const; +const lex_result = null as unknown as Lex; +const parse_result = null as unknown as Parse; +const eval_result = null as unknown as Evaluate; diff --git a/src/lib/core/parser.ts b/src/lib/core/parser.ts index d608eca..27133b2 100644 --- a/src/lib/core/parser.ts +++ b/src/lib/core/parser.ts @@ -199,6 +199,16 @@ export type _Parse = Ctx["remainingTokens"] extends [ }> : Ctx & Error<`Expected nextToken to be a name or close paren at ${Head["type"]}`> + : Ctx["lastToken"] extends Token + ? // case where we ended with a name + _Parse<{ + lastToken: null; + remainingTokens: []; + stack: PushChildToLastElementOfStack< + Ctx["stack"], + ResolveNodeFromToken + >; + }> : Ctx["stack"][0]; export type Parse = _Parse<{ -- cgit v1.2.3-70-g09d2