]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/assembly/compiler/ast_tree.ma
Semantic analysis implemented (sort of).
[helm.git] / helm / software / matita / contribs / assembly / compiler / ast_tree.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 (* ********************************************************************** *)
16 (*                                                                        *)
17 (* Sviluppato da:                                                         *)
18 (*   Cosimo Oliboni, oliboni@cs.unibo.it                                  *)
19 (*                                                                        *)
20 (* ********************************************************************** *)
21
22 include "compiler/environment.ma".
23
24 (* *************** *)
25 (* ALBERO DI TOKEN *)
26 (* *************** *)
27
28 (* id: accesso all'ambiente con stringa *)
29 inductive ast_id (e:aux_env_type) : bool → ast_type → Type ≝
30   AST_ID: ∀str:aux_str_type.
31           (* D *) (check_desc_env e str) → (ast_id e (get_const_desc (get_desc_env e str)) (get_type_desc (get_desc_env e str))).
32
33 (* -------------------------- *)
34
35 (* espressioni *)
36 inductive ast_expr (e:aux_env_type) : ast_base_type → Type ≝
37   AST_EXPR_BYTE8 : byte8 → ast_expr e AST_BASE_TYPE_BYTE8
38 | AST_EXPR_WORD16: word16 → ast_expr e AST_BASE_TYPE_WORD16
39 | AST_EXPR_WORD32: word32 → ast_expr e AST_BASE_TYPE_WORD32
40
41 | AST_EXPR_NEG: ∀t:ast_base_type.
42                 ast_expr e t → ast_expr e t
43 | AST_EXPR_NOT: ∀t:ast_base_type.
44                 ast_expr e t → ast_expr e t
45 | AST_EXPR_COM: ∀t:ast_base_type.
46                 ast_expr e t → ast_expr e t
47
48 | AST_EXPR_ADD: ∀t:ast_base_type.
49                 ast_expr e t → ast_expr e t → ast_expr e t
50 | AST_EXPR_SUB: ∀t:ast_base_type.
51                 ast_expr e t → ast_expr e t → ast_expr e t
52 | AST_EXPR_MUL: ∀t:ast_base_type.
53                 ast_expr e t → ast_expr e t → ast_expr e t
54 | AST_EXPR_DIV: ∀t:ast_base_type.
55                 ast_expr e t → ast_expr e t → ast_expr e t
56 | AST_EXPR_SHR: ∀t:ast_base_type.
57                 ast_expr e t → ast_expr e AST_BASE_TYPE_BYTE8 → ast_expr e t
58 | AST_EXPR_SHL: ∀t:ast_base_type.
59                 ast_expr e t → ast_expr e AST_BASE_TYPE_BYTE8 → ast_expr e t
60
61 | AST_EXPR_GT : ∀t:ast_base_type.
62                 ast_expr e t → ast_expr e t → ast_expr e AST_BASE_TYPE_BYTE8
63 | AST_EXPR_GTE: ∀t:ast_base_type.
64                 ast_expr e t → ast_expr e t → ast_expr e AST_BASE_TYPE_BYTE8
65 | AST_EXPR_LT : ∀t:ast_base_type.
66                 ast_expr e t → ast_expr e t → ast_expr e AST_BASE_TYPE_BYTE8
67 | AST_EXPR_LTE: ∀t:ast_base_type.
68                 ast_expr e t → ast_expr e t → ast_expr e AST_BASE_TYPE_BYTE8
69 | AST_EXPR_EQ : ∀t:ast_base_type.
70                 ast_expr e t → ast_expr e t → ast_expr e AST_BASE_TYPE_BYTE8
71 | AST_EXPR_NEQ: ∀t:ast_base_type.
72                 ast_expr e t → ast_expr e t → ast_expr e AST_BASE_TYPE_BYTE8
73
74 | AST_EXPR_B8toW16 : ast_expr e AST_BASE_TYPE_BYTE8 → ast_expr e AST_BASE_TYPE_WORD16
75 | AST_EXPR_B8toW32 : ast_expr e AST_BASE_TYPE_BYTE8 → ast_expr e AST_BASE_TYPE_WORD32
76 | AST_EXPR_W16toB8 : ast_expr e AST_BASE_TYPE_WORD16 → ast_expr e AST_BASE_TYPE_BYTE8
77 | AST_EXPR_W16toW32: ast_expr e AST_BASE_TYPE_WORD16 → ast_expr e AST_BASE_TYPE_WORD32
78 | AST_EXPR_W32toB8 : ast_expr e AST_BASE_TYPE_WORD32 → ast_expr e AST_BASE_TYPE_BYTE8
79 | AST_EXPR_W32toW16: ast_expr e AST_BASE_TYPE_WORD32 → ast_expr e AST_BASE_TYPE_WORD16
80
81 | AST_EXPR_ID: ∀b:bool.∀t:ast_base_type.
82                ast_var e b (AST_TYPE_BASE t) → ast_expr e t
83
84 (* variabile: modificatori di array/struct dell'id *)
85 with ast_var : bool → ast_type → Type ≝
86   AST_VAR_ID: ∀b:bool.∀t:ast_type.
87               ast_id e b t → ast_var e b t
88 | AST_VAR_ARRAY: ∀b:bool.∀t:ast_type.∀n:nat.
89                  ast_var e b (AST_TYPE_ARRAY t n) → ast_base_expr e → ast_var e b t
90 | AST_VAR_STRUCT: ∀b:bool.∀nel:ne_list ast_type.∀n:nat.
91                   ast_var e b (AST_TYPE_STRUCT nel) → (* D *) (ltb n (len_neList ? nel) = true) → ast_var e b (abs_nth_neList ? nel n)
92
93 (* espressioni generalizzate: anche non uniformi per tipo *)
94 with ast_base_expr : Type ≝
95   AST_BASE_EXPR: ∀t:ast_base_type.
96                  ast_expr e t → ast_base_expr e.
97
98 (* -------------------------- *)
99
100 (* espressioni destre di assegnamento *)
101 inductive ast_right_expr (e:aux_env_type) : ast_type → Type ≝
102   AST_RIGHT_EXPR_BASE: ∀t:ast_base_type.
103                        ast_expr e t → ast_right_expr e (AST_TYPE_BASE t)
104 | AST_RIGHT_EXPR_VAR: ∀b:bool.∀t:ast_type.
105                       ast_var e b t → ast_right_expr e t.
106
107 (* -------------------------- *)
108
109 let rec aux_ast_init_type (t:ast_type) on t : Type ≝
110  match t with
111   [ AST_TYPE_BASE bType ⇒ match bType with
112    [ AST_BASE_TYPE_BYTE8 ⇒ byte8
113    | AST_BASE_TYPE_WORD16 ⇒ word16
114    | AST_BASE_TYPE_WORD32 ⇒ word32
115    ] 
116   | AST_TYPE_ARRAY subType dim ⇒
117    let T ≝ aux_ast_init_type subType in
118    let rec aux (n:nat) on n ≝
119     match n with
120      [ O ⇒ T
121      | S n' ⇒ Prod T (aux n')
122      ] in
123    aux dim 
124   | AST_TYPE_STRUCT nelSubType ⇒
125    let rec aux (nel:ne_list ast_type) on nel ≝
126     match nel with
127      [ ne_nil h ⇒ aux_ast_init_type h
128      | ne_cons h t ⇒ Prod (aux_ast_init_type h) (aux t)
129      ] in
130     aux nelSubType 
131   ].
132
133 (*
134  inizializzatori: ammesse solo due forme, no ibridi
135   1) var1 = var2
136   2) var = ... valori ...
137 *) 
138 inductive ast_init (e:aux_env_type) : ast_type → Type ≝
139   AST_INIT_VAR: ∀b:bool.∀t:ast_type.
140                 ast_var e b t → ast_init e t
141 | AST_INIT_VAL: ∀t:ast_type.
142                 aux_ast_init_type t → ast_init e t.
143
144 (* -------------------------- *)
145
146 (* statement: assegnamento/while/if else if else *)
147 inductive ast_stm : aux_env_type → Type ≝
148   AST_STM_ASG: ∀e:aux_env_type.∀t:ast_type.
149                ast_var e false t → ast_right_expr e t → ast_stm e
150 | AST_STM_WHILE: ∀e:aux_env_type.
151                  ast_base_expr e → ast_decl e → ast_stm e
152 | AST_STM_IF: ∀e:aux_env_type.
153               ne_list (Prod (ast_base_expr e) (ast_decl e)) → option (ast_decl e) → ast_stm e
154
155 (* dichiarazioni *)
156 with ast_decl : aux_env_type → Type ≝
157   AST_NO_DECL: ∀e:aux_env_type.
158                list (ast_stm e) → ast_decl e
159 | AST_DECL: ∀e:aux_env_type.∀c:bool.∀str:aux_str_type.∀t:ast_type.
160             (* D *) (check_not_already_def_env e str) → option (ast_init e t) → ast_decl (add_desc_env e str c t) → ast_decl e.
161
162 (* -------------------------- *)
163
164 (* programma *)
165 inductive ast_root : Type ≝
166   AST_ROOT: ast_decl empty_env → ast_root.
167
168 (* -------------------------- *)
169
170 (* programma vuoto *)
171 definition empty_ast_prog ≝ AST_ROOT (AST_NO_DECL empty_env (nil ?)).