Skip to content

Commit

Permalink
Tighten up parenthesization rules with unary prefix operators
Browse files Browse the repository at this point in the history
  • Loading branch information
ghewgill committed Jul 4, 2024
1 parent 715863c commit 07156a7
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 22 deletions.
8 changes: 4 additions & 4 deletions lib/complex.neon
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ END FUNCTION
* http://mathworld.wolfram.com/Cosine.html
*/
FUNCTION cos(a: Complex): Complex
RETURN make(math.cos(a.re)*rcosh(a.im), -math.sin(a.re)*rsinh(a.im))
RETURN make(math.cos(a.re)*rcosh(a.im), -(math.sin(a.re)*rsinh(a.im)))
END FUNCTION

/* Function: cosh
Expand Down Expand Up @@ -288,7 +288,7 @@ END FUNCTION
*/
FUNCTION inv(a: Complex): Complex
LET d: Number := (a.re*a.re) + (a.im*a.im)
RETURN make(a.re/d, -a.im/d)
RETURN make(a.re/d, -(a.im/d))
END FUNCTION

/* Function: log
Expand Down Expand Up @@ -335,7 +335,7 @@ END FUNCTION
FUNCTION pow(a, b: Complex): Complex
LET p: Number := a.arg()
LET m: Number := a.abs()
LET r: Number := (m^b.re) * math.exp(-b.im * p)
LET r: Number := (m^b.re) * math.exp(-(b.im * p))
LET t: Number := (b.re * p) + (b.im * math.log(m))
RETURN make(r * math.cos(t), r * math.sin(t))
END FUNCTION
Expand Down Expand Up @@ -402,7 +402,7 @@ END FUNCTION
*/
FUNCTION tan(a: Complex): Complex
LET u: Complex := make(math.tan(a.re), rtanh(a.im))
RETURN u.div(make(1, -u.re*u.im))
RETURN u.div(make(1, -(u.re*u.im)))
END FUNCTION

/* Function: tanh
Expand Down
2 changes: 1 addition & 1 deletion neon/lexer.neon
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ FUNCTION tokenize_fragment(source_path: String, INOUT line: Number, column_start
DEC nest
END IF
WHEN ":" DO
IF NOT inquote AND nest = 1 THEN
IF (NOT inquote) AND nest = 1 THEN
colon := i - 1
END IF
WHEN "\"" DO
Expand Down
9 changes: 7 additions & 2 deletions neon/parser.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1603,8 +1603,9 @@ FUNCTION Parser.parseAtom(INOUT self: Parser): POINTER TO ParseTreeNode
END FUNCTION

FUNCTION Parser.parseArithmetic(INOUT self: Parser): POINTER TO ParseTreeNode
LET leading_minus := self.token.type = TokenType.minus
VAR left: POINTER TO ParseTreeNode := self.parseCompoundExpression()
LET tokOp: lexer.Token := self.token
LET tokOp: lexer.Token := (IF leading_minus THEN lexer.Token(type WITH TokenType.plus) ELSE self.token)
CASE tokOp.type
WHEN TokenType.plus DO
WHILE self.token.type = TokenType.plus DO
Expand Down Expand Up @@ -1862,8 +1863,10 @@ FUNCTION Parser.parseMembership(INOUT self: Parser): POINTER TO ParseTreeNode
END FUNCTION

FUNCTION Parser.parseLogical(INOUT self: Parser): POINTER TO ParseTreeNode
LET leading_not := self.token.type = TokenType.not
VAR left: POINTER TO ParseTreeNode := self.parseMembership()
CASE self.token.type
LET op: lexer.Token := (IF leading_not THEN lexer.Token(type WITH TokenType.not) ELSE self.token)
CASE op.type
WHEN TokenType.and DO
WHILE self.token.type = TokenType.and DO
LET tokOp: lexer.Token := self.token
Expand Down Expand Up @@ -1896,6 +1899,8 @@ FUNCTION Parser.parseLogical(INOUT self: Parser): POINTER TO ParseTreeNode
)
)
END WHILE
WHEN TokenType.not DO
-- pass
WHEN OTHERS DO
RETURN left
END CASE
Expand Down
2 changes: 1 addition & 1 deletion samples/othello/othello.neon
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ FUNCTION main()
initialize()
updateBoard()
VAR game_over: Boolean := FALSE
WHILE NOT game_over AND NOT quit DO
WHILE (NOT game_over) AND (NOT quit) DO
LET any: Boolean := displayBoard()
IF NOT any THEN
game_over := TRUE
Expand Down
2 changes: 1 addition & 1 deletion samples/sudoku/sudoku.neon
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ FUNCTION draw_grid(grid: multiarray.ArrayNumber2D, cx, cy: Number)
curses.mvaddch(2+(2*i), 36, curses.ACS_RTEE())
END IF
END FOR
IF NOT any_errors AND puzzleFilled(grid) THEN
IF (NOT any_errors) AND puzzleFilled(grid) THEN
curses.mvaddstr(15, 40, "Puzzle completed.")
ELSE
curses.move(15, 40)
Expand Down
2 changes: 1 addition & 1 deletion samples/tetris/tetris.neon
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ FUNCTION main()
VAR new_piece: Boolean := TRUE
VAR last_drop: Number := 0
VAR game_over: Boolean := FALSE
WHILE NOT game_over AND NOT quit DO
WHILE (NOT game_over) AND (NOT quit) DO
IF new_piece THEN
p := next
next := 1 + (random.uint32() MOD 7)
Expand Down
11 changes: 8 additions & 3 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,10 @@ std::unique_ptr<Expression> Parser::parseCompoundExpression()

std::unique_ptr<Expression> Parser::parseArithmetic()
{
bool leading_minus = tokens[i].type == MINUS;
std::unique_ptr<Expression> left = parseCompoundExpression();
auto &tok_op = tokens[i];
switch (tokens[i].type) {
auto tok_op = leading_minus ? Token(PLUS, "+") : tokens[i];
switch (tok_op.type) {
case PLUS: {
while (tokens[i].type == PLUS) {
auto &tok_op = tokens[i];
Expand Down Expand Up @@ -935,8 +936,10 @@ std::unique_ptr<Expression> Parser::parseMembership()

std::unique_ptr<Expression> Parser::parseLogical()
{
bool leading_not = tokens[i].type == NOT;
std::unique_ptr<Expression> left = parseMembership();
switch (tokens[i].type) {
auto &tok_op = leading_not ? Token(NOT, "NOT") : tokens[i];
switch (tok_op.type) {
case AND:
while (tokens[i].type == AND) {
auto &tok_op = tokens[i];
Expand All @@ -953,6 +956,8 @@ std::unique_ptr<Expression> Parser::parseLogical()
left.reset(new DisjunctionExpression(tok_op, std::move(left), std::move(right)));
}
break;
case NOT:
break;
default:
return left;
}
Expand Down
2 changes: 1 addition & 1 deletion t/expr.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
VAR a: Number
VAR b: Number
b := 4
a := -(-5 * (3 + b))
a := -(-(5 * (3 + b)))
print(str(a))

--= 35
4 changes: 2 additions & 2 deletions t/intdiv.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TESTCASE 5 INTDIV 2 = 2
TESTCASE 5 INTDIV -2 = -2
TESTCASE -5 INTDIV 2 = -2
TESTCASE -5 INTDIV -2 = 2
TESTCASE (-5) INTDIV 2 = -2
TESTCASE (-5) INTDIV -2 = 2
8 changes: 4 additions & 4 deletions t/modulo.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ print(str(3 MOD 3))
print(str(10 MOD 3))
--= 1

print(str(-10 MOD 3))
print(str((-10) MOD 3))
--= 2

print(str(10 MOD -3))
--= -2

print(str(-10 MOD -3))
print(str((-10) MOD -3))
--= -1

print(str(3.14 MOD 0.7))
--= 0.34

print(str(-3.14 MOD 0.7))
print(str((-3.14) MOD 0.7))
--= 0.36

print(str(3.14 MOD -0.7))
--= -0.36

print(str(-3.14 MOD -0.7))
print(str((-3.14) MOD -0.7))
--= -0.34
8 changes: 6 additions & 2 deletions tools/helium.py
Original file line number Diff line number Diff line change
Expand Up @@ -1994,8 +1994,9 @@ def parse_compound_expression(self):
return expr

def parse_arithmetic(self):
leading_minus = self.tokens[self.i] is MINUS
left = self.parse_compound_expression()
if self.tokens[self.i] is PLUS:
if leading_minus or self.tokens[self.i] is PLUS:
while self.tokens[self.i] is PLUS:
self.i += 1
right = self.parse_compound_expression()
Expand Down Expand Up @@ -2088,8 +2089,11 @@ def parse_membership(self):
return left

def parse_logical(self):
leading_not = self.tokens[self.i] is NOT
left = self.parse_membership()
if self.tokens[self.i] is AND:
if leading_not:
pass
elif self.tokens[self.i] is AND:
while self.tokens[self.i] is AND:
self.i += 1
right = self.parse_membership()
Expand Down

0 comments on commit 07156a7

Please sign in to comment.