From c4da8a68ca4eb2b02b2ae4f2b1d9535e6168f46e Mon Sep 17 00:00:00 2001 From: Beliakov Artem Date: Tue, 16 Mar 2021 13:11:29 +0300 Subject: [PATCH] Belyakov Artem HW1 --- src/Expr.lama | 23 ++++++++++++++++++++++- src/SM.lama | 26 ++++++++++++++++++++++---- src/Stmt.lama | 9 ++++++++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/Expr.lama b/src/Expr.lama index b51d5967..6747e42e 100644 --- a/src/Expr.lama +++ b/src/Expr.lama @@ -3,6 +3,23 @@ import List; import State; +public fun evalOp (op, l, r) { + case op of + "+" -> l + r + | "-" -> l - r + | "*" -> l * r + | "/" -> l / r + | "%" -> l % r + | "==" -> l == r + | "!=" -> l != r + | "<" -> l < r + | ">" -> l > r + | "<=" -> l <= r + | ">=" -> l >= r + | "&&" -> l && r + | "!!" -> l !! r + esac +} -- The evaluator itself: takes a state and an expression, -- returns integer value @@ -13,5 +30,9 @@ import State; -- Const (int) | -- 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) -> evalOp(op, evalExpr(st, l), evalExpr(st, r)) + esac } diff --git a/src/SM.lama b/src/SM.lama index bedf3518..b8e1d564 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -8,8 +8,16 @@ import Expr; -- Stack machine interpreter. Takes an SM-configuration and a program, -- returns a final configuration -- Instruction = READ | WRITE | BINOP String | LD X | ST X | CONST N -fun eval (c, insns) { - failure ("SM eval not implemented\n") +fun eval (c@[st, state, w], insns) { + case insns of + READ : p -> local world = readWorld(w); eval([world[0] : st, state, world[1]], p) + | WRITE : p -> eval([st[1], state, writeWorld(st[0], w)], p) + | BINOP(op) : p -> eval([evalOp(op, st[1][0], st[0]) : st[1][1], state, w], p) + | CONST(n) : p -> eval([n : st, state, w], p) + | LD(x) : p -> eval([state(x) : st, state, w], p) + | ST(x) : p -> eval([st[1], state <- [x, st[0]], w], p) + | {} -> [st, state, w] + esac } -- Runs a stack machine for a given input and a given program, returns an output @@ -21,12 +29,22 @@ public fun evalSM (input, insns) { -- Takes an expression, returns a list (of, possibly, lists) -- of stack machine instructions fun compileExpr (expr) { - failure ("compileExpr not implemented\n") + case expr of + Const(n) -> singleton(CONST(n)) + | Var(elem) -> singleton(LD(elem)) + | Binop(op, l, r) -> compileExpr(l) +++ compileExpr(r) +++ singleton(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(x, e) -> compileExpr(e) +++ singleton(ST(x)) + | Write(x) -> compileExpr(x) +++ singleton(WRITE) + | Read(x) -> singleton(READ) +++ singleton(ST(x)) + | Seq(s1, s2) -> compileSM(s1) +++ compileSM(s2) + | Skip -> {} + esac } diff --git a/src/Stmt.lama b/src/Stmt.lama index 9fc3c8c4..f11e381e 100644 --- a/src/Stmt.lama +++ b/src/Stmt.lama @@ -16,7 +16,14 @@ import World; -- Read (string) | -- Write (expr) | fun eval (c, stmt) { - failure ("Stmt eval not implemented\n") + case stmt of + Assn(str, expr) -> [c[0] <- [str, evalExpr(c[0], expr)], c[1]] + | Seq(s1, s2) -> eval(eval(c, s1), s2) + | Skip -> c + | Read(str) -> local res = readWorld(c[1]); + [c[0] <- [str, res[0]], res[1]] + | Write(str) -> [c[0], writeWorld(evalExpr(c[0], str), c[1])] + esac } -- Evaluates a program with a given input and returns an output