Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPBGU] A01 Stanislav Mishchenko #1496

Open
wants to merge 2 commits into
base: A01-straight-line-int-sm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ import State;
-- Const (int) |
-- Binop (string, expr, expr)

public fun applyBinop(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)
| "&&" -> (x && y)
| "!!" -> (x !! y)
esac
}

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (x) -> st(x)
| Const (i) -> i
| Binop (op, e1, e2) -> applyBinop (op, evalExpr (st, e1), evalExpr (st, e2))
esac
}
48 changes: 44 additions & 4 deletions src/SM.lama
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- Stack machine.
-- SM CONFIG: [STACK, [STATE, WORLD]]

import List;
import World;
Expand All @@ -11,7 +12,7 @@ import Fun;
-- string representation.
public fun showSMInsn (i) {
case i of
READ -> sprintf ("READ")
READ (x) -> sprintf ("READ %s", x)
| WRITE -> sprintf ("WRITE")
| BINOP (s) -> sprintf ("BINOP %s", s)
| LD (x) -> sprintf ("LD %s", x)
Expand All @@ -24,10 +25,39 @@ public fun showSM (prg) {
map (fun (i) {showSMInsn (i) ++ "\n"}, prg).stringcat
}

fun stack(x) { x[0] }

fun state(x) { x[1] }

fun world(x) { x[2] }

fun evalRead (c) {
var newWorld;
newWorld := readWorld(c.world);
[newWorld.fst : c.stack, c.state, newWorld.snd]
}

fun evalIns (c, i) {
case i of
READ -> evalRead (c)
| WRITE -> [c.stack.tl, c.state, writeWorld (c.stack.hd, c.world)]
| BINOP (s) ->
case c.stack of
x : y : tl -> [applyBinop (s, y, x) : tl, c.state, c.world]
esac
| LD (x) -> [state(c) (x) : c.stack, c.state, c.world]
| ST (x) -> [c.stack.tl, (c.state <- [x, c.stack.hd]), c.world]
| CONST (n) -> [n : c.stack, c.state, c.world]
esac
}

-- Stack machine interpreter. Takes an SM-configuration and a program,
-- returns a final configuration
fun eval (c, insns) {
failure ("SM eval not implemented\n")
case insns of
i : insns_tail -> eval(evalIns(c, i), insns_tail)
| {} -> c
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +68,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 (i) -> { CONST (i) }
| Binop (op, e1, e2) -> 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) -> compileExpr (e) +++ { ST (x) }
| Seq (s1, s2) -> compileSM (s1) +++ compileSM (s2)
| Skip -> {}
| Read (x) -> READ : { ST (x) }
| Write (e) -> compileExpr (e) +++ { WRITE }
esac
}
14 changes: 13 additions & 1 deletion src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ import World;
-- Read (string) |
-- Write (expr) |

fun evalRead (c, x) {
var newWorld;
newWorld := readWorld(c.snd);
[(c.fst <- [x, newWorld.fst]), newWorld.snd]
}

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
case stmt of
Assn (x, e) -> [c.fst <- [x, evalExpr (c.fst, e)], c.snd]
| Seq (s1, s2) -> eval (eval (c, s1), s2)
| Skip -> c
| Read (x) -> evalRead(c, x)
| Write (e) -> [c.fst, writeWorld (evalExpr (c.fst, e), c.snd)]
esac
}

-- Evaluates a program with a given input and returns an output
Expand Down
Loading