constructor application
This commit is contained in:
parent
aace8a5a77
commit
232d9351c1
2 changed files with 19 additions and 1 deletions
|
|
@ -65,7 +65,7 @@ export function evaluate(ast: AST, env: Env): Value {
|
|||
return {
|
||||
kind: 'constructor',
|
||||
name: ast.name,
|
||||
args: [] // TODO: constructors args
|
||||
args: []
|
||||
};
|
||||
|
||||
case 'let': {
|
||||
|
|
@ -105,6 +105,16 @@ export function evaluate(ast: AST, env: Env): Value {
|
|||
|
||||
const func = evaluate(ast.func, env);
|
||||
|
||||
// Constructor application
|
||||
if (func.kind === 'constructor') {
|
||||
const argValues = ast.args.map(arg => evaluate(arg, env));
|
||||
return {
|
||||
kind: 'constructor',
|
||||
name: func.name,
|
||||
args: [...func.args, ...argValues]
|
||||
};
|
||||
}
|
||||
|
||||
if (func.kind !== 'closure')
|
||||
throw new Error('Not a function');
|
||||
|
||||
|
|
|
|||
|
|
@ -37,3 +37,11 @@ e(`factorial = n \\ n
|
|||
| n \\ n * factorial (n - 1);
|
||||
|
||||
factorial 5`)
|
||||
|
||||
e(`factorial = n \\ n
|
||||
| 0 \\ 1
|
||||
| n \\ n * factorial (n - 1);
|
||||
|
||||
factorial 5`)
|
||||
|
||||
e('some5 = Some 5; some5');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue