Compare commits
8 commits
fix_cast_e
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa9e66b4a3 | ||
|
|
a9b92647f3 |
||
|
|
b568863527 |
||
|
|
217bce386a | ||
|
|
1b8aa46b8a | ||
|
|
316a56107a | ||
|
|
00941e8925 | ||
|
|
93c83728c7 |
8 changed files with 74 additions and 8 deletions
|
|
@ -9,7 +9,7 @@ bin = @["nimterop/toast", "nimterop/loaf"]
|
||||||
installDirs = @["nimterop"]
|
installDirs = @["nimterop"]
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
requires "nim >= 0.20.2", "regex >= 0.15.0", "cligen >= 1.0.0"
|
requires "nim >= 0.20.2", "regex >= 0.15.0", "cligen >= 1.5.3"
|
||||||
|
|
||||||
import nimterop/docs
|
import nimterop/docs
|
||||||
import os
|
import os
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,17 @@ macro isDefined*(def: untyped): untyped =
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
macro getDefine*(def: untyped): untyped =
|
||||||
|
let version = newIdentNode(def.strVal())
|
||||||
|
let verVal =
|
||||||
|
if gDefines.hasKey(def.strVal()):
|
||||||
|
gDefines[def.strVal()]
|
||||||
|
else:
|
||||||
|
""
|
||||||
|
result = quote do:
|
||||||
|
const `version` {.strdefine.} = `verVal`
|
||||||
|
`version`
|
||||||
|
|
||||||
proc getDynlibExt(): string =
|
proc getDynlibExt(): string =
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
result = "[0-9.\\-]*\\.dll"
|
result = "[0-9.\\-]*\\.dll"
|
||||||
|
|
|
||||||
|
|
@ -206,13 +206,13 @@ proc getFileDate*(fullpath: string): string =
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
let
|
let
|
||||||
(head, tail) = fullpath.splitPath()
|
(head, tail) = fullpath.splitPath()
|
||||||
&"cmd /c forfiles /P {head.sanitizePath()} /M {tail.sanitizePath} /C \"cmd /c echo @fdate @ftime @fsize\""
|
&"forfiles /P {head.sanitizePath()} /M {tail.sanitizePath} /C \"cmd /c echo @fdate @ftime\""
|
||||||
elif defined(Linux):
|
elif defined(Linux):
|
||||||
&"stat -c %y {fullpath.sanitizePath}"
|
&"stat -c %Y {fullpath.sanitizePath}"
|
||||||
elif defined(OSX) or defined(FreeBSD):
|
elif defined(OSX) or defined(FreeBSD):
|
||||||
&"stat -f %m {fullpath.sanitizePath}"
|
&"stat -f %m {fullpath.sanitizePath}"
|
||||||
|
|
||||||
(result, ret) = execAction(cmd)
|
(result, ret) = execAction(cmd, die=false)
|
||||||
|
|
||||||
proc touchFile*(fullpath: string) =
|
proc touchFile*(fullpath: string) =
|
||||||
## Touch file to update modified date
|
## Touch file to update modified date
|
||||||
|
|
|
||||||
42
nimterop/enumtypepub.nim
Normal file
42
nimterop/enumtypepub.nim
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
import macros
|
||||||
|
|
||||||
|
macro defineEnum*(typ: untyped): untyped =
|
||||||
|
result = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
|
# Enum mapped to distinct cint
|
||||||
|
result.add quote do:
|
||||||
|
type `typ`* = distinct cint
|
||||||
|
|
||||||
|
for i in ["+", "-", "*", "div", "mod", "shl", "shr", "or", "and", "xor", "<", "<=", "==", ">", ">="]:
|
||||||
|
let
|
||||||
|
ni = newIdentNode(i)
|
||||||
|
typout = if i[0] in "<=>": newIdentNode("bool") else: typ # comparisons return bool
|
||||||
|
if i[0] == '>': # cannot borrow `>` and `>=` from templates
|
||||||
|
let
|
||||||
|
nopp = if i.len == 2: newIdentNode("<=") else: newIdentNode("<")
|
||||||
|
result.add quote do:
|
||||||
|
proc `ni`*(x: `typ`, y: cint): `typout` = `nopp`(y, x)
|
||||||
|
proc `ni`*(x: cint, y: `typ`): `typout` = `nopp`(y, x)
|
||||||
|
proc `ni`*(x, y: `typ`): `typout` = `nopp`(y, x)
|
||||||
|
else:
|
||||||
|
result.add quote do:
|
||||||
|
proc `ni`*(x: `typ`, y: cint): `typout` {.borrow.}
|
||||||
|
proc `ni`*(x: cint, y: `typ`): `typout` {.borrow.}
|
||||||
|
proc `ni`*(x, y: `typ`): `typout` {.borrow.}
|
||||||
|
result.add quote do:
|
||||||
|
proc `ni`*(x: `typ`, y: int): `typout` = `ni`(x, y.cint)
|
||||||
|
proc `ni`*(x: int, y: `typ`): `typout` = `ni`(x.cint, y)
|
||||||
|
|
||||||
|
let
|
||||||
|
divop = newIdentNode("/") # `/`()
|
||||||
|
dlrop = newIdentNode("$") # `$`()
|
||||||
|
notop = newIdentNode("not") # `not`()
|
||||||
|
result.add quote do:
|
||||||
|
proc `divop`*(x, y: `typ`): `typ` = `typ`((x.float / y.float).cint)
|
||||||
|
proc `divop`*(x: `typ`, y: cint): `typ` = `divop`(x, `typ`(y))
|
||||||
|
proc `divop`*(x: cint, y: `typ`): `typ` = `divop`(`typ`(x), y)
|
||||||
|
proc `divop`*(x: `typ`, y: int): `typ` = `divop`(x, y.cint)
|
||||||
|
proc `divop`*(x: int, y: `typ`): `typ` = `divop`(x.cint, y)
|
||||||
|
|
||||||
|
proc `dlrop`*(x: `typ`): string {.borrow.}
|
||||||
|
proc `notop`*(x: `typ`): `typ` {.borrow.}
|
||||||
|
|
@ -379,18 +379,28 @@ proc processBinaryExpression(gState: State, node: TSNode, typeofNode: var PNode)
|
||||||
|
|
||||||
result.add gState.getIdent(nimSym)
|
result.add gState.getIdent(nimSym)
|
||||||
let leftNode = gState.processTSNode(left, typeofNode)
|
let leftNode = gState.processTSNode(left, typeofNode)
|
||||||
|
var tyNode = typeofNode
|
||||||
|
|
||||||
if typeofNode.isNil:
|
if typeofNode.isNil:
|
||||||
typeofNode = nkCall.newTree(
|
typeofNode = nkCall.newTree(
|
||||||
gState.getIdent("typeof"),
|
gState.getIdent("typeof"),
|
||||||
leftNode
|
leftNode
|
||||||
)
|
)
|
||||||
|
tyNode = typeofNode
|
||||||
|
|
||||||
let rightNode = gState.processTSNode(right, typeofNode)
|
# Special case of setting the shift left/right type
|
||||||
|
# to be the type of the direct left operand
|
||||||
|
if binarySym in [">>", "<<"]:
|
||||||
|
tyNode = nkCall.newTree(
|
||||||
|
gState.getIdent("typeof"),
|
||||||
|
leftNode
|
||||||
|
)
|
||||||
|
|
||||||
|
let rightNode = gState.processTSNode(right, tyNode)
|
||||||
|
|
||||||
result.add leftNode
|
result.add leftNode
|
||||||
result.add nkCall.newTree(
|
result.add nkCall.newTree(
|
||||||
typeofNode,
|
tyNode,
|
||||||
rightNode
|
rightNode
|
||||||
)
|
)
|
||||||
if binarySym == "/":
|
if binarySym == "/":
|
||||||
|
|
@ -399,7 +409,7 @@ proc processBinaryExpression(gState: State, node: TSNode, typeofNode: var PNode)
|
||||||
# So we need to emulate C here and cast the whole
|
# So we need to emulate C here and cast the whole
|
||||||
# expression to the type of the first arg
|
# expression to the type of the first arg
|
||||||
result = nkCall.newTree(
|
result = nkCall.newTree(
|
||||||
typeofNode,
|
tyNode,
|
||||||
result
|
result
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ yield""".split(Whitespace).toHashSet()
|
||||||
|
|
||||||
const
|
const
|
||||||
# Enum macro read from file - written into wrapper when required
|
# Enum macro read from file - written into wrapper when required
|
||||||
gEnumMacroConst = staticRead(currentSourcePath.parentDir().parentDir() / "enumtype.nim")
|
gEnumMacroConst = "import nimterop / enumtypepub"
|
||||||
|
|
||||||
var
|
var
|
||||||
gEnumMacro* = gEnumMacroConst
|
gEnumMacro* = gEnumMacroConst
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ extern "C" {
|
||||||
#define EQ4 AVAL < BVAL
|
#define EQ4 AVAL < BVAL
|
||||||
#define EQ5 AVAL != BVAL
|
#define EQ5 AVAL != BVAL
|
||||||
#define EQ6 AVAL == BVAL
|
#define EQ6 AVAL == BVAL
|
||||||
|
#define SX_NEAR_ZERO (1.0f / (1 << 28))
|
||||||
|
|
||||||
// testing integer out of long int range
|
// testing integer out of long int range
|
||||||
#define INT_FAST16_MIN (-9223372036854775807L-1)
|
#define INT_FAST16_MIN (-9223372036854775807L-1)
|
||||||
|
|
|
||||||
|
|
@ -134,6 +134,8 @@ assert EQ4 == (AVAL < BVAL)
|
||||||
assert EQ5 == (AVAL != BVAL)
|
assert EQ5 == (AVAL != BVAL)
|
||||||
assert EQ6 == (AVAL == BVAL)
|
assert EQ6 == (AVAL == BVAL)
|
||||||
|
|
||||||
|
assert SX_NEAR_ZERO == 3.725290298461914e-09
|
||||||
|
|
||||||
assert SIZEOF == 1
|
assert SIZEOF == 1
|
||||||
|
|
||||||
assert COERCE == 645635670332'u64
|
assert COERCE == 645635670332'u64
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue