Skip to content

Commit eb437d0

Browse files
Change in Unary operator
1 parent 191db35 commit eb437d0

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

go_lexer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def find_column(token):
2828
states = (("InsertSemi", "exclusive"),)
2929

3030
# List of literal tokens
31-
literals = ";,.=+-*/%()[]{}"
31+
literals = ";,.=+-*/%()[]!{}"
3232

3333
# List of token names. This is always required
3434
tokens = (
@@ -45,7 +45,7 @@ def find_column(token):
4545
"BAR",
4646
"AMPER_AMPER",
4747
"BAR_BAR",
48-
"EXCLAMATION",
48+
# "EXCLAMATION",
4949
"COLON",
5050
# assignment operators
5151
"WALRUS",
@@ -197,7 +197,7 @@ def t_ANY_ignore_MULTI_COMMENT(t):
197197
t_BAR = r"\|"
198198
t_AMPER_AMPER = r"&&"
199199
t_BAR_BAR = r"\|\|"
200-
t_EXCLAMATION = r"!"
200+
# t_EXCLAMATION = r"!"
201201
t_COLON = r":"
202202
t_WALRUS = r":="
203203
# TODO: Move the assignment operators above, below

go_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ def p_UnaryExpr(p):
617617
def p_UnaryOp(p):
618618
"""UnaryOp : '+' %prec UNARY
619619
| '-' %prec UNARY
620+
| '!' %prec UNARY
620621
"""
621622
# TODO : Add other unary operators
622623
p[0] = p[1]

syntree.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class BinOp(Node):
4444
"""Node for binary operations"""
4545

4646
rel_ops = {"==", "!=", "<", ">", "<=", ">="}
47+
logical_ops = {"&&", "||"}
4748

4849
def __init__(self, operator, left=None, right=None, lineno=None):
4950
super().__init__("Binary", children=[left, right], data=operator)
@@ -53,6 +54,7 @@ def __init__(self, operator, left=None, right=None, lineno=None):
5354
self.lineno = lineno
5455

5556
self.is_relop = self.operator in self.rel_ops
57+
self.is_logical = self.operator in self.logical_ops
5658

5759
self.type_ = None
5860
try:
@@ -75,10 +77,6 @@ def get_type(child: Node) -> str:
7577

7678
return x
7779

78-
Logical_Oper = ["&&", "||"]
79-
if self.data in Logical_Oper:
80-
self.type = "bool"
81-
8280
x = get_type(self.children[0])
8381
y = get_type(self.children[1])
8482

@@ -105,11 +103,17 @@ def check_type(x, y):
105103
print_line_marker_nowhitespace(self.lineno)
106104

107105
if self.is_relop:
108-
self.type = "bool"
106+
self.type_ = "bool"
109107

110108
else:
111109
if self.is_relop:
112-
self.type = "bool"
110+
self.type_ = "bool"
111+
112+
elif self.is_logical:
113+
if x == "bool":
114+
self.type_ = "bool"
115+
else:
116+
print_error("Invalid Operation", kind="Operation Error")
113117

114118
else:
115119
self.type_ = x
@@ -138,6 +142,12 @@ def __init__(self, operator, operand):
138142
else:
139143
self.type_ = operand.data[0]
140144

145+
if self.type_ == "string":
146+
print_error("Type Error", kind="Invalid Operation")
147+
148+
if self.operator == '!' and self.type_ != "bool":
149+
print_error("Type Error", kind="Invalid Operation")
150+
141151

142152
class PrimaryExpr(Node):
143153
"""Node for PrimaryExpr
@@ -560,9 +570,7 @@ def __init__(self, body, expr, statement=None, next_=None, lineno=None):
560570
self._no_optim = True
561571

562572
if isinstance(self.expr, BinOp):
563-
if self.expr.data in ["&&", "||"]:
564-
pass
565-
elif not self.expr.is_relop:
573+
if self.expr.type_ != "bool":
566574
print_error("Invalid operator in condition", kind="ERROR")
567575
print("Cannot use non-boolean binary operator "
568576
f"{self.expr.operator}"

tests/logical_oper.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ func main() {
44
var p int = 23
55
var q int = 60
66

7+
// if(1 && 8){
8+
// fmt.Println("True")
9+
// }
10+
711
if(p!=q && p<=q){
812
fmt.Println("True")
913
}
@@ -12,8 +16,9 @@ func main() {
1216
fmt.Println("True")
1317
}
1418

15-
// if(!(p==q)){
16-
// fmt.Println("True")
17-
// }
19+
if(!(p==q)){
20+
fmt.Println("True")
21+
}
22+
// const abc = !(true)
1823

1924
}

0 commit comments

Comments
 (0)