Skip to content

Commit

Permalink
Optimize task 1
Browse files Browse the repository at this point in the history
  • Loading branch information
asurkis committed Oct 9, 2023
1 parent 4886eb8 commit c5ef9cc
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ fun compileExpr (expr) {
case expr of
Var (name) -> {LD (name)}
| Const (x) -> {CONST (x)}
| Binop (op, ex, ey) -> compileExpr (ex) +++ compileExpr (ey) +++ {BINOP (op)}
| Binop (op, ex, ey) -> BINOP (op) : compileExpr (ey) +++ compileExpr (ex)
esac
}

fun compileStmt (stmt) {
case stmt of
Skip -> {}
| Assn (name, expr) -> compileExpr (expr) +++ {ST (name)}
| Seq (stmt1, stmt2) -> compileSM (stmt1) +++ compileSM (stmt2)
| Read (name) -> {READ, ST (name)}
| Write (expr) -> compileExpr (expr) +++ {WRITE}
| Assn (name, expr) -> ST (name) : compileExpr (expr)
| Seq (stmt1, stmt2) -> compileStmt (stmt2) +++ compileStmt (stmt1)
| Read (name) -> {ST (name), READ}
| Write (expr) -> WRITE : compileExpr (expr)
esac
}

-- Compiles a statement into a stack machine code.
-- Takes a statement, returns a list of stack machine
-- instructions.
public fun compileSM (stmt) {
compileStmt (stmt)
reverse (compileStmt (stmt))
}

0 comments on commit c5ef9cc

Please sign in to comment.