Use references, reduce RAM usage
This commit is contained in:
parent
4ace90476b
commit
b6ca0a815a
5 changed files with 23 additions and 19 deletions
|
|
@ -14,3 +14,6 @@ requires "nim >= 0.19.0", "treesitter >= 0.1.0", "treesitter_c >= 0.1.0", "trees
|
|||
|
||||
task test, "Test":
|
||||
exec "nim c -r tests/tnimterop"
|
||||
|
||||
task testext, "Test":
|
||||
exec "nim c -r tests/tnimteropext"
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ proc addHeader*(fullpath: string) =
|
|||
# Preprocessor
|
||||
#
|
||||
|
||||
proc preprocDef(node: Ast) =
|
||||
proc preprocDef(node: ref Ast) =
|
||||
if node.children.len() == 2:
|
||||
let
|
||||
name = getNodeValIf(node.children[0], "identifier")
|
||||
|
|
@ -36,7 +36,7 @@ proc preprocDef(node: Ast) =
|
|||
# Types
|
||||
#
|
||||
|
||||
proc typeScan(node: Ast, sym, identifier, offset: string): string =
|
||||
proc typeScan(node: ref Ast, sym, identifier, offset: string): string =
|
||||
if node.sym != sym or node.children.len() != 2:
|
||||
return
|
||||
|
||||
|
|
@ -58,7 +58,7 @@ proc typeScan(node: Ast, sym, identifier, offset: string): string =
|
|||
else:
|
||||
return
|
||||
|
||||
proc structSpecifier(node: Ast, name = "") =
|
||||
proc structSpecifier(node: ref Ast, name = "") =
|
||||
var stmt: string
|
||||
if node.children.len() == 1 and name notin gTypes:
|
||||
case node.children[0].sym:
|
||||
|
|
@ -96,7 +96,7 @@ proc structSpecifier(node: Ast, name = "") =
|
|||
gTypes.add(name)
|
||||
gTypeStr &= stmt
|
||||
|
||||
proc enumSpecifier(node: Ast, name = "") =
|
||||
proc enumSpecifier(node: ref Ast, name = "") =
|
||||
var
|
||||
ename: string
|
||||
elid: int
|
||||
|
|
@ -131,7 +131,7 @@ proc enumSpecifier(node: Ast, name = "") =
|
|||
gTypes.add(name)
|
||||
gTypeStr &= stmt
|
||||
|
||||
proc typeDefinition(node: Ast) =
|
||||
proc typeDefinition(node: ref Ast) =
|
||||
if node.children.len() == 2:
|
||||
let
|
||||
name = getNodeValIf(node.children[1], "type_identifier")
|
||||
|
|
@ -154,7 +154,7 @@ proc typeDefinition(node: Ast) =
|
|||
of "enum_specifier":
|
||||
enumSpecifier(node.children[0], name)
|
||||
|
||||
proc functionDeclarator(node: Ast, typ: string) =
|
||||
proc functionDeclarator(node: ref Ast, typ: string) =
|
||||
if node.children.len() == 2:
|
||||
let
|
||||
name = getNodeValIf(node.children[0], "identifier")
|
||||
|
|
@ -181,7 +181,7 @@ proc functionDeclarator(node: Ast, typ: string) =
|
|||
gProcs.add(name)
|
||||
gProcStr &= stmt
|
||||
|
||||
proc declaration*(node: Ast) =
|
||||
proc declaration*(node: ref Ast) =
|
||||
if node.children.len() == 2 and node.children[1].sym == "function_declarator":
|
||||
let
|
||||
ptyp = getNodeValIf(node.children[0], "primitive_type")
|
||||
|
|
@ -196,7 +196,7 @@ proc declaration*(node: Ast) =
|
|||
if styp.nBl:
|
||||
functionDeclarator(node.children[1], styp)
|
||||
|
||||
proc genNimAst*(node: Ast) =
|
||||
proc genNimAst*(node: ref Ast) =
|
||||
case node.sym:
|
||||
of "ERROR":
|
||||
let (line, col) = getLineCol(node)
|
||||
|
|
@ -213,6 +213,8 @@ proc genNimAst*(node: Ast) =
|
|||
of "enum_specifier":
|
||||
if node.parent.sym notin ["type_definition", "declaration"]:
|
||||
enumSpecifier(node)
|
||||
of "":
|
||||
return
|
||||
|
||||
for child in node.children:
|
||||
genNimAst(child)
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ proc getLit*(str: string): string =
|
|||
str.contains(re"^0x[\d]+$"):
|
||||
return str
|
||||
|
||||
proc getNodeValIf*(node: Ast, esym: string): string =
|
||||
proc getNodeValIf*(node: ref Ast, esym: string): string =
|
||||
if esym != node.sym:
|
||||
return
|
||||
|
||||
|
|
@ -28,7 +28,7 @@ proc getGccPaths*(mode = "c"): string =
|
|||
|
||||
return staticExec("gcc -Wp,-v -x" & mode & " " & nul)
|
||||
|
||||
proc getLineCol*(node: Ast): tuple[line, col: int] =
|
||||
proc getLineCol*(node: ref Ast): tuple[line, col: int] =
|
||||
result.line = 1
|
||||
result.col = 1
|
||||
echo gCode[node.start .. node.stop-1]
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ type
|
|||
Ast* = object
|
||||
sym*: string
|
||||
start*, stop*: int
|
||||
parent*: ptr Ast
|
||||
children*: seq[Ast]
|
||||
parent*: ref Ast
|
||||
children*: seq[ref Ast]
|
||||
|
||||
var
|
||||
gDefines* {.compiletime.}: seq[string]
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ proc tokenize(fullpath: string) =
|
|||
else:
|
||||
collect &= $i
|
||||
|
||||
proc readFromTokens(): Ast =
|
||||
proc readFromTokens(): ref Ast =
|
||||
if idx == gTokens.len():
|
||||
echo "Bad AST"
|
||||
quit(1)
|
||||
|
|
@ -35,6 +35,7 @@ proc readFromTokens(): Ast =
|
|||
if gTokens.len() - idx < 2:
|
||||
echo "Corrupt AST"
|
||||
quit(1)
|
||||
result = new(Ast)
|
||||
result.sym = gTokens[idx+1]
|
||||
result.start = gTokens[idx+2].parseInt()
|
||||
result.stop = gTokens[idx+3].parseInt()
|
||||
|
|
@ -42,18 +43,16 @@ proc readFromTokens(): Ast =
|
|||
result.children = @[]
|
||||
while gTokens[idx] != ")":
|
||||
var res = readFromTokens()
|
||||
if res.sym.nBl:
|
||||
res.parent = addr result
|
||||
if not res.isNil() and res.sym.nBl:
|
||||
res.parent = result
|
||||
result.children.add(res)
|
||||
idx += 1
|
||||
return
|
||||
elif gTokens[idx] == ")":
|
||||
echo "Poor AST"
|
||||
quit(1)
|
||||
|
||||
idx += 1
|
||||
|
||||
proc printAst*(node: Ast, offset=""): string =
|
||||
proc printAst*(node: ref Ast, offset=""): string =
|
||||
result = offset & "(" & node.sym & " " & $node.start & " " & $node.stop
|
||||
if node.children.len() != 0:
|
||||
result &= "\n"
|
||||
|
|
@ -63,7 +62,7 @@ proc printAst*(node: Ast, offset=""): string =
|
|||
else:
|
||||
result &= ")\n"
|
||||
|
||||
proc parseLisp*(fullpath: string): Ast =
|
||||
proc parseLisp*(fullpath: string): ref Ast =
|
||||
tokenize(fullpath)
|
||||
|
||||
return readFromTokens()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue