Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

haxscramper/hnimast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

readme

Statically typed wrapper for nim ast at compile (NimNode) and rutime (PNode). Another take on solving problem of node[0][0][1][0]. Mostly concerned with processing of types and procedure declarations. Provides helper API for working with nimble packages.

Installation

nimble install hnimast

note: hnimast relies on the compiler API. In order to avoid mismatches between nim compiler that you are compiling code with, and actual compiler API it is necessary to add --path:"$nim" to your nim.cfg file. This also prevents downloading of the whole compiler package, which is by far the largest dependency.

Links

  • github
  • documentation
    compiler_aux
    Helper procs for interfacing with nim compiler. Common module graph setup logic, stdlib search etc.
    cst_lexer, cst_parser, cst_types
    Fork of the nim code parser to be used for pretty-printing. Stores all positional and comment information.
    enum_decl
    Handle enum declaration - parse and generate
    hast_common
    Basic AST construction logic. Provides shared generic interface for operating on both std/macros.NimNode and compiler/ast.PNode. Implements huge number of convenience functions for constructing different node kinds.
    hast_quote
    Reimplementation of the quote do: that doesn’t gensym identifiers, but allows for more complex node interoplation. Provides nquote do: (for NimNode) and pquote do: (for PNode). Latter one can be used for codegen.
    idents_types
    Handle type declarations, identifiers
    nimble_aux
    Helper code for working with nimble. Parse manifest files without having to execute external scripts.
    nim_decl
    Wrap different toplevel entry declarations in a single variant object.
    object_decl
    Type declarations for object parsing
    obj_field_macros
    Object parsing implementation
    pnode_parse
    Wrapper around compiler ‘s parser
    pragmas
    Pragma processing
    proc_decl
    Procedure declaration processing

Description

Provides logical structure for objects/fields - you can set/get type, check if field is a switch for case statement or not. Visit eachField in the object, map object to some code structure.

import hnimast, hnimast/obj_field_macros
import hpprint

let node = """
type Type = object
  hello: float
""".parsePNodeStr()

var obj = parseObject(node, parsePPragma)

echo "# ~~~~ make object exported ~~~~ #"

obj.exported = true
echo obj.toNNode()


echo "# ~~~~ internal structure of object ~~~~ #"
pprint obj
# ~~~~ make object exported ~~~~ #
Type* = object
  hello: float

# ~~~~ internal structure of object ~~~~ #
Object[ast.PNode, Pragma[ast.PNode]]
  exported:   true
  annotation:
    Option[Pragma[ast.PNode]]
      val: Pragma[ast.PNode](kind: oakCaseOfBranch, elements: [])
      has: false
  name:
    NType[ast.PNode]
      kind:      ntkIdent
      head:      "Type"
      genParams: []
  flds:
    - ObjectField[ast.PNode, Pragma[ast.PNode]]
        annotation:
          Option[Pragma[ast.PNode]]
            val:
              Pragma[ast.PNode]
                kind:     oakCaseOfBranch
                elements: []
            has: false
        value:      Option[ast.PNode](val: <nil tree>)
        exported:   false
        isTuple:    false
        name:       "hello"
        fldType:
          NType[ast.PNode]
            kind:      ntkIdent
            head:      "float"
            genParams: []
        isKind:     false
  • working with enums
    • parseEnumImpl - parse as enum implementation. Tested on alias, symbol, value, enum-as-generic-parameter, typedesc[Enum] etc.
    • getEnumPref - for NEP-conforming enums (prefixEnumValueName) get prefix
    • getEnumNames - get list of all enum names
    • enumNames - macro. Generate seq[string] of names for all enums.
  • working with object declarations
    • ObjectBranch, Object and ObjectField types - wrappers on top of nim object declarations. Supports arbitrarily named case fields, annotations for objects etc. Currently does not cover all possible cases.
    • eachField - visit each field in object
    • eachField - recursively generate case expression for each possible and use insert result of callback for each field. For use case example see tests/tHnimAst.nim
    • can be conveted to and from NimNode
    • eachCase - generate case statement for object’s kind fields
    • eachParallelCase - generate case statement for two object’s kind fields.
    • eachPath
  • working with object values
    • ObjTree - ‘stringly typed’ representation of object value. mainly used in hpprint, but due to dependency reasons type definitions is still here.