Skip to content

Commit

Permalink
Removed redundant pass statement
Browse files Browse the repository at this point in the history
  • Loading branch information
SageTendo committed Jan 8, 2024
1 parent e69a22b commit 1a7e648
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 42 deletions.
4 changes: 1 addition & 3 deletions nyaa.g4
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ funcDef : DEFINE ID LPAR (ID (',' ID)*)? RPAR TO LBRACE body RBRACE;
body : statement*;
conditionalBody : statement conditionalBody? | BREAK | CONTINUE;

statement : PASS
|assignmentStatement
statement : assignmentStatement
|retStatement
|whileStatement
|ifStatement
Expand Down Expand Up @@ -83,7 +82,6 @@ ELSE : 'baka';
RANGE: 'from';
BREAK: 'yamete';
CONTINUE: 'motto';
PASS: 'pasu';
DEFINE: 'kawaii';
TRUE: 'HAI';
FALSE: 'IIE';
Expand Down
7 changes: 1 addition & 6 deletions src/Interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from src.core.ASTNodes import PrintNode, BodyNode, ProgramNode, ArgsNode, ExprNode, SimpleExprNode, TermNode, \
FactorNode, OperatorNode, IdentifierNode, NumericLiteralNode, StringLiteralNode, InputNode, AssignmentNode, \
PostfixExprNode, CallNode, FuncDefNode, ReturnNode, BooleanNode, IfNode, WhileNode, ForNode, BreakNode, \
ContinueNode, PassNode
ContinueNode
from src.core.CacheMemory import cache_mem
from src.core.Environment import Environment
from src.core.RuntimeObject import RunTimeObject
Expand Down Expand Up @@ -145,11 +145,6 @@ def visit_return(self, node: 'ReturnNode'):
if node.expr is not None:
return node.expr.accept(self)

def visit_pass(self, node: 'PassNode'):
self.node_start_pos = node.start_pos
self.node_end_pos = node.end_pos
pass

def visit_break(self, node: 'BreakNode'):
self.node_start_pos = node.start_pos
self.node_end_pos = node.end_pos
Expand Down
11 changes: 5 additions & 6 deletions src/Lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
RESERVED_WORDS = {
"uWu_nyaa": TokenType.MAIN, "yomu": TokenType.PRINT, "ohayo": TokenType.INPUT,
"daijoubu": TokenType.WHILE, "nani": TokenType.IF, "nandesuka": TokenType.ELIF,
"baka": TokenType.ELSE, 'yamete': TokenType.BREAK, 'pasu': TokenType.PASS,
'motto': TokenType.CONTINUE, 'kawaii': TokenType.DEF, 'HAI': TokenType.TRUE,
'IIE': TokenType.FALSE, 'wa': TokenType.ASSIGN, "modoru": TokenType.RET,
'purasu': TokenType.PLUS, 'mainasu': TokenType.MINUS, 'purodakuto': TokenType.MULTIPLY,
'supuritto': TokenType.DIVIDE, 'ando': TokenType.AND, 'matawa': TokenType.OR,
'nai': TokenType.NOT, 'for': TokenType.FOR
"baka": TokenType.ELSE, 'yamete': TokenType.BREAK, 'motto': TokenType.CONTINUE,
'kawaii': TokenType.DEF, 'HAI': TokenType.TRUE, 'IIE': TokenType.FALSE,
'wa': TokenType.ASSIGN, "modoru": TokenType.RET, 'purasu': TokenType.PLUS,
'mainasu': TokenType.MINUS, 'purodakuto': TokenType.MULTIPLY, 'supuritto': TokenType.DIVIDE,
'ando': TokenType.AND, 'matawa': TokenType.OR, 'nai': TokenType.NOT, 'for': TokenType.FOR
}


Expand Down
11 changes: 2 additions & 9 deletions src/Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from src.Lexer import Lexer
from src.core.AComponent import AComponent
from src.core.ASTNodes import BodyNode, ReturnNode, PassNode, ProgramNode, SimpleExprNode, AssignmentNode, \
from src.core.ASTNodes import BodyNode, ReturnNode, ProgramNode, SimpleExprNode, AssignmentNode, \
IdentifierNode, \
PostfixExprNode, PrintNode, ArgsNode, NumericLiteralNode, StringLiteralNode, BooleanNode, FactorNode, ExprNode, \
CallNode, InputNode, BreakNode, ContinueNode, \
Expand Down Expand Up @@ -192,14 +192,7 @@ def parse_statement(self):
statement_node = None
start_pos = self.curr_tkn.pos

if self.match(TokenType.PASS):
self.__expect_and_consume(TokenType.PASS)

self.debug("<PASS>")
statement_node = PassNode()
self.debug("</PASS>")

elif self.match(TokenType.RET):
if self.match(TokenType.RET):
statement_node = self.parse_return()

elif self.match(TokenType.ID):
Expand Down
5 changes: 0 additions & 5 deletions src/core/ASTNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ def append(self, statement):
self.statements.append(statement)


class PassNode(Node):
def __init__(self):
super().__init__("pass")


class BreakNode(Node):
def __init__(self):
super().__init__("break")
Expand Down
7 changes: 3 additions & 4 deletions src/core/Types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class TokenType(Enum):
FALSE = auto() # IIE

# Breaks
PASS = auto() # pasu
CONTINUE = auto() # motto
BREAK = auto() # yamete
RET = auto() # modoru
Expand Down Expand Up @@ -72,9 +71,9 @@ class TokenType(Enum):
@classmethod
def statement_start(cls, token):
return token.type in [
TokenType.PASS, TokenType.RET,
TokenType.ID, TokenType.WHILE, TokenType.FOR, TokenType.IF,
TokenType.PRINT, TokenType.INPUT
TokenType.RET, TokenType.ID, TokenType.WHILE,
TokenType.FOR, TokenType.IF, TokenType.PRINT,
TokenType.INPUT
]

@classmethod
Expand Down
19 changes: 13 additions & 6 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from src.Parser import Parser
from src.core.Token import Token
from src.core.Types import TokenType
from src.utils.Constants import WARNING, SUCCESS, ENDC
from src.utils.Constants import WARNING, SUCCESS, ENDC, ERROR


class TestNyaa(TestCase):
Expand All @@ -25,6 +25,7 @@ def tearDown(self):
self.lexer = None
self.parser = None
self.Interpreter = None
print("", flush=True)

def test_lexer(self):
header = "Testing Lexer"
Expand All @@ -41,10 +42,9 @@ def test_lexer(self):
token = Token()
while token.type != TokenType.ENDMARKER:
token = self.lexer.get_token()
print(" ", token)

print(f"{SUCCESS} Passed{ENDC}")
except Exception as e:
print(f"{ERROR} Failed{ENDC}")
print(e, file=sys.stderr)
self.fail()

Expand All @@ -69,17 +69,19 @@ def test_lexer_errors(self):
token = Token()
while token.type != TokenType.ENDMARKER:
token = self.lexer.get_token()
print(" ", token)
print(" ", token)

self.fail()
except Exception as e:
if expected.lower() not in str(e).lower():
print(f"{ERROR} Failed{ENDC}")
print(expected in str(e))
print(e)
self.fail(f"EXPECTED:\n"
f" {expected}\n"
f"ACTUAL:\n"
f" {e}")
print(f"{SUCCESS} Passed{ENDC}")
finally:
self.lexer.__init__()

Expand All @@ -93,9 +95,10 @@ def test_parser(self):
print(f"[Parser] Running test on: {file}")

try:
self.parser.parse_source(source_path=test_dir + file, dflags={"parser": True})
print(f"{SUCCESS} Passed{ENDC}\n")
self.parser.parse_source(source_path=test_dir + file)
print(f"{SUCCESS} Passed{ENDC}")
except Exception as e:
print(f"{ERROR} Failed{ENDC}")
print(e, file=sys.stderr)
self.fail()

Expand Down Expand Up @@ -169,10 +172,12 @@ def test_interpreter_errors(self):

print(f"[Interpreter Error] Running test on: {file}")
if expected.lower().strip() not in str(proc.stderr).lower().strip():
print(f"{ERROR} Failed{ENDC}")
self.fail(f"EXPECTED:\n"
f" {expected}\n"
f"ACTUAL:\n"
f" {proc.stderr}")
print(f"{SUCCESS} Passed{ENDC}")

def test_operator_precedence_expressions(self):
header = "Testing Operator Precedence Expressions"
Expand Down Expand Up @@ -206,6 +211,7 @@ def test_operator_precedence_expressions(self):
result = self.interpreter.interpret(ast)

if result.value != expected:
print(f"{ERROR} Failed{ENDC}")
self.fail(f"EXPRESSION:\n"
f" {eval_input}\n"
f"EXPECTED RESULT= {expected}\n"
Expand Down Expand Up @@ -247,6 +253,7 @@ def test_prioritized_expressions(self):
result = self.interpreter.interpret(ast)

if result.value != expected:
print(f"{WARNING} Failed{ENDC}")
self.fail(f"EXPRESSION:\n"
f" {eval_input}\n"
f"EXPECTED RESULT= {expected}\n"
Expand Down
1 change: 0 additions & 1 deletion tests/errors/interpreter/for_range_invalid.ny
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
uWu_nyaa() => {
for _ => (3.3, "3") {
pasu
}
}
1 change: 0 additions & 1 deletion tests/lexer/random.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ kawaii add(x, y) => {
}

kawaii test() => {
pasu
yamete
motto
}
Expand Down
1 change: 0 additions & 1 deletion tests/lexer/tokens.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ nani
daijoubu
for
yomu
pasu
motto
yamete
modoru
Expand Down

0 comments on commit 1a7e648

Please sign in to comment.