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

[HSE] A01 Ekaterina Nikolaeva #1499

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
25 changes: 24 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ import State;
-- Const (int) |
-- Binop (string, expr, expr)

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

}

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
}
33 changes: 30 additions & 3 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,25 @@ public fun showSM (prg) {

-- Stack machine interpreter. Takes an SM-configuration and a program,
-- returns a final configuration

fun evalInsn ([stack, states, world], insn) {
case insn of
CONST (n) -> [n:stack, states, world]
| ST (x) -> [snd (stack), states <- [x, fst (stack)], world]
| READ -> [fst (readWorld(world)):stack, states, snd (readWorld(world))]
| LD (x) -> [states(x):stack, states, world]
| WRITE -> [snd (stack), states, writeWorld (fst (stack), world)]
| BINOP (op) -> case stack of
r:l:tail -> [evalOp(op, l, r):tail, states, world]
esac
esac
}

fun eval (c, insns) {
failure ("SM eval not implemented\n")
case insns of
{} -> c
| head:tail -> eval(evalInsn(c, head), tail)
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +55,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 (n) -> {CONST (n)}
| Binop (op, l, r) -> compileExpr(l) +++ compileExpr(r) +++ {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
Read (x) -> {READ, ST (x)}
| Write (expr) -> compileExpr(expr) +++ {WRITE}
| Seq (l, r) -> compileSM(l) +++ compileSM(r)
| Assn (x, e) -> compileExpr(e) +++ {ST (x)}
| Skip -> {}
esac
}
13 changes: 11 additions & 2 deletions src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ import World;
-- Read (string) |
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
fun eval ([states, world], stmt) {
case stmt of
Skip -> [states, world]
| Read (x) -> case readWorld(world) of
[value, new_world] -> [states <- [x, value], new_world]
esac
| Seq (l, r) -> eval (eval ([states, world], l), r)
| Assn (x, e) -> [states <- [x, evalExpr(states, e)], world]
| Write (expr) -> [states, writeWorld(evalExpr (states, expr), world) ]
esac
}


-- Evaluates a program with a given input and returns an output
public fun evalStmt (input, stmt) {
eval ([emptyState, createWorld (input)], stmt).snd.getOutput
Expand Down
Loading