Skip to main content
Sign in
Snippets Groups Projects
Commit 589b4a25 authored by Augustin Jaujay's avatar Augustin Jaujay
Browse files

Instruction Exec fonctionnelle

parent 6e082ac0
Branches
No related tags found
No related merge requests found
......@@ -7,5 +7,7 @@ type program = int * command list
(* Converting a command to a string for printing *)
val string_of_command : command -> string
val string_of_commands : command list -> string
(* Converting a program to a string for printing *)
val string_of_program : program -> string
open Ast
open Printf
let string_of_stack stack = sprintf "[%s]" (String.concat ";" (List.map string_of_int stack))
type argsType = Int of int | InstructionSeq of command list
let rec string_of_stack stack = match stack with
| [] -> ""
| (Int i)::s -> (string_of_int i) ^ string_of_stack s
| (InstructionSeq seq)::s -> (string_of_commands seq) ^ string_of_stack s
let string_of_state (cmds,stack) =
(match cmds with
......@@ -19,30 +24,43 @@ let step state =
| Swap::_, [] -> Error("Nothing to swap",state)
| Add::_, _::[] -> Error("Nothing to add",state)
| Add::_, [] -> Error("Nothing to add",state)
| Add::_, (InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Add::_, _::(InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Sub::_, _::[] -> Error("Nothing to substract",state)
| Sub::_, [] -> Error("Nothing to substract",state)
| Sub::_, (InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Sub::_, _::(InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Mul::_, _::[] -> Error("Nothing to multiply",state)
| Mul::_, [] -> Error("Nothing to multiply",state)
| Mul::_, (InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Mul::_, _::(InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Div::_, _::[] -> Error("Nothing to divide",state)
| Div::_, [] -> Error("Nothing to divide",state)
| Div::_, _::0::_ -> Error("Forbidden operation",state)
| Div::_, _::(Int 0)::_ -> Error("Forbidden operation",state)
| Div::_, (InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Div::_, _::(InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Rem::_, _::[] -> Error("Nothing to divide",state)
| Rem::_, [] -> Error("Nothing to divide",state)
| Rem::_, _::0::_ -> Error("Forbidden operation",state)
| Get::_, i::stack when (List.length stack) < i -> Error("Invalid get operation", state)
| Get::_, i::_ when i < 0 -> Error("Cannot get negative index", state)
| Rem::_, _::(Int 0)::_ -> Error("Forbidden operation",state)
| Rem::_, (InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Rem::_, _::(InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Get::_, (Int i)::stack when (List.length stack) < i -> Error("Invalid get operation", state)
| Get::_, (Int i)::_ when i < 0 -> Error("Cannot get negative index", state)
| Get::_, [] -> Error("Nothing to get", state)
| Exec::_, _ -> Error("Not yet supported", state) (* TODO *)
| Get::_, (InstructionSeq _)::_ -> Error("Wrong arg type", state)
| Exec::_, [] -> Error("Nothing to exec", state)
| Exec::_, (Int _)::_ -> Error("Wrong arg type", state)
(* Valid configurations *)
| (Push x)::q, stack -> Ok (q, x::stack)
| (Push x)::q, stack -> Ok (q, (Int x)::stack)
| Pop::q, _::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)
| Get::q, i::stack -> Ok (q, (List.nth stack i)::stack)
| Add::q, (Int x)::(Int y)::stack -> Ok (q, (Int (x + y))::stack)
| Sub::q, (Int x)::(Int y)::stack -> Ok (q, (Int (x - y))::stack)
| Mul::q, (Int x)::(Int y)::stack -> Ok (q, (Int (x * y))::stack)
| Div::q, (Int x)::(Int y)::stack -> Ok (q, (Int (x / y))::stack)
| Rem::q, (Int x)::(Int y)::stack -> Ok (q, (Int (x mod y))::stack)
| Get::q, (Int i)::stack -> Ok (q, (List.nth stack i)::stack)
| Exec::q, (InstructionSeq s)::stack -> Ok (s @ q, stack)
let eval_program (numargs, cmds) args =
let rec execute = function
......@@ -58,6 +76,11 @@ let eval_program (numargs, cmds) args =
if numargs = List.length args then
match execute (cmds,args) with
| Ok None -> printf "No result\n"
| Ok(Some result) -> printf "= %i\n" result
| Ok(Some result) ->
begin
match result with
| Int x -> printf "= %i\n" x
| InstructionSeq _ -> printf "No result\n"
end
| Error(msg,s) -> printf "Raised error %s in state %s\n" msg (string_of_state s)
else printf "Raised error \nMismatch between expected and actual number of args\n"
val eval_program: Ast.program -> int list -> unit
open Ast
type argsType = Int of int | InstructionSeq of command list
val eval_program: Ast.program -> argsType list -> unit
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment