Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
+ add an example
  • Loading branch information
jd-develop committed Apr 5, 2022
1 parent bf098a6 commit 54042f0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
7 changes: 7 additions & 0 deletions examples/dice.noug
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# dice
import random
while True then
var input_ = input("'exit' to exit, anything else to roll a dice: ")
if input_ == 'exit' then break
print(random_randint(1, 6))
end
28 changes: 20 additions & 8 deletions src/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,45 @@ def statements(self, stop: list[Union[tuple[str, Any], str]] = None) -> ParseRes
if self.current_token.type == TT_EOF:
return result.success(NoNode())

last_token_type = self.current_token.type

statement = result.register(self.statement())
if result.error is not None:
return result
statements.append(statement)

more_statements = True

while True:
newline_count = 0
while self.current_token.type == TT_NEWLINE:
result.register_advancement()
self.advance()
newline_count += 1
if newline_count == 0:
more_statements = False

have_to_break = False
for e in stop:
if isinstance(e, tuple):
if not more_statements or self.current_token.matches(*e):
if self.current_token.matches(*e):
have_to_break = True
else:
if not more_statements or self.current_token.type == e:
if self.current_token.type == e:
have_to_break = True
if have_to_break:
break
else:
if newline_count == 0:
if last_token_type == TT_IDENTIFIER and self.current_token.type in EQUALS:
return result.failure(
InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
"unexpected token. To declare a variable, use 'var' keyword."
)
)
return result.failure(
InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
"unexpected token."
)
)

statement = result.register(self.statement())
if result.error is not None:
Expand Down Expand Up @@ -187,8 +200,7 @@ def expr(self):
self.advance()

equal = self.current_token
equals = [TT_EQ, TT_PLUSEQ, TT_MINUSEQ, TT_MULTEQ, TT_DIVEQ, TT_POWEQ, TT_FLOORDIVEQ, TT_PERCEQ, TT_OREQ,
TT_ANDEQ, TT_XOREQ, TT_BITWISEANDEQ, TT_BITWISEOREQ, TT_BITWISEXOREQ]
equals = EQUALS

if self.current_token.type not in equals:
if self.current_token.type not in TOKENS_TO_QUOTE:
Expand Down
17 changes: 17 additions & 0 deletions src/token_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,20 @@
TT_COMMA,
TT_ARROW
]

EQUALS = [
TT_EQ,
TT_PLUSEQ,
TT_MINUSEQ,
TT_MULTEQ,
TT_DIVEQ,
TT_POWEQ,
TT_FLOORDIVEQ,
TT_PERCEQ,
TT_OREQ,
TT_ANDEQ,
TT_XOREQ,
TT_BITWISEANDEQ,
TT_BITWISEOREQ,
TT_BITWISEXOREQ
]

0 comments on commit 54042f0

Please sign in to comment.