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