/
mpl_syntaxtree.ml
139 lines (123 loc) · 4.75 KB
/
mpl_syntaxtree.ml
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(*
* Copyright (c) 2005 Anil Madhavapeddy <anil@recoil.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* $Id: mpl_syntaxtree.ml,v 1.17 2005/12/05 00:46:06 avsm Exp $
*)
open Mpl_utils
module L = Mpl_location
type id = string
type expr =
| And of (expr * expr)
| Or of (expr * expr)
| Identifier of id
| Not of expr
| True
| False
| Greater of (expr * expr)
| Less of (expr * expr)
| Greater_or_equal of (expr * expr)
| Less_or_equal of (expr * expr)
| Equals of (expr * expr)
| Plus of (expr * expr)
| Minus of (expr * expr)
| Multiply of (expr * expr)
| Divide of (expr * expr)
| Int_constant of int
| String_constant of string
| Range of (expr * expr)
| Function_call of (id * string option)
and exprs = expr list
type vlist = (expr * string) list
type attr =
|Align of expr
|Min of expr
|Max of expr
|Const of expr
|Variant of (vlist * expr option)
|Value of expr
|Default of expr
and attrs = attr list
type packet_var =
|P_bool of string
|P_int of string
type statement =
| Packet of (id * id * (id list))
(* Variable name * type in string * size expression * attrs *)
| Variable of (id * id * expr option * attrs)
(* classify (variable) (pattern * name * guard * statements) *)
| Classify of (id * (expr * expr * expr option * statements) list)
| Array of (id * expr * statements)
| Unit
and statements = (Mpl_location.t * statement) list
type packet = {
name: string;
args: packet_var list;
body: statements;
loc: L.t;
}
type packets = {
pdefs: packet list; (* packets *)
tdefs: (string, (L.t * string)) Hashtbl.t; (* typedefs *)
sdefs: (string, statements) Hashtbl.t; (* structs *)
}
let packet_var_of_string nm = function
|"bool"|"boolean" -> Some (P_bool nm)
|"int"|"integer" -> Some (P_int nm)
|_ -> None
let attr_of_string e = function
|"min" -> Some (Min e)
|"max" -> Some (Max e)
|"align" -> Some (Align e)
|"const" -> Some (Const e)
|"value" -> Some (Value e)
|"default" -> Some (Default e)
|_ -> None
exception Syntax_error of Mpl_location.t
open Printf
(* Convert expression to a string *)
let rec string_of_expr = function
| And (a,b) -> sprintf "(%s && %s)" (string_of_expr a) (string_of_expr b)
| Or (a,b) -> sprintf "(%s || %s)" (string_of_expr a) (string_of_expr b)
| Identifier i -> i
| Not e -> sprintf "(!%s)" (string_of_expr e)
| True -> "true"
| False -> "false"
| Greater (a,b) -> sprintf "(%s > %s)" (string_of_expr a) (string_of_expr b)
| Less (a,b) -> sprintf "(%s < %s)" (string_of_expr a) (string_of_expr b)
| Greater_or_equal (a,b) -> sprintf "(%s >= %s)" (string_of_expr a) (string_of_expr b)
| Less_or_equal (a,b) -> sprintf "(%s <= %s)" (string_of_expr a) (string_of_expr b)
| Equals (a,b) -> sprintf "(%s == %s)" (string_of_expr a) (string_of_expr b)
| Plus (a,b) -> sprintf "(%s + %s)" (string_of_expr a) (string_of_expr b)
| Minus (a,b) -> sprintf "(%s - %s)" (string_of_expr a) (string_of_expr b)
| Multiply (a,b) -> sprintf "(%s * %s)" (string_of_expr a) (string_of_expr b)
| Divide (a,b) -> sprintf "(%s / %s)" (string_of_expr a) (string_of_expr b)
| Int_constant a -> sprintf "%d" a
| String_constant a -> sprintf "\"%s\"" a
| Range (a,b) -> sprintf "(%s .. %s)" (string_of_expr a) (string_of_expr b)
| Function_call (i,a) ->
let x = match a with |None -> "" |Some b -> b in
sprintf "%s(%s)" i x
let string_of_attr = function
|Align e -> sprintf "align=(%s)" (string_of_expr e)
|Min e -> sprintf "min=(%s)" (string_of_expr e)
|Max e -> sprintf "max=(%s)" (string_of_expr e)
|Value e -> sprintf "value=(%s)" (string_of_expr e)
|Const e -> sprintf "const=(%s)" (string_of_expr e)
|Default e -> sprintf "default=(%s)" (string_of_expr e)
|Variant (x,def) -> sprintf "variant={%s}" (String.concat ", "
(List.map (fun (i,t) -> sprintf "%s->%s" (string_of_expr i) t) x))
let string_of_packet_var = function
|P_bool x -> sprintf "bool %s" x
|P_int x -> sprintf "int %s" x