annotate lab4/dict.mli @ 1:b5139af1a420 tip basis

Fixed permissions on compile scripts
author Mike Spivey <mike@cs.ox.ac.uk>
date Fri, 13 Oct 2017 17:27:58 +0100
parents bfdcc3820b32
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* lab4/dict.mli *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 (* Copyright (c) 2017 J. M. Spivey *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 (* This module defines the types that represent definitions and Pascal
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 types in the semantic analyser, and provides an abstract data type of
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 'environments' used in the analyser. There are also some functions that
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 operate on types. *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 (* |ident| -- type of identifiers *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 type ident
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 (* |intern| -- convert string to identifier *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 val intern : string -> ident
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 (* |spelling| -- retrieve identifier as string *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 val spelling : ident -> string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 (* |fId| -- format identifier for printing *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 val fId : ident -> Print.arg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 (* |location| -- runtime locations *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 type location =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 Local of int (* Local (offset) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 | Global of Optree.symbol (* Global (label) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 | Register of int (* Register *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 | Nowhere (* Compile-time only *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 (* |Loc| -- printf format for locations *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 val fLoc: location -> Print.arg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 (* |libid| -- type of picoPascal library procedures *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 type libid = ChrFun | OrdFun | PrintNum | PrintChar | PrintString
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 | NewLine | ReadChar | ExitProc | NewProc | ArgcFun | ArgvProc
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 | OpenIn | CloseIn | Operator of Optree.op
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 val fLibId : libid -> Print.arg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 (* |def_kind| -- kinds of definition *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 type def_kind =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 ConstDef of int (* Constant (value) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 | StringDef (* String constant *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 | TypeDef (* Type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 | VarDef (* Variable *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 | CParamDef (* Value parameter *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 | VParamDef (* Var parameter *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 | FieldDef (* Field of record *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 | ProcDef (* Procedure *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 | PParamDef (* Proc parameter *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 | LibDef of libproc (* Lib proc (data) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 | HoleDef of ptype ref (* Pending type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 | DummyDef (* Dummy *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 (* |def| -- definitions in environment *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 and def =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 { d_tag: ident; (* Name *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 d_kind: def_kind; (* Kind of object *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 d_type: ptype; (* Type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 d_level: int; (* Static level *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 mutable d_mem: bool; (* Whether addressible *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 mutable d_addr: location } (* Run-time location *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 and basic_type = VoidType | IntType | CharType | BoolType | AddrType
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 (* |ptype| -- picoPascal types *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 and ptype =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 { t_id: int; (* Unique identifier *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 t_guts: type_guts; (* Shape of the type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 t_rep: Mach.metrics } (* Runtime representation *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 and type_guts =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 BasicType of basic_type
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 | ArrayType of int * ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 | RecordType of def list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 | ProcType of proc_data
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 | PointerType of ptype ref
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 (* |proc_data| -- data about a procedure type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 and proc_data =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 { p_fparams: def list;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 p_pcount : int;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 p_result: ptype }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 (* |libproc| -- data about a library procedure *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 and libproc =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 { q_id: libid;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 q_nargs: int;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 q_argtypes: ptype list }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 val offset_of : def -> int
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 val integer : ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 val character : ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 val boolean : ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 val voidtype : ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 val addrtype : ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 type environment
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 (* |new_block| -- add a new top block *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 val new_block : environment -> environment
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 (* |add_block| -- add an existing def list as top block *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 val add_block : def list -> environment -> environment
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 (* |top_block| -- return top block as a def list *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 val top_block : environment -> def list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 (* |define| -- add a definition, raise Exit if already declared *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 val define : def -> environment -> environment
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111 (* |replace| -- replace tentative definition with final definition *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 val replace : def -> environment -> environment
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
114 (* |find_def| -- search a def list or raise Not_found *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 val find_def : ident -> def list -> def
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
116
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
117 (* |lookup| -- search an environment or raise Not_found *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
118 val lookup : ident -> environment -> def
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
119
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
120 (* |empty| -- empty environment *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
121 val empty : environment
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
122
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
123
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
124 (* Here are some functions that operate on types. The chief one is the
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
125 function |same_type| that tests whether two types are the same; it
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
126 uses name equivalence.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
127
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
128 |match_args| checks two formal paramter lists for 'congurence', i.e.,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
129 that corresponding parameters have the same types, and that value
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
130 parameters are not confused with var parameters. This test is used
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
131 for procedures that take other procedures as parameters. *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
132
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
133 (* |row| -- construct array type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
134 val row : int -> ptype -> ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
135
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
136 (* |mk_type| -- construct new (uniquely labelled) type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
137 val mk_type : type_guts -> Mach.metrics -> ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
138
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
139 (* |discrete| -- test if a type has discrete values *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
140 val discrete : ptype -> bool
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
141
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
142 (* |scalar| -- test if a type is simple *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
143 val scalar : ptype -> bool
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
144
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
145 (* |is_string| -- test if a type is 'array N of char' *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
146 val is_string : ptype -> bool
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
147
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
148 (* |bound| -- get bound of array type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
149 val bound : ptype -> int
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
150
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
151 (* |is_pointer| -- test if a type is 'pointer to T' *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
152 val is_pointer : ptype -> bool
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
153
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
154 (* |base_type| -- get base type of pointer or array *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
155 val base_type : ptype -> ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
156
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
157 (* |get_proc| -- get data from procedure type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
158 val get_proc : ptype -> proc_data
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
159
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
160 (* |same_type| -- test two types for equality (name equivalence) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
161 val same_type : ptype -> ptype -> bool
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
162
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
163 (* |match_args| -- test two formal parameter lists for congruence *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
164 val match_args : def list -> def list -> bool
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
165