From abd7ecbe6110920c70d333a38cdc4efda7f918de Mon Sep 17 00:00:00 2001 From: das747 <40293014+das747@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:07:52 +0100 Subject: [PATCH] task 1 --- src/Expr.lama | 24 +++++++++++++++++++++++- src/SM.lama | 37 +++++++++++++++++++++++++++++++++---- src/Stmt.lama | 14 ++++++++++++-- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/Expr.lama b/src/Expr.lama index b75803563..3f1d9a14f 100644 --- a/src/Expr.lama +++ b/src/Expr.lama @@ -14,5 +14,27 @@ 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, e1, e2) -> execOp(op, evalExpr(st, e1), evalExpr(st, e2)) + esac +} + +public fun execOp (op, left, rigth) { + case op of + "+" -> left + rigth + | "-" -> left - rigth + | "/" -> left / rigth + | "*" -> left * rigth + | "%" -> left % rigth + | "<" -> left < rigth + | "<=" -> left <= rigth + | ">" -> left > rigth + | ">=" -> left >= rigth + | "==" -> left == rigth + | "!=" -> left != rigth + | "!!" -> left !! rigth + | "&&" -> left && rigth + esac } diff --git a/src/SM.lama b/src/SM.lama index 5e9e82bd9..01f840eed 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -26,8 +26,27 @@ 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") +fun eval ([stack, state, w], insns) { + -- showSMInsn(insns); + -- failure("aboba"); + case insns of + ins:insns -> + case ins of + READ -> case readWorld(w) of + [n, w] -> eval ([n:stack, state, w], insns) + esac + | WRITE -> eval ([tl(stack), state, writeWorld(hd(stack), w)], insns) + | BINOP (op) -> case stack of + r:l:stack -> eval ([execOp(op, l, r):stack, state, w], insns) + esac + | LD (x) -> eval ([state(x):stack, state, w], insns) + | ST (x) -> case stack of + n:stack -> eval ([stack, state <- [x, n], w], insns) + esac + | CONST (n) -> eval ([n:stack, state, w], insns) + esac + | _ -> [stack, state, w] + esac } -- Runs a stack machine for a given input and a given program, returns an output @@ -38,12 +57,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 (x) -> {LD (x)} + | Const (n) -> {CONST (n)} + | Binop (op, e1, e2) -> flatten({compileExpr(e1), compileExpr(e2), {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) -> flatten({compileExpr(e), {ST (x)}}) + | Seq (s1, s2) -> flatten({compileSM(s1), compileSM(s2)}) + | Skip -> {} + | Read (x) -> {READ, ST (x)} + | Write (e) -> flatten({compileExpr(e), {WRITE}}) + esac } diff --git a/src/Stmt.lama b/src/Stmt.lama index 67ec6db9e..b35f72af5 100644 --- a/src/Stmt.lama +++ b/src/Stmt.lama @@ -16,8 +16,18 @@ import World; -- Read (string) | -- Write (expr) | -fun eval (c, stmt) { - failure ("Stmt eval not implemented\n") +fun eval ([st, w], stmt) { + case stmt of + Assn (x, e) -> [st <- [x, evalExpr(st, e)], w] + | Seq (s1, s2) -> case eval([st, w], s1) of + [st, w] -> eval([st, w], s2) + esac + | Skip -> [st, w] + | Read (x) -> case readWorld(w) of + [n, w] -> [st <- [x, n], w] + esac + | Write (e) -> [st, writeWorld(evalExpr(st, e), w)] + esac } -- Evaluates a program with a given input and returns an output