diff options
author | Alex AUVOLAT <alex.auvolat@ens.fr> | 2014-03-12 10:11:12 +0100 |
---|---|---|
committer | Alex AUVOLAT <alex.auvolat@ens.fr> | 2014-03-12 10:11:12 +0100 |
commit | bfafa7dbc3325749358538a95cbb4831db66b03c (patch) | |
tree | 1eefa8d984472cb47f80b1ff1362c01675d0faa6 /khb/khs_exec_seq.ml | |
download | SystemeReseaux-Projet-bfafa7dbc3325749358538a95cbb4831db66b03c.tar.gz SystemeReseaux-Projet-bfafa7dbc3325749358538a95cbb4831db66b03c.zip |
First commit
Diffstat (limited to 'khb/khs_exec_seq.ml')
-rw-r--r-- | khb/khs_exec_seq.ml | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/khb/khs_exec_seq.ml b/khb/khs_exec_seq.ml new file mode 100644 index 0000000..6e34767 --- /dev/null +++ b/khb/khs_exec_seq.ml @@ -0,0 +1,62 @@ +open Util +open Khs_ast +open Khs_exec + +let chans = Hashtbl.create 12 +let ch_id = + let p = ref 0 in + fun () -> p := !p + 1; !p + +let proc = ref [] +let proc_spawned = ref [] + +let spawn proc pos = + proc_spawned := + { proc with xpos = pos }::(!proc_spawned) + +let newchan proc = + let id = ch_id () in + Hashtbl.add chans id (Queue.create()); + Many (Smap.add (psep^"in") (VInt id) + (Smap.add (psep^"out") (VInt id) Smap.empty)) + +let proc_step proc = + match proc.xstatus with + | PSDone -> false + | PSExec | PSExecRecvd _ -> + exec_stmt proc; + true + | PSSend(c, kv) -> + proc.xstatus <- PSExec; + begin + if (int_of_kbval c) == 0 then + Format.printf "%s@." (kval_descr kv) + else + Queue.push kv (Hashtbl.find chans (int_of_kbval c)) + end; + true + | PSRecv(c) -> + let q = Hashtbl.find chans (int_of_kbval c) in + if not (Queue.is_empty q) then + proc.xstatus <- PSExecRecvd (Queue.pop q); + true + +let exec_program p = + let proc0 = { + xspawn = spawn; + xnewchan = newchan; + xprog = p; + xvals = Smap.empty; + xstatus = PSExec; + xpos = 0 + } in + proc0.xvals <- Smap.add framevar (VInt 0) proc0.xvals; + proc0.xvals <- Smap.add "stdout" (VInt 0) proc0.xvals; + proc := [ proc0 ]; + while List.length !proc > 0 do + proc := List.filter proc_step !proc; + proc := !proc_spawned @ !proc; + proc_spawned := []; + done + + |