aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2018-11-20 16:49:14 -0600
committerGanesh Viswanathan <dev@genotrance.com>2018-11-20 16:49:14 -0600
commitb6ca0a815a5a651ed90b2150fdda5ecf1bac9632 (patch)
tree92898bae8261f66467c7574b1770988818c588f9
parent4ace90476b77e08bb54414ec5131f11f894eca78 (diff)
downloadnimterop-b6ca0a815a5a651ed90b2150fdda5ecf1bac9632.tar.gz
nimterop-b6ca0a815a5a651ed90b2150fdda5ecf1bac9632.zip
Use references, reduce RAM usage
-rw-r--r--nimterop.nimble3
-rw-r--r--nimterop/ast.nim18
-rw-r--r--nimterop/getters.nim4
-rw-r--r--nimterop/globals.nim4
-rw-r--r--nimterop/lisp.nim13
5 files changed, 23 insertions, 19 deletions
diff --git a/nimterop.nimble b/nimterop.nimble
index 6ec3afa..b3def89 100644
--- a/nimterop.nimble
+++ b/nimterop.nimble
@@ -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"
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index 7f977a7..1a30ad7 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -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)
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index b65d800..dd6ca40 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -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]
diff --git a/nimterop/globals.nim b/nimterop/globals.nim
index bc113af..9802417 100644
--- a/nimterop/globals.nim
+++ b/nimterop/globals.nim
@@ -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]
diff --git a/nimterop/lisp.nim b/nimterop/lisp.nim
index a472a54..83f9e4f 100644
--- a/nimterop/lisp.nim
+++ b/nimterop/lisp.nim
@@ -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()