X-Git-Url: https://scm.cri.ensmp.fr/git/Faustine.git/blobdiff_plain/50baab1849bddf73ee88b54f52b28060de1ce289..02c1ef905d2101df872ccccccb2e4c7c0a508571:/interpreter/nest.ml diff --git a/interpreter/nest.ml b/interpreter/nest.ml new file mode 100644 index 0000000..bc581ff --- /dev/null +++ b/interpreter/nest.ml @@ -0,0 +1,42 @@ +open Types;; +open Basic;; + +let nest_from_string : string -> nest = + fun (s : string) -> + Nstparser.main Nstlexer.token (Lexing.from_string s);; + +let rec basic_from_nest : nest -> basic = + fun (nst : nest) -> + let rec basic_from_nestpar : nestpar -> basic = + fun (nstpar : nestpar) -> + let rec list_from_nestpar : nestpar -> basic list = + fun (np : nestpar) -> + match np with + | Unary n -> [basic_from_nest n] + | Binary (n1, np2) -> [basic_from_nest n1] @ (list_from_nestpar np2) + in + match nstpar with + | Unary n -> basic_from_nest n + | Binary (n1, np2) -> + let bl = list_from_nestpar nstpar in + Vec (new vector (List.length bl) (Array.get (Array.of_list bl))) + in + match nst with + | Scalar s -> R s + | Vector np -> basic_from_nestpar np;; + +let rec basic_to_neststring : basic -> string = + fun (b : basic) -> + match b with + | N i -> string_of_int i + | R f -> string_of_float f + | Zero -> basic_to_neststring (N 0) + | Error -> "Error" + | Vec vec -> + let lpar = "[" in + let rpar = "]" in + let comma_space = ", " in + let basics = Array.init vec#size vec#nth in + let strings = Array.map basic_to_neststring basics in + let combine = String.concat comma_space (Array.to_list strings) in + lpar ^ combine ^ rpar;; \ No newline at end of file