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]:
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

View file

@ -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]]

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 =
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) =

View file

@ -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'
})