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 Aleksandr Shefer #1212

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

public fun evalOp (op, left, right) {
case op 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
| o -> failure ("incorrect op\n")
esac
}

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (s) -> st (s)
| Const (x) -> x
| Binop (op, left, right) -> evalOp(op, (evalExpr(st, left)), (evalExpr(st, right)))
| e -> failure ("incorrect expr\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failure("... %s ...", expr.string) or do not do it at all (then you will receive match failure with the object that was unmatched). In future it will be easier to establish forgotten case

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,23 @@ public fun showSM (prg) {

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

fun evalInsn (c@[stack, state, world], insn) {
case insn of
ST (x) -> case stack of (z : s) -> [s, state <- [x, z], world] esac
| LD (x) -> [state(x) : stack, state, world]
| CONST (x) -> [x : stack, state, world]
| BINOP (op) -> case stack of (y : (x : s)) -> [evalOp(op, x, y) : s, state, world] esac
| READ -> case readWorld(world) of [res, newWorld] -> [res : stack, state, newWorld] esac
| WRITE -> case stack of (z : s) -> [s, state, writeWorld(z, world)] esac
esac
}

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

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +53,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 (x) -> {LD (x)}
| Const (x) -> {CONST (x)}
| Binop (op, left, right) -> compileExpr(left) +++ compileExpr(right) +++ {BINOP(op)}
| e -> failure ("incorrect expr (compileExpr)\n")
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 (x, expr) -> compileExpr(expr) +++ {ST (x)}
| Seq (stmt1, stmt2) -> compileSM(stmt1) +++ compileSM(stmt2)
| Read (x) -> {READ} +++ {ST (x)}
| Write (expr) -> compileExpr(expr) +++ {WRITE}
| Skip -> {}
| s -> failure ("incorrect stmt (compileSM)\n")
esac
}
11 changes: 9 additions & 2 deletions src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ import World;
-- Read (string) |
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
fun eval (c@[state, world], stmt) {
case stmt of
Assn (x, expr) -> [state <- [x, evalExpr(state, expr)], world]
| Seq (stmt1, stmt2) -> eval(eval(c, stmt1), stmt2)
| Read (x) -> case readWorld(world) of [res, newWorld] -> [state <- [x, res], newWorld] esac
| Write (expr) -> [state, writeWorld(evalExpr(state, expr), world)]
| Skip -> c
| s -> failure ("Stmt eval not implemented\n")
esac
}

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