Skip to content

Commit 090f67e

Browse files
authored
Fix compound assignments (#4)
1 parent fe8a6d1 commit 090f67e

9 files changed

+369
-244
lines changed

MyLexer.g4

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// $antlr-format columnLimit 100, useTab false, minEmptyLines 0
1+
// $antlr-format columnLimit 120, useTab false, minEmptyLines 0
22
// $antlr-format alignSemicolons none, alignColons trailing
33
// $antlr-format alignLexerCommands true, alignLabels true, alignTrailers true
44

MyParser.g4

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// $antlr-format columnLimit 100, useTab false, minEmptyLines 1
1+
// $antlr-format columnLimit 120, useTab false, minEmptyLines 1
22
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true
33
// $antlr-format alignSemicolons hanging, alignColons hanging
44

@@ -57,13 +57,13 @@ comparison
5757
;
5858

5959
assignment
60-
: (id | elementReference) (
61-
ASSIGN
62-
| ASSIGN_PLUS
60+
: (id | elementReference) ASSIGN expression SEMICOLON # simpleAssignment
61+
| (id | elementReference) (
62+
ASSIGN_PLUS
6363
| ASSIGN_MINUS
6464
| ASSIGN_MULTIPLY
6565
| ASSIGN_DIVIDE
66-
) expression SEMICOLON
66+
) expression SEMICOLON # compoundAssignment
6767
;
6868

6969
print

ast_listener.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,24 @@ def exitWhileLoop(self, ctx: MyParser.WhileLoopContext):
5050
self.level -= 1
5151

5252
def enterComparison(self, ctx: MyParser.ComparisonContext):
53-
operator = ctx.getChild(1)
54-
print(IND * self.level + operator.getText())
53+
print(IND * self.level + ctx.getChild(1).getText())
5554
self.level += 1
5655

5756
def exitComparison(self, ctx: MyParser.ComparisonContext):
5857
self.level -= 1
5958

60-
def enterAssignment(self, ctx: MyParser.AssignmentContext):
61-
operator = ctx.getChild(1)
62-
print(IND * self.level + operator.getText())
59+
def enterSimpleAssignment(self, ctx: MyParser.SimpleAssignmentContext):
60+
print(IND * self.level + ctx.getChild(1).getText())
61+
self.level += 1
62+
63+
def exitSimpleAssignment(self, ctx: MyParser.SimpleAssignmentContext):
64+
self.level -= 1
65+
66+
def enterCompoundAssignment(self, ctx: MyParser.CompoundAssignmentContext):
67+
print(IND * self.level + ctx.getChild(1).getText())
6368
self.level += 1
6469

65-
def exitAssignment(self, ctx: MyParser.AssignmentContext):
70+
def exitCompoundAssignment(self, ctx: MyParser.CompoundAssignmentContext):
6671
self.level -= 1
6772

6873
def enterPrint(self, ctx: MyParser.PrintContext):

generated/MyParser.interp

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)