vmhooks.nim 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import pathutils
  10. template setX(k, field) {.dirty.} =
  11. var s: seq[TFullReg]
  12. move(s, cast[seq[TFullReg]](a.slots))
  13. if s[a.ra].kind != k:
  14. myreset(s[a.ra])
  15. s[a.ra].kind = k
  16. s[a.ra].field = v
  17. proc setResult*(a: VmArgs; v: BiggestInt) = setX(rkInt, intVal)
  18. proc setResult*(a: VmArgs; v: BiggestFloat) = setX(rkFloat, floatVal)
  19. proc setResult*(a: VmArgs; v: bool) =
  20. let v = v.ord
  21. setX(rkInt, intVal)
  22. proc setResult*(a: VmArgs; v: string) =
  23. var s: seq[TFullReg]
  24. move(s, cast[seq[TFullReg]](a.slots))
  25. if s[a.ra].kind != rkNode:
  26. myreset(s[a.ra])
  27. s[a.ra].kind = rkNode
  28. s[a.ra].node = newNode(nkStrLit)
  29. s[a.ra].node.strVal = v
  30. proc setResult*(a: VmArgs; n: PNode) =
  31. var s: seq[TFullReg]
  32. move(s, cast[seq[TFullReg]](a.slots))
  33. if s[a.ra].kind != rkNode:
  34. myreset(s[a.ra])
  35. s[a.ra].kind = rkNode
  36. s[a.ra].node = n
  37. proc setResult*(a: VmArgs; v: AbsoluteDir) = setResult(a, v.string)
  38. proc setResult*(a: VmArgs; v: seq[string]) =
  39. var s: seq[TFullReg]
  40. move(s, cast[seq[TFullReg]](a.slots))
  41. if s[a.ra].kind != rkNode:
  42. myreset(s[a.ra])
  43. s[a.ra].kind = rkNode
  44. var n = newNode(nkBracket)
  45. for x in v: n.add newStrNode(nkStrLit, x)
  46. s[a.ra].node = n
  47. template getX(k, field) {.dirty.} =
  48. doAssert i < a.rc-1
  49. let s = cast[seq[TFullReg]](a.slots)
  50. doAssert s[i+a.rb+1].kind == k
  51. result = s[i+a.rb+1].field
  52. proc getInt*(a: VmArgs; i: Natural): BiggestInt = getX(rkInt, intVal)
  53. proc getBool*(a: VmArgs; i: Natural): bool = getInt(a, i) != 0
  54. proc getFloat*(a: VmArgs; i: Natural): BiggestFloat = getX(rkFloat, floatVal)
  55. proc getString*(a: VmArgs; i: Natural): string =
  56. doAssert i < a.rc-1
  57. let s = cast[seq[TFullReg]](a.slots)
  58. doAssert s[i+a.rb+1].kind == rkNode
  59. result = s[i+a.rb+1].node.strVal
  60. proc getNode*(a: VmArgs; i: Natural): PNode =
  61. doAssert i < a.rc-1
  62. let s = cast[seq[TFullReg]](a.slots)
  63. doAssert s[i+a.rb+1].kind == rkNode
  64. result = s[i+a.rb+1].node