vmops.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, 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 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 wrap1s_os(op) {.dirty.} =
  32. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  33. setResult(a, op(getString(a, 0)))
  34. osop op
  35. template wrap1s_ospaths(op) {.dirty.} =
  36. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  37. setResult(a, op(getString(a, 0)))
  38. ospathsop op
  39. template wrap1s_system(op) {.dirty.} =
  40. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  41. setResult(a, op(getString(a, 0)))
  42. systemop op
  43. template wrap2svoid_system(op) {.dirty.} =
  44. proc `op Wrapper`(a: VmArgs) {.nimcall.} =
  45. op(getString(a, 0), getString(a, 1))
  46. systemop op
  47. proc getCurrentExceptionMsgWrapper(a: VmArgs) {.nimcall.} =
  48. setResult(a, if a.currentException.isNil: ""
  49. else: a.currentException.sons[3].skipColon.strVal)
  50. proc staticWalkDirImpl(path: string, relative: bool): PNode =
  51. result = newNode(nkBracket)
  52. for k, f in walkDir(path, relative):
  53. result.add newTree(nkPar, newIntNode(nkIntLit, k.ord),
  54. newStrNode(nkStrLit, f))
  55. proc gorgeExWrapper(a: VmArgs) {.nimcall.} =
  56. let (s, e) = opGorge(getString(a, 0), getString(a, 1), getString(a, 2),
  57. a.currentLineInfo)
  58. setResult a, newTree(nkPar, newStrNode(nkStrLit, s), newIntNode(nkIntLit, e))
  59. proc registerAdditionalOps*(c: PCtx) =
  60. wrap1f_math(sqrt)
  61. wrap1f_math(ln)
  62. wrap1f_math(log10)
  63. wrap1f_math(log2)
  64. wrap1f_math(exp)
  65. wrap1f_math(round)
  66. wrap1f_math(arccos)
  67. wrap1f_math(arcsin)
  68. wrap1f_math(arctan)
  69. wrap2f_math(arctan2)
  70. wrap1f_math(cos)
  71. wrap1f_math(cosh)
  72. wrap2f_math(hypot)
  73. wrap1f_math(sinh)
  74. wrap1f_math(sin)
  75. wrap1f_math(tan)
  76. wrap1f_math(tanh)
  77. wrap2f_math(pow)
  78. wrap1f_math(trunc)
  79. wrap1f_math(floor)
  80. wrap1f_math(ceil)
  81. wrap2f_math(fmod)
  82. wrap1s_ospaths(getEnv)
  83. wrap1s_ospaths(existsEnv)
  84. wrap1s_os(dirExists)
  85. wrap1s_os(fileExists)
  86. wrap2svoid_system(writeFile)
  87. wrap1s_system(readFile)
  88. systemop getCurrentExceptionMsg
  89. registerCallback c, "stdlib.*.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
  90. setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
  91. systemop gorgeEx