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 / omc / tests / recursion / nest.om
| 1 | note nest.om: a bounded recursive-descent evaluator in seeded Omegese (issue #645). It is the |
| 2 | note teaching fixture for direct and mutual recursion under explicit bounds, checked by |
| 3 | note seed/omc/gate-recursion.sh. The grammar over stdin (one trailing newline is ignored): |
| 4 | note expr = term { + term } term = primary { * primary } primary = digits | ( expr ) |
| 5 | note Arithmetic is ulong, wrapping mod 2^64. Evaluation is left to right. |
| 6 | note Two bounds, each checked before the call or frame it would admit: |
| 7 | note depth: an open parenthesis that would make the nesting deeper than DMAX is refused before |
| 8 | note any frame for it exists (depth 16 is accepted, depth 17 is refused). |
| 9 | note fuel: every primary costs one unit of FUEL; the primary that finds no fuel left is refused. |
| 10 | note The depth bound limits frames (at most 3 calls per level plus main); the fuel bound limits work. |
| 11 | note The first refusal wins and ends the evaluation; a refused input prints no value. |
| 12 | note Exit status and stdout (statuses stay below 100 so they never look like a trap): |
| 13 | note 0 "value V depth D fuel F" accepted; D deepest nesting reached, F primaries evaluated |
| 14 | note 40 "refused depth" nesting over DMAX |
| 15 | note 41 "refused syntax" not in the grammar (incl. empty input, missing base case "()") |
| 16 | note 42 "refused fuel" more than FUEL primaries |
| 17 | note 43 "refused input" stdin unreadable or over 4096 bytes |
| 18 | |
| 19 | const DMAX ulong 16 |
| 20 | const FUEL ulong 64 |
| 21 | global Src uchar 4096 |
| 22 | global St ulong 5 |
| 23 | global Out uchar 32 |
| 24 | global One uchar 1 |
| 25 | text tval "value " |
| 26 | text tdep " depth " |
| 27 | text tfue " fuel " |
| 28 | text tnl "\n" |
| 29 | text rdep "refused depth\n" |
| 30 | text rsyn "refused syntax\n" |
| 31 | text rfue "refused fuel\n" |
| 32 | text rinp "refused input\n" |
| 33 | |
| 34 | note St: 0 cursor, 1 input length, 2 status (0 while evaluating), 3 fuel left, 4 deepest depth |
| 35 | |
| 36 | fn peek to ulong |
| 37 | p = get St 0 |
| 38 | if ge p get St 1 |
| 39 | ret 256 |
| 40 | end |
| 41 | ret ulong get Src p |
| 42 | end |
| 43 | |
| 44 | fn skip to void |
| 45 | put St 0 add get St 0 1 |
| 46 | end |
| 47 | |
| 48 | fn stop code ulong to void |
| 49 | if eq get St 2 0 |
| 50 | put St 2 code |
| 51 | end |
| 52 | end |
| 53 | |
| 54 | fn ok to bool |
| 55 | ret eq get St 2 0 |
| 56 | end |
| 57 | |
| 58 | fn number to ulong |
| 59 | v = ulong 0 |
| 60 | n = ulong 0 |
| 61 | c = peek |
| 62 | while and ge c 48 le c 57 bound 4096 |
| 63 | v = add mul v 10 sub c 48 |
| 64 | skip |
| 65 | n = add n 1 |
| 66 | c = peek |
| 67 | end |
| 68 | if eq n 0 |
| 69 | stop 41 |
| 70 | end |
| 71 | ret v |
| 72 | end |
| 73 | |
| 74 | note the only place a frame deeper than the caller's is admitted: the depth check precedes the call |
| 75 | fn primary depth ulong to ulong |
| 76 | if eq get St 3 0 |
| 77 | stop 42 |
| 78 | ret 0 |
| 79 | end |
| 80 | put St 3 sub get St 3 1 |
| 81 | if ne peek 40 |
| 82 | ret number |
| 83 | end |
| 84 | nd = add depth 1 |
| 85 | if gt nd DMAX |
| 86 | stop 40 |
| 87 | ret 0 |
| 88 | end |
| 89 | if gt nd get St 4 |
| 90 | put St 4 nd |
| 91 | end |
| 92 | skip |
| 93 | inner = expr nd |
| 94 | if not ok |
| 95 | ret 0 |
| 96 | end |
| 97 | if ne peek 41 |
| 98 | stop 41 |
| 99 | ret 0 |
| 100 | end |
| 101 | skip |
| 102 | ret inner |
| 103 | end |
| 104 | |
| 105 | fn term depth ulong to ulong |
| 106 | v = primary depth |
| 107 | while ok bound 4096 |
| 108 | if ne peek 42 |
| 109 | break |
| 110 | end |
| 111 | skip |
| 112 | r = primary depth |
| 113 | v = mul v r |
| 114 | end |
| 115 | ret v |
| 116 | end |
| 117 | |
| 118 | fn expr depth ulong to ulong |
| 119 | v = term depth |
| 120 | while ok bound 4096 |
| 121 | if ne peek 43 |
| 122 | break |
| 123 | end |
| 124 | skip |
| 125 | r = term depth |
| 126 | v = add v r |
| 127 | end |
| 128 | ret v |
| 129 | end |
| 130 | |
| 131 | fn putnum v ulong to void |
| 132 | p = ulong 32 |
| 133 | x = v |
| 134 | while true bound 20 |
| 135 | p = sub p 1 |
| 136 | put Out p uchar add rem x 10 48 |
| 137 | x = div x 10 |
| 138 | if eq x 0 |
| 139 | break |
| 140 | end |
| 141 | end |
| 142 | w = write 1 Out p sub 32 p |
| 143 | end |
| 144 | |
| 145 | fn slurp to ulong |
| 146 | t = ulong 0 |
| 147 | while true bound 4097 |
| 148 | room = sub len Src t |
| 149 | if eq room 0 |
| 150 | r = read 0 One 0 1 |
| 151 | if ne r 0 |
| 152 | ret 4097 |
| 153 | end |
| 154 | break |
| 155 | end |
| 156 | r = read 0 Src t room |
| 157 | if lt r 0 |
| 158 | ret 4097 |
| 159 | end |
| 160 | if eq r 0 |
| 161 | break |
| 162 | end |
| 163 | t = add t ulong r |
| 164 | end |
| 165 | ret t |
| 166 | end |
| 167 | |
| 168 | fn main to int |
| 169 | t = slurp |
| 170 | if gt t 4096 |
| 171 | w = write 1 rinp 0 len rinp |
| 172 | ret 43 |
| 173 | end |
| 174 | if gt t 0 |
| 175 | if eq get Src sub t 1 10 |
| 176 | t = sub t 1 |
| 177 | end |
| 178 | end |
| 179 | put St 1 t |
| 180 | put St 3 FUEL |
| 181 | v = expr 0 |
| 182 | if ok |
| 183 | if ne get St 0 t |
| 184 | stop 41 |
| 185 | end |
| 186 | end |
| 187 | s = get St 2 |
| 188 | if eq s 40 |
| 189 | w = write 1 rdep 0 len rdep |
| 190 | ret 40 |
| 191 | end |
| 192 | if eq s 41 |
| 193 | w = write 1 rsyn 0 len rsyn |
| 194 | ret 41 |
| 195 | end |
| 196 | if eq s 42 |
| 197 | w = write 1 rfue 0 len rfue |
| 198 | ret 42 |
| 199 | end |
| 200 | w = write 1 tval 0 len tval |
| 201 | putnum v |
| 202 | w = write 1 tdep 0 len tdep |
| 203 | putnum get St 4 |
| 204 | w = write 1 tfue 0 len tfue |
| 205 | putnum sub FUEL get St 3 |
| 206 | w = write 1 tnl 0 len tnl |
| 207 | ret 0 |
| 208 | end |