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

[ITMO] A01 Fedor Vikhnin #1503

Open
wants to merge 1 commit 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 @@ -14,5 +14,27 @@ import State;
-- Binop (string, expr, expr)

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (name) -> st(name)
| Const (value) -> value
| Binop (op, leftExpr, rightExpr) -> evalOp(op, evalExpr(st, leftExpr), evalExpr(st, rightExpr))
esac
}

public fun evalOp (oper, left, right) {
case oper 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
esac
}
45 changes: 42 additions & 3 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,39 @@ public fun showSM (prg) {
map (fun (i) {showSMInsn (i) ++ "\n"}, prg).stringcat
}

fun evalInstruction(c, i) {
case c of
[s, st, w] ->
case i of
READ ->
case readWorld(w) of
[z, w] -> [z:s, st, w]
esac
| WRITE ->
case s of
z:s -> [s, st, writeWorld(z, w)]
esac
| BINOP (op) ->
case s of
y:x:s -> [evalOp(op, x, y):s, st, w]
esac
| LD (name) -> [st(name):s, st, w]
| ST (name) ->
case s of
z:s -> [s, st <- [name, z], w]
esac
| CONST (z) -> [z:s, st, w]
esac
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
{} -> c
| i:insns -> eval(evalInstruction(c, i), insns)
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +67,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 (name) -> {LD (name)}
| Const (value) -> {CONST (value)}
| Binop (op, leftExpr, rightExpr) -> compileExpr (leftExpr) +++ compileExpr (rightExpr) +++ {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 (name, expr) -> compileExpr(expr) +++ {ST (name)}
| Seq (stmt1, stmt2) -> compileSM(stmt1) +++ compileSM(stmt2)
| Skip -> {}
| Read (name) -> {READ, ST (name)}
| Write (expr) -> compileExpr(expr) +++ {WRITE}
esac
}
11 changes: 10 additions & 1 deletion src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ import World;
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
case stmt of
Assn (name, expr) -> [c.fst <- [name, evalExpr(c.fst, expr)], c.snd]
| Seq (stmt1, stmt2) -> eval (eval(c, stmt1), stmt2)
| Skip -> c
| Read (name) ->
case readWorld(c.snd) of
[value, cNew] -> [c.fst <- [name, value], cNew]
esac
| Write (expr) -> [c.fst, writeWorld(evalExpr(c.fst, expr), c.snd)]
esac
}

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