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 / seed / node / gram.om
| 1 | note seed/node/gram.om -- the bounded repair grammar of the round40 protocol |
| 2 | note (repair.py PATTERNS), shared by the tool adapter (seed/node/tool.om, |
| 3 | note which checks a file) and the coding workflow (seed/node/code.om, whose |
| 4 | note creator checks a proposed edit before granting it). Compiles as OM-1 and |
| 5 | note OM-2; compose before its user. Lines are split at newline bytes only, |
| 6 | note after the source is trimmed of ASCII white space (bytes 0..32). |
| 7 | note Lines: fn calc x int to int | fn main to int | end | s = x | ret s | |
| 8 | note ret (add|mul|sub) x int [1-6] | ret calc int \d{1,2} | |
| 9 | note for i ulong [1-6] | s = add s int [1-6]. |
| 10 | text gcalc "fn calc x int to int" |
| 11 | text gmain "fn main to int" |
| 12 | text gend "end" |
| 13 | text gradd "ret add x int " |
| 14 | text grmul "ret mul x int " |
| 15 | text grsub "ret sub x int " |
| 16 | text grcal "ret calc int " |
| 17 | text gsx "s = x" |
| 18 | text gfor "for i ulong " |
| 19 | text gsadd "s = add s int " |
| 20 | text grets "ret s" |
| 21 | |
| 22 | note Grammar text k (0 calc, 1 main, 2 end, 3 s = x, 4 ret s, 5 ret calc |
| 23 | note int, 6 ret add, 7 ret mul, 8 ret sub, 9 for, 10 s = add): its length. |
| 24 | fn gr_tln k ulong to ulong |
| 25 | if eq k 0 |
| 26 | ret len gcalc |
| 27 | end |
| 28 | if eq k 1 |
| 29 | ret len gmain |
| 30 | end |
| 31 | if eq k 2 |
| 32 | ret len gend |
| 33 | end |
| 34 | if eq k 3 |
| 35 | ret len gsx |
| 36 | end |
| 37 | if eq k 4 |
| 38 | ret len grets |
| 39 | end |
| 40 | if eq k 5 |
| 41 | ret len grcal |
| 42 | end |
| 43 | if le k 8 |
| 44 | ret len gradd |
| 45 | end |
| 46 | if eq k 9 |
| 47 | ret len gfor |
| 48 | end |
| 49 | ret len gsadd |
| 50 | end |
| 51 | |
| 52 | note Byte i of grammar text k. |
| 53 | fn gr_tch k ulong i ulong to uchar |
| 54 | if eq k 0 |
| 55 | ret get gcalc i |
| 56 | end |
| 57 | if eq k 1 |
| 58 | ret get gmain i |
| 59 | end |
| 60 | if eq k 2 |
| 61 | ret get gend i |
| 62 | end |
| 63 | if eq k 3 |
| 64 | ret get gsx i |
| 65 | end |
| 66 | if eq k 4 |
| 67 | ret get grets i |
| 68 | end |
| 69 | if eq k 5 |
| 70 | ret get grcal i |
| 71 | end |
| 72 | if eq k 6 |
| 73 | ret get gradd i |
| 74 | end |
| 75 | if eq k 7 |
| 76 | ret get grmul i |
| 77 | end |
| 78 | if eq k 8 |
| 79 | ret get grsub i |
| 80 | end |
| 81 | if eq k 9 |
| 82 | ret get gfor i |
| 83 | end |
| 84 | ret get gsadd i |
| 85 | end |
| 86 | |
| 87 | note Line s[a, e) is text k followed by one digit 1..6 when dg is 1, by one |
| 88 | note or two digits when dg is 2, or exactly text k when dg is 0. |
| 89 | fn gr_lin s view uchar 4096 a ulong e ulong k ulong dg ulong to bool |
| 90 | n = gr_tln k |
| 91 | if lt sub e a n |
| 92 | ret false |
| 93 | end |
| 94 | for i ulong n |
| 95 | if ne get s add a i gr_tch k i |
| 96 | ret false |
| 97 | end |
| 98 | end |
| 99 | r = sub sub e a n |
| 100 | if eq dg 0 |
| 101 | ret eq r 0 |
| 102 | end |
| 103 | if eq dg 1 |
| 104 | if ne r 1 |
| 105 | ret false |
| 106 | end |
| 107 | c = get s add a n |
| 108 | ret and ge c 49 le c 54 |
| 109 | end |
| 110 | if or eq r 0 gt r 2 |
| 111 | ret false |
| 112 | end |
| 113 | for j ulong r |
| 114 | c = get s add add a n j |
| 115 | if or lt c 48 gt c 57 |
| 116 | ret false |
| 117 | end |
| 118 | end |
| 119 | ret true |
| 120 | end |
| 121 | |
| 122 | note Line s[a, e) is in the grammar. |
| 123 | fn gr_gram s view uchar 4096 a ulong e ulong to bool |
| 124 | for k ulong 5 |
| 125 | if gr_lin s a e k 0 |
| 126 | ret true |
| 127 | end |
| 128 | end |
| 129 | if gr_lin s a e 5 2 |
| 130 | ret true |
| 131 | end |
| 132 | for k2 ulong 5 |
| 133 | if gr_lin s a e add 6 k2 1 |
| 134 | ret true |
| 135 | end |
| 136 | end |
| 137 | ret false |
| 138 | end |
| 139 | |
| 140 | note The end of the line that starts at a (a newline or e). |
| 141 | fn gr_eol b view uchar 4096 a ulong e ulong to ulong |
| 142 | z = a |
| 143 | for j ulong sub e a |
| 144 | if eq get b z 10 |
| 145 | ret z |
| 146 | end |
| 147 | z = add z 1 |
| 148 | end |
| 149 | ret z |
| 150 | end |
| 151 | |
| 152 | note Every line of b[0, n), trimmed, is in the grammar (an empty source too). |
| 153 | fn gr_src b view uchar 4096 n ulong to bool |
| 154 | s = ulong 0 |
| 155 | for i ulong n |
| 156 | if gt get b s 32 |
| 157 | break |
| 158 | end |
| 159 | s = add s 1 |
| 160 | end |
| 161 | e = n |
| 162 | for j ulong sub n s |
| 163 | if gt get b sub e 1 32 |
| 164 | break |
| 165 | end |
| 166 | e = sub e 1 |
| 167 | end |
| 168 | if ge s e |
| 169 | ret true |
| 170 | end |
| 171 | a = s |
| 172 | for it ulong add n 1 |
| 173 | if gt a e |
| 174 | ret true |
| 175 | end |
| 176 | z = gr_eol b a e |
| 177 | if not gr_gram b a z |
| 178 | ret false |
| 179 | end |
| 180 | a = add z 1 |
| 181 | end |
| 182 | ret true |
| 183 | end |