From 3c9fe1dfe5e96e0d23d98f96308de176ac5645fb Mon Sep 17 00:00:00 2001 From: Sleepwalking Date: Wed, 7 Feb 2024 17:22:49 +0100 Subject: [PATCH] done --- src/Expr.lama | 26 +++++++++++++++++++++++++- src/SM.lama | 33 ++++++++++++++++++++++++++++++--- src/Stmt.lama | 11 +++++++++-- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/Expr.lama b/src/Expr.lama index b758035636..db86765919 100644 --- a/src/Expr.lama +++ b/src/Expr.lama @@ -13,6 +13,30 @@ import State; -- Const (int) | -- Binop (string, expr, expr) +public fun evalOp (op, left, right) { + case op of + "+" -> left + right + | "-" -> left - right + | "*" -> left * right + | "/" -> left / right + | "%" -> left % right + | "<" -> left < right + | ">" -> left > right + | "<=" -> left <= right + | ">=" -> left >= right + | "==" -> left == right + | "!=" -> left != right + | "&&" -> left && right + | "!!" -> left !! right + | o -> failure ("incorrect op\n") + esac +} + public fun evalExpr (st, expr) { - failure ("evalExpr not implemented\n") + case expr of + Var (s) -> st (s) + | Const (x) -> x + | Binop (op, left, right) -> evalOp(op, (evalExpr(st, left)), (evalExpr(st, right))) + | e -> failure ("incorrect expr\n") + esac } diff --git a/src/SM.lama b/src/SM.lama index 5e9e82bd9e..bb234187ee 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -26,8 +26,23 @@ public fun showSM (prg) { -- Stack machine interpreter. Takes an SM-configuration and a program, -- returns a final configuration + +fun evalInsn (c@[stack, state, world], insn) { + case insn of + ST (x) -> case stack of (z : s) -> [s, state <- [x, z], world] esac + | LD (x) -> [state(x) : stack, state, world] + | CONST (x) -> [x : stack, state, world] + | BINOP (op) -> case stack of (y : (x : s)) -> [evalOp(op, x, y) : s, state, world] esac + | READ -> case readWorld(world) of [res, newWorld] -> [res : stack, state, newWorld] esac + | WRITE -> case stack of (z : s) -> [s, state, writeWorld(z, world)] esac + esac +} + fun eval (c, insns) { - failure ("SM eval not implemented\n") + case insns of + {} -> c + | i : rest -> eval (evalInsn(c, i), rest) + esac } -- Runs a stack machine for a given input and a given program, returns an output @@ -38,12 +53,24 @@ 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 (x) -> {CONST (x)} + | Binop (op, left, right) -> compileExpr(left) +++ compileExpr(right) +++ {BINOP(op)} + | e -> failure ("incorrect expr (compileExpr)\n") + 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, expr) -> compileExpr(expr) +++ {ST (x)} + | Seq (stmt1, stmt2) -> compileSM(stmt1) +++ compileSM(stmt2) + | Read (x) -> {READ} +++ {ST (x)} + | Write (expr) -> compileExpr(expr) +++ {WRITE} + | Skip -> {} + | s -> failure ("incorrect stmt (compileSM)\n") + esac } diff --git a/src/Stmt.lama b/src/Stmt.lama index 67ec6db9e6..14916e6783 100644 --- a/src/Stmt.lama +++ b/src/Stmt.lama @@ -16,8 +16,15 @@ import World; -- Read (string) | -- Write (expr) | -fun eval (c, stmt) { - failure ("Stmt eval not implemented\n") +fun eval (c@[state, world], stmt) { + case stmt of + Assn (x, expr) -> [state <- [x, evalExpr(state, expr)], world] + | Seq (stmt1, stmt2) -> eval(eval(c, stmt1), stmt2) + | Read (x) -> case readWorld(world) of [res, newWorld] -> [state <- [x, res], newWorld] esac + | Write (expr) -> [state, writeWorld(evalExpr(state, expr), world)] + | Skip -> c + | s -> failure ("Stmt eval not implemented\n") + esac } -- Evaluates a program with a given input and returns an output