Skip to content

Conversation

@inmaldrerah
Copy link

This PR supports a new convenient feature: printing an AST into YAML format. This may be helpful for debugging.

A YamlPrinter is defined to print any AST Node to YAML format. Node also has a yamlPrint() convenient method to print its contents to YAML format directly.

For example, the following code:

func factorial(n: Int64): Int64 {
    if (n <= 1) {
        1
    } else {
        n * factorial(n - 1)
    }
}

main() {
    factorial(5)
}

is printed as:

type: Program
decls:
- type: FuncDecl
  keyword: func # Position: 1:5
  identifier: factorial # Position: 1:15
  declType:
    type: PrimitiveType
    typeParameterName: factorial # Position: 1:15
    typeName: Int64 # Position: 1:32
  params:
  - type: FuncParam
    declType: null
    name: n # Position: 1:17
    paramType:
      type: PrimitiveType
      typeParameterName: n # Position: 1:17
      typeName: Int64 # Position: 1:24
  block:
    type: Block
    nodes:
    - type: IfExpr
      condition:
        type: BinaryExpr
        left:
          type: RefExpr
          identifier: n # Position: 2:10
        oper: '<=' # Position: 2:13
        right:
          type: LitConstExpr
          kind: 'integer literal'
          literal: 1 # Position: 2:15
      ifBlock:
        type: Block
        nodes:
        - type: LitConstExpr
          kind: 'integer literal'
          literal: 1 # Position: 3:10
      elseBranch:
        type: Block
        nodes:
        - type: BinaryExpr
          left:
            type: RefExpr
            identifier: n # Position: 5:10
          oper: '*' # Position: 5:12
          right:
            type: CallExpr
            callee:
              type: RefExpr
              identifier: factorial # Position: 5:22
            arguments:
            - type: Argument
              expr:
                type: BinaryExpr
                left:
                  type: RefExpr
                  identifier: n # Position: 5:24
                oper: '-' # Position: 5:26
                right:
                  type: LitConstExpr
                  kind: 'integer literal'
                  literal: 1 # Position: 5:28
- type: MainDecl
  declType: null
  param: null
  block:
    type: Block
    nodes:
    - type: CallExpr
      callee:
        type: RefExpr
        identifier: factorial # Position: 10:14
      arguments:
      - type: Argument
        expr:
          type: LitConstExpr
          kind: 'integer literal'
          literal: 5 # Position: 10:16

@inmaldrerah inmaldrerah marked this pull request as ready for review November 12, 2025 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant