Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chapter 5, 6 and 7 #15

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions course/lecture5-intermediate-representation/lec5.mbt
Original file line number Diff line number Diff line change
@@ -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 = <a> in
// let b_name = <b> 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 = <a> in
// let b_name = <b> 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 = <value> in
// <body>
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 = <cond> in
// if cond_name then <then> else <els>
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"))))))
}
Binary file not shown.
Binary file added course/lecture6-closure/lec6.pdf
Binary file not shown.
Binary file added course/lecture7-regalloc/lec7.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | | | |