From 248761da11e03d4cf78739d7af9073e6179d0b8b Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Wed, 1 Jan 2020 16:37:33 -0600 Subject: [PATCH] Print debug, more comments --- config.nims | 5 ++- nimterop/ast2.nim | 75 ++++++++++++++++++++++++++++++++--- nimterop/getters.nim | 93 +++++++++++++++++++++++++++++++++++++++++++- nimterop/nim.cfg | 1 - nimterop/toast.nim | 51 +----------------------- 5 files changed, 166 insertions(+), 59 deletions(-) delete mode 100644 nimterop/nim.cfg diff --git a/config.nims b/config.nims index 54a9a06..d312c07 100644 --- a/config.nims +++ b/config.nims @@ -9,4 +9,7 @@ when defined(Windows): switch("gc", "markAndSweep") # Retain stackTrace for clear errors -switch("stackTrace", "on") \ No newline at end of file +switch("stackTrace", "on") + +# Path to compiler +switch("path", "$nim") diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim index dc62d4f..8790e6f 100644 --- a/nimterop/ast2.nim +++ b/nimterop/ast2.nim @@ -1,34 +1,97 @@ import macros, os, strutils, tables, times -import compiler/[ast, idents, options, renderer] +import compiler/[ast, astalgo, idents, options, renderer] import "."/treesitter/api import "."/[compat, globals, getters] proc addConst(nimState: NimState, node: TSNode) = - echo "Const: " & nimState.getNodeVal(node) - if node.len() >= 2: + # (preproc_def + # (identifier) + # (preproc_arg) + # ) + nimState.printDebug(node) + + if node[0].getName() == "identifier" and + node[1].getName() == "preproc_arg": let constDef = newNode(nkConstDef) + + # node[0] = identifier = const name (name, info) = nimState.getNameInfo(node.getAtom(), nskConst) + # TODO - check blank and override ident = nimState.getIdent(name, info) + + # node[1] = preproc_arg = value val = nimState.getNodeVal(node[1]).getLit() + + # If supported literal if val.kind != nkNilLit: + # nnkConstDef( + # nnkPostfix( + # nnkIdent("*"), + # nnkIdent(name) + # ), + # nnkEmpty(), + # nnkXLit(val) + # ) constDef.add ident constDef.add newNode(nkEmpty) constDef.add val + # nnkConstSection.add nimState.constSection.add constDef + nimState.printDebug(constDef) + proc addType(nimState: NimState, node: TSNode) = - echo "Type: " & nimState.getNodeVal(node) + # CASE1: + # + # typedef struct X Y; + # + # (type_definition + # (struct_specifier + # (type_identifier) + # ) + # (type_identifier) + # ) + # + # CASE 2 + # + # typedef struct X *Y; + # + # (type_definition + # (struct_specifier + # (type_identifier) + # ) + # (pointer_declarator + # (type_identifier) + # ) + # ) + # + nimState.printDebug(node) + + let + typeDef = newNode(nkTypeDef) + + fdlist = node.inTree("field_declaration_list") + + identNode = if fdlist: node[1] else: node + + (name, info) = nimState.getNameInfo(identNode.getAtom(), nskType) + # TODO - check blank and override + ident = nimState.getIdent(name, info) + + typeNode = + if fdlist: node[0] else: node[1] + proc addEnum(nimState: NimState, node: TSNode) = - echo "Enum: " & nimState.getNodeVal(node) + nimState.printDebug(node) proc addProc(nimState: NimState, node: TSNode) = - echo "Proc: " & nimState.getNodeVal(node) + nimState.printDebug(node) proc processNode(nimState: NimState, node: TSNode): bool = result = true diff --git a/nimterop/getters.nim b/nimterop/getters.nim index 52a77a6..5e53f94 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -2,7 +2,7 @@ import dynlib, macros, os, sequtils, sets, strformat, strutils, tables, times import regex -import compiler/[ast, idents, lineinfos, msgs, pathutils] +import compiler/[ast, idents, lineinfos, msgs, pathutils, renderer] import "."/[build, compat, globals, plugin, treesitter/api] @@ -221,6 +221,7 @@ 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)) @@ -326,6 +327,96 @@ proc getPxName*(node: TSNode, offset: int): string = if count == offset and not np.tsNodeIsNull(): return np.getName() +proc printLisp*(gState: State, root: TSNode): string = + var + node = root + nextnode: TSNode + depth = 0 + + while true: + if not node.tsNodeIsNull() and depth > -1: + if gState.pretty: + result &= spaces(depth) + let + (line, col) = gState.getLineCol(node) + result &= &"({$node.tsNodeType()} {line} {col} {node.tsNodeEndByte() - node.tsNodeStartByte()}" + else: + break + + if node.tsNodeNamedChildCount() != 0: + if gState.pretty: + result &= "\n" + nextnode = node.tsNodeNamedChild(0) + depth += 1 + else: + if gState.pretty: + result &= ")\n" + else: + result &= ")" + nextnode = node.tsNodeNextNamedSibling() + + if nextnode.tsNodeIsNull(): + while true: + node = node.tsNodeParent() + depth -= 1 + if depth == -1: + break + if gState.pretty: + result &= spaces(depth) & ")\n" + else: + result &= ")" + if node == root: + break + if not node.tsNodeNextNamedSibling().tsNodeIsNull(): + node = node.tsNodeNextNamedSibling() + break + else: + node = nextnode + + if node == root: + break + +proc getCommented*(str: string): string = + "\n# " & str.strip().replace("\n", "\n# ") + +proc printTree*(nimState: NimState, pnode: PNode, offset = "") = + if nimState.gState.debug and pnode.kind != nkNone: + stdout.write "\n# " & offset & $pnode.kind & "(" + case pnode.kind + of nkCharLit: + stdout.write "'" & pnode.intVal.char & "')" + of nkIntLit..nkUInt64Lit: + stdout.write $pnode.intVal & ")" + of nkFloatLit..nkFloat128Lit: + stdout.write $pnode.floatVal & ")" + of nkStrLit..nkTripleStrLit: + stdout.write "\"" & pnode.strVal & "\")" + of nkSym: + stdout.write $pnode.sym & ")" + of nkIdent: + stdout.write "\"" & $pnode.ident.s & "\")" + else: + if pnode.sons.len != 0: + for i in 0 ..< pnode.sons.len: + nimState.printTree(pnode.sons[i], offset & " ") + if i != pnode.sons.len - 1: + stdout.write "," + stdout.write "\n# " & offset & ")" + else: + stdout.write ")" + if offset.len == 0: + echo "" + +proc printDebug*(nimState: NimState, node: TSNode) = + if nimState.gState.debug: + echo nimState.getNodeVal(node).getCommented() + echo nimState.gState.printLisp(node).getCommented() + +proc printDebug*(nimState: NimState, pnode: PNode) = + if nimState.gState.debug: + echo ($pnode).getCommented() + nimState.printTree(pnode) + # Compiler shortcuts proc getLineInfo*(nimState: NimState, node: TSNode): TLineInfo = diff --git a/nimterop/nim.cfg b/nimterop/nim.cfg deleted file mode 100644 index 913df8c..0000000 --- a/nimterop/nim.cfg +++ /dev/null @@ -1 +0,0 @@ ---path:"$nim" diff --git a/nimterop/toast.nim b/nimterop/toast.nim index 1ad10a2..db68ce4 100644 --- a/nimterop/toast.nim +++ b/nimterop/toast.nim @@ -4,55 +4,6 @@ import "."/treesitter/[api, c, cpp] import "."/[ast2, compat, globals, getters] -proc printLisp(gState: State, root: TSNode) = - var - node = root - nextnode: TSNode - depth = 0 - - while true: - if not node.tsNodeIsNull() and depth > -1: - if gState.pretty: - stdout.write spaces(depth) - let - (line, col) = gState.getLineCol(node) - stdout.write &"({$node.tsNodeType()} {line} {col} {node.tsNodeEndByte() - node.tsNodeStartByte()}" - else: - break - - if node.tsNodeNamedChildCount() != 0: - if gState.pretty: - gecho "" - nextnode = node.tsNodeNamedChild(0) - depth += 1 - else: - if gState.pretty: - gecho ")" - else: - stdout.write ")" - nextnode = node.tsNodeNextNamedSibling() - - if nextnode.tsNodeIsNull(): - while true: - node = node.tsNodeParent() - depth -= 1 - if depth == -1: - break - if gState.pretty: - gecho spaces(depth) & ")" - else: - stdout.write ")" - if node == root: - break - if not node.tsNodeNextNamedSibling().tsNodeIsNull(): - node = node.tsNodeNextNamedSibling() - break - else: - node = nextnode - - if node == root: - break - proc process(gState: State, path: string) = doAssert existsFile(path), &"Invalid path {path}" @@ -91,7 +42,7 @@ proc process(gState: State, path: string) = tree.tsTreeDelete() if gState.past: - gState.printLisp(root) + echo gState.printLisp(root) elif gState.pnim: gState.printNim(path, root) elif gState.preprocess: