From 59b718619c18cba6aebd01ebdb56f12f27c84629 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Sun, 1 Feb 2026 16:23:52 -0700 Subject: [PATCH] interpreting record access --- src/interpreter.ts | 14 ++++++++++++++ src/main.ts | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/interpreter.ts b/src/interpreter.ts index 9c4d071..6cfc171 100644 --- a/src/interpreter.ts +++ b/src/interpreter.ts @@ -28,6 +28,20 @@ export function evaluate(ast: AST, env: Env): Value { return { kind: 'record', fields }; + case 'record-access': + const record = evaluate(ast.record, env); + + if (record.kind !== 'record') + throw new Error('Not a record'); + + const value = record.fields[ast.field]; + + if (value === undefined) { + throw new Error(`Field ${ast.field} not found`); + } + + return value; + case 'constructor': return { kind: 'constructor', diff --git a/src/main.ts b/src/main.ts index 17e6278..9aee4b3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -29,7 +29,8 @@ console.log(tokenize(str)); // const tokens2 = tokenize('add1 = (x \\ x + 1); add1 3'); // const tokens2 = tokenize('sum = x y \\ x + y; sum 5 3'); // const tokens2 = tokenize('[1, 2, 3]'); -const tokens2 = tokenize('c = 5; { a = 3, b = c }'); +// const tokens2 = tokenize('c = 5; { a = 3, b = c }'); +const tokens2 = tokenize('rec = { a = 3, b = 5 }; rec.a'); const p2 = new Parser(tokens2); const ast3 = p2.parse(); console.log(ast3);