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 Denis Porsev #1501

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 @@ -4,6 +4,25 @@ import List;
import State;


-- Evaluate the binary operation
public fun evalBinop(op) {
case op of
"+" -> infix +
| "-" -> infix -
| "*" -> infix *
| "/" -> infix /
| "%" -> infix %
| "&&" -> infix &&
| "==" -> infix ==
| "!=" -> infix !=
| "<=" -> infix <=
| "<" -> infix <
| ">=" -> infix >=
| ">" -> infix >
| "!!" -> infix !!
esac
}

-- The evaluator itself: takes a state and an expression,
-- returns integer value
--
Expand All @@ -14,5 +33,9 @@ import State;
-- Binop (string, expr, expr)

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Const (n) -> n
| Var (x) -> st (x)
| Binop (op, x, y) -> evalBinop (op) (evalExpr (st, x), evalExpr (st, y))
esac
}
34 changes: 30 additions & 4 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,21 @@ public fun showSM (prg) {

-- Stack machine interpreter. Takes an SM-configuration and a program,
-- returns a final configuration
fun eval (c, insns) {
failure ("SM eval not implemented\n")
fun eval ([stack, st, w], insns) {
case insns of
{} -> [stack, st, w]
| ins : insns -> eval (
case ins of
LD (x) -> [st (x) : stack, st, w]
| CONST (n) -> [n : stack, st, w]
| BINOP (op) -> case stack of h : m : stack -> [evalBinop (op) (m, h) : stack, st, w] esac
| ST (x) -> case stack of h : stack -> [stack, st <- [x, h], w] esac
| READ -> case readWorld (w) of [n, w] -> [n : stack, st, w] esac
| WRITE -> case stack of h : stack -> [stack, st, writeWorld (h, w)] esac
esac,
insns
)
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +51,25 @@ 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) -> singletonBuffer (LD (x))
| Const (n) -> singletonBuffer (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")
fun compile (stmt) {
case stmt of
Assn (x, expr) -> compileExpr (expr) <+ ST (x)
| Seq (stmt1, stmt2) -> compile (stmt1) <+> compile (stmt2)
| Skip -> emptyBuffer ()
| Read (x) -> singletonBuffer (READ) <+ ST (x)
| Write (expr) -> compileExpr (expr) <+ WRITE
esac
}
getBuffer $ compile (stmt)
}
10 changes: 8 additions & 2 deletions src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ import World;
-- Read (string) |
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
fun eval ([st, w], stmt) {
case stmt of
Assn (x, expr) -> [st <- [x, evalExpr (st, expr)], w]
| Seq (stmt1, stmt2) -> eval (eval ([st, w], stmt1), stmt2)
| Skip -> [st, w]
| Read (x) -> case readWorld (w) of [new_x, new_w] -> [st <- [x, new_x], new_w] esac
| Write (expr) -> [st, writeWorld (evalExpr (st, expr), w)]
esac
}

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