vmops.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. # Unforunately this cannot be a module yet:
  10. #import vmdeps, vm
  11. from math import sqrt, ln, log10, log2, exp, round, arccos, arcsin,
  12. arctan, arctan2, cos, cosh, hypot, sinh, sin, tan, tanh, pow, trunc,
  13. floor, ceil, `mod`
  14. from os import getEnv, existsEnv, dirExists, fileExists, putEnv, walkDir
  15. template mathop(op) {.dirty.} =
  16. registerCallback(c, "stdlib.math." & astToStr(op), `op Wrapper`)
  17. template osop(op) {.dirty.} =
  18. registerCallback(c, "stdlib.os." & astToStr(op), `op Wrapper`)
  19. template systemop(op) {.dirty.} =
  20. registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`)
  21. template macrosop(op) {.dirty.} =
  22. registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`)
  23. template wrap1f_math(op) {.dirty.} =
  24. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  25. setResult(a, op(getFloat(a, 0)))
  26. mathop op
  27. template wrap2f_math(op) {.dirty.} =
  28. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  29. setResult(a, op(getFloat(a, 0), getFloat(a, 1)))
  30. mathop op
  31. template wrap0(op, modop) {.dirty.} =
  32. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  33. setResult(a, op())
  34. modop op
  35. template wrap1s(op, modop) {.dirty.} =
  36. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  37. setResult(a, op(getString(a, 0)))
  38. modop op
  39. template wrap2s(op, modop) {.dirty.} =
  40. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  41. setResult(a, op(getString(a, 0), getString(a, 1)))
  42. modop op
  43. template wrap1svoid(op, modop) {.dirty.} =
  44. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  45. op(getString(a, 0))
  46. modop op
  47. template wrap2svoid(op, modop) {.dirty.} =
  48. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  49. op(getString(a, 0), getString(a, 1))
  50. modop op
  51. proc getCurrentExceptionMsgWrapper(a: VmArgs) {.nimcall.} =
  52. setResult(a, if a.currentException.isNil: ""
  53. else: a.currentException.sons[3].skipColon.strVal)
  54. proc staticWalkDirImpl(path: string, relative: bool): PNode =
  55. result = newNode(nkBracket)
  56. for k, f in walkDir(path, relative):
  57. result.add newTree(nkTupleConstr, newIntNode(nkIntLit, k.ord),
  58. newStrNode(nkStrLit, f))
  59. proc registerAdditionalOps*(c: PCtx) =
  60. proc gorgeExWrapper(a: VmArgs) =
  61. let (s, e) = opGorge(getString(a, 0), getString(a, 1), getString(a, 2),
  62. a.currentLineInfo, c.config)
  63. setResult a, newTree(nkTupleConstr, newStrNode(nkStrLit, s), newIntNode(nkIntLit, e))
  64. proc getProjectPathWrapper(a: VmArgs) =
  65. setResult a, c.config.projectPath.string
  66. wrap1f_math(sqrt)
  67. wrap1f_math(ln)
  68. wrap1f_math(log10)
  69. wrap1f_math(log2)
  70. wrap1f_math(exp)
  71. wrap1f_math(round)
  72. wrap1f_math(arccos)
  73. wrap1f_math(arcsin)
  74. wrap1f_math(arctan)
  75. wrap2f_math(arctan2)
  76. wrap1f_math(cos)
  77. wrap1f_math(cosh)
  78. wrap2f_math(hypot)
  79. wrap1f_math(sinh)
  80. wrap1f_math(sin)
  81. wrap1f_math(tan)
  82. wrap1f_math(tanh)
  83. wrap2f_math(pow)
  84. wrap1f_math(trunc)
  85. wrap1f_math(floor)
  86. wrap1f_math(ceil)
  87. proc `mod Wrapper`(a: VmArgs) {.nimcall.} =
  88. setResult(a, `mod`(getFloat(a, 0), getFloat(a, 1)))
  89. registerCallback(c, "stdlib.math.mod", `mod Wrapper`)
  90. when defined(nimcore):
  91. wrap2s(getEnv, osop)
  92. wrap1s(existsEnv, osop)
  93. wrap2svoid(putEnv, osop)
  94. wrap1s(dirExists, osop)
  95. wrap1s(fileExists, osop)
  96. wrap2svoid(writeFile, systemop)
  97. wrap1s(readFile, systemop)
  98. systemop getCurrentExceptionMsg
  99. registerCallback c, "stdlib.*.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
  100. setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
  101. systemop gorgeEx
  102. macrosop getProjectPath