Blame view

fz/semantic.pl 3.82 KB
0b83a7f1   Salvador Abreu   First stab at sem...
1
2
% == No, Emacs this is -*-Prolog-*- code, not what you thought... =============

57421ab2   Salvador Abreu   first stab at typ...
3
4
5
6
7
8
% == NAMES ====================================================================

% -- sa_names(AST_IN, AST_OUT, ST) --------------------------------------------
%
% sa_names/3: do name analysis (replace identifiers by their dict. entry)

0b83a7f1   Salvador Abreu   First stab at sem...
9
sa_names(fzn(preds(IP), vars(IV), constrs(IC), SOLVE),
57421ab2   Salvador Abreu   first stab at typ...
10
	 fzn(preds(OP), vars(OV), constrs(OC), SOLVE), ST) :-
4029344a   Salvador Abreu   now using free-en...
11
12
13
    sa_n_traverse(IP, OP, ST),  % predicates
    sa_n_traverse(IV, OV, ST),	% variables
    sa_n_traverse(IC, OC, ST).  % constraints
0b83a7f1   Salvador Abreu   First stab at sem...
14
15
16

% -----------------------------------------------------------------------------

f1a115f5   Salvador Abreu   a litte progress ...
17
sa_n_traverse([], [], _ST) :- !.
4029344a   Salvador Abreu   now using free-en...
18
sa_n_traverse([N|Ns], [NN|NNs], ST) :-
f461b0bb   Salvador Abreu   first stab at AST...
19
20
    sa_n(N, NN, ST), !,
    sa_n_traverse(Ns, NNs, ST).
f1a115f5   Salvador Abreu   a litte progress ...
21
sa_n_traverse([_|Ns], NNs, ST) :- !, % ignore failing nodes (beware!)
4029344a   Salvador Abreu   now using free-en...
22
    sa_n_traverse(Ns, NNs, ST).
0b83a7f1   Salvador Abreu   First stab at sem...
23

f1a115f5   Salvador Abreu   a litte progress ...
24
sa_n_traverse(N, NN, ST) :- sa_n(N, NN, ST). % non-list case
0b83a7f1   Salvador Abreu   First stab at sem...
25

57421ab2   Salvador Abreu   first stab at typ...
26

0b83a7f1   Salvador Abreu   First stab at sem...
27
28
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%
4029344a   Salvador Abreu   now using free-en...
29
% sa_n(AST_N_IN, AST_N_OUT, ST)
f1a115f5   Salvador Abreu   a litte progress ...
30
%
57421ab2   Salvador Abreu   first stab at typ...
31
32
33
34
35
% arguments:
%  AST_N_IN         - input AST node
%  AST_N_OUT        - output AST node (same structure, rewritten)
%  ST               - symbol table (gets extended)
%
f1a115f5   Salvador Abreu   a litte progress ...
36
% handle all AST cases:
57421ab2   Salvador Abreu   first stab at typ...
37
%  var(N,T,I,A)     - Name, Type, Initializer, Attrib -- variable declaration
26ed8fd4   Salvador Abreu   arrays
38
%  lit(V,T)         - Value, Type -- literal with type
57421ab2   Salvador Abreu   first stab at typ...
39
40
41
42
%  id(N)            - Name -- identifier
%  constraint(C,A)  - Constraint, Attrib -- constraint
%
% variables with an initializer are treated as SSA & will now stand for
2c862ae2   Salvador Abreu   variables with an...
43
44
% the literal in the initializer itself.

0b83a7f1   Salvador Abreu   First stab at sem...
45
sa_n(var(N,T,[],A), V, ST) :- !,
2c862ae2   Salvador Abreu   variables with an...
46
    sa_attribs(A, AX, ST),
4029344a   Salvador Abreu   now using free-en...
47
    V=var(N,T,[],AX),		% new AST node becomes ST entry value
2c862ae2   Salvador Abreu   variables with an...
48
49
50
    st_insert(ST, N, V).

sa_n(var(N,_T,I,_A), NI, ST) :-	% non-empty initializer
26ed8fd4   Salvador Abreu   arrays
51
    sa_n_traverse(I, NI, ST),	% parse initializer (& ignore T and A),
1fa8bcd2   Salvador Abreu   back-end specific...
52
    V=NI,			% -- just NI, not var(N,T,NI,AX) --
26ed8fd4   Salvador Abreu   arrays
53
54
55
    st_insert(ST, N, V).	% which becomes the ST entry value


0b83a7f1   Salvador Abreu   First stab at sem...
56
sa_n(lit(E,array(T)), lit(NE, array(T)), ST) :-
57421ab2   Salvador Abreu   first stab at typ...
57
    sa_n_traverse(E, NE, ST).
f1a115f5   Salvador Abreu   a litte progress ...
58
59
60
61
62
63
sa_n(lit(E,int), lit(E,int), _ST).
sa_n(lit(E,float), lit(E,float), _ST).
sa_n(lit(E,string), lit(E,string), _ST).


sa_n(id(N), V, ST) :- st_lookup(ST, N, V), !.
57421ab2   Salvador Abreu   first stab at typ...
64
sa_n(id(N), V, ST) :- V=var(N,_,_,_), % type as yet unknown
f1a115f5   Salvador Abreu   a litte progress ...
65
		      st_insert(ST, N, V).
57421ab2   Salvador Abreu   first stab at typ...
66

f1a115f5   Salvador Abreu   a litte progress ...
67

0b83a7f1   Salvador Abreu   First stab at sem...
68
sa_n(constraint(CE, A), constraint(NCE, AX), ST) :-
57421ab2   Salvador Abreu   first stab at typ...
69
    CE=..[C|AS],
7d0195a6   Salvador Abreu   replace initializ...
70
    sa_attribs(A, AX, ST),
57421ab2   Salvador Abreu   first stab at typ...
71
    sa_n_traverse(AS, NAS, ST),
7d0195a6   Salvador Abreu   replace initializ...
72
    NCE=..[C|NAS].
57421ab2   Salvador Abreu   first stab at typ...
73
74
75

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

0b83a7f1   Salvador Abreu   First stab at sem...
76
77
% == TYPES ====================================================================

57421ab2   Salvador Abreu   first stab at typ...
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
% -- sa_types(AST, ST) --------------------------------------------------------
%
% sa_types/2: do type analysis (traverse AST enforcing type correctness).
% as a side-effect, it will do type inference.

sa_types(fzn(_PS, vars(VS), constrs(CS), _S), ST) :-
    sa_t_traverse(VS, ST),
    sa_t_traverse(CS, ST).

% -----------------------------------------------------------------------------

sa_t_traverse([], _ST) :- !.
sa_t_traverse([N|Ns], ST) :-
    sa_t(N, ST), !,
    sa_t_traverse(Ns, ST).
sa_t_traverse([_|Ns], ST) :- !, % ignore failing nodes (beware!)
    sa_t_traverse(Ns, ST).

sa_t_traverse(N, ST) :- sa_t(N, ST). % non-list case

% -- sa_t(NODE, ST) -----------------------------------------------------------

sa_t(var(_N,T,I,_A), _ST) :- type(I,T).
sa_t(lit(E,T), _ST) :- type(lit(E,T), T).
sa_t(constraint(CE, _AT), ST) :-
26ed8fd4   Salvador Abreu   arrays
103
    CE=..[_|AS],
57421ab2   Salvador Abreu   first stab at typ...
104
105
106
107
108
109
110
    sa_t_traverse(AS, ST).

% =============================================================================

sa_attribs([A|As], [NA|NAs], ST) :-
    sa_attrib(A, NA, ST), !,
    sa_attribs(As, NAs, ST).
f461b0bb   Salvador Abreu   first stab at AST...
111
112
113
114
115
116
117
118
sa_attribs([_|As], NAs, ST) :-	% ignore unknown attributes
    sa_attribs(As, NAs, ST).
sa_attribs([], [], _).


sa_attrib(id(output_var), output, _).

% -----------------------------------------------------------------------------
0b83a7f1   Salvador Abreu   First stab at sem...

57421ab2   Salvador Abreu   first stab at typ...