This commit is contained in:
araq
2025-12-23 14:01:51 +01:00
parent e48a73c5cd
commit 6c216d451c
3 changed files with 232 additions and 151 deletions

View File

@@ -1502,8 +1502,28 @@ proc nextSubtree(r: var Stream; dest: var TokenBuf; tok: var PackedToken) =
dec nested
if nested == 0: break
proc processTopLevel(c: var DecodeContext; s: var Stream; loadFullAst: bool; suffix: string; logOps: var seq[LogEntry]; module: int): PNode =
result = newNode(nkStmtList)
proc loadImport(c: var DecodeContext; s: var Stream; deps: var seq[string]; tok: var PackedToken) =
tok = next(s) # skip `(import`
if tok.kind == DotToken:
tok = next(s) # skip dot
if tok.kind == DotToken:
tok = next(s) # skip dot
if tok.kind == StringLit:
deps.add pool.strings[tok.litId]
else:
raiseAssert "expected StringLit but got " & $tok.kind
if tok.kind == ParRi:
tok = next(s) # skip )
else:
raiseAssert "expected ParRi but got " & $tok.kind
type
NifGraph = object
topLevel: PNode # top level statements of the main module
deps: seq[string] # other modules we need to process the top level statements of
proc processTopLevel(c: var DecodeContext; s: var Stream; loadFullAst: bool; suffix: string; logOps: var seq[LogEntry]; module: int): NifGraph =
result = NifGraph(topLevel: newNode(nkStmtList))
var localSyms = initTable[string, PSym]()
var t = next(s) # skip dot
@@ -1520,7 +1540,7 @@ proc processTopLevel(c: var DecodeContext; s: var Stream; loadFullAst: bool; suf
var cursor = cursorAt(buf, 0)
let replayNode = loadNode(c, cursor, suffix, localSyms)
if replayNode != nil:
result.sons.add replayNode
result.topLevel.sons.add replayNode
t = next(s)
if t.kind == ParRi:
t = next(s)
@@ -1548,8 +1568,10 @@ proc processTopLevel(c: var DecodeContext; s: var Stream; loadFullAst: bool; suf
t = loadLogOp(c, logOps, s, MethodEntry, attachedTrace, module)
#elif t.tagId == repClassTag:
# t = loadLogOp(c, logOps, s, ClassEntry, attachedTrace, module)
elif t.tagId == includeTag or t.tagId == importTag:
elif t.tagId == includeTag:
t = skipTree(s)
elif t.tagId == importTag:
loadImport(c, s, result.deps, t)
elif t.tagId == implTag:
cont = false
elif loadFullAst:
@@ -1559,15 +1581,15 @@ proc processTopLevel(c: var DecodeContext; s: var Stream; loadFullAst: bool; suf
var cursor = cursorAt(buf, 0)
let stmtNode = loadNode(c, cursor, suffix, localSyms)
if stmtNode != nil:
result.sons.add stmtNode
result.topLevel.sons.add stmtNode
else:
cont = false
else:
cont = false
proc loadNifModule*(c: var DecodeContext; f: FileIndex; interf, interfHidden: var TStrTable;
proc loadNifGraph*(c: var DecodeContext; f: FileIndex; interf, interfHidden: var TStrTable;
logOps: var seq[LogEntry];
loadFullAst: bool = false): PNode =
loadFullAst: bool = false): NifGraph =
let suffix = moduleSuffix(c.infos.config, f)
# Ensure module index is loaded - moduleId returns the FileIndex for this suffix
@@ -1587,8 +1609,13 @@ proc loadNifModule*(c: var DecodeContext; f: FileIndex; interf, interfHidden: va
t = next(s[]) # skip flags
result = processTopLevel(c, s[], loadFullAst, suffix, logOps, f.int)
else:
result = newNode(nkStmtList)
result = NifGraph(topLevel: newNode(nkStmtList))
proc loadNifModule*(c: var DecodeContext; f: FileIndex; interf, interfHidden: var TStrTable;
logOps: var seq[LogEntry];
loadFullAst: bool = false): PNode =
let g = loadNifGraph(c, f, interf, interfHidden, logOps, loadFullAst)
result = g.topLevel
when isMainModule:
import std / syncio

View File

@@ -404,140 +404,140 @@ proc parse*(t: typedesc[TSymKind]; s: string): TSymKind =
proc toNifTag*(s: TTypeKind): string =
case s
of tyNone: "none"
of tyBool: "bool"
of tyChar: "char"
of tyEmpty: "empty"
of tyAlias: "alias"
of tyNil: "nil"
of tyUntyped: "untyped"
of tyTyped: "typed"
of tyTypeDesc: "typedesc"
of tyGenericInvocation: "ginvoke"
of tyGenericBody: "gbody"
of tyGenericInst: "ginst"
of tyGenericParam: "gparam"
of tyDistinct: "distinct"
of tyEnum: "enum"
of tyOrdinal: "ordinal"
of tyArray: "array"
of tyObject: "object"
of tyTuple: "tuple"
of tySet: "set"
of tyRange: "range"
of tyPtr: "ptr"
of tyRef: "ref"
of tyVar: "mut"
of tySequence: "seq"
of tyProc: "proctype"
of tyPointer: "pointer"
of tyOpenArray: "openarray"
of tyString: "string"
of tyCstring: "cstring"
of tyForward: "forward"
of tyInt: "int"
of tyInt8: "int8"
of tyInt16: "int16"
of tyInt32: "int32"
of tyInt64: "int64"
of tyFloat: "float"
of tyFloat32: "float32"
of tyFloat64: "float64"
of tyFloat128: "float128"
of tyUInt: "uint"
of tyUInt8: "uint8"
of tyUInt16: "uint16"
of tyUInt32: "uint32"
of tyUInt64: "uint64"
of tyOwned: "owned"
of tySink: "sink"
of tyLent: "lent"
of tyVarargs: "varargs"
of tyUncheckedArray: "uarray"
of tyError: "error"
of tyBuiltInTypeClass: "bconcept"
of tyUserTypeClass: "uconcept"
of tyUserTypeClassInst: "uconceptinst"
of tyCompositeTypeClass: "cconcept"
of tyInferred: "inferred"
of tyAnd: "and"
of tyOr: "or"
of tyNot: "not"
of tyAnything: "anything"
of tyStatic: "static"
of tyFromExpr: "fromx"
of tyConcept: "concept"
of tyVoid: "void"
of tyIterable: "iterable"
of tyNone: "n0"
of tyBool: "b0"
of tyChar: "c0"
of tyEmpty: "e0"
of tyAlias: "a0"
of tyNil: "n1"
of tyUntyped: "U0"
of tyTyped: "t0"
of tyTypeDesc: "t1"
of tyGenericInvocation: "g0"
of tyGenericBody: "g1"
of tyGenericInst: "g2"
of tyGenericParam: "g4"
of tyDistinct: "d0"
of tyEnum: "e1"
of tyOrdinal: "o0"
of tyArray: "a1"
of tyObject: "o1"
of tyTuple: "t2"
of tySet: "s0"
of tyRange: "r0"
of tyPtr: "p0"
of tyRef: "r1"
of tyVar: "v0"
of tySequence: "s1"
of tyProc: "p1"
of tyPointer: "p2"
of tyOpenArray: "o3"
of tyString: "s2"
of tyCstring: "c1"
of tyForward: "F0"
of tyInt: "i0"
of tyInt8: "i1"
of tyInt16: "i2"
of tyInt32: "i3"
of tyInt64: "i4"
of tyFloat: "f0"
of tyFloat32: "f1"
of tyFloat64: "f2"
of tyFloat128: "f3"
of tyUInt: "u0"
of tyUInt8: "u1"
of tyUInt16: "u2"
of tyUInt32: "u3"
of tyUInt64: "u4"
of tyOwned: "o2"
of tySink: "s3"
of tyLent: "L0"
of tyVarargs: "v1"
of tyUncheckedArray: "U1"
of tyError: "e2"
of tyBuiltInTypeClass: "b1"
of tyUserTypeClass: "U2"
of tyUserTypeClassInst: "U3"
of tyCompositeTypeClass: "c2"
of tyInferred: "I0"
of tyAnd: "a2"
of tyOr: "o4"
of tyNot: "n2"
of tyAnything: "a3"
of tyStatic: "s4"
of tyFromExpr: "F1"
of tyConcept: "c3"
of tyVoid: "v2"
of tyIterable: "I1"
proc parse*(t: typedesc[TTypeKind]; s: string): TTypeKind =
case s
of "none": tyNone
of "bool": tyBool
of "char": tyChar
of "empty": tyEmpty
of "alias": tyAlias
of "nil": tyNil
of "untyped": tyUntyped
of "typed": tyTyped
of "typedesc": tyTypeDesc
of "ginvoke": tyGenericInvocation
of "gbody": tyGenericBody
of "ginst": tyGenericInst
of "gparam": tyGenericParam
of "distinct": tyDistinct
of "enum": tyEnum
of "ordinal": tyOrdinal
of "array": tyArray
of "object": tyObject
of "tuple": tyTuple
of "set": tySet
of "range": tyRange
of "ptr": tyPtr
of "ref": tyRef
of "mut": tyVar
of "seq": tySequence
of "proctype": tyProc
of "pointer": tyPointer
of "openarray": tyOpenArray
of "string": tyString
of "cstring": tyCstring
of "forward": tyForward
of "int": tyInt
of "int8": tyInt8
of "int16": tyInt16
of "int32": tyInt32
of "int64": tyInt64
of "float": tyFloat
of "float32": tyFloat32
of "float64": tyFloat64
of "float128": tyFloat128
of "uint": tyUInt
of "uint8": tyUInt8
of "uint16": tyUInt16
of "uint32": tyUInt32
of "uint64": tyUInt64
of "owned": tyOwned
of "sink": tySink
of "lent": tyLent
of "varargs": tyVarargs
of "uarray": tyUncheckedArray
of "error": tyError
of "bconcept": tyBuiltInTypeClass
of "uconcept": tyUserTypeClass
of "uconceptinst": tyUserTypeClassInst
of "cconcept": tyCompositeTypeClass
of "inferred": tyInferred
of "and": tyAnd
of "or": tyOr
of "not": tyNot
of "anything": tyAnything
of "static": tyStatic
of "fromx": tyFromExpr
of "concept": tyConcept
of "void": tyVoid
of "iterable": tyIterable
of "n0": tyNone
of "b0": tyBool
of "c0": tyChar
of "e0": tyEmpty
of "a0": tyAlias
of "n1": tyNil
of "U0": tyUntyped
of "t0": tyTyped
of "t1": tyTypeDesc
of "g0": tyGenericInvocation
of "g1": tyGenericBody
of "g2": tyGenericInst
of "g4": tyGenericParam
of "d0": tyDistinct
of "e1": tyEnum
of "o0": tyOrdinal
of "a1": tyArray
of "o1": tyObject
of "t2": tyTuple
of "s0": tySet
of "r0": tyRange
of "p0": tyPtr
of "r1": tyRef
of "v0": tyVar
of "s1": tySequence
of "p1": tyProc
of "p2": tyPointer
of "o3": tyOpenArray
of "s2": tyString
of "c1": tyCstring
of "F0": tyForward
of "i0": tyInt
of "i1": tyInt8
of "i2": tyInt16
of "i3": tyInt32
of "i4": tyInt64
of "f0": tyFloat
of "f1": tyFloat32
of "f2": tyFloat64
of "f3": tyFloat128
of "u0": tyUInt
of "u1": tyUInt8
of "u2": tyUInt16
of "u3": tyUInt32
of "u4": tyUInt64
of "o2": tyOwned
of "s3": tySink
of "L0": tyLent
of "v1": tyVarargs
of "U1": tyUncheckedArray
of "e2": tyError
of "b1": tyBuiltInTypeClass
of "U2": tyUserTypeClass
of "U3": tyUserTypeClassInst
of "c2": tyCompositeTypeClass
of "I0": tyInferred
of "a2": tyAnd
of "o4": tyOr
of "n2": tyNot
of "a3": tyAnything
of "s4": tyStatic
of "F1": tyFromExpr
of "c3": tyConcept
of "v2": tyVoid
of "I1": tyIterable
else: tyNone

View File

@@ -28,10 +28,6 @@ const
("nkError", "err"),
("nkType", "onlytype"),
("nkTypeSection", "type"),
("tySequence", "seq"),
("tyVar", "mut"),
("tyProc", "proctype"),
("tyUncheckedArray", "uarray"),
("nkExprEqExpr", "vv"),
("nkExprColonExpr", "kv"),
("nkDerefExpr", "deref"),
@@ -55,17 +51,75 @@ const
("mVar", "varm"),
("mInSet", "contains"),
("mNil", "nilm"),
("tyBuiltInTypeClass", "bconcept"),
("tyUserTypeClass", "uconcept"),
("tyUserTypeClassInst", "uconceptinst"),
("tyCompositeTypeClass", "cconcept"),
("tyGenericInvocation", "ginvoke"),
("tyGenericBody", "gbody"),
("tyGenericInst", "ginst"),
("tyGenericParam", "gparam"),
("nkStmtList", "stmts"),
("nkDotExpr", "dot"),
("nkBracketExpr", "at")
("nkBracketExpr", "at"),
("tyNone", "n0"), # we always use a digit for type kinds so there can be no overlap with node kinds
("tyBool", "b0"),
("tyChar", "c0"),
("tyEmpty", "e0"),
("tyAlias", "a0"),
("tyNil", "n1"),
("tyUntyped", "U0"),
("tyTyped", "t0"),
("tyTypeDesc", "t1"),
("tyGenericInvocation", "g0"),
("tyGenericBody", "g1"),
("tyGenericInst", "g2"),
("tyGenericParam", "g4"),
("tyDistinct", "d0"),
("tyEnum", "e1"),
("tyOrdinal", "o0"),
("tyArray", "a1"),
("tyObject", "o1"),
("tyTuple", "t2"),
("tySet", "s0"),
("tyRange", "r0"),
("tyPtr", "p0"),
("tyRef", "r1"),
("tyVar", "v0"),
("tySequence", "s1"),
("tyProc", "p1"),
("tyPointer", "p2"),
("tyOpenArray", "o3"),
("tyString", "s2"),
("tyCstring", "c1"),
("tyForward", "F0"),
("tyInt", "i0"),
("tyInt8", "i1"),
("tyInt16", "i2"),
("tyInt32", "i3"),
("tyInt64", "i4"),
("tyFloat", "f0"),
("tyFloat32", "f1"),
("tyFloat64", "f2"),
("tyFloat128", "f3"),
("tyUInt", "u0"),
("tyUInt8", "u1"),
("tyUInt16", "u2"),
("tyUInt32", "u3"),
("tyUInt64", "u4"),
("tyOwned", "o2"),
("tySink", "s3"),
("tyLent", "L0"),
("tyVarargs", "v1"),
("tyUncheckedArray", "U1"),
("tyError", "e2"),
("tyBuiltInTypeClass", "b1"),
("tyUserTypeClass", "U2"),
("tyUserTypeClassInst", "U3"),
("tyCompositeTypeClass", "c2"),
("tyInferred", "I0"),
("tyAnd", "a2"),
("tyOr", "o4"),
("tyNot", "n2"),
("tyAnything", "a3"),
("tyStatic", "s4"),
("tyFromExpr", "F1"),
("tyConcept", "c3"),
("tyVoid", "v2"),
("tyIterable", "I1")
]
SuffixesToReplace = [
("Section", ""), ("Branch", ""), ("Stmt", ""), ("I", ""),