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