make 'inline' come before 'word' instead of after the function definition

This commit is contained in:
IgorCielniak
2026-01-10 22:19:55 +01:00
parent 88ee10face
commit 8139aa66d9
2 changed files with 24 additions and 11 deletions

32
main.py
View File

@@ -361,6 +361,7 @@ class Parser:
self.compile_time_vm = CompileTimeVM(self) self.compile_time_vm = CompileTimeVM(self)
self.custom_prelude: Optional[List[str]] = None self.custom_prelude: Optional[List[str]] = None
self.custom_bss: Optional[List[str]] = None self.custom_bss: Optional[List[str]] = None
self._pending_inline_definition: bool = False
def location_for_token(self, token: Token) -> Tuple[str, int, int]: def location_for_token(self, token: Token) -> Tuple[str, int, int]:
for span in self.file_spans: for span in self.file_spans:
@@ -452,6 +453,7 @@ class Parser:
self._last_token = None self._last_token = None
self.custom_prelude = None self.custom_prelude = None
self.custom_bss = None self.custom_bss = None
self._pending_inline_definition = False
try: try:
while not self._eof(): while not self._eof():
@@ -469,7 +471,8 @@ class Parser:
self._handle_list_end(token) self._handle_list_end(token)
continue continue
if lexeme == "word": if lexeme == "word":
self._begin_definition(token, terminator="end") inline_def = self._consume_pending_inline()
self._begin_definition(token, terminator="end", inline=inline_def)
continue continue
if lexeme == "end": if lexeme == "end":
if self.control_stack: if self.control_stack:
@@ -813,19 +816,30 @@ class Parser:
self._end_definition(token) self._end_definition(token)
return True return True
def _begin_definition(self, token: Token, terminator: str = "end") -> None: def _consume_pending_inline(self) -> bool:
pending = self._pending_inline_definition
self._pending_inline_definition = False
return pending
def _begin_definition(self, token: Token, terminator: str = "end", inline: bool = False) -> None:
if self._eof(): if self._eof():
raise ParseError( raise ParseError(
f"definition name missing after '{token.lexeme}' at {token.line}:{token.column}" f"definition name missing after '{token.lexeme}' at {token.line}:{token.column}"
) )
name_token = self._consume() name_token = self._consume()
definition = Definition(name=name_token.lexeme, body=[], terminator=terminator) definition = Definition(
name=name_token.lexeme,
body=[],
terminator=terminator,
inline=inline,
)
self.context_stack.append(definition) self.context_stack.append(definition)
word = self.dictionary.lookup(definition.name) word = self.dictionary.lookup(definition.name)
if word is None: if word is None:
word = Word(name=definition.name) word = Word(name=definition.name)
self.dictionary.register(word) self.dictionary.register(word)
word.definition = definition word.definition = definition
word.inline = inline
self.definition_stack.append(word) self.definition_stack.append(word)
def _end_definition(self, token: Token) -> None: def _end_definition(self, token: Token) -> None:
@@ -2141,12 +2155,12 @@ def macro_compile_only(ctx: MacroContext) -> Optional[List[Op]]:
def macro_inline(ctx: MacroContext) -> Optional[List[Op]]: def macro_inline(ctx: MacroContext) -> Optional[List[Op]]:
parser = ctx.parser parser = ctx.parser
word = parser.most_recent_definition() next_tok = parser.peek_token()
if word is None: if next_tok is None or next_tok.lexeme != "word":
raise ParseError("'inline' must follow a definition") raise ParseError("'inline' must be followed by 'word'")
word.inline = True if parser._pending_inline_definition:
if word.definition is not None: raise ParseError("duplicate 'inline' before 'word'")
word.definition.inline = True parser._pending_inline_definition = True
return None return None

View File

@@ -1,7 +1,6 @@
word fn inline word fn
"hello" "hello"
end end
inline
word main word main
1 fn 3 1 syscall 0 1 fn 3 1 syscall 0