Skip to content

Commit

Permalink
First task complete
Browse files Browse the repository at this point in the history
  • Loading branch information
bachish committed Oct 10, 2023
1 parent f7d02e9 commit 867f0c3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
28 changes: 27 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ import State;
-- Const (int) |
-- Binop (string, expr, expr)


public fun evalOperation(operation, l, r){
case operation of
"+" -> l + r
|"-" -> l - r
|"*" -> l * r
|"/" -> l / r
|"%" -> l % r
|"==" -> l == r
|"!=" -> l != r
|"<" -> l < r
|"<=" -> l <= r
|">" -> l > r
|">=" -> l >= r
|"&&" -> l > r
|"!!" -> l !! r
| _ -> failure ("Unknown operation '" ++ operation ++ "'!!!\n")
esac
}

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var(varName) -> st(varName)
| Const(x) -> x
| Binop (operation, l, r) -> evalOperation(operation, evalExpr(st, l), evalExpr(st, r))
| _ -> failure ("\nUnknown evalExpr'" ++ expr.string ++ "'!!!\n")
esac
}

40 changes: 35 additions & 5 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,29 @@ public fun showSM (prg) {
map (fun (i) {showSMInsn (i) ++ "\n"}, prg).stringcat
}

-- Stack machine interpreter. Takes an SM-configuration and a program,
fun evalOne(c@[stack, state, world], insn){
case insn of
READ -> case readWorld(world) of [n, w2] -> [n: stack, state, w2] esac
| WRITE -> case stack of s:tail -> [tail, state, writeWorld(s, world)] esac
| BINOP (s) -> case stack of y:x:tail -> [evalOperation(s, x, y): tail, state, world] esac
| LD (x) -> [state(x) : stack, state, world]
| ST (x) -> case stack of s:tail -> [tail, state <- [x, s], world] esac
| CONST (n) -> [n : stack, state, world]
esac
}

-- Stack machine interpreter. Takes
-- an SM-configuration (stack of integers, state, word)
-- and a program
-- returns a final configuration
fun eval (c, insns) {
failure ("SM eval not implemented\n")
fun eval (c@[stack, state, world], insns) {
case insns of
{} -> c
| i: insns -> eval(evalOne(c, i), insns)
esac
}


-- Runs a stack machine for a given input and a given program, returns an output
public fun evalSM (input, insns) {
eval ([{}, emptyState, createWorld (input)], insns)[2].getOutput
Expand All @@ -38,12 +55,25 @@ 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(varName) -> {LD(varName)}
| Const(x) -> {CONST(x)}
| Binop (op, l, r) -> compileExpr(l) +++ compileExpr(r) +++ {BINOP(op)}
| _ -> failure ("\nUnknown evalExpr '" ++ expr.string ++ "' in 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 (varName, expr) -> compileExpr(expr) +++ {ST(varName)}
|Seq (s1, s2) -> compileSM(s1) +++ compileSM(s2)
|Skip -> {}
|Read (varName) -> {READ} +++ {ST(varName)}
|Write (expr) -> compileExpr(expr) +++ {WRITE}
| _ -> failure("Unknown statement '" ++ stmt.string ++ "' im CompileSM\n")
esac
}

23 changes: 20 additions & 3 deletions src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,28 @@ import World;
-- Read (string) |
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
fun read(world, state, varName){
case readWorld(world) of
[value, w2] -> [state <- [varName, value], w2]
| _ -> failure("Reading error")
esac
}

fun eval (c@[state, world], stmt) {
case stmt of
Assn (varName, expr) -> [state <- [varName, evalExpr(state, expr)], world]
|Seq (s1, s2) -> eval(eval(c, s1), s2)
|Skip -> c
|Read (varName) -> case readWorld(world) of
[value, w2] -> [state <- [varName, value], w2]
| _ -> failure("Reading error")
esac
|Write (expr) -> [state, writeWorld(evalExpr(state, expr), world)]
| _ -> failure("Unknown statement \n")
esac
}

-- Evaluates a program with a given input and returns an output
public fun evalStmt (input, stmt) {
eval ([emptyState, createWorld (input)], stmt).snd.getOutput
}
}

0 comments on commit 867f0c3

Please sign in to comment.