summaryrefslogtreecommitdiff
path: root/main.ml
blob: bd67cb494e7a1d2fefef81a1a72b871754b265c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
open Ast

module Interpret = Interpret2.I

(* command line options *)
let dump = ref false
let dumprn = ref false
let test = ref false
let vtest = ref false
let ifile = ref ""

let usage = "usage: analyzer [options] file.scade"

let options = [
    "--dump", Arg.Set dump, "Dump program source.";
    "--dump-rn", Arg.Set dumprn, "Dump program source, after renaming.";
    "--vtest", Arg.Set vtest, "Verbose testing.";
    "--test", Arg.Set test, "Simple testing.";
]

let () =
    Arg.parse options (fun f -> ifile := f) usage;

    if !ifile = "" then begin
        Format.eprintf "No input file...@.";
        exit 1
    end;

    try
      let prog = File_parser.parse_file !ifile in
      if !dump then Ast_printer.print_prog Format.std_formatter prog;

      let prog = Rename.rename_prog prog in
      if !dumprn then Ast_printer.print_prog Format.std_formatter prog;

      if !vtest then begin
        let s0 = Interpret.init_state prog "test" in
        Format.printf "Init state:@.";
        Interpret.print_state Format.std_formatter s0;
        let rec it i st =
          let next_st, out =
            Interpret.step st
            ["i", Interpret.int_value i]
          in
          Format.printf "@.> Step %d:@." i;
          Interpret.print_state Format.std_formatter st;
          Format.printf "Outputs:@.";
          List.iter
            (fun (k, v) -> Format.printf "%s = %s@."
                k (Interpret.str_repr_of_val v))
              out;
          if not (Interpret.as_bool (List.assoc "exit" out)) then
            it (i+1) next_st
        in
        it 0 s0
      end;

      if !test then begin
        let s0 = Interpret.init_state prog "test" in
        let rec it i st =
          let next_st, outputs =
            Interpret.step st
            ["i", Interpret.int_value i]
          in
          Format.printf "%d. %s %s %s@." i 
            (Interpret.str_repr_of_val (List.assoc "a" outputs))
            (Interpret.str_repr_of_val (List.assoc "b" outputs))
            (Interpret.str_repr_of_val (List.assoc "c" outputs));
          if not (Interpret.as_bool (List.assoc "exit" outputs)) then
            it (i+1) next_st
        in
        it 0 s0
      end
    with
    | Util.NoLocError e -> Format.eprintf "Error: %s@." e
    | Util.LocError(l, e) ->
        Format.eprintf "Error: %s@." e;
        List.iter
          (fun loc -> Format.eprintf "At: %s@." (Ast_printer.string_of_extent loc))
          l