From 87fd9fd1964d42206c9bc3639d942fd1c1cecde5 Mon Sep 17 00:00:00 2001 From: Andrew Gladkikh Date: Sat, 26 Oct 2024 17:19:12 +0300 Subject: [PATCH] task1 done --- src/Expr.lama | 23 ++++++++++++++++++++++- src/SM.lama | 44 ++++++++++++++++++++++++++++++++++++++++---- src/Stmt.lama | 14 +++++++++++++- 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/Expr.lama b/src/Expr.lama index b758035636..104fa472d5 100644 --- a/src/Expr.lama +++ b/src/Expr.lama @@ -3,6 +3,23 @@ import List; import State; +public fun evalBinOp(op, x, y) { + case op of + "+" -> x + y + | "-" -> x - y + | "*" -> x * y + | "/" -> x / y + | "%" -> x % y + | "<" -> x < y + | "<=" -> x <= y + | ">" -> x > y + | ">=" -> x >= y + | "==" -> x == y + | "!=" -> x != y + | "&&" -> x && y + | "!!" -> x !! y + esac +} -- The evaluator itself: takes a state and an expression, -- returns integer value @@ -14,5 +31,9 @@ import State; -- Binop (string, expr, expr) public fun evalExpr (st, expr) { - failure ("evalExpr not implemented\n") + case expr of + Var(x) -> st (x) + | Const(n) -> n + | Binop(op, l, r) -> evalBinOp(op, evalExpr(st, l), evalExpr(st, r)) + esac } diff --git a/src/SM.lama b/src/SM.lama index 5e9e82bd9e..306f6c0069 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -27,7 +27,33 @@ public fun showSM (prg) { -- Stack machine interpreter. Takes an SM-configuration and a program, -- returns a final configuration fun eval (c, insns) { - failure ("SM eval not implemented\n") + case c of + [stack, st, w] -> + case insns of + {} -> c + | cur_insn : next_insns -> + case cur_insn of + READ -> + case readWorld(w) of + [read_value, new_world] -> eval([read_value : stack, st, new_world], next_insns) + esac + | WRITE -> + case stack of + top : rem_stack -> eval([rem_stack, st, writeWorld(top, w)], next_insns) + esac + | BINOP(op) -> + case stack of + y : x : rem_stack -> eval([evalBinOp(op, x, y) : rem_stack, st, w], next_insns) + esac + | LD(name) -> eval([st (name) : stack, st, w], next_insns) + | ST(name) -> + case stack of + top : rem_stack -> eval([rem_stack, st <- [name, top], w], next_insns) + esac + | CONST(n) -> eval([n : stack, st, w], next_insns) + esac + esac + esac } -- Runs a stack machine for a given input and a given program, returns an output @@ -38,12 +64,22 @@ public fun evalSM (input, insns) { -- Compiles an expression into a stack machine code. -- Takes an expression, returns a list of stack machine instructions fun compileExpr (expr) { - failure ("compileExpr not implemented\n") + case expr of + Var(name) -> LD(name) : {} + | Const(n) -> CONST(n) : {} + | Binop(op, l, r) -> compileExpr(l) +++ compileExpr(r) +++ (BINOP(op) : {}) + esac } -- Compiles a statement into a stack machine code. -- Takes a statement, returns a list of stack machine -- instructions. public fun compileSM (stmt) { - failure ("compileSM not implemented\n") -} + case stmt of + Assn(name, expr) -> compileExpr(expr) +++ (ST(name) : {}) + | Seq(stmt1, stmt2) -> compileSM(stmt1) +++ compileSM(stmt2) + | Skip -> {} + | Read(name) -> READ : ST(name) : {} + | Write(expr) -> compileExpr(expr) +++ (WRITE : {}) + esac +} \ No newline at end of file diff --git a/src/Stmt.lama b/src/Stmt.lama index 67ec6db9e6..1486b9fad1 100644 --- a/src/Stmt.lama +++ b/src/Stmt.lama @@ -17,7 +17,19 @@ import World; -- Write (expr) | fun eval (c, stmt) { - failure ("Stmt eval not implemented\n") + case c of + [st, w] -> + case stmt of + Assn(name, expr) -> [st <- [name, evalExpr(st, expr)], w] + | Seq(stmt1, stmt2) -> eval(eval(c, stmt1), stmt2) + | Skip -> c + | Read(name) -> + case readWorld(w) of + [read_value, new_world] -> [st <- [name, read_value], new_world] + esac + | Write(expr) -> [st, writeWorld(evalExpr(st, expr), w)] + esac + esac } -- Evaluates a program with a given input and returns an output