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_type.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Parse a scalar, fixed array, reference body or parameter type. The longest |
| 3 | note production is "& mut [ scalar ; INT ]". Input beyond EOF is padded with OT_EOF. |
| 4 | note used counts consumed tokens, including on failure. Named references stop |
| 5 | note at NAME so the caller can resolve it against the current declaration table. |
| 6 | note bridge c-define OSC_TYPE_WINDOW 7 |
| 7 | note bridge c-define OSC_TYPE_SCALAR 1 |
| 8 | note bridge c-define OSC_TYPE_ARRAY 2 |
| 9 | note bridge c-define OSC_TYPE_REF_BODY 3 |
| 10 | note bridge c-define OSC_TYPE_PARAM 4 |
| 11 | note bridge c-define OSC_TYPE_EXPECT_SCALAR 1 |
| 12 | note bridge c-define OSC_TYPE_SLICE_ESCAPE 2 |
| 13 | note bridge c-define OSC_TYPE_EXPECT_OPEN 3 |
| 14 | note bridge c-define OSC_TYPE_EXPECT_SEMI 4 |
| 15 | note bridge c-define OSC_TYPE_EXPECT_LENGTH 5 |
| 16 | note bridge c-define OSC_TYPE_ZERO_LENGTH 6 |
| 17 | note bridge c-define OSC_TYPE_LONG_ARRAY 7 |
| 18 | note bridge c-define OSC_TYPE_EXPECT_CLOSE 8 |
| 19 | note bridge c-define OSC_TYPE_NAMED_REF 9 |
| 20 | note bridge c-define OSC_TYPE_BARE_NAME 10 |
| 21 | note bridge c-define OSC_TYPE_LOCAL_ONLY 11 |
| 22 | note bridge c-type typerec OscParsedType |
| 23 | om2 |
| 24 | |
| 25 | layout typerec 24 |
| 26 | field used uint at 0 little |
| 27 | field scalar uint at 4 little |
| 28 | field refkind uint at 8 little |
| 29 | field elem uint at 12 little |
| 30 | field count ulong at 16 little |
| 31 | end |
| 32 | |
| 33 | note Token and type IDs are the public enums in osc_lex.h and osc_ir.h. |
| 34 | const OT_NAME ushort 1 |
| 35 | const OT_INT ushort 2 |
| 36 | const OT_MUT ushort 6 |
| 37 | const OT_OWN ushort 7 |
| 38 | const OT_U8 ushort 21 |
| 39 | const OT_I64 ushort 28 |
| 40 | const OT_BOOL ushort 29 |
| 41 | const OT_LBRACK ushort 34 |
| 42 | const OT_RBRACK ushort 35 |
| 43 | const OT_SEMI ushort 37 |
| 44 | const OT_AMP ushort 47 |
| 45 | const OT_HANDLE ushort 66 |
| 46 | const OT_POOL ushort 65 |
| 47 | const OT_BYTES ushort 68 |
| 48 | const OT_CELLS ushort 69 |
| 49 | const OSC_TYPE_SCALAR ulong 1 |
| 50 | const OSC_TYPE_ARRAY ulong 2 |
| 51 | const OSC_TYPE_REF_BODY ulong 3 |
| 52 | const OSC_TYPE_PARAM ulong 4 |
| 53 | const OSC_TYPE_EXPECT_SCALAR ulong 1 |
| 54 | const OSC_TYPE_SLICE_ESCAPE ulong 2 |
| 55 | const OSC_TYPE_EXPECT_OPEN ulong 3 |
| 56 | const OSC_TYPE_EXPECT_SEMI ulong 4 |
| 57 | const OSC_TYPE_EXPECT_LENGTH ulong 5 |
| 58 | const OSC_TYPE_ZERO_LENGTH ulong 6 |
| 59 | const OSC_TYPE_LONG_ARRAY ulong 7 |
| 60 | const OSC_TYPE_EXPECT_CLOSE ulong 8 |
| 61 | const OSC_TYPE_NAMED_REF ulong 9 |
| 62 | const OSC_TYPE_BARE_NAME ulong 10 |
| 63 | const OSC_TYPE_LOCAL_ONLY ulong 11 |
| 64 | const TYPE_BOOL uint 1 |
| 65 | const TYPE_U8 uint 2 |
| 66 | const TYPE_REF uint 10 |
| 67 | const TYPE_BYTES uint 11 |
| 68 | const TYPE_CELLS uint 12 |
| 69 | const REF_OWN uint 1 |
| 70 | const REF_SHARED uint 2 |
| 71 | const REF_MUT uint 3 |
| 72 | |
| 73 | fn type_scalar kind ushort to uint |
| 74 | if eq kind OT_BOOL |
| 75 | ret TYPE_BOOL |
| 76 | end |
| 77 | if and ge kind OT_U8 le kind OT_I64 |
| 78 | ret add TYPE_U8 uint sub kind OT_U8 |
| 79 | end |
| 80 | ret 0 |
| 81 | end |
| 82 | |
| 83 | fn type_scalar_error kind ushort to ulong |
| 84 | if or eq kind OT_BYTES eq kind OT_CELLS |
| 85 | ret OSC_TYPE_SLICE_ESCAPE |
| 86 | end |
| 87 | ret OSC_TYPE_EXPECT_SCALAR |
| 88 | end |
| 89 | |
| 90 | fn type_array kinds view ushort 7 values view ulong 7 pos ulong limit ulong result edit typerec to ulong |
| 91 | if ne get kinds pos OT_LBRACK |
| 92 | ret OSC_TYPE_EXPECT_OPEN |
| 93 | end |
| 94 | pos = add pos 1 |
| 95 | put result used uint pos |
| 96 | kind = get kinds pos |
| 97 | scalar = type_scalar kind |
| 98 | if eq scalar 0 |
| 99 | ret type_scalar_error kind |
| 100 | end |
| 101 | pos = add pos 1 |
| 102 | put result used uint pos |
| 103 | if ne get kinds pos OT_SEMI |
| 104 | ret OSC_TYPE_EXPECT_SEMI |
| 105 | end |
| 106 | pos = add pos 1 |
| 107 | put result used uint pos |
| 108 | if ne get kinds pos OT_INT |
| 109 | ret OSC_TYPE_EXPECT_LENGTH |
| 110 | end |
| 111 | size = get values pos |
| 112 | if eq size 0 |
| 113 | ret OSC_TYPE_ZERO_LENGTH |
| 114 | end |
| 115 | if gt size limit |
| 116 | ret OSC_TYPE_LONG_ARRAY |
| 117 | end |
| 118 | pos = add pos 1 |
| 119 | put result used uint pos |
| 120 | if ne get kinds pos OT_RBRACK |
| 121 | ret OSC_TYPE_EXPECT_CLOSE |
| 122 | end |
| 123 | put result used uint add pos 1 |
| 124 | put result scalar TYPE_REF |
| 125 | put result elem scalar |
| 126 | put result count size |
| 127 | ret 0 |
| 128 | end |
| 129 | |
| 130 | export fn osc_read_type kinds view ushort 7 values view ulong 7 form ulong refkind ulong limit ulong result edit typerec to ulong |
| 131 | put result used 0 |
| 132 | put result scalar 0 |
| 133 | put result refkind uint refkind |
| 134 | put result elem 0 |
| 135 | put result count 0 |
| 136 | pos = ulong 0 |
| 137 | kind = get kinds pos |
| 138 | if eq form OSC_TYPE_PARAM |
| 139 | if eq kind OT_OWN |
| 140 | put result refkind REF_OWN |
| 141 | pos = 1 |
| 142 | form = OSC_TYPE_REF_BODY |
| 143 | end |
| 144 | if eq kind OT_AMP |
| 145 | put result refkind REF_SHARED |
| 146 | pos = 1 |
| 147 | if eq get kinds pos OT_MUT |
| 148 | put result refkind REF_MUT |
| 149 | pos = 2 |
| 150 | end |
| 151 | form = OSC_TYPE_REF_BODY |
| 152 | end |
| 153 | put result used uint pos |
| 154 | if eq form OSC_TYPE_PARAM |
| 155 | if eq kind OT_NAME |
| 156 | ret OSC_TYPE_BARE_NAME |
| 157 | end |
| 158 | if or eq kind OT_HANDLE eq kind OT_POOL |
| 159 | ret OSC_TYPE_LOCAL_ONLY |
| 160 | end |
| 161 | if or eq kind OT_BYTES eq kind OT_CELLS |
| 162 | if eq kind OT_BYTES |
| 163 | put result scalar TYPE_BYTES |
| 164 | else |
| 165 | put result scalar TYPE_CELLS |
| 166 | end |
| 167 | put result used 1 |
| 168 | ret 0 |
| 169 | end |
| 170 | end |
| 171 | end |
| 172 | if eq form OSC_TYPE_REF_BODY |
| 173 | if eq get kinds pos OT_NAME |
| 174 | ret OSC_TYPE_NAMED_REF |
| 175 | end |
| 176 | form = OSC_TYPE_ARRAY |
| 177 | end |
| 178 | if eq form OSC_TYPE_ARRAY |
| 179 | ret type_array kinds values pos limit result |
| 180 | end |
| 181 | scalar = type_scalar kind |
| 182 | if eq scalar 0 |
| 183 | ret type_scalar_error kind |
| 184 | end |
| 185 | put result scalar scalar |
| 186 | put result used 1 |
| 187 | ret 0 |
| 188 | end |