diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2019-01-15 20:17:10 -0600 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2019-01-15 20:17:10 -0600 |
| commit | da06be80d1a3b00cf5b380aa9529d403ca8aab9b (patch) | |
| tree | e56b4c52777de8d815298add1e81437326baa151 | |
| parent | 11c896f82e00fce82e159d781243c2036346cc58 (diff) | |
| download | nimterop-da06be80d1a3b00cf5b380aa9529d403ca8aab9b.tar.gz nimterop-da06be80d1a3b00cf5b380aa9529d403ca8aab9b.zip | |
typedef arrays, debug output
| -rw-r--r-- | nimterop/ast.nim | 5 | ||||
| -rw-r--r-- | nimterop/globals.nim | 2 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 38 | ||||
| -rw-r--r-- | toast.nim | 4 |
4 files changed, 40 insertions, 9 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim index 8a61fe0..6c5ab71 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -99,6 +99,8 @@ proc searchAst(root: TSNode) = for ast in gStateRT.ast[name]: if searchAstForNode(ast, node): ast.tonim(ast, node) + if gStateRT.debug: + gStateRT.debugStr &= "\n\n# " & gStateRT.data.join("\n# ") break gStateRT.data = @[] else: @@ -145,3 +147,6 @@ proc printNim*(fullpath: string, root: TSNode) = if gStateRT.procStr.nBl: echo gStateRT.procStr + + if gStateRT.debug and gStateRT.debugStr.nBl: + echo gStateRT.debugStr
\ No newline at end of file diff --git a/nimterop/globals.nim b/nimterop/globals.nim index e5bb6ac..e6a1d55 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -26,7 +26,7 @@ type consts*, enums*, procs*, types*: seq[string] - code*, constStr*, currentHeader*, enumStr*, mode*, procStr*, typeStr*: string + code*, constStr*, currentHeader*, debugStr*, enumStr*, mode*, procStr*, typeStr*: string sourceFile*: string # eg, C or C++ source or header file ast*: Table[string, seq[ref Ast]] diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index a72df53..3caeb3d 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -59,6 +59,16 @@ proc initGrammar() = ) """ + arrGrammar = &""" + (array_declarator! + (pointer_declarator! + (type_identifier) + ) + (type_identifier) + (identifier|number_literal) + ) + """ + template funcParamCommon(pname, ptyp, pptr, pout, count, i: untyped): untyped = ptyp = gStateRT.data[i].val.getIdentifier() if i+1 < gStateRT.data.len and gStateRT.data[i+1].name == "pointer_declarator": @@ -84,9 +94,11 @@ proc initGrammar() = gStateRT.grammar.add((&""" (type_definition {typeGrammar} - (type_identifier?) - (pointer_declarator? + (type_identifier!) + {arrGrammar} + (pointer_declarator! (type_identifier!) + {arrGrammar} {funcGrammar} ) {funcGrammar} @@ -98,12 +110,17 @@ proc initGrammar() = typ = gStateRT.data[i].val.getIdentifier() name = "" tptr = "" + aptr = "" i += 1 if i < gStateRT.data.len: - if gStateRT.data[i].name == "pointer_declarator": - tptr = "ptr " - i += 1 + case gStateRT.data[i].name: + of "pointer_declarator": + tptr = "ptr " + i += 1 + of "array_pointer_declarator": + aptr = "ptr " + i += 1 if i < gStateRT.data.len: name = gStateRT.data[i].val.getIdentifier() @@ -129,10 +146,15 @@ proc initGrammar() = gStateRT.typeStr &= &" {name}* = proc({pout}) {{.nimcall.}}\n" else: gStateRT.types.add(name) - if name == typ or typ == "object": - gStateRT.typeStr &= &" {name}* = object\n" + if i < gStateRT.data.len and gStateRT.data[i].name in ["identifier", "number_literal"]: + let + flen = gStateRT.data[i].val.getIdentifier() + gStateRT.typeStr &= &" {name}*: = {aptr}array[{flen}, {tptr}{typ}]\n" else: - gStateRT.typeStr &= &" {name}* = {tptr}{typ}\n" + if name == typ or typ == "object": + gStateRT.typeStr &= &" {name}* = object\n" + else: + gStateRT.typeStr &= &" {name}* = {tptr}{typ}\n" )) proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int) = @@ -114,6 +114,7 @@ proc main( preprocess = false, pgrammar = false, recurse = false, + debug = false, defines: seq[string] = @[], includeDirs: seq[string] = @[], source: seq[string], @@ -126,6 +127,7 @@ proc main( pretty: pretty, preprocess: preprocess, recurse: recurse, + debug: debug, defines: defines, includeDirs: includeDirs, ) @@ -147,6 +149,7 @@ when isMainModule: "preprocess": "run preprocessor on header", "pgrammar": "print grammar", "recurse": "process #include files", + "debug": "enable debug output", "source" : "C/C++ source/header", }, short = { "past": 'a', @@ -155,5 +158,6 @@ when isMainModule: "includeDirs": 'I', "preprocess": 'p', "recurse": 'r', + "debug": 'd', "pgrammar": 'g' }) |
