-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make unary minus part of number literals, refs #17020
- Loading branch information
Showing
4 changed files
with
172 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
discard """ | ||
targets: "c cpp js" | ||
""" | ||
|
||
# Test numeric literals and handling of minus symbol | ||
|
||
import std/[macros, strutils] | ||
|
||
macro lispReprStr*(a: untyped): untyped = newLit(a.lispRepr) | ||
|
||
macro assertAST*(expected: string, struct: untyped): untyped = | ||
var ast = newLit(struct.treeRepr) | ||
result = quote do: | ||
if `ast` != `expected`: | ||
doAssert false, "\nGot:\n" & `ast`.indent(2) & "\nExpected:\n" & `expected`.indent(2) | ||
|
||
const one = 1 | ||
const minusOne = `-`(one) | ||
|
||
# border cases that *should* generate compiler errors: | ||
assertAST dedent """ | ||
StmtList | ||
Asgn | ||
Ident "x" | ||
Command | ||
IntLit 4 | ||
IntLit -1""": | ||
x = 4 -1 | ||
assertAST dedent """ | ||
StmtList | ||
VarSection | ||
IdentDefs | ||
Ident "x" | ||
Ident "uint" | ||
IntLit -1""": | ||
var x: uint = -1 | ||
template bad() = | ||
x = 4 -1 | ||
doAssert not compiles(bad()) | ||
|
||
template main = | ||
block: # check when a minus (-) is a negative sign for a literal | ||
doAssert -1 == minusOne: | ||
"unable to parse a spaced-prefixed negative int" | ||
doAssert lispReprStr(-1) == """(IntLit -1)""" | ||
doAssert -1.0'f64 == minusOne.float64 | ||
doAssert lispReprStr(-1.000'f64) == """(Float64Lit -1.0)""" | ||
doAssert lispReprStr( -1.000'f64) == """(Float64Lit -1.0)""" | ||
doAssert [-1].contains(minusOne): | ||
"unable to handle negatives after square bracket" | ||
doAssert lispReprStr([-1]) == """(Bracket (IntLit -1))""" | ||
doAssert (-1, 2)[0] == minusOne: | ||
"unable to handle negatives after parenthesis" | ||
doAssert lispReprStr((-1, 2)) == """(Par (IntLit -1) (IntLit 2))""" | ||
proc x(): int = | ||
var a = 1;-1 # the -1 should act as the return value | ||
doAssert x() == minusOne: | ||
"unable to handle negatives after semi-colon" | ||
|
||
block: # check when a minus (-) is an unary op | ||
doAssert -one == minusOne: | ||
"unable to a negative prior to identifier" | ||
|
||
block: # check when a minus (-) is a a subtraction op | ||
doAssert 4-1 == 3: | ||
"unable to handle subtraction sans surrounding spaces with a numeric literal" | ||
doAssert 4-one == 3: | ||
"unable to handle subtraction sans surrounding spaces with an identifier" | ||
doAssert 4 - 1 == 3: | ||
"unable to handle subtraction with surrounding spaces with a numeric literal" | ||
doAssert 4 - one == 3: | ||
"unable to handle subtraction with surrounding spaces with an identifier" | ||
|
||
|
||
static: main() | ||
main() |