aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2019-01-17 22:19:26 -0600
committerGanesh Viswanathan <dev@genotrance.com>2019-01-17 22:19:26 -0600
commit287980c9edbb1f037b1827b62ca8cc3453b2203a (patch)
tree5c22d6b28a6c0a8ec715024ea881f5070ea55d7b
parent8a09f3e290a7d36c9a7b8dc3795e993a8fe3a94b (diff)
downloadnimterop-287980c9edbb1f037b1827b62ca8cc3453b2203a.tar.gz
nimterop-287980c9edbb1f037b1827b62ca8cc3453b2203a.zip
Use HashSet, fix #40, compare nimNames
-rw-r--r--nimterop/ast.nim4
-rw-r--r--nimterop/getters.nim14
-rw-r--r--nimterop/globals.nim4
-rw-r--r--nimterop/grammar.nim27
4 files changed, 27 insertions, 22 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index 13dcf2c..6981deb 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -1,4 +1,4 @@
-import strformat, strutils, tables
+import sets, strformat, strutils, tables
import regex
@@ -14,7 +14,7 @@ const gAtoms = @[
"primitive_type",
"sized_type_specifier",
"type_identifier"
-]
+].toSet()
proc saveNodeData(node: TSNode): bool =
let name = $node.tsNodeType()
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index d6f17c6..59dbb15 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -1,4 +1,4 @@
-import macros, os, strformat, strutils, tables
+import macros, os, sets, strformat, strutils, tables
import regex
@@ -24,7 +24,7 @@ using
var
when while
xor
-yield""".split(Whitespace)
+yield""".split(Whitespace).toSet()
const gTypeMap = {
# char
@@ -92,21 +92,25 @@ proc getType*(str: string): string =
result = gTypeMap[result]
proc getIdentifier*(str: string): string =
- result = str.strip(chars={'_'}).replace(re"_+", "_").getType()
+ result = str.strip(chars={'_'}).replace(re"_+", "_")
if result in gReserved:
result = &"`{result}`"
-proc getUniqueIdentifier*(exists: seq[string], prefix = ""): string =
+proc getUniqueIdentifier*(existing: HashSet[string], prefix = ""): string =
var
name = prefix & "_" & gStateRT.sourceFile.extractFilename().multiReplace([(".", ""), ("-", "")])
+ nimName = name.replace("_", "").toLowerAscii
count = 1
- while (name & $count) in exists:
+ while (nimName & $count) in existing:
count += 1
return name & $count
+proc addNewIdentifer*(existing: var HashSet[string], name: string): bool =
+ return not existing.containsOrIncl(name.replace("_", "").toLowerAscii)
+
proc getPtrType*(str: string): string =
result = case str:
of "ptr cchar":
diff --git a/nimterop/globals.nim b/nimterop/globals.nim
index 121b799..ed69ff5 100644
--- a/nimterop/globals.nim
+++ b/nimterop/globals.nim
@@ -1,4 +1,4 @@
-import tables
+import sets, tables
import regex
@@ -26,7 +26,7 @@ type
debug*, past*, preprocess*, pnim*, pretty*, recurse*: bool
- consts*, enums*, procs*, types*: seq[string]
+ consts*, enums*, procs*, types*: HashSet[string]
code*, constStr*, currentHeader*, debugStr*, enumStr*, mode*, procStr*, typeStr*: string
sourceFile*: string # eg, C or C++ source or header file
diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim
index d2632b8..ae46404 100644
--- a/nimterop/grammar.nim
+++ b/nimterop/grammar.nim
@@ -1,4 +1,4 @@
-import strformat, strutils, tables
+import sets, strformat, strutils, tables
import regex
@@ -17,8 +17,7 @@ proc initGrammar() =
name = gStateRT.data[0].val.getIdentifier()
val = gStateRT.data[1].val.getLit()
- if name notin gStateRT.consts and val.nBl:
- gStateRT.consts.add(name)
+ if val.nBl and gStateRT.consts.addNewIdentifer(name):
gStateRT.constStr &= &" {name}* = {val}\n"
))
@@ -82,7 +81,8 @@ proc initGrammar() =
pname = "a" & $count
count += 1
i += 1
- pout &= &"{pname}: {getPtrType(pptr&ptyp)},"
+ if pptr == "ptr " or ptyp != "object":
+ pout &= &"{pname}: {getPtrType(pptr&ptyp)},"
# typedef int X
# typedef X Y
@@ -123,7 +123,7 @@ proc initGrammar() =
name = gStateRT.data[i].val.getIdentifier()
i += 1
- if name notin gStateRT.types:
+ if gStateRT.types.addNewIdentifer(name):
if i < gStateRT.data.len and gStateRT.data[^1].name == "function_declarator":
var
pout, pname, ptyp, pptr = ""
@@ -142,7 +142,6 @@ proc initGrammar() =
else:
gStateRT.typeStr &= &" {name}* = proc({pout}) {{.nimcall.}}\n"
else:
- gStateRT.types.add(name)
if i < gStateRT.data.len and gStateRT.data[i].name in ["identifier", "number_literal"]:
let
flen = gStateRT.data[i].val.getIdentifier()
@@ -176,8 +175,7 @@ proc initGrammar() =
union = " {.union.}"
break
- if nname notin gStateRT.types:
- gStateRT.types.add(nname)
+ if gStateRT.types.addNewIdentifer(nname):
gStateRT.typeStr &= &" {nname}* {{.importc: \"{prefix}{name}\", header: {gStateRT.currentHeader}, bycopy.}} = object{union}\n"
var
@@ -310,8 +308,7 @@ proc initGrammar() =
if nname.len == 0:
nname = getUniqueIdentifier(gStateRT.enums, "Enum")
- if nname notin gStateRT.enums:
- gStateRT.enums.add(nname)
+ if gStateRT.enums.addNewIdentifer(nname):
gStateRT.enumStr &= &"\ntype {nname}* = distinct int"
gStateRT.enumStr &= &"\nconverter enumToInt(en: {nname}): int {{.used.}} = en.int\n"
@@ -326,7 +323,7 @@ proc initGrammar() =
i += 1
continue
- if fname notin gStateRT.consts:
+ if gStateRT.consts.addNewIdentifer(fname):
if i+1 < gStateRT.data.len-fend and
gStateRT.data[i+1].name in ["identifier", "shift_expression", "math_expression", "number_literal"]:
if " " in gStateRT.data[i+1].val:
@@ -432,8 +429,7 @@ proc initGrammar() =
if pout.len != 0 and pout[^1] == ',':
pout = pout[0 .. ^2]
- if fnname notin gStateRT.procs:
- gStateRT.procs.add(fnname)
+ if gStateRT.procs.addNewIdentifer(fnname):
if ftyp != "object":
gStateRT.procStr &= &"proc {fnname}*({pout}): {getPtrType(fptr&ftyp)} {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n"
else:
@@ -456,6 +452,11 @@ proc initRegex(ast: ref Ast) =
raise newException(Exception, getCurrentExceptionMsg())
proc parseGrammar*() =
+ gStateRT.consts.init()
+ gStateRT.enums.init()
+ gStateRT.procs.init()
+ gStateRT.types.init()
+
initGrammar()
gStateRT.ast = initTable[string, seq[ref Ast]]()