summaryrefslogtreecommitdiff
path: root/_khb_experiment/ksh_print.ml
blob: 6b0a7e347eea02b94988d21bcfe5cf1dde7e8993 (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
open Khs_ast

let rec expr_str = function
    | EEmpty -> "()"
    | EInt i -> string_of_int i
    | EStr s -> "\"" ^ s ^ "\""
    | EBool b -> if b then "true" else "false"
    | EFrame -> "#"
    | ELocal s -> "." ^ s
    | EBinary (e1, op, e2) ->
        "(" ^ expr_str e1 ^
        (match op with
        | PLUS -> " + "
        | MINUS -> " - "
        | TIMES -> " * "
        | DIV -> " / "
        | MOD -> " % "
        | EQUAL -> " == "
        | NEQUAL -> " != "
        | GT -> " > "
        | LT -> " < "
        | GE -> " >= "
        | LE -> " <= "
        | AND -> " && "
        | OR -> " || "
        | XOR -> " ^^ "
        ) ^ expr_str e2 ^ ")"
    | EUnary (op, e) ->
        (match op with
        | MINUS -> "-"
        | NOT -> "!"
        ) ^ expr_str e
    | ETernary(c, a, b) ->
        "(" ^ expr_str c ^ " ? " ^ expr_str a ^ " : " ^ expr_str b ^ ")"
    | ECat(x, y) -> expr_str x ^ "\\" ^ expr_str y
    | ELoad(v) -> "@" ^ expr_str v
    | ENewChan -> "<>"

let print_stmt = function
    | SLabel s -> Format.printf "%s:@." s
    | SSet(k, v) -> Format.printf "  %s := %s@." (expr_str k) (expr_str v)
    | SGoto l -> Format.printf "  < %s >@." (expr_str l)
    | SPar l -> Format.printf "  | %s |@." (expr_str l)
    | SRecv (e, c) -> Format.printf "  %s << %s@." (expr_str e) (expr_str c)
    | SSend (e, c) -> Format.printf "  %s >> %s@." (expr_str e) (expr_str c)
    | SUnset l ->
        let rec aux = function
        | [] -> "  ~("
        | [a] -> " ~(" ^ (expr_str a)
        | a::b -> (aux b) ^ ", " ^ (expr_str a)
        in Format.printf "%s)@." (aux l)
    | SExit ->
        Format.printf " exit@."