summaryrefslogtreecommitdiff
path: root/src/ts-lang/core/common.ts
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2025-11-03 23:40:02 -0800
committerKai Stevenson <kai@kaistevenson.com>2025-11-03 23:40:02 -0800
commit56040f3ff85e77311f0c864a89afd63fcf1bdb50 (patch)
tree2eb0166756e76b0483692e79830329c92e7fdcf3 /src/ts-lang/core/common.ts
parenta11e6780fbb8bd4143dfec44e2ce147b795772d8 (diff)
add js-lang, refactor some ts-lang stuff
Diffstat (limited to 'src/ts-lang/core/common.ts')
-rw-r--r--src/ts-lang/core/common.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/ts-lang/core/common.ts b/src/ts-lang/core/common.ts
new file mode 100644
index 0000000..fa5cb7c
--- /dev/null
+++ b/src/ts-lang/core/common.ts
@@ -0,0 +1,66 @@
+export enum TokenType {
+ OPEN_PAREN = "(",
+ CLOSE_PAREN = ")",
+ SPACE = " ",
+ SEMICOLON = ";",
+ COMMA = ",",
+ NAME = "NAME",
+}
+
+export type Token<
+ Type extends TokenType = TokenType,
+ Name extends string = string
+> = {
+ type: Type;
+ name: Name;
+};
+
+export type LexerCtx = {
+ next: string;
+ nameCollection: string;
+ tokens: readonly Token[];
+};
+
+export enum NodeType {
+ INT = "INT",
+ EXT = "EXT",
+ PARSER_ERROR = "PARSER_ERROR",
+}
+
+export type ASTNode<
+ Type extends NodeType = NodeType,
+ Name extends string = string,
+ Value extends any = any,
+ Children extends readonly ASTNode[] = readonly ASTNode<
+ NodeType,
+ string,
+ any,
+ any
+ >[]
+> = {
+ type: Type;
+ name: Name;
+ value: Value;
+ children: Children;
+};
+
+export type ParserCtx = {
+ remainingTokens: readonly Token[];
+ lastToken: Token | null;
+ stack: readonly ASTNode[];
+};
+
+export type StackFrame<
+ Bindings extends Record<ASTNode["name"], any> = Record<ASTNode["name"], any>,
+ Parent extends StackFrame | null = any
+> = {
+ bindings: Bindings;
+ parent: Parent;
+};
+
+export type EmptyStackFrame = StackFrame<{}, null>;
+
+export type WithPushedBindings<
+ OldFrame extends StackFrame,
+ Bindings extends StackFrame["bindings"]
+> = StackFrame<Bindings, OldFrame>;