Add ast2 array expression support, cleanup debug output
This commit is contained in:
parent
077981c3a5
commit
3f500898aa
5 changed files with 36 additions and 26 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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; };
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
Loading…
Add table
Add a link
Reference in a new issue