tunary_minus.nim 2.4 KB

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