Skip to content
Snippets Groups Projects
Commit d3f68a60 authored by Claire GIROT--PERSON's avatar Claire GIROT--PERSON
Browse files

4.2

parent 05febac8
No related branches found
No related tags found
No related merge requests found
type command = Push of int | Pop | Swap | Add | Sub | Mul | Div | Rem
DefineMe (* Question 4.1 *)
type program = int * command list
......
......@@ -13,8 +13,30 @@ let string_of_state (cmds,stack) =
let step state =
match state with
| [], _ -> Error("Nothing to step",state)
| Pop::_, [] -> Error("Nothing to pop",state)
| Swap::_, _::[] -> Error("Nothing to swap",state)
| Swap::_, [] -> Error("Nothing to swap",state)
| Add::_, _::[] -> Error("Nothing to add",state)
| Add::_, [] -> Error("Nothing to add",state)
| Sub::_, _::[] -> Error("Nothing to substract",state)
| Sub::_, [] -> Error("Nothing to substract",state)
| Mul::_, _::[] -> Error("Nothing to multiply",state)
| Mul::_, [] -> Error("Nothing to multiply",state)
| Div::_, _::[] -> Error("Nothing to divide",state)
| Div::_, [] -> Error("Nothing to divide",state)
| Div::_, _::0::_ -> Error("Forbidden operation",state)
| Rem::_, _::[] -> Error("Nothing to divide",state)
| Rem::_, [] -> Error("Nothing to divide",state)
| Rem::_, _::0::_ -> Error("Forbidden operation",state)
(* Valid configurations *)
| DefineMe :: q , stack -> Ok (q, stack)
| (Push x)::t, stack -> Ok (q, x::stack)
| Pop::q, x::stack -> Ok (q, stack)
| Swap::q, x::y::stack -> Ok (q, y::x::stack)
| Add::q, x::y::stack -> Ok (q, (x + y)::stack)
| Sub::q, x::y::stack -> Ok (q, (x - y)::stack)
| Mul::q, x::y::stack -> Ok (q, (x * y)::stack)
| Div::q, x::y::stack -> Ok (q, (x / y)::stack)
| Rem::q, x::y::stack -> Ok (q, (x mod y)::stack)
let eval_program (numargs, cmds) args =
let rec execute = function
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment