vmops.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, getAppFilename
  15. from md5 import getMD5
  16. from sighashes import symBodyDigest
  17. template mathop(op) {.dirty.} =
  18. registerCallback(c, "stdlib.math." & astToStr(op), `op Wrapper`)
  19. template osop(op) {.dirty.} =
  20. registerCallback(c, "stdlib.os." & astToStr(op), `op Wrapper`)
  21. template systemop(op) {.dirty.} =
  22. registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`)
  23. template ioop(op) {.dirty.} =
  24. registerCallback(c, "stdlib.io." & astToStr(op), `op Wrapper`)
  25. template macrosop(op) {.dirty.} =
  26. registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`)
  27. template md5op(op) {.dirty.} =
  28. registerCallback(c, "stdlib.md5." & astToStr(op), `op Wrapper`)
  29. template wrap1f_math(op) {.dirty.} =
  30. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  31. setResult(a, op(getFloat(a, 0)))
  32. mathop op
  33. template wrap2f_math(op) {.dirty.} =
  34. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  35. setResult(a, op(getFloat(a, 0), getFloat(a, 1)))
  36. mathop op
  37. template wrap0(op, modop) {.dirty.} =
  38. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  39. setResult(a, op())
  40. modop op
  41. template wrap1s(op, modop) {.dirty.} =
  42. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  43. setResult(a, op(getString(a, 0)))
  44. modop op
  45. template wrap2s(op, modop) {.dirty.} =
  46. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  47. setResult(a, op(getString(a, 0), getString(a, 1)))
  48. modop op
  49. template wrap2si(op, modop) {.dirty.} =
  50. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  51. setResult(a, op(getString(a, 0), getInt(a, 1)))
  52. modop op
  53. template wrap1svoid(op, modop) {.dirty.} =
  54. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  55. op(getString(a, 0))
  56. modop op
  57. template wrap2svoid(op, modop) {.dirty.} =
  58. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  59. op(getString(a, 0), getString(a, 1))
  60. modop op
  61. proc getCurrentExceptionMsgWrapper(a: VmArgs) {.nimcall.} =
  62. setResult(a, if a.currentException.isNil: ""
  63. else: a.currentException.sons[3].skipColon.strVal)
  64. proc staticWalkDirImpl(path: string, relative: bool): PNode =
  65. result = newNode(nkBracket)
  66. for k, f in walkDir(path, relative):
  67. result.add newTree(nkTupleConstr, newIntNode(nkIntLit, k.ord),
  68. newStrNode(nkStrLit, f))
  69. proc registerAdditionalOps*(c: PCtx) =
  70. proc gorgeExWrapper(a: VmArgs) =
  71. let (s, e) = opGorge(getString(a, 0), getString(a, 1), getString(a, 2),
  72. a.currentLineInfo, c.config)
  73. setResult a, newTree(nkTupleConstr, newStrNode(nkStrLit, s), newIntNode(nkIntLit, e))
  74. proc getProjectPathWrapper(a: VmArgs) =
  75. setResult a, c.config.projectPath.string
  76. wrap1f_math(sqrt)
  77. wrap1f_math(ln)
  78. wrap1f_math(log10)
  79. wrap1f_math(log2)
  80. wrap1f_math(exp)
  81. wrap1f_math(round)
  82. wrap1f_math(arccos)
  83. wrap1f_math(arcsin)
  84. wrap1f_math(arctan)
  85. wrap2f_math(arctan2)
  86. wrap1f_math(cos)
  87. wrap1f_math(cosh)
  88. wrap2f_math(hypot)
  89. wrap1f_math(sinh)
  90. wrap1f_math(sin)
  91. wrap1f_math(tan)
  92. wrap1f_math(tanh)
  93. wrap2f_math(pow)
  94. wrap1f_math(trunc)
  95. wrap1f_math(floor)
  96. wrap1f_math(ceil)
  97. wrap1s(getMD5, md5op)
  98. proc `mod Wrapper`(a: VmArgs) {.nimcall.} =
  99. setResult(a, `mod`(getFloat(a, 0), getFloat(a, 1)))
  100. registerCallback(c, "stdlib.math.mod", `mod Wrapper`)
  101. when defined(nimcore):
  102. wrap2s(getEnv, osop)
  103. wrap1s(existsEnv, osop)
  104. wrap2svoid(putEnv, osop)
  105. wrap1s(dirExists, osop)
  106. wrap1s(fileExists, osop)
  107. wrap2svoid(writeFile, ioop)
  108. wrap1s(readFile, ioop)
  109. wrap2si(readLines, ioop)
  110. systemop getCurrentExceptionMsg
  111. registerCallback c, "stdlib.*.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
  112. setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
  113. systemop gorgeEx
  114. macrosop getProjectPath
  115. registerCallback c, "stdlib.os.getCurrentCompilerExe", proc (a: VmArgs) {.nimcall.} =
  116. setResult(a, getAppFilename())
  117. registerCallback c, "stdlib.macros.symBodyHash", proc (a: VmArgs) {.nimcall.} =
  118. let n = getNode(a, 0)
  119. if n.kind != nkSym: raise newException(ValueError, "node is not a symbol")
  120. setResult(a, $symBodyDigest(c.graph, n.sym))