From 6a528bc7e9eb81c745161bbcf0eaeec9e5384df8 Mon Sep 17 00:00:00 2001 From: ReneSac Date: Wed, 8 Apr 2015 01:00:14 -0300 Subject: [PATCH 1/3] Consider #!strongSpaces for keyword operators too. When #!strongSpaces is on, every operator affected by it gains priority higher than any operator not affected by it. This includes comparison operators, addition, etc. It seems that counting spaces for keywords operators don't break anything in the parser. Of course, they can't have 0 spaces between their operands, but at least their precedence will work accordingly to their 1+ spaces. --- compiler/parser.nim | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index dcd5401e8a..0f97cb7579 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -227,11 +227,12 @@ proc getPrecedence(tok: TToken, strongSpaces: bool): int = of '.': considerAsgn(6) of '?': result = considerStrongSpaces(2) else: considerAsgn(2) - of tkDiv, tkMod, tkShl, tkShr: result = 9 - of tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs: result = 5 + of tkDiv, tkMod, tkShl, tkShr: result = considerStrongSpaces(9) + of tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs: + result = considerStrongSpaces(5) of tkDotDot: result = considerStrongSpaces(6) - of tkAnd: result = 4 - of tkOr, tkXor, tkPtr, tkRef: result = 3 + of tkAnd: result = considerStrongSpaces(4) + of tkOr, tkXor, tkPtr, tkRef: result = considerStrongSpaces(3) else: result = -10 proc isOperator(tok: TToken): bool = From 80050a09a07af06b888ac4ab41f8e1e83c6ce392 Mon Sep 17 00:00:00 2001 From: ReneSac Date: Wed, 8 Apr 2015 02:06:18 -0300 Subject: [PATCH 2/3] Refactored getPrecedence() after last change The considerStrongSpaces() is now applied to almost all results, so it is better to do it at the end. --- compiler/parser.nim | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index 0f97cb7579..f135c540c0 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -215,25 +215,25 @@ proc getPrecedence(tok: TToken, strongSpaces: bool): int = if L > 1 and tok.ident.s[L-1] == '>': return considerStrongSpaces(1) template considerAsgn(value: expr) = - result = if tok.ident.s[L-1] == '=': 1 else: considerStrongSpaces(value) + result = if tok.ident.s[L-1] == '=': 1 else: value case relevantChar of '$', '^': considerAsgn(10) of '*', '%', '/', '\\': considerAsgn(9) - of '~': result = considerStrongSpaces(8) + of '~': result = 8 of '+', '-', '|': considerAsgn(8) of '&': considerAsgn(7) - of '=', '<', '>', '!': result = considerStrongSpaces(5) + of '=', '<', '>', '!': result = 5 of '.': considerAsgn(6) - of '?': result = considerStrongSpaces(2) + of '?': result = 2 else: considerAsgn(2) - of tkDiv, tkMod, tkShl, tkShr: result = considerStrongSpaces(9) - of tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs: - result = considerStrongSpaces(5) - of tkDotDot: result = considerStrongSpaces(6) - of tkAnd: result = considerStrongSpaces(4) - of tkOr, tkXor, tkPtr, tkRef: result = considerStrongSpaces(3) - else: result = -10 + of tkDiv, tkMod, tkShl, tkShr: result = 9 + of tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs: result = 5 + of tkDotDot: result = 6 + of tkAnd: result = 4 + of tkOr, tkXor, tkPtr, tkRef: result = 3 + else: return -10 + result = considerStrongSpaces(result) proc isOperator(tok: TToken): bool = ## Determines if the given token is an operator type token. From 5bbebe4a8913b3f304701aa3b4eaf082bd4fe8ca Mon Sep 17 00:00:00 2001 From: ReneSac Date: Wed, 8 Apr 2015 02:08:29 -0300 Subject: [PATCH 3/3] Added tests for keyword operators in tstrongspaces Changed one old test to account for the effect of strong spaces on "and" and cia now. --- tests/parser/tstrongspaces.nim | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/parser/tstrongspaces.nim b/tests/parser/tstrongspaces.nim index 91506daf07..568abda4ca 100644 --- a/tests/parser/tstrongspaces.nim +++ b/tests/parser/tstrongspaces.nim @@ -2,6 +2,12 @@ discard """ output: '''35 +true +true +4 +true +1 +false 77 (Field0: 1, Field1: 2, Field2: 2) ha @@ -14,6 +20,17 @@ all args echo 2+5 * 5 +# Keyword operators +echo 1 + 16 shl 1 == 1 + (16 shl 1) +echo 2 and 1 in {0, 30} +echo 2+2 * 2 shr 1 +echo false or 2 and 1 in {0, 30} + +proc `^`(a, b: int): int = a + b div 2 +echo 19 mod 16 ^ 4 + 2 and 1 +echo 18 mod 16 ^ 4 > 0 + +# echo $foo gotcha let foo = 77 echo $foo @@ -27,7 +44,7 @@ when true: let b = 66 let c = 90 let bar = 8000 - if foo+4 * 4 == 8 and b&c | 9 ++ + if foo+4 * 4 == 8 and b&c | 9 ++ bar: echo "ho" else: