Print debug, more comments

This commit is contained in:
Ganesh Viswanathan 2020-01-01 16:37:33 -06:00
commit 248761da11
5 changed files with 166 additions and 59 deletions

View file

@ -9,4 +9,7 @@ when defined(Windows):
switch("gc", "markAndSweep")
# Retain stackTrace for clear errors
switch("stackTrace", "on")
switch("stackTrace", "on")
# Path to compiler
switch("path", "$nim")

View file

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

View file

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

View file

@ -1 +0,0 @@
--path:"$nim"

View file

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