Got parser working. now i'm switching from toy language to CG syntax. lexer is... done??? maybe
This commit is contained in:
parent
f74d374555
commit
71237d0307
3 changed files with 312 additions and 132 deletions
67
src/main.ts
67
src/main.ts
|
|
@ -2,6 +2,7 @@ import { evaluate } from './interpreter'
|
|||
import type { AST } from './ast'
|
||||
import type { Env } from './env'
|
||||
import { tokenize } from './lexer'
|
||||
import { Parser } from './parser'
|
||||
|
||||
const ast: AST = {
|
||||
kind: 'binaryop',
|
||||
|
|
@ -26,52 +27,28 @@ const ast: AST = {
|
|||
|
||||
const env: Env = new Map();
|
||||
|
||||
const res = evaluate(ast, env);
|
||||
// const res = evaluate(ast, env);
|
||||
|
||||
console.log(res);
|
||||
// console.log(res);
|
||||
|
||||
const ast2: AST = {
|
||||
kind: 'let',
|
||||
name: 'add',
|
||||
value: {
|
||||
kind: 'lambda',
|
||||
params: ['x', 'y'],
|
||||
body: {
|
||||
kind: 'binaryop',
|
||||
operator: '+',
|
||||
left: {
|
||||
kind: 'variable',
|
||||
name: 'x',
|
||||
},
|
||||
right: {
|
||||
kind: 'variable',
|
||||
name: 'y',
|
||||
}
|
||||
}
|
||||
},
|
||||
body: {
|
||||
kind: 'apply',
|
||||
func: {
|
||||
kind: 'variable',
|
||||
name: 'add'
|
||||
},
|
||||
args: [
|
||||
{
|
||||
kind: 'literal',
|
||||
value: { kind: 'int', value: 2 }
|
||||
},
|
||||
{
|
||||
kind: 'literal',
|
||||
value: { kind: 'int', value: 3 }
|
||||
},
|
||||
]
|
||||
}
|
||||
};
|
||||
const str = `
|
||||
# This is a comment
|
||||
double = x \\ x * 2
|
||||
read_line! : Unit \\ String
|
||||
`;
|
||||
console.log(tokenize(str));
|
||||
|
||||
const env2: Env = new Map();
|
||||
// const tokens = tokenize("x");
|
||||
// const p = new Parser(tokens);
|
||||
// console.log(p.parse());
|
||||
|
||||
const res2 = evaluate(ast2, env2);
|
||||
|
||||
console.log(res2);
|
||||
|
||||
console.log(tokenize("let x = 5"));
|
||||
/*
|
||||
const tokens2 = tokenize("let x = (y) => 5 + y in x(3)");
|
||||
// const tokens2 = tokenize("let x = 5 in x * 4");
|
||||
// const tokens2 = tokenize("(x, y) => x + y");
|
||||
const p2 = new Parser(tokens2);
|
||||
const ast3 = p2.parse();
|
||||
const env3: Env = new Map();
|
||||
console.log(ast3);
|
||||
console.log(evaluate(ast3, env3));
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue