more compiler stuff
This commit is contained in:
parent
bdb3117e07
commit
4289d04ec3
79
compile.phi
79
compile.phi
@ -2,7 +2,9 @@
|
|||||||
(fn Emitter (ast filename) (do
|
(fn Emitter (ast filename) (do
|
||||||
(let output ())
|
(let output ())
|
||||||
|
|
||||||
(let (enter_scope leave_scope define_sym get_sym) (call Syms))
|
(let (enter_scope leave_scope define_sym get_sym print_syms) (call Syms))
|
||||||
|
|
||||||
|
(let (let_node_reg_count let_node_reg_increment) (call Counter))
|
||||||
|
|
||||||
(fn generate () (do
|
(fn generate () (do
|
||||||
(call emit "#!/usr/bin/env node\n")
|
(call emit "#!/usr/bin/env node\n")
|
||||||
@ -62,6 +64,7 @@
|
|||||||
|
|
||||||
(let sym (call get_sym value))
|
(let sym (call get_sym value))
|
||||||
(if (== sym null) (do
|
(if (== sym null) (do
|
||||||
|
(call print_syms)
|
||||||
(call panic "undefined symbol '%' on line %" value line)
|
(call panic "undefined symbol '%' on line %" value line)
|
||||||
))
|
))
|
||||||
|
|
||||||
@ -122,11 +125,11 @@
|
|||||||
(call leave_scope)
|
(call leave_scope)
|
||||||
) (if (== id "let") (do
|
) (if (== id "let") (do
|
||||||
(let (_ pat expr) s)
|
(let (_ pat expr) s)
|
||||||
(call emit "let ")
|
(let reg (call let_node_reg_count))
|
||||||
(call emit_pat pat)
|
(call let_node_reg_increment)
|
||||||
(call emit " = runtime.assignValue(")
|
(call emit (call format "let r_% = " reg))
|
||||||
(call emit_expr expr)
|
(call emit_expr expr)
|
||||||
(call emit ")")
|
(call emit_let_node pat reg)
|
||||||
) (if (== id "do") (do
|
) (if (== id "do") (do
|
||||||
(call enter_scope)
|
(call enter_scope)
|
||||||
(call discover_syms (call slice s 1))
|
(call discover_syms (call slice s 1))
|
||||||
@ -139,7 +142,7 @@
|
|||||||
(call emit_pat pat)
|
(call emit_pat pat)
|
||||||
(call emit " of ")
|
(call emit " of ")
|
||||||
(call emit_expr expr)
|
(call emit_expr expr)
|
||||||
(call emit ") {\n")
|
(call emit ".values) {\n")
|
||||||
(call emit_expr body)
|
(call emit_expr body)
|
||||||
(call emit "}")
|
(call emit "}")
|
||||||
(call leave_scope)
|
(call leave_scope)
|
||||||
@ -251,6 +254,31 @@
|
|||||||
))))))))))))))))))))))))
|
))))))))))))))))))))))))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(fn emit_let_node (pat base_reg) (do
|
||||||
|
(let (pat_ty line) pat)
|
||||||
|
(if (== pat_ty "ident") (do
|
||||||
|
(let (_ _ ident) pat)
|
||||||
|
|
||||||
|
(if (== ident "_") (return))
|
||||||
|
|
||||||
|
(call emit (call format ";\nlet _% = r_%" ident base_reg))
|
||||||
|
(call define_sym ident ("let" line))
|
||||||
|
) (if (== pat_ty "list") (do
|
||||||
|
(let (_ _ pats) pat)
|
||||||
|
|
||||||
|
(let i 0)
|
||||||
|
(for pat pats (do
|
||||||
|
(let reg (call let_node_reg_count))
|
||||||
|
(call let_node_reg_increment)
|
||||||
|
(call emit (call format ";\nlet r_% = r_%.values[%]" reg base_reg i))
|
||||||
|
(call emit_let_node pat reg)
|
||||||
|
(+= i 1)
|
||||||
|
))
|
||||||
|
) (do
|
||||||
|
(call panic "malformed pattern on line %" line)
|
||||||
|
)))
|
||||||
|
))
|
||||||
|
|
||||||
(fn emit_binary_op (s id) (do
|
(fn emit_binary_op (s id) (do
|
||||||
(let (_ left right) s)
|
(let (_ left right) s)
|
||||||
(call emit (call format "runtime.%(" id))
|
(call emit (call format "runtime.%(" id))
|
||||||
@ -263,7 +291,6 @@
|
|||||||
(fn emit_assign_expr (s line id) (do
|
(fn emit_assign_expr (s line id) (do
|
||||||
(let (_ (target_type) expr) s)
|
(let (_ (target_type) expr) s)
|
||||||
(if (!= target_type "ident") (do
|
(if (!= target_type "ident") (do
|
||||||
) (do
|
|
||||||
(call panic "cannot assign to expression on line %" line)
|
(call panic "cannot assign to expression on line %" line)
|
||||||
))
|
))
|
||||||
(let (_ (_ _ ident)) s)
|
(let (_ (_ _ ident)) s)
|
||||||
@ -327,6 +354,20 @@
|
|||||||
(return (generate))
|
(return (generate))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(fn Counter () (do
|
||||||
|
(let counter 0)
|
||||||
|
|
||||||
|
(fn count () (do
|
||||||
|
(return counter)
|
||||||
|
))
|
||||||
|
|
||||||
|
(fn increment () (do
|
||||||
|
(+= counter 1)
|
||||||
|
))
|
||||||
|
|
||||||
|
(return (count increment))
|
||||||
|
))
|
||||||
|
|
||||||
(fn Syms () (do
|
(fn Syms () (do
|
||||||
(let syms (null (
|
(let syms (null (
|
||||||
("format" ("builtin" "builtinFormat"))
|
("format" ("builtin" "builtinFormat"))
|
||||||
@ -390,11 +431,26 @@
|
|||||||
(return (call find_sym syms ident))
|
(return (call find_sym syms ident))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(fn print_syms_node (syms depth) (do
|
||||||
|
(let (parent map) syms)
|
||||||
|
(for (ident sym) map (do
|
||||||
|
(call println "%- %: %" (call indent depth) ident sym)
|
||||||
|
))
|
||||||
|
(if (!= parent null) (do
|
||||||
|
(call print_syms_node parent (+ depth 1))
|
||||||
|
))
|
||||||
|
))
|
||||||
|
|
||||||
|
(fn print_syms () (do
|
||||||
|
(call print_syms_node syms 0)
|
||||||
|
))
|
||||||
|
|
||||||
(return (
|
(return (
|
||||||
enter_scope
|
enter_scope
|
||||||
leave_scope
|
leave_scope
|
||||||
define
|
define
|
||||||
get
|
get
|
||||||
|
print_syms
|
||||||
))
|
))
|
||||||
))
|
))
|
||||||
|
|
||||||
@ -581,7 +637,6 @@
|
|||||||
(call println "illegal char '%'" ch)
|
(call println "illegal char '%'" ch)
|
||||||
(+= i 1)
|
(+= i 1)
|
||||||
)))))))
|
)))))))
|
||||||
|
|
||||||
))
|
))
|
||||||
(return tokens)
|
(return tokens)
|
||||||
))
|
))
|
||||||
@ -658,16 +713,18 @@
|
|||||||
(if (not silent) (call println "reading file..."))
|
(if (not silent) (call println "reading file..."))
|
||||||
(let text (call read_text_file input_filename))
|
(let text (call read_text_file input_filename))
|
||||||
|
|
||||||
//(call println "=== text ===")
|
(call println "=== text ===")
|
||||||
//(call println text)
|
// (call println text)
|
||||||
|
(call println (call len text))
|
||||||
|
|
||||||
(if (not silent) (call println "tokenizing..."))
|
(if (not silent) (call println "tokenizing..."))
|
||||||
(let tokens (call tokenize text))
|
(let tokens (call tokenize text))
|
||||||
|
|
||||||
// (call println "=== tokens ===")
|
(call println "=== tokens ===")
|
||||||
// (for (tok line value) tokens (do
|
// (for (tok line value) tokens (do
|
||||||
// (call println "%\t%\t%" line tok (if (!= value null) value ""))
|
// (call println "%\t%\t%" line tok (if (!= value null) value ""))
|
||||||
// ))
|
// ))
|
||||||
|
(call println (call len tokens))
|
||||||
|
|
||||||
(if (not silent) (call println "parsing..."))
|
(if (not silent) (call println "parsing..."))
|
||||||
(let parser (call Parser tokens))
|
(let parser (call Parser tokens))
|
||||||
|
@ -2,8 +2,7 @@ import fs from "node:fs";
|
|||||||
import process from "node:process";
|
import process from "node:process";
|
||||||
|
|
||||||
export class Runtime {
|
export class Runtime {
|
||||||
constructor({ filename, args }) {
|
constructor({ filename, args } = {}) {
|
||||||
this.syms = { parent: undefined, map: new Map(this.builtins) };
|
|
||||||
this.callStack = [{ name: "<entry>", line: 0 }];
|
this.callStack = [{ name: "<entry>", line: 0 }];
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
this.args = args ?? process.argv.slice(2);
|
this.args = args ?? process.argv.slice(2);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user