interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / compiler / osc_expr.om

210 lines · 5149 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note OSC infix expressions. Pending operators are strictly increasing in precedence.
3note Thus nine precedence levels need nine slots regardless of expression length.
4note The caller supplies one parsed unary operand at a time, allocates AST nodes
5note on shift, and attaches their edges on reduction. No syntax tree is buffered.
6note bridge c-type exprstk OscExprStack
7note bridge c-define OSC_EXPR_DONE 0
8note bridge c-define OSC_EXPR_SHIFT 1
9note bridge c-define OSC_EXPR_REDUCE 2
10note bridge c-define OSC_EXPR_CHAINED 3
11note bridge c-type opstate OscOperandState
12note bridge c-define OSC_OPERAND_DONE 0
13note bridge c-define OSC_OPERAND_PREFIX 1
14note bridge c-define OSC_OPERAND_INTEGER 2
15note bridge c-define OSC_OPERAND_TRUE 3
16note bridge c-define OSC_OPERAND_FALSE 4
17note bridge c-define OSC_OPERAND_NAME 5
18note bridge c-define OSC_OPERAND_CALL 6
19note bridge c-define OSC_OPERAND_FIELD 7
20note bridge c-define OSC_OPERAND_INDEX 8
21note bridge c-define OSC_OPERAND_POOL 9
22note bridge c-define OSC_OPERAND_GROUP 10
23note bridge c-define OSC_OPERAND_BORROW_ERROR 11
24note bridge c-define OSC_OPERAND_SYNTAX 12
25note bridge c-define OSC_OPERAND_CAST 13
26note bridge c-define OSC_OPERAND_CAST_ERROR 14
27note bridge c-define OSC_OPERAND_UNWIND 15
28om2
29
30layout exprstk 120
31field depth uint at 0 little
32field rank uint 9 at 4 little
33field node int 9 at 40 little
34field lhs int 9 at 76 little
35field parent int at 112 little
36field left int at 116 little
37end
38
39note Prefix nodes are consecutive until the primary begins. Unwind links them
40note only after a successful primary and cast, preserving partial ASTs on errors.
41layout opstate 12
42field phase uint at 0 little
43field prefixes uint at 4 little
44field pending uint at 8 little
45end
46
47const OP_DONE ulong 0
48const OP_PREFIX ulong 1
49const OP_INTEGER ulong 2
50const OP_TRUE ulong 3
51const OP_FALSE ulong 4
52const OP_NAME ulong 5
53const OP_CALL ulong 6
54const OP_FIELD ulong 7
55const OP_INDEX ulong 8
56const OP_POOL ulong 9
57const OP_GROUP ulong 10
58const OP_BORROW_ERROR ulong 11
59const OP_SYNTAX ulong 12
60const OP_CAST ulong 13
61const OP_CAST_ERROR ulong 14
62const OP_UNWIND ulong 15
63
64const OT_PLUS uint 42
65const OT_MINUS uint 43
66const OT_STAR uint 44
67const OT_SLASH uint 45
68const OT_PERCENT uint 46
69const OT_AMP uint 47
70const OT_PIPE uint 48
71const OT_CARET uint 49
72const OT_ANDAND uint 52
73const OT_OROR uint 53
74const OT_SHL uint 54
75const OT_SHR uint 55
76const OT_EQ uint 56
77const OT_GE uint 61
78const CMP_RANK uint 3
79const EXPR_DONE ulong 0
80const EXPR_SHIFT ulong 1
81const EXPR_REDUCE ulong 2
82const EXPR_CHAINED ulong 3
83
84fn expr_rank kind uint to uint
85if eq kind OT_OROR
86 ret 1
87end
88if eq kind OT_ANDAND
89 ret 2
90end
91if and ge kind OT_EQ le kind OT_GE
92 ret CMP_RANK
93end
94if eq kind OT_PIPE
95 ret 4
96end
97if eq kind OT_CARET
98 ret 5
99end
100if eq kind OT_AMP
101 ret 6
102end
103if or eq kind OT_SHL eq kind OT_SHR
104 ret 7
105end
106if or eq kind OT_PLUS eq kind OT_MINUS
107 ret 8
108end
109if or eq kind OT_STAR or eq kind OT_SLASH eq kind OT_PERCENT
110 ret 9
111end
112ret 0
113end
114
115export fn osc_expr_step stack edit exprstk look ulong operand ulong to ulong
116rank = expr_rank uint look
117depth = get stack depth
118if ne depth 0
119 top = ulong sub depth 1
120 pending = get stack rank top
121 if ge pending rank
122 put stack depth sub depth 1
123 put stack parent get stack node top
124 put stack left get stack lhs top
125 if and eq pending CMP_RANK eq rank CMP_RANK
126 ret EXPR_CHAINED
127 end
128 ret EXPR_REDUCE
129 end
130end
131if eq rank 0
132 ret EXPR_DONE
133end
134put stack rank ulong depth rank
135put stack lhs ulong depth int operand
136put stack depth add depth 1
137ret EXPR_SHIFT
138end
139const OT_LPAREN uint 30
140const OT_LBRACK uint 34
141const OT_DOT uint 63
142const OT_ALLOC uint 8
143const OT_AS uint 18
144const OT_INT uint 2
145const OT_NAME uint 1
146const OT_TRUE uint 19
147const OT_FALSE uint 20
148const OT_BANG uint 51
149const OT_TILDE uint 50
150
151note phase 0 scans prefixes and selects the primary, phase 1 permits one cast,
152note phase 2 refuses a second cast, phase 3 attaches the pending prefix nodes.
153export fn osc_operand_step state edit opstate kind ulong nextkind ulong third ulong to ulong
154phase = get state phase
155if eq phase 0
156 if or eq uint kind OT_MINUS or eq uint kind OT_TILDE eq uint kind OT_BANG
157 put state prefixes add get state prefixes 1
158 ret OP_PREFIX
159 end
160 put state phase 1
161 if eq uint kind OT_INT
162 ret OP_INTEGER
163 end
164 if eq uint kind OT_TRUE
165 ret OP_TRUE
166 end
167 if eq uint kind OT_FALSE
168 ret OP_FALSE
169 end
170 if eq uint kind OT_NAME
171 if eq uint nextkind OT_LPAREN
172 ret OP_CALL
173 end
174 if eq uint nextkind OT_LBRACK
175 ret OP_INDEX
176 end
177 if eq uint nextkind OT_DOT
178 if eq uint third OT_ALLOC
179 ret OP_POOL
180 end
181 ret OP_FIELD
182 end
183 ret OP_NAME
184 end
185 if eq uint kind OT_LPAREN
186 ret OP_GROUP
187 end
188 if eq uint kind OT_AMP
189 ret OP_BORROW_ERROR
190 end
191 ret OP_SYNTAX
192end
193if eq uint kind OT_AS
194 if eq phase 1
195 put state phase 2
196 ret OP_CAST
197 end
198 if eq phase 2
199 ret OP_CAST_ERROR
200 end
201end
202put state phase 3
203count = get state prefixes
204if ne count 0
205 put state pending sub count 1
206 put state prefixes sub count 1
207 ret OP_UNWIND
208end
209ret OP_DONE
210end