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"]
|
||||
|
||||
# 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 os
|
||||
|
|
|
|||
|
|
@ -59,6 +59,17 @@ macro isDefined*(def: untyped): untyped =
|
|||
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 =
|
||||
when defined(Windows):
|
||||
result = "[0-9.\\-]*\\.dll"
|
||||
|
|
|
|||
|
|
@ -206,13 +206,13 @@ proc getFileDate*(fullpath: string): string =
|
|||
when defined(Windows):
|
||||
let
|
||||
(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):
|
||||
&"stat -c %y {fullpath.sanitizePath}"
|
||||
&"stat -c %Y {fullpath.sanitizePath}"
|
||||
elif defined(OSX) or defined(FreeBSD):
|
||||
&"stat -f %m {fullpath.sanitizePath}"
|
||||
|
||||
(result, ret) = execAction(cmd)
|
||||
(result, ret) = execAction(cmd, die=false)
|
||||
|
||||
proc touchFile*(fullpath: string) =
|
||||
## 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)
|
||||
let leftNode = gState.processTSNode(left, typeofNode)
|
||||
var tyNode = typeofNode
|
||||
|
||||
if typeofNode.isNil:
|
||||
typeofNode = nkCall.newTree(
|
||||
gState.getIdent("typeof"),
|
||||
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 nkCall.newTree(
|
||||
typeofNode,
|
||||
tyNode,
|
||||
rightNode
|
||||
)
|
||||
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
|
||||
# expression to the type of the first arg
|
||||
result = nkCall.newTree(
|
||||
typeofNode,
|
||||
tyNode,
|
||||
result
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ yield""".split(Whitespace).toHashSet()
|
|||
|
||||
const
|
||||
# Enum macro read from file - written into wrapper when required
|
||||
gEnumMacroConst = staticRead(currentSourcePath.parentDir().parentDir() / "enumtype.nim")
|
||||
gEnumMacroConst = "import nimterop / enumtypepub"
|
||||
|
||||
var
|
||||
gEnumMacro* = gEnumMacroConst
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ extern "C" {
|
|||
#define EQ4 AVAL < BVAL
|
||||
#define EQ5 AVAL != BVAL
|
||||
#define EQ6 AVAL == BVAL
|
||||
#define SX_NEAR_ZERO (1.0f / (1 << 28))
|
||||
|
||||
// testing integer out of long int range
|
||||
#define INT_FAST16_MIN (-9223372036854775807L-1)
|
||||
|
|
|
|||
|
|
@ -134,6 +134,8 @@ assert EQ4 == (AVAL < BVAL)
|
|||
assert EQ5 == (AVAL != BVAL)
|
||||
assert EQ6 == (AVAL == BVAL)
|
||||
|
||||
assert SX_NEAR_ZERO == 3.725290298461914e-09
|
||||
|
||||
assert SIZEOF == 1
|
||||
|
||||
assert COERCE == 645635670332'u64
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue