tunary_minus.nim 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. when hasWorkingInt64:
  58. doAssert -9223372036854775808 == int64.low
  59. block: # check when a minus (-) is an unary op
  60. doAssert -one == minusOne:
  61. "unable to a negative prior to identifier"
  62. block: # check when a minus (-) is a a subtraction op
  63. doAssert 4-1 == 3:
  64. "unable to handle subtraction sans surrounding spaces with a numeric literal"
  65. doAssert 4-one == 3:
  66. "unable to handle subtraction sans surrounding spaces with an identifier"
  67. doAssert 4 - 1 == 3:
  68. "unable to handle subtraction with surrounding spaces with a numeric literal"
  69. doAssert 4 - one == 3:
  70. "unable to handle subtraction with surrounding spaces with an identifier"
  71. static: main()
  72. main()