diff --git a/course/lecture5-intermediate-representation/lec5.mbt b/course/lecture5-intermediate-representation/lec5.mbt new file mode 100644 index 0000000..6853533 --- /dev/null +++ b/course/lecture5-intermediate-representation/lec5.mbt @@ -0,0 +1,119 @@ +typealias Name = String + +enum Ast { + Var(Name) + Int(Int) + Add(Ast, Ast) + Mul(Ast, Ast) + Let(Name, Ast, Ast) + If(Ast, Ast, Ast) +} derive(Eq, Show) + +enum Knf { + Var(Name) + Int(Int) + Add(Name, Name) + Mul(Name, Name) + Let(Name, Knf, Knf) + If(Name, Knf, Knf) +} derive(Eq, Show) + +pub struct AstToKnfEnv { + mut counter : Int +} + +pub fn AstToKnfEnv::new() -> AstToKnfEnv { + { counter: 0 } +} + +/// Generate a fresh variable name +fn AstToKnfEnv::fresh(self : AstToKnfEnv) -> Name { + let name = "x\{self.counter}" + self.counter += 1 + name +} + +pub fn ast_to_knf(env : AstToKnfEnv, ast : Ast) -> Knf { + match ast { + // Trivial cases + Var(name) => Knf::Var(name) + Int(i) => Knf::Int(i) + Add(a, b) => { + let a_name = env.fresh() + let b_name = env.fresh() + + // let a_name = in + // let b_name = in + // a_name + b_name + Knf::Let( + a_name, + ast_to_knf(env, a), + Knf::Let(b_name, ast_to_knf(env, b), Knf::Add(a_name, b_name)), + ) + } + Mul(a, b) => { + let a_name = env.fresh() + let b_name = env.fresh() + + // let a_name = in + // let b_name = in + // a_name * b_name + Knf::Let( + a_name, + ast_to_knf(env, a), + Knf::Let(b_name, ast_to_knf(env, b), Knf::Mul(a_name, b_name)), + ) + } + Let(name, value, body) => + // let name = in + // + Knf::Let(name, ast_to_knf(env, value), ast_to_knf(env, body)) + If(cond, then, els) => { + let cond_name = env.fresh() + + // let cond_name = in + // if cond_name then else + Knf::Let( + cond_name, + ast_to_knf(env, cond), + Knf::If(cond_name, ast_to_knf(env, then), ast_to_knf(env, els)), + ) + } + } +} + +fn main { + let ast = Ast::Add(Ast::Int(1), Ast::Int(2)) + let knf = Knf::Let( + "x0", + Knf::Int(1), + Knf::Let("x1", Knf::Int(2), Knf::Add("x0", "x1")), + ) + if ast_to_knf(AstToKnfEnv::new(), ast) == knf { + println("Test passed") + } else { + println("Test failed") + } + + // A more complex example + let ast = Ast::Let( + "x", + Ast::Int(1), + Ast::If( + Ast::Var("x"), + Ast::Add(Ast::Var("x"), Ast::Int(2)), + Ast::Mul(Ast::Var("x"), Ast::Int(2)), + ), + ) + let knf = ast_to_knf(AstToKnfEnv::new(), ast) + println(knf) + // Let("x", Int(1), + // Let("x0", Var("x"), + // If("x0", + // Let("x1", Var("x"), + // Let("x2", Int(2), + // Add("x1", "x2"))), + // Let("x3", Var("x"), + // Let("x4", Int(2), + // Mul("x3", "x4")))))) +} diff --git a/course/lecture5-intermediate-representation/lec5.pdf b/course/lecture5-intermediate-representation/lec5.pdf new file mode 100755 index 0000000..3879675 Binary files /dev/null and b/course/lecture5-intermediate-representation/lec5.pdf differ diff --git a/course/lecture6-closure/lec6.pdf b/course/lecture6-closure/lec6.pdf new file mode 100755 index 0000000..4b6426d Binary files /dev/null and b/course/lecture6-closure/lec6.pdf differ diff --git a/course/lecture7-regalloc/lec7.pdf b/course/lecture7-regalloc/lec7.pdf new file mode 100755 index 0000000..20d4128 Binary files /dev/null and b/course/lecture7-regalloc/lec7.pdf differ diff --git a/readme.md b/readme.md index 0baa275..92a3640 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,7 @@ | Part 2 | Parsing | [lec2](./course/lecture2-lex-parse/lec2.pdf) | [课时三](https://www.bilibili.com/video/BV1Eq4EeyEES/?spm_id_from=333.999.0.0) | [lec2.mbt](./course/lecture2-lex-parse/lec2.mbt) | | | Part 3 | Type inferences | [lec3](./course/lecture3-typing/lec3.pdf) |[课时四](https://www.bilibili.com/video/BV1qVtHeQEX5/) | | | | Part 4 | Bidrectional type checking | [lec4](./course/lecture4-bidir/lec4.pdf) | [课时五](https://www.bilibili.com/video/BV151toe2EpF/) | -| Part 5 | IR designs (ANF, CPS, KNF) | | [课时六](https://www.bilibili.com/video/BV1txxjenEXs/) -| Part 6 | Closure calculus | | [课时七](https://www.bilibili.com/video/BV1FysdemEZv/) | -| Part 7 | Register allocation | | [课时八](https://www.bilibili.com/video/BV1GxsZeBENm/) | | | +| Part 5 | IR designs (ANF, CPS, KNF) | [lec5](./course/lecture5-intermediate-representation/lec5.pdf) | [课时六](https://www.bilibili.com/video/BV1txxjenEXs/) | [lec5.mbt](./course/lecture5-intermediate-representation/lec5.mbt) +| Part 6 | Closure calculus | [lec6](./course/lecture6-closure/lec6.pdf) | [课时七](https://www.bilibili.com/video/BV1FysdemEZv/) | +| Part 7 | Register allocation |[lec7](./course/lecture7-regalloc/lec7.pdf) | [课时八](https://www.bilibili.com/video/BV1GxsZeBENm/) | | | | Part 8 | Garbage collection | | | | |