Skip to content

Commit

Permalink
task1 done
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowJust committed Oct 26, 2024
1 parent 8d24383 commit 87fd9fd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
23 changes: 22 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
import List;
import State;

public fun evalBinOp(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
| "&&" -> x && y
| "!!" -> x !! y
esac
}

-- The evaluator itself: takes a state and an expression,
-- returns integer value
Expand All @@ -14,5 +31,9 @@ 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, l, r) -> evalBinOp(op, evalExpr(st, l), evalExpr(st, r))
esac
}
44 changes: 40 additions & 4 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,33 @@ 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, st, w] ->
case insns of
{} -> c
| cur_insn : next_insns ->
case cur_insn of
READ ->
case readWorld(w) of
[read_value, new_world] -> eval([read_value : stack, st, new_world], next_insns)
esac
| WRITE ->
case stack of
top : rem_stack -> eval([rem_stack, st, writeWorld(top, w)], next_insns)
esac
| BINOP(op) ->
case stack of
y : x : rem_stack -> eval([evalBinOp(op, x, y) : rem_stack, st, w], next_insns)
esac
| LD(name) -> eval([st (name) : stack, st, w], next_insns)
| ST(name) ->
case stack of
top : rem_stack -> eval([rem_stack, st <- [name, top], w], next_insns)
esac
| CONST(n) -> eval([n : stack, st, w], next_insns)
esac
esac
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +64,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(name) -> LD(name) : {}
| Const(n) -> CONST(n) : {}
| Binop(op, l, r) -> compileExpr(l) +++ compileExpr(r) +++ (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(name, expr) -> compileExpr(expr) +++ (ST(name) : {})
| Seq(stmt1, stmt2) -> compileSM(stmt1) +++ compileSM(stmt2)
| Skip -> {}
| Read(name) -> READ : ST(name) : {}
| Write(expr) -> compileExpr(expr) +++ (WRITE : {})
esac
}
14 changes: 13 additions & 1 deletion src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ import World;
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
case c of
[st, w] ->
case stmt of
Assn(name, expr) -> [st <- [name, evalExpr(st, expr)], w]
| Seq(stmt1, stmt2) -> eval(eval(c, stmt1), stmt2)
| Skip -> c
| Read(name) ->
case readWorld(w) of
[read_value, new_world] -> [st <- [name, read_value], new_world]
esac
| Write(expr) -> [st, writeWorld(evalExpr(st, expr), w)]
esac
esac
}

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

0 comments on commit 87fd9fd

Please sign in to comment.