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 regex
|
||||||
|
|
||||||
import compiler/[ast, idents, options, renderer]
|
import compiler/[ast, idents, modulegraphs, options, parser, renderer]
|
||||||
|
|
||||||
import "."/treesitter/api
|
import "."/treesitter/api
|
||||||
|
|
||||||
|
|
@ -19,8 +19,9 @@ proc getPtrType*(str: string): string =
|
||||||
else:
|
else:
|
||||||
str
|
str
|
||||||
|
|
||||||
proc getLit*(str: string): PNode =
|
proc getLit*(nimState: NimState, str: string): PNode =
|
||||||
# Used to convert #define literals into const
|
# Used to convert #define literals into const and expressions
|
||||||
|
# in array sizes
|
||||||
let
|
let
|
||||||
str = str.replace(re"/[/*].*?(?:\*/)?$", "").strip()
|
str = str.replace(re"/[/*].*?(?:\*/)?$", "").strip()
|
||||||
|
|
||||||
|
|
@ -30,9 +31,9 @@ proc getLit*(str: string): PNode =
|
||||||
elif str.contains(re"^[\-]?[\d]*[.]?[\d]+$"): # float
|
elif str.contains(re"^[\-]?[\d]*[.]?[\d]+$"): # float
|
||||||
result = newFloatNode(nkFloatLit, parseFloat(str))
|
result = newFloatNode(nkFloatLit, parseFloat(str))
|
||||||
|
|
||||||
# TODO - hex becomes int on render
|
# # TODO - hex becomes int on render
|
||||||
elif str.contains(re"^0x[\da-fA-F]+$"): # hexadecimal
|
# elif str.contains(re"^0x[\da-fA-F]+$"): # hexadecimal
|
||||||
result = newIntNode(nkIntLit, parseHexInt(str))
|
# result = newIntNode(nkIntLit, parseHexInt(str))
|
||||||
|
|
||||||
elif str.contains(re"^'[[:ascii:]]'$"): # char
|
elif str.contains(re"^'[[:ascii:]]'$"): # char
|
||||||
result = newNode(nkCharLit)
|
result = newNode(nkCharLit)
|
||||||
|
|
@ -42,7 +43,11 @@ proc getLit*(str: string): PNode =
|
||||||
result = newStrNode(nkStrLit, str[1 .. ^2])
|
result = newStrNode(nkStrLit, str[1 .. ^2])
|
||||||
|
|
||||||
else:
|
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) =
|
proc addConst(nimState: NimState, node: TSNode) =
|
||||||
# #define X Y
|
# #define X Y
|
||||||
|
|
@ -65,7 +70,7 @@ proc addConst(nimState: NimState, node: TSNode) =
|
||||||
ident = nimState.getIdent(name, info)
|
ident = nimState.getIdent(name, info)
|
||||||
|
|
||||||
# node[1] = preproc_arg = value
|
# node[1] = preproc_arg = value
|
||||||
val = nimState.getNodeVal(node[1]).getLit()
|
val = nimState.getLit(nimState.getNodeVal(node[1]))
|
||||||
|
|
||||||
# If supported literal
|
# If supported literal
|
||||||
if val.kind != nkNilLit:
|
if val.kind != nkNilLit:
|
||||||
|
|
@ -464,7 +469,7 @@ proc getTypeArray(nimState: NimState, node: TSNode): PNode =
|
||||||
|
|
||||||
for i in 0 ..< acount:
|
for i in 0 ..< acount:
|
||||||
let
|
let
|
||||||
size = nimState.getNodeVal(cnode[1]).getLit()
|
size = nimState.getLit(nimState.getNodeVal(cnode[1]))
|
||||||
if size.kind != nkNilLit:
|
if size.kind != nkNilLit:
|
||||||
result = nimState.newArrayTree(cnode, result, size)
|
result = nimState.newArrayTree(cnode, result, size)
|
||||||
cnode = cnode[0]
|
cnode = cnode[0]
|
||||||
|
|
@ -877,6 +882,7 @@ proc printNim*(gState: State, fullpath: string, root: TSNode) =
|
||||||
# Nim compiler objects
|
# Nim compiler objects
|
||||||
nimState.identCache = newIdentCache()
|
nimState.identCache = newIdentCache()
|
||||||
nimState.config = newConfigRef()
|
nimState.config = newConfigRef()
|
||||||
|
nimstate.graph = newModuleGraph(nimState.identCache, nimState.config)
|
||||||
|
|
||||||
nimState.constSection = newNode(nkConstSection)
|
nimState.constSection = newNode(nkConstSection)
|
||||||
nimState.enumSection = newNode(nkStmtList)
|
nimState.enumSection = newNode(nkStmtList)
|
||||||
|
|
|
||||||
|
|
@ -386,33 +386,33 @@ proc printLisp*(gState: State, root: TSNode): string =
|
||||||
proc getCommented*(str: string): string =
|
proc getCommented*(str: string): string =
|
||||||
"\n# " & str.strip().replace("\n", "\n# ")
|
"\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:
|
if nimState.gState.debug and pnode.kind != nkNone:
|
||||||
stdout.write "\n# " & offset & $pnode.kind & "("
|
result &= "\n# " & offset & $pnode.kind & "("
|
||||||
case pnode.kind
|
case pnode.kind
|
||||||
of nkCharLit:
|
of nkCharLit:
|
||||||
stdout.write "'" & pnode.intVal.char & "')"
|
result &= "'" & pnode.intVal.char & "')"
|
||||||
of nkIntLit..nkUInt64Lit:
|
of nkIntLit..nkUInt64Lit:
|
||||||
stdout.write $pnode.intVal & ")"
|
result &= $pnode.intVal & ")"
|
||||||
of nkFloatLit..nkFloat128Lit:
|
of nkFloatLit..nkFloat128Lit:
|
||||||
stdout.write $pnode.floatVal & ")"
|
result &= $pnode.floatVal & ")"
|
||||||
of nkStrLit..nkTripleStrLit:
|
of nkStrLit..nkTripleStrLit:
|
||||||
stdout.write "\"" & pnode.strVal & "\")"
|
result &= "\"" & pnode.strVal & "\")"
|
||||||
of nkSym:
|
of nkSym:
|
||||||
stdout.write $pnode.sym & ")"
|
result &= $pnode.sym & ")"
|
||||||
of nkIdent:
|
of nkIdent:
|
||||||
stdout.write "\"" & $pnode.ident.s & "\")"
|
result &= "\"" & $pnode.ident.s & "\")"
|
||||||
else:
|
else:
|
||||||
if pnode.sons.len != 0:
|
if pnode.sons.len != 0:
|
||||||
for i in 0 ..< pnode.sons.len:
|
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:
|
if i != pnode.sons.len - 1:
|
||||||
stdout.write ","
|
result &= ","
|
||||||
stdout.write "\n# " & offset & ")"
|
result &= "\n# " & offset & ")"
|
||||||
else:
|
else:
|
||||||
stdout.write ")"
|
result &= ")"
|
||||||
if offset.len == 0:
|
if offset.len == 0:
|
||||||
necho ""
|
result &= "\n"
|
||||||
|
|
||||||
proc printDebug*(nimState: NimState, node: TSNode) =
|
proc printDebug*(nimState: NimState, node: TSNode) =
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
|
|
@ -421,8 +421,8 @@ proc printDebug*(nimState: NimState, node: TSNode) =
|
||||||
|
|
||||||
proc printDebug*(nimState: NimState, pnode: PNode) =
|
proc printDebug*(nimState: NimState, pnode: PNode) =
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
necho ("Output => " & $pnode).getCommented()
|
necho ("Output => " & $pnode).getCommented() & "\n" &
|
||||||
nimState.printTree(pnode)
|
nimState.printTree(pnode)
|
||||||
|
|
||||||
# Compiler shortcuts
|
# Compiler shortcuts
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import regex
|
||||||
import "."/plugin
|
import "."/plugin
|
||||||
|
|
||||||
when not declared(CIMPORT):
|
when not declared(CIMPORT):
|
||||||
import compiler/[ast, idents, options]
|
import compiler/[ast, idents, modulegraphs, options]
|
||||||
|
|
||||||
import "."/treesitter/api
|
import "."/treesitter/api
|
||||||
|
|
||||||
|
|
@ -81,6 +81,7 @@ type
|
||||||
constSection*, enumSection*, procSection*, typeSection*: PNode
|
constSection*, enumSection*, procSection*, typeSection*: PNode
|
||||||
identCache*: IdentCache
|
identCache*: IdentCache
|
||||||
config*: ConfigRef
|
config*: ConfigRef
|
||||||
|
graph*: ModuleGraph
|
||||||
|
|
||||||
gState*: State
|
gState*: State
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ typedef struct { char *a1; int *a2[1]; } A19, *A19p;
|
||||||
typedef struct A20 { char a1; } A20, A21, *A21p;
|
typedef struct A20 { char a1; } A20, A21, *A21p;
|
||||||
|
|
||||||
//Expression
|
//Expression
|
||||||
//typedef struct A21 { int **f1; int abc[123+132]; } A21;
|
typedef struct A22 { int **f1; int *f2[123+132]; } A22;
|
||||||
|
|
||||||
//Unions
|
//Unions
|
||||||
//union UNION1 {int f1; };
|
//union UNION1 {int f1; };
|
||||||
|
|
|
||||||
|
|
@ -69,3 +69,6 @@ assert A20 is object
|
||||||
testFields(A20, {"a1": "cchar"}.toTable())
|
testFields(A20, {"a1": "cchar"}.toTable())
|
||||||
assert A21 is A20
|
assert A21 is A20
|
||||||
assert A21p is ptr 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