Unfortunately, the new URL leads to few information on the compiler 
architecture for a user like I am. My idea was to use Nim's first-class 
meta-programming features to plot simple control flow graphs.

>From 
>[parseString](https://github.com/nim-lang/Nim/blob/devel/compiler/parser.nim#L2574)
> docs, `PNode` is actually the proper AST, not `NimNode`. Also, `PNode` is 
>really [a ref to `TNode`](https://nim-lang.org/docs/compiler/ast.html#PNode), 
>which is defined in 
>[`compiler/ast`](https://nim-lang.org/docs/compiler/ast.html#TNode).

But I have some problem running `parseString` on a code where `let x = 5` spans 
2 separate lines:
    
    
    import compiler/parser
    import compiler/idents
    import compiler/options
    import compiler/ast
    
    let
      code_string: string = """
    echo "Hello world!"
    
    #let x = 5 #  <---------- works
    # the following breaks:
    let
      x = 5
    
    proc foo() =
      echo "bar"
    
    foo()
    """
    
    echo code_string
    
    let
      code_ident = IdentCache()
      code_conf  = ConfigRef()
      code_ast     = parseString(code_string, code_ident, code_conf)
    
    let
      offset_by: uint = 2
    
    import strutils
    
    proc recurse_repr(ast_node: PNode, offset: uint = 0) =
      echo repeat(" ", offset_by*offset), ast_node.typ.repr, ast_node.kind.repr
      
      #for s in ast_node.sons:
      #    recurse_repr(s, offset+1)
      
      # how to test if the node contains `sons`?
      # it is hardcoded in the `kind`s?
      # https://nim-lang.org/docs/compiler/ast.html#TNode
      try:
        for s in ast_node.sons:
          recurse_repr(s, offset+1)
      
      except:
        discard
    
    recurse_repr(code_ast)
    
    
    Run

When `let x_var = 5` spans 2 lines, I get a segfault:
    
    
    Traceback (most recent call last)
    //parse_nim_code.nim(40) parse_nim_code
    
//nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/parser.nim(2579) 
parseString
    //nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/ast.nim(2528) 
parseAll
    
//nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/parser.nim(173) 
parMessage
    
//nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/lexer.nim(236) 
lexMessageTok
    
//nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/lexer.nim(227) 
dispMessage
    //nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/msgs.nim(586) 
liMessage
    //nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/msgs.nim(445) 
handleError
    //nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/msgs.nim(417) 
quit
    
//nim-2.0.4-f81677aa0cf688e364f894141dee30ca8c39065c/compiler/options.nim(627) 
isDefined
    //nim/2.0.4/nim/lib/pure/strtabs.nim(211) hasKey
    //nim/2.0.4/nim/lib/pure/strtabs.nim(137) rawGet
    //nim/2.0.4/nim/lib/pure/strtabs.nim(118) myhash
    SIGSEGV: Illegal storage access. (Attempt to read from nil?)
    Error: execution of an external program failed: '//parse_nim_code'
    
    
    Run

Can I change something in `ConfigRef()` that goes into `parseString()` to make 
it work? I.e. I'd like to make it work on any regular Nim code.

Reply via email to