|
| 1 | +(** Abstraktne süntaksipuu. *) |
| 2 | + |
| 3 | +(** Tüübid. *) |
| 4 | + |
| 5 | +(** Muutuja nimi. *) |
| 6 | +type var = string [@@deriving eq, ord] |
| 7 | + |
| 8 | +(** Binaarne operaator. *) |
| 9 | +type binary = |
| 10 | + | Add (** + *) |
| 11 | + | Sub (** - *) |
| 12 | + | Mul (** * *) |
| 13 | + | Div (** / *) |
| 14 | + | Mod (** % *) |
| 15 | + | Eq (** == *) |
| 16 | + | Ne (** <> *) |
| 17 | + | Lt (** < *) |
| 18 | + | Le (** <= *) |
| 19 | + | Gt (** > *) |
| 20 | + | Ge (** >= *) |
| 21 | +[@@deriving eq, ord] |
| 22 | + |
| 23 | +(** Avaldis. *) |
| 24 | +type expr = |
| 25 | + | Num of int (** Konstant *) |
| 26 | + | Var of var (** Muutuja *) |
| 27 | + | Rand of int * int (** Juhuarv intervallist *) |
| 28 | + | Binary of expr * binary * expr (** Binaarne operaator *) |
| 29 | +[@@deriving eq, ord] |
| 30 | + |
| 31 | +(** Lause. *) |
| 32 | +type stmt = |
| 33 | + | Assign of var * expr (** Omistamine *) |
| 34 | + | Error (** Vea tekkimine *) |
| 35 | + | If of expr * stmt * stmt (** Tingimuslause *) |
| 36 | + | While of expr * stmt (** While-tsükkel *) |
| 37 | + | Seq of stmt * stmt (** Järjestik-kompositsioon *) |
| 38 | + | Nop (** Mitte millegi tegemine *) |
| 39 | +[@@deriving eq, ord] |
| 40 | + |
| 41 | + |
| 42 | +(** Väljatrüki funktsioonid. *) |
| 43 | + |
| 44 | +let pp_var ppf var = Format.pp_print_string ppf var |
| 45 | +let show_var var = var |
| 46 | + |
| 47 | +let pp_binary ppf binary = |
| 48 | + match binary with |
| 49 | + | Add -> Format.pp_print_string ppf "+" |
| 50 | + | Sub -> Format.pp_print_string ppf "-" |
| 51 | + | Mul -> Format.pp_print_string ppf "*" |
| 52 | + | Div -> Format.pp_print_string ppf "/" |
| 53 | + | Mod -> Format.pp_print_string ppf "%" |
| 54 | + | Eq -> Format.pp_print_string ppf "==" |
| 55 | + | Ne -> Format.pp_print_string ppf "<>" |
| 56 | + | Lt -> Format.pp_print_string ppf "<" |
| 57 | + | Le -> Format.pp_print_string ppf "<=" |
| 58 | + | Gt -> Format.pp_print_string ppf ">" |
| 59 | + | Ge -> Format.pp_print_string ppf ">=" |
| 60 | + |
| 61 | +let show_binary binary = Format.asprintf "%a" pp_binary binary |
| 62 | + |
| 63 | +let rec pp_expr ppf expr = |
| 64 | + match expr with |
| 65 | + | Num i -> Format.pp_print_int ppf i |
| 66 | + | Var v -> pp_var ppf v |
| 67 | + | Rand (l, u) -> Format.fprintf ppf "[%d, %d]" l u |
| 68 | + | Binary (l, b, r) -> Format.fprintf ppf "(%a %a %a)" pp_expr l pp_binary b pp_expr r |
| 69 | + |
| 70 | +let show_expr expr = Format.asprintf "%a" pp_expr expr |
| 71 | + |
| 72 | +let rec pp_stmt ppf stmt = |
| 73 | + match stmt with |
| 74 | + | Assign (v, e) -> Format.fprintf ppf "%a = %a" pp_var v pp_expr e |
| 75 | + | Error -> Format.fprintf ppf "error()" |
| 76 | + | If (c, t, f) -> Format.fprintf ppf "if (%a) %a else %a" pp_expr c pp_stmt t pp_stmt f |
| 77 | + | While (c, b) -> Format.fprintf ppf "while (%a) %a" pp_expr c pp_stmt b |
| 78 | + | Seq (a, b) -> Format.fprintf ppf "{%a; %a}" pp_stmt a pp_stmt b |
| 79 | + | Nop -> Format.pp_print_string ppf "" |
| 80 | + |
| 81 | +let show_stmt stmt = Format.asprintf "%a" pp_stmt stmt |
| 82 | + |
| 83 | + |
| 84 | +(** OCaml-i DSL AST-ide loomiseks. *) |
| 85 | +module Syntax = |
| 86 | +struct |
| 87 | + let (!) v = Var v |
| 88 | + let (~$) i = Num i |
| 89 | + let (+) a b = Binary (a, Add, b) |
| 90 | + let (-) a b = Binary (a, Sub, b) |
| 91 | + let ( * ) a b = Binary (a, Mul, b) |
| 92 | + let (/) a b = Binary (a, Div, b) |
| 93 | + let (mod) a b = Binary (a, Mod, b) |
| 94 | + let (=) a b = Binary (a, Eq, b) |
| 95 | + let (<>) a b = Binary (a, Ne, b) |
| 96 | + let (<) a b = Binary (a, Lt, b) |
| 97 | + let (<=) a b = Binary (a, Le, b) |
| 98 | + let (>) a b = Binary (a, Gt, b) |
| 99 | + let (>=) a b = Binary (a, Ge, b) |
| 100 | + |
| 101 | + let (:=) v e = Assign (v, e) |
| 102 | + let if_ c t f = If (c, t, f) |
| 103 | + let while_ c b = While (c, b) |
| 104 | + |
| 105 | + let assert_ e = If (e, Nop, Error) |
| 106 | + let assume e = If (e, Nop, While (Num 1, Nop)) |
| 107 | + |
| 108 | + (** Järjestik-kompositsioon suvalisest arvust lausetest. *) |
| 109 | + let rec seq = function |
| 110 | + | [] -> Nop |
| 111 | + | [x] -> x |
| 112 | + | x :: xs -> Seq (x, seq xs) |
| 113 | +end |
0 commit comments