Skip to content

Commit

Permalink
Solve first
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriyNovitskiy committed Oct 30, 2024
1 parent 8d24383 commit eb4f819
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
24 changes: 23 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ import State;
-- Const (int) |
-- Binop (string, expr, expr)

public fun evalOp (op, x, y) {
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
esac
}

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
-- fun (st) {
case expr of
Var (a) -> st (a)
| Const (c) -> c
| Binop (op, l, r) -> evalOp (op, evalExpr (st, l), evalExpr (st, r))
esac
-- }
}
40 changes: 36 additions & 4 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,28 @@ 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 (c@[stack, st, w], insns) {
-- c.fst - stack, c.snd -> c
case insns of
{} -> c
| head:tail ->
-- printf(head.string, "\n");
case head of
WRITE -> case stack of
h:t -> eval ([t, st, writeWorld(h, w)], tail)
esac
| READ -> var new_c = readWorld(w);
eval ([new_c.fst : stack, st, ((new_c).snd)], tail)
| BINOP (s) -> case stack of
y:x:t -> eval([evalOp(s, x, y): t, st, w] , tail)
esac
| LD (x) -> eval ([(st)(x) : stack, st, w], tail)
| ST (x) -> case stack of
h:t -> eval ([t, (st) <- [x, h], w], tail)
esac
| CONST (n) -> eval ([n : stack, st, w], tail)
esac
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +58,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")
-- failure ("compileExpr not implemented\n")
case expr of
Var (x) -> {LD (x)}
| Const (n) -> {CONST (n)}
| Binop (op, x, y) -> compileExpr (x) +++ compileExpr (y) +++ { 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")
-- failure ("compileSM not implemented\n")
case stmt of
Assn (x, e) -> compileExpr(e) +++ { ST (x)}
| Seq (lhs, rhs) -> compileSM(lhs) +++ compileSM(rhs)
| Skip -> {}
| Read (x) -> { READ, ST (x)}
| Write (e) -> compileExpr(e) +++ { WRITE }
esac
}
16 changes: 15 additions & 1 deletion src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ import World;
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
-- printf("bbbbbb\n");
case stmt of
Assn (s, e) -> --printf("assn\n");
[c.fst <- [s, evalExpr (c.fst, e)], c.snd]
| Seq (l, r) -> eval (eval (c, l), r)
| Skip -> --printf("skip\n");
c
| Read (s) ->
var v = readWorld(c.snd);
-- printf("read\n");
[c.fst <- [s, v.fst], v.snd]
| Write (e) ->
[c.fst, [(c.snd).fst, evalExpr(c.fst, e) : ((c.snd).snd)]]
esac
-- failure ("Stmt eval not implemented\n")
}

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

0 comments on commit eb4f819

Please sign in to comment.