Skip to content

Commit

Permalink
task 1
Browse files Browse the repository at this point in the history
  • Loading branch information
das747 committed Feb 7, 2024
1 parent f7d02e9 commit abd7ecb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
24 changes: 23 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,27 @@ import State;
-- Binop (string, expr, expr)

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (x) -> st(x)
| Const (n) -> n
| Binop (op, e1, e2) -> execOp(op, evalExpr(st, e1), evalExpr(st, e2))
esac
}

public fun execOp (op, left, rigth) {
case op of
"+" -> left + rigth
| "-" -> left - rigth
| "/" -> left / rigth
| "*" -> left * rigth
| "%" -> left % rigth
| "<" -> left < rigth
| "<=" -> left <= rigth
| ">" -> left > rigth
| ">=" -> left >= rigth
| "==" -> left == rigth
| "!=" -> left != rigth
| "!!" -> left !! rigth
| "&&" -> left && rigth
esac
}
37 changes: 33 additions & 4 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,27 @@ 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, state, w], insns) {
-- showSMInsn(insns);
-- failure("aboba");
case insns of
ins:insns ->
case ins of
READ -> case readWorld(w) of
[n, w] -> eval ([n:stack, state, w], insns)
esac
| WRITE -> eval ([tl(stack), state, writeWorld(hd(stack), w)], insns)
| BINOP (op) -> case stack of
r:l:stack -> eval ([execOp(op, l, r):stack, state, w], insns)
esac
| LD (x) -> eval ([state(x):stack, state, w], insns)
| ST (x) -> case stack of
n:stack -> eval ([stack, state <- [x, n], w], insns)
esac
| CONST (n) -> eval ([n:stack, state, w], insns)
esac
| _ -> [stack, state, w]
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +57,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, e1, e2) -> flatten({compileExpr(e1), compileExpr(e2), {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
Assn (x, e) -> flatten({compileExpr(e), {ST (x)}})
| Seq (s1, s2) -> flatten({compileSM(s1), compileSM(s2)})
| Skip -> {}
| Read (x) -> {READ, ST (x)}
| Write (e) -> flatten({compileExpr(e), {WRITE}})
esac
}
14 changes: 12 additions & 2 deletions src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ 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, e) -> [st <- [x, evalExpr(st, e)], w]
| Seq (s1, s2) -> case eval([st, w], s1) of
[st, w] -> eval([st, w], s2)
esac
| Skip -> [st, w]
| Read (x) -> case readWorld(w) of
[n, w] -> [st <- [x, n], w]
esac
| Write (e) -> [st, writeWorld(evalExpr(st, e), w)]
esac
}

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

0 comments on commit abd7ecb

Please sign in to comment.