Skip to content

Commit

Permalink
Added an eval function and a run function. Eval currently only works …
Browse files Browse the repository at this point in the history
…for Disp while run works for all.
  • Loading branch information
ars13025 committed Jan 30, 2018
1 parent 4637682 commit 95038cc
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Homework_1.sml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,29 @@ fun getB (m : MachineState) = #b m;
fun setA (m : MachineState, newA) = newMachine(newA, #b m);
fun setB (m : MachineState, newB) = newMachine(#a m, newB);

(*Instruction Set*)
datatype Instruction = SetA of int | SetB of int | Add | Sub | Disp;
(*Converts Instruction to a string*)
fun i2s (SetA a) = "SetA " ^ Int.toString(a)
| i2s (SetB b) = "SetB " ^ Int.toString(b)
| i2s Add = "Add"
| i2s Sub = "Sub"
| i2s Disp = "Disp";

(*Evaluates the given Instruction*)
fun eval (m: MachineState, SetA a) : MachineState = m;
| eval (m: MachineState, SetB b): MachineState = m;
| eval (m: MachineState, Add) : MachineState = m;
| eval (m: MachineState, Sub) : MachineState = m;
| eval (m: MachineState, Disp) : MachineState =
(print (Int.toString (#a m) ^ "\n"); m);

(*Runs the list of Instructions*)
fun run (m: MachineState, []: Instruction list) = m
| run (m: MachineState, prog: Instruction list) =
let val instr = hd prog
val instrs = tl prog
val _ = print (i2s instr ^ "\n");
val m1 = eval (m, instr);
in print (i2s instr ^ "\n");
run (m1: MachineState, tl prog : Instruction list)
end;

0 comments on commit 95038cc

Please sign in to comment.