Crystal: Add macro methods to lib-related nodes

Created on 5 Nov 2017  路  3Comments  路  Source: crystal-lang/crystal

I noticed there were no way to inspect and navigate in lib-related nodes like lib, fun, and C-struct/union/enum. I think it is missing.

The macro methods I suggest would be:

  • LibNode

    • name

    • convenient methods to access its content: structs, unions, enums, aliases, typedefs (or simply types to access all these?), functions, constants

    • some helpers like in TypeNode: has_constant?, has_method?, etc..

  • FunDef

    • real_name, varargs?
    • method's node like methods: name, args, return_type, body
  • Typedef (not sure about this one)

    • name, type
  • StructDef & UnionDef (or simply on CStructOrUnionDef?)

    • name
    • fields
    • union? (if on CStructOrUnionDef)
  • EnumDef

    • name
    • members
    • base_type
  • ExternalVar (e.g: $errno : Int32)

    • name
    • real_name
    • type

I attempted to add them, first to LibDef but I realized that a lib node is currently recognized as a TypeNode in macros:

lib LibFoo
  fun bar
end

macro describe_lib(lib_name)
  {% l = lib_name.resolve %}
  {% puts l.class_name %}
  {% puts l.methods %}
end

describe_lib LibFoo

Gives (https://carc.in/#/r/3178):

"TypeNode"
[]

Which I think is wrong, a lib is more a namespace than a type, it cannot respond to a type-like node methods like methods, union?, ancestors, etc... It should be recognized as a LibDef, like its AST node.

Most helpful comment

I don't think we need usecases, completeness of the language is enough of an argument. It is surprising that macros can't traverse or inspect lib AST nodes, we should remove that surprise.

All 3 comments

Any use cases?

I don't think we need usecases, completeness of the language is enough of an argument. It is surprising that macros can't traverse or inspect lib AST nodes, we should remove that surprise.

I actually started to work on this some time ago, it works pretty well for accessing the lib functions and the types (struct/enum/union) nodes, but I'm not sure I did it the right way (still in the process of understanding how the macros actully works, how it get the types and stuff..), and I'm stuck on accessing the types fields..

I'll probably re-work on this in the next few days and try a pr to have some feedback on it.

Edit in 2019: well thoses days are long... :innocent:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  60Comments

HCLarsen picture HCLarsen  路  162Comments

sergey-kucher picture sergey-kucher  路  66Comments

asterite picture asterite  路  78Comments

ezrast picture ezrast  路  84Comments