summaryrefslogtreecommitdiff
path: root/main.ml
blob: 20e3dd32720bbda1b914cde5a75251752b349f86 (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
open Ast

(* command line options *)
let dump = 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.";
    "--vest", 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;

    let prog = File_parser.parse_file !ifile in
    if !dump then Ast_printer.print_prog Format.std_formatter prog;
    try
      if !vtest then begin
        let s0 = Interpret.program_init_state prog "test" in
        Format.printf "Init state:@.";
        Data.print_state s0;
        let rec it i st =
          let st, outputs, next_st =
            Interpret.program_step prog st
            ["i", Data.VInt i] "test"
          in
          Format.printf "@.> Step %d:@." i;
          Data.print_state st;
          match List.assoc "exit" outputs with
          | Data.VBool false -> it (i+1) next_st
          | _ -> ()
        in
        it 0 s0
      end;
      if !test then begin
        let s0 = Interpret.program_init_state prog "test" in
        let rec it i st =
          let st, outputs, next_st =
            Interpret.program_step prog st
            ["i", Data.VInt i] "test"
          in
          Format.printf "%d. %s %s %s@." i 
            (Data.str_of_value (List.assoc "a" outputs))
            (Data.str_of_value (List.assoc "b" outputs))
            (Data.str_of_value (List.assoc "c" outputs));
          match List.assoc "exit" outputs with
          | Data.VBool false -> it (i+1) next_st
          | _ -> ()
        in
        it 0 s0
      end
    with
    | Data.Combinatorial_cycle v ->
      Format.eprintf "Combinatorial cycle (%s)@." v
    | Data.Type_error e ->
      Format.eprintf "Typing error: %s@." e
    | Data.Not_implemented l ->
      Format.eprintf "Not implemented: %s@." l
    | Data.No_variable id ->
      Format.eprintf "No such variable: %s@." id