Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ITMO] A01 Aleksandr Eliseev #1491

Open
wants to merge 1 commit into
base: A01-straight-line-int-sm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,31 @@ import State;
-- Binop (string, expr, expr)

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (vName) -> st (vName)
| Const (value) -> value
| Binop (op, left, right) -> case evalExpr(st, left) of
leftValue -> case evalExpr(st, right) of
rightValue -> evalBinop(op, leftValue, rightValue)
esac
esac
esac
}

public fun evalBinop (op, l, r) {
case op 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
esac
}
44 changes: 41 additions & 3 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,34 @@ public fun showSM (prg) {
map (fun (i) {showSMInsn (i) ++ "\n"}, prg).stringcat
}

fun evalStep (c, inst) {
case c of
[stack, state, world] -> case inst of
READ -> case readWorld (world) of
[value, nextWorld] -> [value : stack, state, nextWorld]
esac
| WRITE -> case stack of
value : newStack -> [newStack, state, writeWorld (value, world)]
esac
| BINOP (op) -> case stack of
right : left : newStack -> [evalBinop (op, left, right) : newStack, state, world]
esac
| LD (varName) -> [state (varName) : stack, state, world]
| ST (varName) -> case stack of
value : newStack -> [newStack, state <- [varName, value], world]
esac
| CONST (value) -> [value : stack, state, world]
esac
esac
}

-- 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 insns of
{} -> c
| inst : rest -> eval (evalStep (c, inst), rest)
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +62,26 @@ 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 (value) -> CONST (value) : {}
| Binop (op, left, right) -> case compileExpr (left) of
leftInsns -> case compileExpr (right) of
rightInsns -> leftInsns +++ rightInsns +++ (BINOP (op) : {})
esac
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 (varName, expr) -> compileExpr (expr) +++ (ST (varName) : {})
| Seq (first, second) -> compileSM (first) +++ compileSM (second)
| Skip -> {}
| Read (varName) -> READ : ST (varName) : {}
| Write (expr) -> compileExpr (expr) +++ (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")
case c of
[state, world] -> case stmt of
Assn (varName, expr) -> [state <- [varName, evalExpr (state, expr)], world]
| Seq (first, second) -> case eval (c, first) of
nextC -> eval(nextC, second)
esac
| Skip -> c
| Read (varName) -> case readWorld (world) of
[value, nextWorld] -> [state <- [varName, value], nextWorld]
esac
| Write (expr) -> case evalExpr (state, expr) of
value -> [state, writeWorld (value, world)]
esac
esac
esac
}

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