interpreting record access
This commit is contained in:
parent
b5bd084ee4
commit
59b718619c
2 changed files with 16 additions and 1 deletions
|
|
@ -28,6 +28,20 @@ export function evaluate(ast: AST, env: Env): Value {
|
||||||
|
|
||||||
return { kind: 'record', fields };
|
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':
|
case 'constructor':
|
||||||
return {
|
return {
|
||||||
kind: 'constructor',
|
kind: 'constructor',
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,8 @@ console.log(tokenize(str));
|
||||||
// const tokens2 = tokenize('add1 = (x \\ x + 1); add1 3');
|
// const tokens2 = tokenize('add1 = (x \\ x + 1); add1 3');
|
||||||
// const tokens2 = tokenize('sum = x y \\ x + y; sum 5 3');
|
// const tokens2 = tokenize('sum = x y \\ x + y; sum 5 3');
|
||||||
// const tokens2 = tokenize('[1, 2, 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 p2 = new Parser(tokens2);
|
||||||
const ast3 = p2.parse();
|
const ast3 = p2.parse();
|
||||||
console.log(ast3);
|
console.log(ast3);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue