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 / seed / omc / tests / recursion / nest.om

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