typedef arrays, debug output

This commit is contained in:
Ganesh Viswanathan 2019-01-15 20:17:10 -06:00
commit da06be80d1
4 changed files with 40 additions and 9 deletions

View file

@ -99,6 +99,8 @@ proc searchAst(root: TSNode) =
for ast in gStateRT.ast[name]: for ast in gStateRT.ast[name]:
if searchAstForNode(ast, node): if searchAstForNode(ast, node):
ast.tonim(ast, node) ast.tonim(ast, node)
if gStateRT.debug:
gStateRT.debugStr &= "\n\n# " & gStateRT.data.join("\n# ")
break break
gStateRT.data = @[] gStateRT.data = @[]
else: else:
@ -145,3 +147,6 @@ proc printNim*(fullpath: string, root: TSNode) =
if gStateRT.procStr.nBl: if gStateRT.procStr.nBl:
echo gStateRT.procStr echo gStateRT.procStr
if gStateRT.debug and gStateRT.debugStr.nBl:
echo gStateRT.debugStr

View file

@ -26,7 +26,7 @@ type
consts*, enums*, procs*, types*: seq[string] 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 sourceFile*: string # eg, C or C++ source or header file
ast*: Table[string, seq[ref Ast]] ast*: Table[string, seq[ref Ast]]

View file

@ -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 = template funcParamCommon(pname, ptyp, pptr, pout, count, i: untyped): untyped =
ptyp = gStateRT.data[i].val.getIdentifier() ptyp = gStateRT.data[i].val.getIdentifier()
if i+1 < gStateRT.data.len and gStateRT.data[i+1].name == "pointer_declarator": if i+1 < gStateRT.data.len and gStateRT.data[i+1].name == "pointer_declarator":
@ -84,9 +94,11 @@ proc initGrammar() =
gStateRT.grammar.add((&""" gStateRT.grammar.add((&"""
(type_definition (type_definition
{typeGrammar} {typeGrammar}
(type_identifier?)
(pointer_declarator?
(type_identifier!) (type_identifier!)
{arrGrammar}
(pointer_declarator!
(type_identifier!)
{arrGrammar}
{funcGrammar} {funcGrammar}
) )
{funcGrammar} {funcGrammar}
@ -98,12 +110,17 @@ proc initGrammar() =
typ = gStateRT.data[i].val.getIdentifier() typ = gStateRT.data[i].val.getIdentifier()
name = "" name = ""
tptr = "" tptr = ""
aptr = ""
i += 1 i += 1
if i < gStateRT.data.len: if i < gStateRT.data.len:
if gStateRT.data[i].name == "pointer_declarator": case gStateRT.data[i].name:
of "pointer_declarator":
tptr = "ptr " tptr = "ptr "
i += 1 i += 1
of "array_pointer_declarator":
aptr = "ptr "
i += 1
if i < gStateRT.data.len: if i < gStateRT.data.len:
name = gStateRT.data[i].val.getIdentifier() name = gStateRT.data[i].val.getIdentifier()
@ -129,6 +146,11 @@ proc initGrammar() =
gStateRT.typeStr &= &" {name}* = proc({pout}) {{.nimcall.}}\n" gStateRT.typeStr &= &" {name}* = proc({pout}) {{.nimcall.}}\n"
else: else:
gStateRT.types.add(name) 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()
gStateRT.typeStr &= &" {name}*: = {aptr}array[{flen}, {tptr}{typ}]\n"
else:
if name == typ or typ == "object": if name == typ or typ == "object":
gStateRT.typeStr &= &" {name}* = object\n" gStateRT.typeStr &= &" {name}* = object\n"
else: else:

View file

@ -114,6 +114,7 @@ proc main(
preprocess = false, preprocess = false,
pgrammar = false, pgrammar = false,
recurse = false, recurse = false,
debug = false,
defines: seq[string] = @[], defines: seq[string] = @[],
includeDirs: seq[string] = @[], includeDirs: seq[string] = @[],
source: seq[string], source: seq[string],
@ -126,6 +127,7 @@ proc main(
pretty: pretty, pretty: pretty,
preprocess: preprocess, preprocess: preprocess,
recurse: recurse, recurse: recurse,
debug: debug,
defines: defines, defines: defines,
includeDirs: includeDirs, includeDirs: includeDirs,
) )
@ -147,6 +149,7 @@ when isMainModule:
"preprocess": "run preprocessor on header", "preprocess": "run preprocessor on header",
"pgrammar": "print grammar", "pgrammar": "print grammar",
"recurse": "process #include files", "recurse": "process #include files",
"debug": "enable debug output",
"source" : "C/C++ source/header", "source" : "C/C++ source/header",
}, short = { }, short = {
"past": 'a', "past": 'a',
@ -155,5 +158,6 @@ when isMainModule:
"includeDirs": 'I', "includeDirs": 'I',
"preprocess": 'p', "preprocess": 'p',
"recurse": 'r', "recurse": 'r',
"debug": 'd',
"pgrammar": 'g' "pgrammar": 'g'
}) })