Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
reshilkin committed Oct 25, 2024
1 parent 8d24383 commit d7474c0
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 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) -> (
var v1 = evalExpr (st, e1);
var v2 = evalExpr (st, e2);
case op of
"+" -> v1 + v2
| "-" -> v1 - v2
| "*" -> v1 * v2
| "/" -> v1 / v2
| "%" -> v1 % v2
| "<" -> v1 < v2
| "<=" -> v1 <= v2
| ">" -> v1 > v2
| ">=" -> v1 >= v2
| "==" -> v1 == v2
| "!=" -> v1 != v2
| "&&" -> v1 && v2
| "!!" -> v1 !! v2
esac
)
esac
}
84 changes: 81 additions & 3 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,57 @@ 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")
case c of [stack, state, [input, output]] ->
case insns of
{} -> c
| READ : insns -> eval (
case input of v : input ->
[v : stack, state, [input, output]]
esac,
insns
)
| WRITE : insns -> eval (
case stack of v : stack ->
[stack, state, [input, v : output]]
esac,
insns
)
| BINOP (op) : insns -> eval (
case stack of v2 : v1 : stack ->
case op of
"+" -> [(v1 + v2) : stack, state, [input, output]]
| "-" -> [(v1 - v2) : stack, state, [input, output]]
| "*" -> [(v1 * v2) : stack, state, [input, output]]
| "/" -> [(v1 / v2) : stack, state, [input, output]]
| "%" -> [(v1 % v2) : stack, state, [input, output]]
| "<" -> [(v1 < v2) : stack, state, [input, output]]
| "<=" -> [(v1 <= v2) : stack, state, [input, output]]
| ">" -> [(v1 > v2) : stack, state, [input, output]]
| ">=" -> [(v1 >= v2) : stack, state, [input, output]]
| "==" -> [(v1 == v2) : stack, state, [input, output]]
| "!=" -> [(v1 != v2) : stack, state, [input, output]]
| "&&" -> [(v1 && v2) : stack, state, [input, output]]
| "!!" -> [(v1 !! v2) : stack, state, [input, output]]
esac
esac,
insns
)
| LD (x) : insns -> eval (
[state (x) : stack, state, [input, output]],
insns
)
| ST (x) : insns -> eval (
case stack of v : stack ->
[stack, state <- [x, v], [input, output]]
esac,
insns
)
| CONST (n) : insns -> eval (
[n : stack, state, [input, output]],
insns
)
esac
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +88,40 @@ 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) -> (
var sm1 = compileExpr (e1);
var sm2 = compileExpr (e2);
case op of
"+" -> sm1 +++ sm2 +++ (BINOP ("+") : {})
| "-" -> sm1 +++ sm2 +++ (BINOP ("-") : {})
| "*" -> sm1 +++ sm2 +++ (BINOP ("*") : {})
| "/" -> sm1 +++ sm2 +++ (BINOP ("/") : {})
| "%" -> sm1 +++ sm2 +++ (BINOP ("%") : {})
| "<" -> sm1 +++ sm2 +++ (BINOP ("<") : {})
| "<=" -> sm1 +++ sm2 +++ (BINOP ("<=") : {})
| ">" -> sm1 +++ sm2 +++ (BINOP (">") : {})
| ">=" -> sm1 +++ sm2 +++ (BINOP (">=") : {})
| "==" -> sm1 +++ sm2 +++ (BINOP ("==") : {})
| "!=" -> sm1 +++ sm2 +++ (BINOP ("!=") : {})
| "&&" -> sm1 +++ sm2 +++ (BINOP ("&&") : {})
| "!!" -> sm1 +++ sm2 +++ (BINOP ("!!") : {})
esac
)
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) -> compileExpr (e) +++ (ST (x) : {})
| Seq (s1, s2) -> compileSM (s1) +++ compileSM (s2)
| Skip -> {}
| Read (x) -> READ : ST (x) : {}
| Write (e) -> compileExpr (e) +++ (WRITE : {})
esac
}
17 changes: 16 additions & 1 deletion src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ import World;
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
case c of
[state, world] ->
case stmt of
Assn (x, e) -> [state <- [x, evalExpr (state, e)], world]
| Seq (s1, s2) -> case eval (c, s1) of
c1 -> eval (c1, s2)
esac
| Skip -> [state, world]
| Read (x) -> case world of
[v : i, o] -> [state <- [x, v], [i, o]]
esac
| Write (e) -> case world of
[i, o] -> [state, [i, evalExpr (state, e) : o]]
esac
esac
esac
}

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

0 comments on commit d7474c0

Please sign in to comment.