Skip to content
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
23 changes: 22 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
import List;
import State;

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
Expand All @@ -13,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
}
28 changes: 23 additions & 5 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +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) {
failure ("SM eval not implemented\n")
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
Expand All @@ -21,12 +29,22 @@ 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
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) {
failure ("compileSM not implemented\n")
}
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
}
11 changes: 9 additions & 2 deletions src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ import World;
-- Read (string) |
-- Write (expr) |
fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
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
}
}