Nested vectors are implemented, including parser and class nstio.
[Faustine.git] / interpreter / nest.ml
1 open Types;;
2 open Basic;;
3
4 let nest_from_string : string -> nest =
5 fun (s : string) ->
6 Nstparser.main Nstlexer.token (Lexing.from_string s);;
7
8 let rec basic_from_nest : nest -> basic =
9 fun (nst : nest) ->
10 let rec basic_from_nestpar : nestpar -> basic =
11 fun (nstpar : nestpar) ->
12 let rec list_from_nestpar : nestpar -> basic list =
13 fun (np : nestpar) ->
14 match np with
15 | Unary n -> [basic_from_nest n]
16 | Binary (n1, np2) -> [basic_from_nest n1] @ (list_from_nestpar np2)
17 in
18 match nstpar with
19 | Unary n -> basic_from_nest n
20 | Binary (n1, np2) ->
21 let bl = list_from_nestpar nstpar in
22 Vec (new vector (List.length bl) (Array.get (Array.of_list bl)))
23 in
24 match nst with
25 | Scalar s -> R s
26 | Vector np -> basic_from_nestpar np;;
27
28 let rec basic_to_neststring : basic -> string =
29 fun (b : basic) ->
30 match b with
31 | N i -> string_of_int i
32 | R f -> string_of_float f
33 | Zero -> basic_to_neststring (N 0)
34 | Error -> "Error"
35 | Vec vec ->
36 let lpar = "[" in
37 let rpar = "]" in
38 let comma_space = ", " in
39 let basics = Array.init vec#size vec#nth in
40 let strings = Array.map basic_to_neststring basics in
41 let combine = String.concat comma_space (Array.to_list strings) in
42 lpar ^ combine ^ rpar;;