added while

This commit is contained in:
IgorCielniak
2025-12-25 10:31:40 +01:00
parent 6988a89a51
commit 60ec54e58c
2 changed files with 38 additions and 0 deletions

25
main.py
View File

@@ -450,6 +450,15 @@ class Parser:
if lexeme == "next": if lexeme == "next":
self._handle_next_control() self._handle_next_control()
continue continue
if lexeme == "begin":
self._handle_begin_control()
continue
if lexeme == "while":
self._handle_while_control()
continue
if lexeme == "repeat":
self._handle_repeat_control()
continue
if self._maybe_expand_macro(token): if self._maybe_expand_macro(token):
continue continue
self._handle_token(token) self._handle_token(token)
@@ -723,6 +732,22 @@ class Parser:
entry = self._pop_control(("for",)) entry = self._pop_control(("for",))
self._append_node(ForNext(loop_label=entry["loop"], end_label=entry["end"])) self._append_node(ForNext(loop_label=entry["loop"], end_label=entry["end"]))
def _handle_begin_control(self) -> None:
begin_label = self._new_label("begin")
end_label = self._new_label("end")
self._append_node(Label(name=begin_label))
self._push_control({"type": "begin", "begin": begin_label, "end": end_label})
def _handle_while_control(self) -> None:
entry = self._pop_control(("begin",))
self._append_node(BranchZero(target=entry["end"]))
self._push_control(entry)
def _handle_repeat_control(self) -> None:
entry = self._pop_control(("begin",))
self._append_node(Jump(target=entry["begin"]))
self._append_node(Label(name=entry["end"]))
def _begin_definition(self, token: Token) -> None: def _begin_definition(self, token: Token) -> None:
if self._eof(): if self._eof():
raise ParseError(f"definition name missing after ':' at {token.line}:{token.column}") raise ParseError(f"definition name missing after ':' at {token.line}:{token.column}")

13
while_test.sl Normal file
View File

@@ -0,0 +1,13 @@
import stdlib/stdlib.sl
import stdlib/io.sl
: main
10
begin
dup 0 >
while
dup puti cr
1 -
repeat
drop
;