|
@ -1,5 +1,7 @@ |
|
|
|
|
|
import { convertASM } from "./asm" |
|
|
|
|
|
import { convertIR, prettyPrintIR } from "./ir" |
|
|
import { parse } from "./parser" |
|
|
import { parse } from "./parser" |
|
|
import { convertIR } from "./ir" |
|
|
|
|
|
|
|
|
|
|
|
import type { Attr, Decl, Stmt, Type, VarDecl } from "./ast" |
|
|
import type { Attr, Decl, Stmt, Type, VarDecl } from "./ast" |
|
|
|
|
|
|
|
|
type SymbolDefn = { |
|
|
type SymbolDefn = { |
|
@ -21,31 +23,35 @@ export const compile = (source: string): string => { |
|
|
const decls = ast.filter((x): x is Decl => x.type == "decl") |
|
|
const decls = ast.filter((x): x is Decl => x.type == "decl") |
|
|
const stmts = ast.filter((x): x is Stmt => x.type == "stmt") |
|
|
const stmts = ast.filter((x): x is Stmt => x.type == "stmt") |
|
|
|
|
|
|
|
|
console.log("Declarations", decls) |
|
|
|
|
|
console.log("Statements", stmts) |
|
|
|
|
|
|
|
|
|
|
|
// 3. Create top-level symbol map
|
|
|
// 3. Create top-level symbol map
|
|
|
const symbols = processDecls(decls) |
|
|
const symbols = processDecls(decls) |
|
|
|
|
|
|
|
|
console.log("Symbols", symbols) |
|
|
|
|
|
|
|
|
|
|
|
// TODO: Some form of type-checking
|
|
|
// TODO: Some form of type-checking
|
|
|
|
|
|
|
|
|
// 4. Generate IR
|
|
|
// 4. Generate IR
|
|
|
const ir = convertIR(stmts) |
|
|
const ir = convertIR(stmts) |
|
|
|
|
|
|
|
|
console.log("IR", ir) |
|
|
|
|
|
|
|
|
console.log("=== IR === ") |
|
|
|
|
|
console.log(ir.map(prettyPrintIR).join("\n")) |
|
|
|
|
|
|
|
|
|
|
|
// 5. Generate code
|
|
|
|
|
|
const insns = convertASM(ir) |
|
|
|
|
|
const code = insns.join("\n") |
|
|
|
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
|
console.log("=== ASM === ") |
|
|
|
|
|
return code |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export const processDecls = (decls: Array<Decl>): SymbolMap => decls.reduce((symbols, decl) => { |
|
|
|
|
|
|
|
|
export const processDecls = (decls: Array<Decl>): SymbolMap => decls.reduce(processDecl, {}) |
|
|
|
|
|
|
|
|
|
|
|
export const processDecl = (symbols: SymbolMap, decl: Decl): SymbolMap => { |
|
|
const symbol = processVarDecl(symbols, decl) |
|
|
const symbol = processVarDecl(symbols, decl) |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
...symbols, |
|
|
...symbols, |
|
|
[symbol.name]: symbol, |
|
|
[symbol.name]: symbol, |
|
|
} |
|
|
} |
|
|
}, {}) |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
export const processVarDecl = (symbols: SymbolMap, decl: VarDecl): SymbolDefn => { |
|
|
export const processVarDecl = (symbols: SymbolMap, decl: VarDecl): SymbolDefn => { |
|
|
if (typeof symbols[decl.name] !== 'undefined') { |
|
|
if (typeof symbols[decl.name] !== 'undefined') { |
|
|