From 95038cc2f7ca0bd5193da3ac46856f7928f22822 Mon Sep 17 00:00:00 2001 From: Ashley Sanders Date: Tue, 30 Jan 2018 13:15:52 -0500 Subject: [PATCH] Added an eval function and a run function. Eval currently only works for Disp while run works for all. --- Homework_1.sml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Homework_1.sml b/Homework_1.sml index bf08a19..bba3520 100644 --- a/Homework_1.sml +++ b/Homework_1.sml @@ -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; \ No newline at end of file