From 8bb7d497b3a0a6d5b8e70e6525567c8c980a3610 Mon Sep 17 00:00:00 2001 From: rakuzzan Date: Wed, 6 Mar 2024 22:02:36 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20switch=5Fstatement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parser/list_statements/list_statements.py | 156 ++++++++---------- 1 file changed, 73 insertions(+), 83 deletions(-) diff --git a/practice/stend_language/parser/list_statements/list_statements.py b/practice/stend_language/parser/list_statements/list_statements.py index 1cfe06b..b7f252f 100644 --- a/practice/stend_language/parser/list_statements/list_statements.py +++ b/practice/stend_language/parser/list_statements/list_statements.py @@ -1,25 +1,27 @@ from parser.expression.expression import expression -from parser.common.identifier import identifier -from parser.common.number import number -from lexer.token_provider import match_token, pop_token -from lexer.lexer import * -from lexer.token_type import TokenType +from identifier import identifier +from number import number +from lexer.token_provider import pop_token, read_token +from lexer.temp_token_provider import NoNextTokenException # LIST STATEMENTS # def list_statements() -> bool: """ -> ;|""" - if statement(): - if match_token(TokenType.SEMICOLON): - pop_token() - return list_statements() - return True - return False + try: + if (statement()): + if (read_token() == ';'): + pop_token() + return list_statements() + return True + return False + except NoNextTokenException: + print('out of tokens') + return False # RULES # - def statement() -> bool: - """ -> | | | [ LIST STATEMENT ]""" + """ -> | | """ """TODO поддержать в [ ]""" return ( empty_statement() @@ -29,142 +31,130 @@ def statement() -> bool: or if_statement() or write_statement() or read_statement() - or - ( - match_token(TokenType.LBRACKET) and pop_token() - and list_statements() and - match_token(TokenType.RBRACKET) and pop_token() - ) + or switch_statement() ) def empty_statement() -> bool: """ -> ;""" - if match_token(TokenType.SEMICOLON): + if (read_token() == ';'): pop_token() return True return False - def assignment_statement() -> bool: """ -> := """ - return identifier() and pop_token() and match_token(TokenType.ASSIGN) and pop_token() and expression() - + return identifier() and pop_token() and pop_token() == ":=" and expression() def if_statement() -> bool: """ -> IF THEN FI""" return ( - match_token(TokenType.IF) + read_token() == 'IF' and pop_token() and expression() - and match_token(TokenType.THEN) - and pop_token() + and pop_token() == 'THEN' and list_statements() and optional_else() - and match_token(TokenType.FI) - and pop_token() + and pop_token() == 'FI' ) - def optional_else() -> bool: """ -> e | ELSE """ - if match_token(TokenType.ELSE): + if read_token() == 'ELSE': pop_token() return list_statements() return True - def for_statement() -> bool: """ -> for := to do rof""" + return ( - match_token(TokenType.FOR) + read_token() == "FOR" and pop_token() and identifier() and pop_token() - and match_token(TokenType.ASSIGN) - and pop_token() + and pop_token() == ":=" and number() and pop_token() - and match_token(TokenType.TO) - and pop_token() + and pop_token() == "TO" and number() and pop_token() - and match_token(TokenType.DO) - and pop_token() + and pop_token() == "DO" and list_statements() - and match_token(TokenType.ROF) - and pop_token() + and pop_token() == "ROF" ) - def while_statement() -> bool: """ -> WHILE do el""" return ( - match_token(TokenType.WHILE) + read_token() == "WHILE" and pop_token() and expression() - and match_token(TokenType.DO) + and read_token() == "DO" and pop_token() and list_statements() - and match_token(TokenType.EL) + and pop_token() == "EL" + ) + +def switch_statement() -> bool: + """ -> SWITCH CASE """ + + return ( + read_token() == "SWITCH" + and pop_token() + and identifier() and pop_token() + and case() ) +def case() -> bool(): + """CASE -> CASE | CASE iws""" + + if read_token() == "CASE": + return ( + read_token() == "CASE" + and pop_token() + and number() + and pop_token() + and list_statements() + and case() + ) + + return read_token() == "IWS" + def read_statement() -> bool: """ -> | """ return readline() or read() - def readline() -> bool: """ -> ReadLine()""" - return (match_token(TokenType.READLINE) - and pop_token() - and match_token(TokenType.LPAREN) - and pop_token() - and identifier() - and pop_token() - and match_token(TokenType.RPAREN) - and pop_token() - ) - + try: + return read_token() == "READLINE" and pop_token() and pop_token() == "(" and identifier() and pop_token() and pop_token() == ")" + except Exception: + return False def read() -> bool: """ -> Read()""" - return (match_token(TokenType.READ) - and pop_token() - and match_token(TokenType.LPAREN) - and pop_token() - and identifier() - and pop_token() - and match_token(TokenType.RPAREN) - and pop_token() - ) - + try: + return read_token() == "READ" and pop_token() and pop_token() == "(" and identifier() and pop_token() and pop_token() == ")" + except Exception: + return False def write_statement() -> bool: """ -> | """ return writeline() or write() - def writeline() -> bool: """ -> WriteLine()""" - return (match_token(TokenType.WRITELINE) - and pop_token() - and match_token(TokenType.LPAREN) - and pop_token() - and expression() - and match_token(TokenType.RPAREN) - and pop_token() - ) - + try: + return read_token() == "WRITELINE" and pop_token() and pop_token() == "(" and expression() and pop_token() == ")" + except Exception: + return False def write() -> bool: """ -> Write()""" - return (match_token(TokenType.WRITE) - and pop_token() - and match_token(TokenType.LPAREN) - and pop_token() - and expression() - and match_token(TokenType.RPAREN) - and pop_token() - ) + try: + return read_token() == "WRITE" and pop_token() and pop_token() == "(" and expression() and pop_token() == ")" + except Exception: + return False + From d7e9b2d4939f50dc8bd2fb9cec66ba3b7a72be56 Mon Sep 17 00:00:00 2001 From: rakuzzan Date: Thu, 14 Mar 2024 05:50:00 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice/stend_language/lexer/token_type.py | 5 +++- .../parser/list_statements/list_statements.py | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/practice/stend_language/lexer/token_type.py b/practice/stend_language/lexer/token_type.py index 7ec959b..84d9a55 100644 --- a/practice/stend_language/lexer/token_type.py +++ b/practice/stend_language/lexer/token_type.py @@ -56,4 +56,7 @@ class TokenType(Enum): FALSE = 52 EQUAL = 53 NOT_EQUAL = 54 - COMMA = 55 \ No newline at end of file + COMMA = 55 + SWITCH = 56 + CASE = 57 + IWS = 58 \ No newline at end of file diff --git a/practice/stend_language/parser/list_statements/list_statements.py b/practice/stend_language/parser/list_statements/list_statements.py index 1cfe06b..9389216 100644 --- a/practice/stend_language/parser/list_statements/list_statements.py +++ b/practice/stend_language/parser/list_statements/list_statements.py @@ -29,6 +29,7 @@ def statement() -> bool: or if_statement() or write_statement() or read_statement() + or switch_statement() or ( match_token(TokenType.LBRACKET) and pop_token() @@ -110,6 +111,33 @@ def while_statement() -> bool: and pop_token() ) +def switch_statement() -> bool: + """ -> SWITCH CASE """ + + return ( + match_token(TokenType.SWITCH) + and pop_token() + and identifier() + and pop_token() + and case() + ) + + +def case() -> bool(): + """CASE -> CASE | CASE iws""" + + if match_token(TokenType.CASE): + return ( + pop_token() + and number() + and pop_token() + and list_statements() + and case() + ) + + return match_token(TokenType.IWS) + + def read_statement() -> bool: """ -> | """ return readline() or read()