From eb4f819cc7ca949451723e2899437afd7638f20d Mon Sep 17 00:00:00 2001 From: GrigoriyNovitskiy Date: Thu, 31 Oct 2024 02:10:35 +0300 Subject: [PATCH] Solve first --- src/Expr.lama | 24 +++++++++++++++++++++++- src/SM.lama | 40 ++++++++++++++++++++++++++++++++++++---- src/Stmt.lama | 16 +++++++++++++++- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/Expr.lama b/src/Expr.lama index b758035636..c75df75dfe 100644 --- a/src/Expr.lama +++ b/src/Expr.lama @@ -13,6 +13,28 @@ import State; -- Const (int) | -- Binop (string, expr, expr) +public fun evalOp (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 + esac +} + public fun evalExpr (st, expr) { - failure ("evalExpr not implemented\n") + -- fun (st) { + case expr of + Var (a) -> st (a) + | Const (c) -> c + | Binop (op, l, r) -> evalOp (op, evalExpr (st, l), evalExpr (st, r)) + esac + -- } } diff --git a/src/SM.lama b/src/SM.lama index 5e9e82bd9e..5d6f3cc6ad 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -26,8 +26,28 @@ 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 (c@[stack, st, w], insns) { + -- c.fst - stack, c.snd -> c + case insns of + {} -> c + | head:tail -> + -- printf(head.string, "\n"); + case head of + WRITE -> case stack of + h:t -> eval ([t, st, writeWorld(h, w)], tail) + esac + | READ -> var new_c = readWorld(w); + eval ([new_c.fst : stack, st, ((new_c).snd)], tail) + | BINOP (s) -> case stack of + y:x:t -> eval([evalOp(s, x, y): t, st, w] , tail) + esac + | LD (x) -> eval ([(st)(x) : stack, st, w], tail) + | ST (x) -> case stack of + h:t -> eval ([t, (st) <- [x, h], w], tail) + esac + | CONST (n) -> eval ([n : stack, st, w], tail) + esac + esac } -- Runs a stack machine for a given input and a given program, returns an output @@ -38,12 +58,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") + -- failure ("compileExpr not implemented\n") + case expr of + Var (x) -> {LD (x)} + | Const (n) -> {CONST (n)} + | Binop (op, x, y) -> compileExpr (x) +++ compileExpr (y) +++ { 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") + -- failure ("compileSM not implemented\n") + case stmt of + Assn (x, e) -> compileExpr(e) +++ { ST (x)} + | Seq (lhs, rhs) -> compileSM(lhs) +++ compileSM(rhs) + | Skip -> {} + | Read (x) -> { READ, ST (x)} + | Write (e) -> compileExpr(e) +++ { WRITE } + esac } diff --git a/src/Stmt.lama b/src/Stmt.lama index 67ec6db9e6..4ef27bd21b 100644 --- a/src/Stmt.lama +++ b/src/Stmt.lama @@ -17,7 +17,21 @@ import World; -- Write (expr) | fun eval (c, stmt) { - failure ("Stmt eval not implemented\n") + -- printf("bbbbbb\n"); + case stmt of + Assn (s, e) -> --printf("assn\n"); + [c.fst <- [s, evalExpr (c.fst, e)], c.snd] + | Seq (l, r) -> eval (eval (c, l), r) + | Skip -> --printf("skip\n"); + c + | Read (s) -> + var v = readWorld(c.snd); + -- printf("read\n"); + [c.fst <- [s, v.fst], v.snd] + | Write (e) -> + [c.fst, [(c.snd).fst, evalExpr(c.fst, e) : ((c.snd).snd)]] + esac + -- failure ("Stmt eval not implemented\n") } -- Evaluates a program with a given input and returns an output