From c5ef9cc39c14da5d31454f7c482d1cbc65d4611b Mon Sep 17 00:00:00 2001 From: Anton Surkis Date: Sat, 7 Oct 2023 18:49:54 +0300 Subject: [PATCH] Optimize task 1 --- src/SM.lama | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/SM.lama b/src/SM.lama index f0deec027..cb3538585 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -70,17 +70,17 @@ 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 } @@ -88,5 +88,5 @@ fun compileStmt (stmt) { -- Takes a statement, returns a list of stack machine -- instructions. public fun compileSM (stmt) { - compileStmt (stmt) + reverse (compileStmt (stmt)) }