Skip to content

Commit

Permalink
[feat] 完成了词法解析
Browse files Browse the repository at this point in the history
  • Loading branch information
BreakingLead committed Sep 7, 2024
1 parent 9e39105 commit 214b1c1
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 264 deletions.
274 changes: 137 additions & 137 deletions src/bin/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,151 +5,151 @@ fn main {
@fs.write_to_string("run/tmp.out", result)
}

// fn main {
// let argv = @env.get_args()
// let mut file = None
// let no_run = Ref::new(false)
// let print_ast = Ref::new(false)
// let print_knf = Ref::new(false)
// let print_each_knf = Ref::new(false)
// let print_final_knf = Ref::new(false)
// let print_closure_ir = Ref::new(false)
// let print_asm = Ref::new(false)
// let knf_opt_iters = Ref::new(10)
// let run_with_interpreter = Ref::new(false)
// @ArgParser.parse(
// [
// (
// "--interpret",
// "-i",
// @ArgParser.Set(run_with_interpreter),
// "Run with interpreter",
// ),
// ("--print-ast", "", @ArgParser.Set(print_ast), "Print AST"),
// ("--no-run", "", @ArgParser.Set(no_run), "Do not run the program"),
// ("--print-knf", "", @ArgParser.Set(print_knf), "Print initial KNF"),
// (
// "--print-each-knf",
// "",
// @ArgParser.Set(print_each_knf),
// "Print each KNF in optimization",
// ),
// (
// "--print-final-knf",
// "",
// @ArgParser.Set(print_final_knf),
// "Print final KNF",
// ),
// (
// "--print-closure-ir",
// "",
// @ArgParser.Set(print_closure_ir),
// "Print closure IR",
// ),
// ("--print-asm", "", @ArgParser.Set(print_asm), "Print assembly"),
// (
// "--knf-opt-iters",
// "N",
// @ArgParser.String(
// fn(s) {
// let i = @strconv.parse_int?(s)
// match i {
// Ok(i) => knf_opt_iters.val = i
// Err(e) => @util.die("Invalid number")
// }
// },
// ),
// "Number of optimization iterations",
// ),
// ],
// fn(s) {
// if file.is_empty().not() {
// @util.die("multiple files are given")
// }
// file = Some(s)
// },
// "",
// argv,
// )
fn main_w() -> Unit {
let argv = @env.get_args()
let mut file = None
let no_run = Ref::new(false)
let print_ast = Ref::new(false)
let print_knf = Ref::new(false)
let print_each_knf = Ref::new(false)
let print_final_knf = Ref::new(false)
let print_closure_ir = Ref::new(false)
let print_asm = Ref::new(false)
let knf_opt_iters = Ref::new(10)
let run_with_interpreter = Ref::new(false)
@ArgParser.parse(
[
(
"--interpret",
"-i",
@ArgParser.Set(run_with_interpreter),
"Run with interpreter",
),
("--print-ast", "", @ArgParser.Set(print_ast), "Print AST"),
("--no-run", "", @ArgParser.Set(no_run), "Do not run the program"),
("--print-knf", "", @ArgParser.Set(print_knf), "Print initial KNF"),
(
"--print-each-knf",
"",
@ArgParser.Set(print_each_knf),
"Print each KNF in optimization",
),
(
"--print-final-knf",
"",
@ArgParser.Set(print_final_knf),
"Print final KNF",
),
(
"--print-closure-ir",
"",
@ArgParser.Set(print_closure_ir),
"Print closure IR",
),
("--print-asm", "", @ArgParser.Set(print_asm), "Print assembly"),
(
"--knf-opt-iters",
"N",
@ArgParser.String(
fn(s) {
let i = @strconv.parse_int?(s)
match i {
Ok(i) => knf_opt_iters.val = i
Err(e) => @util.die("Invalid number")
}
},
),
"Number of optimization iterations",
),
],
fn(s) {
if file.is_empty().not() {
@util.die("multiple files are given")
}
file = Some(s)
},
"",
argv,
)

// // Input
// let file = if file.is_empty() {
// println("no input file")
// @util.die("no input file")
// } else {
// file.unwrap()
// }
// let contents = @fs.read_to_string(file)
// let typechecked = to_typecheked(contents)
// if print_ast.val {
// println(typechecked.to_string())
// }
// Input
let file = if file.is_empty() {
println("no input file")
@util.die("no input file")
} else {
file.unwrap()
}
let contents = @fs.read_to_string(file)
let typechecked = to_typecheked(contents)
if print_ast.val {
println(typechecked.to_string())
}

// // To KNF
// let external_fns = externals()
// let knf_env = @knf.KnfEnv::new(external_fns)
// let knf = knf_env.to_knf(typechecked)
// if print_knf.val {
// println("Initial KNF:")
// println(knf.to_pretty_print())
// }
// To KNF
let external_fns = externals()
let knf_env = @knf.KnfEnv::new(external_fns)
let knf = knf_env.to_knf(typechecked)
if print_knf.val {
println("Initial KNF:")
println(knf.to_pretty_print())
}

// // Optimization
// let mut knf = knf
// for i = 0; i < knf_opt_iters.val; i = i + 1 {
// let new_knf = knf_env.opt_pass(knf)
// if new_knf == knf {
// break
// }
// knf = new_knf
// if print_each_knf.val {
// println("KNF Iteration \{i}:")
// println(knf.to_pretty_print())
// }
// }
// if print_final_knf.val || no_run.val {
// println("Final KNF:")
// println(knf.to_pretty_print())
// }
// Optimization
let mut knf = knf
for i = 0; i < knf_opt_iters.val; i = i + 1 {
let new_knf = knf_env.opt_pass(knf)
if new_knf == knf {
break
}
knf = new_knf
if print_each_knf.val {
println("KNF Iteration \{i}:")
println(knf.to_pretty_print())
}
}
if print_final_knf.val || no_run.val {
println("Final KNF:")
println(knf.to_pretty_print())
}

// // Interpretation
// if run_with_interpreter.val {
// if no_run.val {
// return
// }
// let interpreter = @knf_eval.KnfInterpreter::new()
// add_interpreter_fns(interpreter)
// let _result = match interpreter.eval_full?(knf) {
// Ok(result) => result
// Err(Failure(e)) => {
// println("Error: " + e)
// @util.die("Evaluation error")
// }
// }
// return
// }
// Interpretation
if run_with_interpreter.val {
if no_run.val {
return
}
let interpreter = @knf_eval.KnfInterpreter::new()
add_interpreter_fns(interpreter)
let _result = match interpreter.eval_full?(knf) {
Ok(result) => result
Err(Failure(e)) => {
println("Error: " + e)
@util.die("Evaluation error")
}
}
return
}

// // KNF to closure
// let closure_ir = @closure.knf_program_to_closure(
// knf,
// Map::from_iter(external_fns.iter()),
// )
// if print_closure_ir.val {
// println("Closure IR:")
// println(closure_ir.to_string())
// }
// KNF to closure
let closure_ir = @closure.knf_program_to_closure(
knf,
Map::from_iter(external_fns.iter()),
)
if print_closure_ir.val {
println("Closure IR:")
println(closure_ir.to_string())
}

// // Fill in the holes here!
// Fill in the holes here!

// // Code generation
// let real_asm = @riscv.emit(abort("TODO"))
// Code generation
let real_asm = @riscv.emit(abort("TODO"))

// // Print asm
// for asm in real_asm {
// println(asm)
// println("")
// }
// }
// Print asm
for asm in real_asm {
println(asm)
println("")
}
}

fn to_typecheked(source : String) -> @types.Syntax {
abort("todo")
Expand Down
3 changes: 2 additions & 1 deletion src/pass_lex/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"import": [
"breakinglead/moonbite/types",
"breakinglead/moonbite"
"breakinglead/moonbite",
"breakinglead/moonbite/util"
]
}
3 changes: 0 additions & 3 deletions src/pass_lex/pass_parser.mbt

This file was deleted.

Loading

0 comments on commit 214b1c1

Please sign in to comment.