From 64c96a2424e0d145f024e5b3c56cf98ced5d3fa5 Mon Sep 17 00:00:00 2001 From: TendaiIb Date: Fri, 25 Dec 2020 03:20:14 +0300 Subject: [PATCH 1/2] HW1 --- src/Expr.lama | 18 ++++++++++++++++++ src/SM.lama | 25 ++++++++++++++++++++++--- src/Stmt.lama | 13 ++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Expr.lama b/src/Expr.lama index b51d5967..35020678 100644 --- a/src/Expr.lama +++ b/src/Expr.lama @@ -3,6 +3,24 @@ import List; import State; + public fun binopParser (str, x, y) { + case str 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 diff --git a/src/SM.lama b/src/SM.lama index bedf3518..563f9e26 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -9,7 +9,17 @@ import Expr; -- 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") + case c of + [s, st, w@[i, o]] -> case insns of + {} -> c + | Binop (b) : p -> case s of x : y : ss -> eval ([binopParser (b, y, x) : ss, st, w], p) esac + | Const (n) : p -> eval ([n : s, st, w], p) + | Read : p -> case i of x : it -> eval ([x : s, st, [it, o]], p) esac + | Write : p -> case s of x : ss -> eval ([ss, st, [i, x : o]], p) esac + | LD (x) : p -> eval ([st (x) : s, st, w], p) + | ST (x) : p -> case s of y : ss -> eval ([s, (st <- [x, y]), w], p) esac + esac + esac } -- Runs a stack machine for a given input and a given program, returns an output @@ -21,12 +31,21 @@ 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 + Var (x) -> singleton (LD (x)) + | Const (n) -> singleton (expr) + | Binop (b, e1, e2) -> compileExpr (e1) +++ compileExpr (e2) +++ singleton (Binop (b)) + 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) +++ singleton (ST (x)) + | Read (x) -> {Read, ST (x)} + | Write (expr) -> compileExpr (expr) +++ singleton (Write) + | Seq (s1, s2) -> compileSM (s1) +++ compileSM (s2) + esac } diff --git a/src/Stmt.lama b/src/Stmt.lama index 9fc3c8c4..f0d5d7e1 100644 --- a/src/Stmt.lama +++ b/src/Stmt.lama @@ -3,6 +3,9 @@ import State; import Expr; import World; +import List; + + -- Evaluates a statement "stmt" in a configuration "c". -- A configuration is a pair of a state "s" and a world "w". @@ -16,7 +19,15 @@ import World; -- Read (string) | -- Write (expr) | fun eval (c, stmt) { - failure ("Stmt eval not implemented\n") + case c of [st, [i, o]] -> + case stmt of + Skip -> c + | Read (x) -> case i of z : it -> [st <- [x, z], [it, o]] esac + | Write (expr) -> [st, [i, evalExpr (st, expr) : o]] + | Assn (x, e) -> [(st <- [x, evalExpr (st, e)]), [i, o]] + | Seq (s1, s2) -> eval (eval (c, s1), s2) + esac + esac } -- Evaluates a program with a given input and returns an output From 2e36cc2e80f95da5db0adb05151f94740807df14 Mon Sep 17 00:00:00 2001 From: TendaiIb Date: Wed, 27 Jan 2021 22:43:55 +0300 Subject: [PATCH 2/2] Baranov Ilya HW1 --- src/Expr.lama | 39 +++++++++++++++++++++------------------ src/SM.lama | 47 +++++++++++++++++++++++------------------------ src/Stmt.lama | 22 +++++++++------------- 3 files changed, 53 insertions(+), 55 deletions(-) diff --git a/src/Expr.lama b/src/Expr.lama index 35020678..6747e42e 100644 --- a/src/Expr.lama +++ b/src/Expr.lama @@ -3,25 +3,24 @@ import List; import State; - public fun binopParser (str, x, y) { - case str 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 +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 -- @@ -31,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 563f9e26..53855066 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -8,18 +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) { - case c of - [s, st, w@[i, o]] -> case insns of - {} -> c - | Binop (b) : p -> case s of x : y : ss -> eval ([binopParser (b, y, x) : ss, st, w], p) esac - | Const (n) : p -> eval ([n : s, st, w], p) - | Read : p -> case i of x : it -> eval ([x : s, st, [it, o]], p) esac - | Write : p -> case s of x : ss -> eval ([ss, st, [i, x : o]], p) esac - | LD (x) : p -> eval ([st (x) : s, st, w], p) - | ST (x) : p -> case s of y : ss -> eval ([s, (st <- [x, y]), w], p) esac - esac - esac +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 @@ -31,21 +29,22 @@ public fun evalSM (input, insns) { -- Takes an expression, returns a list (of, possibly, lists) -- of stack machine instructions fun compileExpr (expr) { - case expr of - Var (x) -> singleton (LD (x)) - | Const (n) -> singleton (expr) - | Binop (b, e1, e2) -> compileExpr (e1) +++ compileExpr (e2) +++ singleton (Binop (b)) - esac + 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) { - case stmt of - Assn (x, expr) -> compileExpr (expr) +++ singleton (ST (x)) - | Read (x) -> {Read, ST (x)} - | Write (expr) -> compileExpr (expr) +++ singleton (Write) - | Seq (s1, s2) -> compileSM (s1) +++ compileSM (s2) - esac -} + 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 +} \ No newline at end of file diff --git a/src/Stmt.lama b/src/Stmt.lama index f0d5d7e1..16a6c5a7 100644 --- a/src/Stmt.lama +++ b/src/Stmt.lama @@ -3,9 +3,6 @@ import State; import Expr; import World; -import List; - - -- Evaluates a statement "stmt" in a configuration "c". -- A configuration is a pair of a state "s" and a world "w". @@ -19,18 +16,17 @@ import List; -- Read (string) | -- Write (expr) | fun eval (c, stmt) { - case c of [st, [i, o]] -> - case stmt of - Skip -> c - | Read (x) -> case i of z : it -> [st <- [x, z], [it, o]] esac - | Write (expr) -> [st, [i, evalExpr (st, expr) : o]] - | Assn (x, e) -> [(st <- [x, evalExpr (st, e)]), [i, o]] - | Seq (s1, s2) -> eval (eval (c, s1), s2) - esac - esac + 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 public fun evalStmt (input, stmt) { eval ([emptyState, createWorld (input)], stmt).snd.getOutput -} +} \ No newline at end of file