vmhooks.nim 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. a.slots[a.ra].ensureKind(k)
  12. a.slots[a.ra].field = v
  13. proc setResult*(a: VmArgs; v: BiggestInt) = setX(rkInt, intVal)
  14. proc setResult*(a: VmArgs; v: BiggestFloat) = setX(rkFloat, floatVal)
  15. proc setResult*(a: VmArgs; v: bool) =
  16. let v = v.ord
  17. setX(rkInt, intVal)
  18. proc setResult*(a: VmArgs; v: string) =
  19. a.slots[a.ra].ensureKind(rkNode)
  20. a.slots[a.ra].node = newNode(nkStrLit)
  21. a.slots[a.ra].node.strVal = v
  22. proc setResult*(a: VmArgs; n: PNode) =
  23. a.slots[a.ra].ensureKind(rkNode)
  24. a.slots[a.ra].node = n
  25. proc setResult*(a: VmArgs; v: AbsoluteDir) = setResult(a, v.string)
  26. proc setResult*(a: VmArgs; v: seq[string]) =
  27. a.slots[a.ra].ensureKind(rkNode)
  28. var n = newNode(nkBracket)
  29. for x in v: n.add newStrNode(nkStrLit, x)
  30. a.slots[a.ra].node = n
  31. template getX(k, field) {.dirty.} =
  32. doAssert i < a.rc-1
  33. doAssert a.slots[i+a.rb+1].kind == k
  34. result = a.slots[i+a.rb+1].field
  35. proc getInt*(a: VmArgs; i: Natural): BiggestInt = getX(rkInt, intVal)
  36. proc getBool*(a: VmArgs; i: Natural): bool = getInt(a, i) != 0
  37. proc getFloat*(a: VmArgs; i: Natural): BiggestFloat = getX(rkFloat, floatVal)
  38. proc getString*(a: VmArgs; i: Natural): string =
  39. doAssert i < a.rc-1
  40. doAssert a.slots[i+a.rb+1].kind == rkNode
  41. result = a.slots[i+a.rb+1].node.strVal
  42. proc getNode*(a: VmArgs; i: Natural): PNode =
  43. doAssert i < a.rc-1
  44. doAssert a.slots[i+a.rb+1].kind == rkNode
  45. result = a.slots[i+a.rb+1].node