tunary_minus.nim 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. # Test numeric literals and handling of minus symbol
  5. import std/[macros, strutils]
  6. import mlexerutils
  7. const one = 1
  8. const minusOne = `-`(one)
  9. # border cases that *should* generate compiler errors:
  10. assertAST dedent """
  11. StmtList
  12. Asgn
  13. Ident "x"
  14. Command
  15. IntLit 4
  16. IntLit -1""":
  17. x = 4 -1
  18. assertAST dedent """
  19. StmtList
  20. VarSection
  21. IdentDefs
  22. Ident "x"
  23. Ident "uint"
  24. IntLit -1""":
  25. var x: uint = -1
  26. template bad() =
  27. x = 4 -1
  28. doAssert not compiles(bad())
  29. template main =
  30. block: # check when a minus (-) is a negative sign for a literal
  31. doAssert -1 == minusOne:
  32. "unable to parse a spaced-prefixed negative int"
  33. doAssert lispReprStr(-1) == """(IntLit -1)"""
  34. doAssert -1.0'f64 == minusOne.float64
  35. doAssert lispReprStr(-1.000'f64) == """(Float64Lit -1.0)"""
  36. doAssert lispReprStr( -1.000'f64) == """(Float64Lit -1.0)"""
  37. doAssert [-1].contains(minusOne):
  38. "unable to handle negatives after square bracket"
  39. doAssert lispReprStr([-1]) == """(Bracket (IntLit -1))"""
  40. doAssert (-1, 2)[0] == minusOne:
  41. "unable to handle negatives after parenthesis"
  42. doAssert lispReprStr((-1, 2)) == """(TupleConstr (IntLit -1) (IntLit 2))"""
  43. proc x(): int =
  44. var a = 1;-1 # the -1 should act as the return value
  45. doAssert x() == minusOne:
  46. "unable to handle negatives after semi-colon"
  47. block:
  48. doAssert -0b111 == -7
  49. doAssert -0xff == -255
  50. doAssert -128'i8 == (-128).int8
  51. doAssert $(-128'i8) == "-128"
  52. doAssert -32768'i16 == int16.low
  53. doAssert -2147483648'i32 == int32.low
  54. when int.sizeof > 4:
  55. doAssert -9223372036854775808 == int.low
  56. when not defined(js):
  57. doAssert -9223372036854775808 == int64.low
  58. block: # check when a minus (-) is an unary op
  59. doAssert -one == minusOne:
  60. "unable to a negative prior to identifier"
  61. block: # check when a minus (-) is a a subtraction op
  62. doAssert 4-1 == 3:
  63. "unable to handle subtraction sans surrounding spaces with a numeric literal"
  64. doAssert 4-one == 3:
  65. "unable to handle subtraction sans surrounding spaces with an identifier"
  66. doAssert 4 - 1 == 3:
  67. "unable to handle subtraction with surrounding spaces with a numeric literal"
  68. doAssert 4 - one == 3:
  69. "unable to handle subtraction with surrounding spaces with an identifier"
  70. static: main()
  71. main()