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 Artyom Muromskiy #1490

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
22 changes: 21 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,25 @@ 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, l, r) ->
case op of
"+" -> evalExpr(st, l) + evalExpr(st, r)
| "-" -> evalExpr(st, l) - evalExpr(st, r)
| "*" -> evalExpr(st, l) * evalExpr(st, r)
| "/" -> evalExpr(st, l) / evalExpr(st, r)
| "%" -> evalExpr(st, l) % evalExpr(st, r)
| "<" -> evalExpr(st, l) < evalExpr(st, r)
| "<=" -> evalExpr(st, l) <= evalExpr(st, r)
| ">" -> evalExpr(st, l) > evalExpr(st, r)
| ">=" -> evalExpr(st, l) >= evalExpr(st, r)
| "==" -> evalExpr(st, l) == evalExpr(st, r)
| "!=" -> evalExpr(st, l) != evalExpr(st, r)
| "&&" -> evalExpr(st, l) && evalExpr(st, r)
| "!!" -> evalExpr(st, l) !! evalExpr(st, r)
esac
esac
-- failure ("evalExpr not implemented\n")
}
58 changes: 55 additions & 3 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,54 @@ public fun showSMInsn (i) {
esac
}


public fun showSM (prg) {
map (fun (i) {showSMInsn (i) ++ "\n"}, prg).stringcat
}

-- 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 c of [stack, st, rw] ->
case insns of
{} -> c
| ins:inss -> case ins of
READ -> case readWorld (rw) of
[res, rrw] -> eval ([res : c, st, rrw], inss)
esac
| WRITE -> case stack of
{} -> failure ("can't write from empty stack")
| x:xs -> eval ([xs, st, writeWorld(x, rw)], inss)
esac
| BINOP (s) -> case stack of
y:(x:xs) -> case s of
"+" -> eval ([(x + y) : xs, st, rw], inss)
| "-" -> eval ([(x - y) : xs, st, rw], inss)
| "*" -> eval ([(x * y) : xs, st, rw], inss)
| "/" -> eval ([(x / y) : xs, st, rw], inss)
| "%" -> eval ([(x % y) : xs, st, rw], inss)
| "<" -> eval ([(x < y) : xs, st, rw], inss)
| "<=" -> eval ([(x <= y) : xs, st, rw], inss)
| ">" -> eval ([(x > y) : xs, st, rw], inss)
| ">=" -> eval ([(x >= y) : xs, st, rw], inss)
| "==" -> eval ([(x == y) : xs, st, rw], inss)
| "!=" -> eval ([(x != y) : xs, st, rw], inss)
| "&&" -> eval ([(x && y) : xs, st, rw], inss)
| "!!" -> eval ([(x !! y) : xs, st, rw], inss)
esac
| _ -> failure ("can't take enough arguments from stack")
esac
| LD (x) -> eval ([(st (x)) : stack, st,rw], inss)
| ST (x) ->
case stack of
{} -> failure ("can't read from empty stack")
| y:xs -> eval ([xs, st <- [x, y], rw], inss)
esac
| CONST (n) -> eval ([n:stack, st, rw], inss)
esac
esac
esac
-- failure ("SM eval not implemented\n")
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +78,24 @@ 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 (v) -> {LD (v)}
| Const (v) -> {CONST (v)}
| Binop (op, lhs, rhs) -> compileExpr (lhs) +++ compileExpr (rhs) +++ {BINOP (op)}
esac
-- failure ("compileExpr not implemented\n")
}

-- 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
Skip -> {}
| Assn (v, expr) -> compileExpr(expr) +++ {ST (v)}
| Write (expr) -> compileExpr(expr) +++ {WRITE}
| Read (v) -> {READ, ST (v)}
| Seq (l, r) -> compileSM(l) +++ compileSM(r)
esac
-- failure ("compileSM not implemented\n")
}
19 changes: 18 additions & 1 deletion src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ import World;
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
case stmt of
Skip -> c
| Assn (v, expr) -> [c.fst <- [v, evalExpr(c.fst, expr)], c.snd]
| Write (expr) -> case c of [st, rw] -> [st, writeWorld(evalExpr(st, expr), rw)] esac
| Read (v) ->
case c of [st, rw] ->
case readWorld(rw) of [va, nrw] ->
[st <- [v, va], nrw]
esac
esac
| Seq (l, r) ->
(
var c1;
c1 := eval (c, l);
eval (c1, r)
)
esac
-- failure ("Stmt eval not implemented\n")
}

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