Skip to content

Commit

Permalink
Merge pull request #2 from moonbitlang/evan/course
Browse files Browse the repository at this point in the history
move lectures0 from pl-lectures to here
  • Loading branch information
bobzhang authored Aug 23, 2024
2 parents 11ab705 + 6208bf0 commit a12d3b2
Show file tree
Hide file tree
Showing 63 changed files with 1,077 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/jekyll-gh-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Sample workflow for building and deploying a Jekyll site to GitHub Pages
name: Deploy Jekyll with GitHub Pages dependencies preinstalled

on:
# Runs on pushes targeting the default branch
push:
branches: ["main","evan/course"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
# Build job
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Delete Submodules
run: |
git rm ./libriscv
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Build with Jekyll
uses: actions/jekyll-build-pages@v1
with:
source: ./
destination: ./_site
- name: Upload artifact
uses: actions/upload-pages-artifact@v3

# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
99 changes: 99 additions & 0 deletions course/lecture0-intro/lec0.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
pub enum Expr {
Cst(Int)
Add(Expr, Expr)
Mul(Expr, Expr)
Var(String)
Let(String, Expr, Expr)
}

// -------------- Tiny Language 2 --------------
pub type Env @immut/list.T[Int]

pub type Cenv @immut/list.T[String]

pub enum ExprNameless {
Cst(Int)
Add(ExprNameless, ExprNameless)
Mul(ExprNameless, ExprNameless)
Var(Int)
Let(ExprNameless, ExprNameless)
}

pub fn index[T : Eq](l : @immut/list.T[T], v : T, ~acc : Int = 0) -> Int? {
match l {
Nil => None
Cons(x, xs) => if x == v { Some(acc) } else { index(xs, v, acc=acc + 1) }
}
}

pub fn comp(e : Expr, cenv : Cenv) -> ExprNameless {
match e {
Cst(i) => Cst(i)
Add(a, b) => Add(comp(a, cenv), comp(b, cenv))
Mul(a, b) => Mul(comp(a, cenv), comp(b, cenv))
Var(x) => Var(index(cenv.0, x).unwrap())
Let(x, e1, e2) => Let(comp(e1, cenv), comp(e2, Cons(x, cenv.0)))
}
}

pub fn eval(e : ExprNameless, env : Env) -> Int {
match e {
Cst(i) => i
Add(a, b) => eval(a, env) + eval(b, env)
Mul(a, b) => eval(a, env) * eval(b, env)
Var(n) => env.0.nth(n).unwrap()
Let(e1, e2) => eval(e2, Cons(eval(e1, env), env.0))
}
}
// ---------------------------------------------

// -------------- Tiny Langauge 1 --------------
pub type EnvTL1 @immut/list.T[(String, Int)]

pub fn assoc[K : Eq, V](key : K, assoc_lst : @immut/list.T[(K, V)]) -> V? {
match assoc_lst {
Nil => None
Cons((k, v), xs) => if k == key { Some(v) } else { assoc(key, xs) }
}
}

pub fn eval_tl1(expr : Expr, env : EnvTL1) -> Int {
match (expr, env) {
(Cst(i), _) => i
(Add(a, b), _) => eval_tl1(a, env) + eval_tl1(b, env)
(Mul(a, b), _) => eval_tl1(a, env) * eval_tl1(b, env)
(Var(x), EnvTL1(env)) => assoc(x, env).unwrap()
(Let(x, e1, e2), EnvTL1(env)) =>
eval_tl1(e2, Cons((x, eval_tl1(e1, env)), env))
}
}
// ---------------------------------------------

// -------------- Stack Machine --------------
pub enum Instr {
Cst(Int)
Add
Mul
Var(Int)
Pop
Swap
}

pub type Instrs @immut/list.T[Instr]

pub type Operand Int

pub type Stack @immut/list.T[Operand]

pub fn eval_stack_machine(instrs : Instrs, stk : Stack) -> Int {
match (instrs.0, stk.0) {
(Cons(Cst(i), rest), _) => eval_stack_machine(rest, Cons(i, stk.0))
(Cons(Add, rest), Cons(Operand(a), Cons(Operand(b), stk))) =>
eval_stack_machine(rest, Cons(a + b, stk))
(Cons(Mul, rest), Cons(Operand(a), Cons(Operand(b), stk))) =>
eval_stack_machine(rest, Cons(a * b, stk))
(Nil, Cons(Operand(a), _)) => a
_ => abort("Matched none")
}
}
// ---------------------------------------------
Loading

0 comments on commit a12d3b2

Please sign in to comment.