aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2018-12-26 13:25:45 -0600
committerGanesh Viswanathan <dev@genotrance.com>2018-12-26 13:25:45 -0600
commit34cb1715b876ac9dd09b97b938638f6beaff926b (patch)
tree4bb782e7ef4848796733e11dff30cae01cc7f10e
parent0697feed0f79cdb1aceac0c4dfc04c170208e40c (diff)
downloadnimterop-34cb1715b876ac9dd09b97b938638f6beaff926b.tar.gz
nimterop-34cb1715b876ac9dd09b97b938638f6beaff926b.zip
Support for math expressions, nameless enums, uintptr, redundant typedef naming
-rw-r--r--nimterop.nimble6
-rw-r--r--nimterop/ast.nim8
-rw-r--r--nimterop/getters.nim13
-rw-r--r--nimterop/grammar.nim46
-rw-r--r--tests/tmath.nim2
5 files changed, 62 insertions, 13 deletions
diff --git a/nimterop.nimble b/nimterop.nimble
index a504ffc..b1ee584 100644
--- a/nimterop.nimble
+++ b/nimterop.nimble
@@ -19,9 +19,9 @@ proc execCmd(cmd:string)=
task test, "Test":
execCmd "nim c -r tests/tnimterop_c.nim"
execCmd "nim cpp -r tests/tnimterop_cpp.nim"
- when defined(windows):
- execCmd "nim c -r tests/tmath.nim"
- execCmd "nim cpp -r tests/tmath.nim"
+ # when defined(windows):
+ # execCmd "nim c -r tests/tmath.nim"
+ # execCmd "nim cpp -r tests/tmath.nim"
task installWithDeps, "install dependencies":
for a in ["http://github.com/genotrance/nimtreesitter?subdir=treesitter",
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index 9a2ee3c..ff7dbcb 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -9,6 +9,7 @@ import "."/[getters, globals, grammar]
const gAtoms = @[
"field_identifier",
"identifier",
+ "math_expression",
"number_literal",
"preproc_arg",
"primitive_type",
@@ -25,7 +26,10 @@ proc saveNodeData(node: TSNode): bool =
if name == "primitive_type" and node.tsNodeParent.tsNodeType() == "sized_type_specifier":
return true
- if name in ["primitive_type", "sized_type_specifier"]:
+ if name == "number_literal" and node.tsNodeParent.tsNodeType() == "math_expression":
+ return true
+
+ if name in ["math_expression", "primitive_type", "sized_type_specifier"]:
val = val.getType()
let
@@ -39,7 +43,7 @@ proc saveNodeData(node: TSNode): bool =
not npparent.tsNodeIsNull() and npparent.tsNodeType() == "pointer_declarator"):
if gStateRT.data[^1].val != "object":
- gStateRT.data[^1].val = "ptr " & gStateRT.data[^1].val
+ gStateRT.data[^1].val = "ptr " & gStateRT.data[^1].val.getIdentifier()
gStateRT.data.add((name, val))
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index e3e00c2..b03f931 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -12,6 +12,16 @@ proc sanitizePath*(path: string): string =
proc getIdentifier*(str: string): string =
result = str.strip(chars={'_'})
+proc getUniqueIdentifier*(exists: seq[string]): string =
+ var
+ name = gStateRT.sourceFile.extractFilename().multiReplace([(".", ""), ("-", "")])
+ count = 1
+
+ while (name & $count) in exists:
+ count += 1
+
+ return name & $count
+
proc getType*(str: string): string =
if str == "void":
return "object"
@@ -22,7 +32,8 @@ proc getType*(str: string): string =
("double ", "cdouble"),
("long ", "clong"),
]).
- replace(re"([u]?int[\d]+)_t", "$1")
+ replace(re"([u]?int[\d]+)_t", "$1").
+ replace(re"([u]?int)ptr_t", "ptr $1")
case result:
of "long":
diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim
index 4a949cb..e86f907 100644
--- a/nimterop/grammar.nim
+++ b/nimterop/grammar.nim
@@ -119,6 +119,7 @@ proc initGrammar() =
gStateRT.grammar.add(("""
(type_definition
(struct_specifier|union_specifier
+ (type_identifier?)
(field_declaration_list
(field_declaration+
(primitive_type|type_identifier?)
@@ -139,12 +140,22 @@ proc initGrammar() =
)
""",
proc (ast: ref Ast, node: TSNode) {.closure, locks: 0.} =
- pStructCommon(ast, node, gStateRT.data[^1].val, 0, 1)
+ var
+ offset = 0
+
+ if gStateRT.data[0].name == "type_identifier":
+ offset = 1
+
+ pStructCommon(ast, node, gStateRT.data[^1].val, offset, 1)
))
proc pEnumCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int) =
- let
+ var
nname = name.getIdentifier()
+
+ if nname.len == 0:
+ nname = getUniqueIdentifier(gStateRT.types)
+
if nname notin gStateRT.types:
gStateRT.types.add(nname)
gStateRT.typeStr &= &" {nname}* = enum\n"
@@ -155,7 +166,7 @@ proc initGrammar() =
let
fname = gStateRT.data[i].val.getIdentifier()
- if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name == "number_literal":
+ if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name in ["math_expression", "number_literal"]:
gStateRT.typeStr &= &" {fname} = {gStateRT.data[i+1].val}\n"
i += 2
else:
@@ -165,27 +176,42 @@ proc initGrammar() =
# enum X {}
gStateRT.grammar.add(("""
(enum_specifier
- (type_identifier)
+ (type_identifier?)
(enumerator_list
(enumerator+
(identifier)
(number_literal?)
+ (math_expression?
+ (number_literal)
+ )
)
)
)
""",
proc (ast: ref Ast, node: TSNode) {.closure, locks: 0.} =
- pEnumCommon(ast, node, gStateRT.data[0].val, 1, 0)
+ var
+ name = ""
+ offset = 0
+
+ if gStateRT.data[0].name == "type_identifier":
+ name = gStateRT.data[0].val
+ offset = 1
+
+ pEnumCommon(ast, node, name, offset, 0)
))
# typedef enum {} X
gStateRT.grammar.add(("""
(type_definition
(enum_specifier
+ (type_identifier?)
(enumerator_list
(enumerator+
(identifier)
(number_literal?)
+ (math_expression?
+ (number_literal)
+ )
)
)
)
@@ -193,13 +219,19 @@ proc initGrammar() =
)
""",
proc (ast: ref Ast, node: TSNode) {.closure, locks: 0.} =
- pEnumCommon(ast, node, gStateRT.data[^1].val, 0, 1)
+ var
+ offset = 0
+
+ if gStateRT.data[0].name == "type_identifier":
+ offset = 1
+
+ pEnumCommon(ast, node, gStateRT.data[^1].val, offset, 1)
))
# typ function(typ param1, ...)
gStateRT.grammar.add(("""
(declaration
- (type_qualifier?)
+ (type_qualifier|storage_class_specifier?)
(primitive_type|type_identifier?)
(sized_type_specifier?
(primitive_type?)
diff --git a/tests/tmath.nim b/tests/tmath.nim
index 0479dda..bee2f29 100644
--- a/tests/tmath.nim
+++ b/tests/tmath.nim
@@ -4,6 +4,8 @@ import unittest
type
locale_t = object
+cDebug()
+
cAddStdDir()
cImport cSearchPath("math.h")