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

A01 Anton Surkis #1139

Closed
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 @@ -14,5 +14,26 @@ import State;
-- Binop (string, expr, expr)

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (name) -> st (name)
| Const (x) -> x
| Binop (op, ex, ey) ->
var x = evalExpr (st, ex);
var y = evalExpr (st, ey);
case op of
"+" -> x + y
| "-" -> x - y
| "*" -> x * y
| "/" -> x / y
| "%" -> x % y
| "<" -> x < y
| ">" -> x > y
| "<=" -> x <= y
| ">=" -> x >= y
| "==" -> x == y
| "!=" -> x != y
| "&&" -> x && y
| "!!" -> x !! y
esac
esac
}
51 changes: 47 additions & 4 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,37 @@ 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 (conf@[stack, state, world], prog) {
case prog of
{} -> conf
| ins:prog ->
case ins of
READ -> case readWorld (world) of [x, world] -> eval ([x : stack, state, world], prog) esac
| WRITE -> case stack of x : stack -> eval ([stack, state, writeWorld (x, world)], prog) esac
| CONST (x) -> eval ([x : stack, state, world], prog)
| LD (name) -> eval ([state (name) : stack, state, world], prog)
| ST (name) -> case stack of x : stack -> eval ([stack, state <- [name, x], world], prog) esac
| BINOP (op) ->
case stack of y : x : stack ->
var z = case op of
"+" -> x + y
| "-" -> x - y
| "*" -> x * y
| "/" -> x / y
| "%" -> x % y
| "<" -> x < y
| ">" -> x > y
| "<=" -> x <= y
| ">=" -> x >= y
| "==" -> x == y
| "!=" -> x != y
| "&&" -> x && y
| "!!" -> x !! y
esac;
eval ([z : stack, state, world], prog)
esac
esac
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +67,26 @@ 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 (x) -> {CONST (x)}
| Binop (op, ex, ey) -> BINOP (op) : compileExpr (ey) +++ compileExpr (ex)
esac
}

fun compileStmt (stmt) {
case stmt of
Skip -> {}
| Assn (name, expr) -> ST (name) : compileExpr (expr)
| Seq (stmt1, stmt2) -> compileStmt (stmt2) +++ compileStmt (stmt1)
| Read (name) -> {ST (name), READ}
| Write (expr) -> WRITE : compileExpr (expr)
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")
reverse (compileStmt (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 (conf@[state, world], stmt) {
case stmt of
Skip -> conf
| Assn (name, expr) -> [state <- [name, evalExpr (state, expr)], world]
| Seq (stmt1, stmt2) -> eval (eval (conf, stmt1), stmt2)
| Read (name) -> case readWorld (world) of [x, world] -> [state <- [name, x], world] esac
| Write (expr) -> [state, writeWorld (evalExpr (state, expr), world)]
esac
}

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