From 93c83728c71132a63c69ecfaab84c4c91bb05615 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sun, 23 Aug 2020 17:11:18 -0600 Subject: [PATCH 1/8] Fix nimterop imports --- nimterop/enumtypepub.nim | 42 +++++++++++++++++++++++++++++++++++ nimterop/toastlib/getters.nim | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 nimterop/enumtypepub.nim diff --git a/nimterop/enumtypepub.nim b/nimterop/enumtypepub.nim new file mode 100644 index 0000000..7700e30 --- /dev/null +++ b/nimterop/enumtypepub.nim @@ -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.} diff --git a/nimterop/toastlib/getters.nim b/nimterop/toastlib/getters.nim index f0a4735..2e1ac4c 100644 --- a/nimterop/toastlib/getters.nim +++ b/nimterop/toastlib/getters.nim @@ -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 From 00941e892588503d18b3d271a65b0596bc6d857b Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 12 Sep 2020 10:59:14 -0600 Subject: [PATCH 2/8] Fix regen issues windows/linux --- nimterop/build/shell.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nimterop/build/shell.nim b/nimterop/build/shell.nim index f0af561..e5a70a8 100644 --- a/nimterop/build/shell.nim +++ b/nimterop/build/shell.nim @@ -206,9 +206,9 @@ 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}" From 316a56107a4ccfcc0773d5a5b7f59abf0e05f922 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 12 Sep 2020 12:48:45 -0600 Subject: [PATCH 3/8] Add ability to get a define value --- nimterop/build/getheader.nim | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nimterop/build/getheader.nim b/nimterop/build/getheader.nim index 4549894..bbc810a 100644 --- a/nimterop/build/getheader.nim +++ b/nimterop/build/getheader.nim @@ -59,6 +59,11 @@ macro isDefined*(def: untyped): untyped = false ) +macro getDefine*(def: untyped): untyped = + if gDefines.hasKey(def.strVal()): + return newStrLitNode(gDefines[def.strVal()]) + return newStrLitNode("") + proc getDynlibExt(): string = when defined(Windows): result = "[0-9.\\-]*\\.dll" From 1b8aa46b8a5b8254cee5aa0c5d73e29881ca7ed9 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 12 Sep 2020 15:24:29 -0600 Subject: [PATCH 4/8] Improve on getdefine --- nimterop/build/getheader.nim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/nimterop/build/getheader.nim b/nimterop/build/getheader.nim index bbc810a..36dd50c 100644 --- a/nimterop/build/getheader.nim +++ b/nimterop/build/getheader.nim @@ -60,9 +60,15 @@ macro isDefined*(def: untyped): untyped = ) macro getDefine*(def: untyped): untyped = - if gDefines.hasKey(def.strVal()): - return newStrLitNode(gDefines[def.strVal()]) - return newStrLitNode("") + 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): From 217bce386a2ef7119e4914b19461d18e0fae821a Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Thu, 17 Sep 2020 19:59:41 -0600 Subject: [PATCH 5/8] Don't die on nimcheck for cached files --- nimterop/build/shell.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nimterop/build/shell.nim b/nimterop/build/shell.nim index e5a70a8..a8511c2 100644 --- a/nimterop/build/shell.nim +++ b/nimterop/build/shell.nim @@ -212,7 +212,7 @@ proc getFileDate*(fullpath: string): string = 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 From 4f6cec613fa159abb354a4b9faa701d8c774e196 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Mon, 29 Jun 2020 20:39:34 -0600 Subject: [PATCH 6/8] Fix #233: make shl/shr use the direct left operand for type casting --- nimterop/toastlib/exprparser.nim | 16 +++++++++++++--- tests/include/tast2.h | 1 + tests/tast2.nim | 2 ++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/nimterop/toastlib/exprparser.nim b/nimterop/toastlib/exprparser.nim index a77dbf9..a67ed59 100644 --- a/nimterop/toastlib/exprparser.nim +++ b/nimterop/toastlib/exprparser.nim @@ -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 ) diff --git a/tests/include/tast2.h b/tests/include/tast2.h index 38c5f18..ede71d5 100644 --- a/tests/include/tast2.h +++ b/tests/include/tast2.h @@ -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) diff --git a/tests/tast2.nim b/tests/tast2.nim index a410de4..e301fb5 100644 --- a/tests/tast2.nim +++ b/tests/tast2.nim @@ -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 From b568863527d8b700922774e64a41db50f9b701cb Mon Sep 17 00:00:00 2001 From: Joey Date: Tue, 20 Oct 2020 11:08:52 -0600 Subject: [PATCH 7/8] Fix #233: make shl/shr use the direct left operand for type casting (#235) --- nimterop/toastlib/exprparser.nim | 16 +++++++++++++--- tests/include/tast2.h | 1 + tests/tast2.nim | 2 ++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/nimterop/toastlib/exprparser.nim b/nimterop/toastlib/exprparser.nim index a77dbf9..a67ed59 100644 --- a/nimterop/toastlib/exprparser.nim +++ b/nimterop/toastlib/exprparser.nim @@ -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 ) diff --git a/tests/include/tast2.h b/tests/include/tast2.h index 38c5f18..ede71d5 100644 --- a/tests/include/tast2.h +++ b/tests/include/tast2.h @@ -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) diff --git a/tests/tast2.nim b/tests/tast2.nim index a410de4..e301fb5 100644 --- a/tests/tast2.nim +++ b/tests/tast2.nim @@ -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 From fa9e66b4a371b78d97d074cf531fad8f55135334 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 15 May 2021 18:39:47 -0600 Subject: [PATCH 8/8] Update cligen --- nimterop.nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nimterop.nimble b/nimterop.nimble index 15a8ed1..87e1bc4 100644 --- a/nimterop.nimble +++ b/nimterop.nimble @@ -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