aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2020-03-16 17:50:43 -0500
committerGanesh Viswanathan <dev@genotrance.com>2020-03-16 17:50:43 -0500
commit3f500898aa5b45b423348846d794463585ef34ee (patch)
treeb386566f73f7f97bc3f2f87123f245126a757ade
parent077981c3a5e577029cf28bffc67648e73d4aafcc (diff)
downloadnimterop-3f500898aa5b45b423348846d794463585ef34ee.tar.gz
nimterop-3f500898aa5b45b423348846d794463585ef34ee.zip
Add ast2 array expression support, cleanup debug output
-rw-r--r--nimterop/ast2.nim24
-rw-r--r--nimterop/getters.nim30
-rw-r--r--nimterop/globals.nim3
-rw-r--r--tests/include/tast2.h2
-rw-r--r--tests/tast2.nim3
5 files changed, 36 insertions, 26 deletions
diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim
index 697262f..f418139 100644
--- a/nimterop/ast2.nim
+++ b/nimterop/ast2.nim
@@ -2,7 +2,7 @@ import macros, os, sets, strutils, tables, times
import regex
-import compiler/[ast, idents, options, renderer]
+import compiler/[ast, idents, modulegraphs, options, parser, renderer]
import "."/treesitter/api
@@ -19,8 +19,9 @@ proc getPtrType*(str: string): string =
else:
str
-proc getLit*(str: string): PNode =
- # Used to convert #define literals into const
+proc getLit*(nimState: NimState, str: string): PNode =
+ # Used to convert #define literals into const and expressions
+ # in array sizes
let
str = str.replace(re"/[/*].*?(?:\*/)?$", "").strip()
@@ -30,9 +31,9 @@ proc getLit*(str: string): PNode =
elif str.contains(re"^[\-]?[\d]*[.]?[\d]+$"): # float
result = newFloatNode(nkFloatLit, parseFloat(str))
- # TODO - hex becomes int on render
- elif str.contains(re"^0x[\da-fA-F]+$"): # hexadecimal
- result = newIntNode(nkIntLit, parseHexInt(str))
+ # # TODO - hex becomes int on render
+ # elif str.contains(re"^0x[\da-fA-F]+$"): # hexadecimal
+ # result = newIntNode(nkIntLit, parseHexInt(str))
elif str.contains(re"^'[[:ascii:]]'$"): # char
result = newNode(nkCharLit)
@@ -42,7 +43,11 @@ proc getLit*(str: string): PNode =
result = newStrNode(nkStrLit, str[1 .. ^2])
else:
- result = newNode(nkNilLit)
+ result = parseString(
+ nimState.getNimExpression(str),
+ nimState.identCache, nimState.config)
+ if result.isNil:
+ result = newNode(nkNilLit)
proc addConst(nimState: NimState, node: TSNode) =
# #define X Y
@@ -65,7 +70,7 @@ proc addConst(nimState: NimState, node: TSNode) =
ident = nimState.getIdent(name, info)
# node[1] = preproc_arg = value
- val = nimState.getNodeVal(node[1]).getLit()
+ val = nimState.getLit(nimState.getNodeVal(node[1]))
# If supported literal
if val.kind != nkNilLit:
@@ -464,7 +469,7 @@ proc getTypeArray(nimState: NimState, node: TSNode): PNode =
for i in 0 ..< acount:
let
- size = nimState.getNodeVal(cnode[1]).getLit()
+ size = nimState.getLit(nimState.getNodeVal(cnode[1]))
if size.kind != nkNilLit:
result = nimState.newArrayTree(cnode, result, size)
cnode = cnode[0]
@@ -877,6 +882,7 @@ proc printNim*(gState: State, fullpath: string, root: TSNode) =
# Nim compiler objects
nimState.identCache = newIdentCache()
nimState.config = newConfigRef()
+ nimstate.graph = newModuleGraph(nimState.identCache, nimState.config)
nimState.constSection = newNode(nkConstSection)
nimState.enumSection = newNode(nkStmtList)
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index 93c4000..2c7da75 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -386,33 +386,33 @@ proc printLisp*(gState: State, root: TSNode): string =
proc getCommented*(str: string): string =
"\n# " & str.strip().replace("\n", "\n# ")
-proc printTree*(nimState: NimState, pnode: PNode, offset = "") =
+proc printTree*(nimState: NimState, pnode: PNode, offset = ""): string =
if nimState.gState.debug and pnode.kind != nkNone:
- stdout.write "\n# " & offset & $pnode.kind & "("
+ result &= "\n# " & offset & $pnode.kind & "("
case pnode.kind
of nkCharLit:
- stdout.write "'" & pnode.intVal.char & "')"
+ result &= "'" & pnode.intVal.char & "')"
of nkIntLit..nkUInt64Lit:
- stdout.write $pnode.intVal & ")"
+ result &= $pnode.intVal & ")"
of nkFloatLit..nkFloat128Lit:
- stdout.write $pnode.floatVal & ")"
+ result &= $pnode.floatVal & ")"
of nkStrLit..nkTripleStrLit:
- stdout.write "\"" & pnode.strVal & "\")"
+ result &= "\"" & pnode.strVal & "\")"
of nkSym:
- stdout.write $pnode.sym & ")"
+ result &= $pnode.sym & ")"
of nkIdent:
- stdout.write "\"" & $pnode.ident.s & "\")"
+ result &= "\"" & $pnode.ident.s & "\")"
else:
if pnode.sons.len != 0:
for i in 0 ..< pnode.sons.len:
- nimState.printTree(pnode.sons[i], offset & " ")
+ result &= nimState.printTree(pnode.sons[i], offset & " ")
if i != pnode.sons.len - 1:
- stdout.write ","
- stdout.write "\n# " & offset & ")"
+ result &= ","
+ result &= "\n# " & offset & ")"
else:
- stdout.write ")"
+ result &= ")"
if offset.len == 0:
- necho ""
+ result &= "\n"
proc printDebug*(nimState: NimState, node: TSNode) =
if nimState.gState.debug:
@@ -421,8 +421,8 @@ proc printDebug*(nimState: NimState, node: TSNode) =
proc printDebug*(nimState: NimState, pnode: PNode) =
if nimState.gState.debug:
- necho ("Output => " & $pnode).getCommented()
- nimState.printTree(pnode)
+ necho ("Output => " & $pnode).getCommented() & "\n" &
+ nimState.printTree(pnode)
# Compiler shortcuts
diff --git a/nimterop/globals.nim b/nimterop/globals.nim
index 35a5575..ba63a76 100644
--- a/nimterop/globals.nim
+++ b/nimterop/globals.nim
@@ -5,7 +5,7 @@ import regex
import "."/plugin
when not declared(CIMPORT):
- import compiler/[ast, idents, options]
+ import compiler/[ast, idents, modulegraphs, options]
import "."/treesitter/api
@@ -81,6 +81,7 @@ type
constSection*, enumSection*, procSection*, typeSection*: PNode
identCache*: IdentCache
config*: ConfigRef
+ graph*: ModuleGraph
gState*: State
diff --git a/tests/include/tast2.h b/tests/include/tast2.h
index 1a2789b..ced3a44 100644
--- a/tests/include/tast2.h
+++ b/tests/include/tast2.h
@@ -32,7 +32,7 @@ typedef struct { char *a1; int *a2[1]; } A19, *A19p;
typedef struct A20 { char a1; } A20, A21, *A21p;
//Expression
-//typedef struct A21 { int **f1; int abc[123+132]; } A21;
+typedef struct A22 { int **f1; int *f2[123+132]; } A22;
//Unions
//union UNION1 {int f1; };
diff --git a/tests/tast2.nim b/tests/tast2.nim
index ffdcd19..dd8fae7 100644
--- a/tests/tast2.nim
+++ b/tests/tast2.nim
@@ -69,3 +69,6 @@ assert A20 is object
testFields(A20, {"a1": "cchar"}.toTable())
assert A21 is A20
assert A21p is ptr A20
+
+assert A22 is object
+testFields(A22, {"f1": "ptr ptr cint", "f2": "array[0..254, ptr cint]"}.toTable()) \ No newline at end of file