interpreting lists, records, constructors

master
Dustin Swan 6 days ago
parent 1d0f1d5423
commit b5bd084ee4
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -63,6 +63,11 @@ export type Pattern =
// Data Structures // Data Structures
export type List = {
kind: 'list'
elements: AST[]
}
export type Record = { export type Record = {
kind: 'record' kind: 'record'
fields: { [key: string]: AST } fields: { [key: string]: AST }
@ -80,11 +85,6 @@ export type RecordUpdate = {
updates: { [key: string]: AST } updates: { [key: string]: AST }
} }
export type List = {
kind: 'list'
elements: AST[]
}
// Top-level constructs // Top-level constructs
export type Definition = { export type Definition = {

@ -13,6 +13,28 @@ export function evaluate(ast: AST, env: Env): Value {
throw new Error(`Unknown variable: ${ast.name}`); throw new Error(`Unknown variable: ${ast.name}`);
return val; return val;
case 'list':
return {
kind: 'list',
elements: ast.elements.map(el => evaluate(el, env))
};
case 'record':
const fields: { [key: string]: Value } = {};
Object.entries(ast.fields).forEach(([k, v]) => {
fields[k] = evaluate(v, env);
});
return { kind: 'record', fields };
case 'constructor':
return {
kind: 'constructor',
name: ast.name,
args: [] // TODO: constructors args
};
case 'let': case 'let':
const val2 = evaluate(ast.value, env); const val2 = evaluate(ast.value, env);
const newEnv = new Map(env).set(ast.name, val2); const newEnv = new Map(env).set(ast.name, val2);

@ -27,7 +27,9 @@ console.log(tokenize(str));
// const tokens2 = tokenize("(x, y) => x + y"); // const tokens2 = tokenize("(x, y) => x + y");
// const tokens2 = tokenize('point { x = 3 }'); // const tokens2 = tokenize('point { x = 3 }');
// 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('c = 5; { a = 3, b = c }');
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…
Cancel
Save