cenotelie / hime

Apache License 2.0
27 stars 4 forks source link

[Beginner] Postfix Unary Semantic Actions #64

Closed industrylol closed 4 years ago

industrylol commented 4 years ago

I might be missing it in the documentation, but is there an idiomatic way to handle a semantic action on a postfix unary expression, like a keyword that will mark it during lexing to translate it to prefix?

For example below, SemanticBody in the action for @OnNot has what I need to properly evaluate since l_unary is coming from the right hand side of the tree, but for @OnIncrement, primary has not yet been parsed.

rules {
   ...

    // Prefix Unary
    l_unary -> primary^
            |  '!'^ l_unary @OnNot
            |  '-'^ l_unary @OnNegate ;

     primary -> function^
            |  literal^
            // Postfix Unary
            | primary '++'^ @OnIncrement
            | primary '--'^ @OnDecrement
            | '('! expression^ ')'! ;
   ...
}
woutersl commented 4 years ago

I may be misunderstanding, when you refer to primary, you mean the one in the rule's body, right? When the @OnIncrement action is called, it should be given the rule's body as argument with the node for the primary in the primary '++'^ @OnIncrement as first item. At this point the node should be the full AST.

If this is not the case this is a bug. I'll try to check this on my end.

industrylol commented 4 years ago

Thanks, that prompted me to take another look and it turns out that I had been using a test case where my literal rule wasn't catching the value right in one of its Semantic Actions and pushing it on the stack. So, that is why @OnIncrement didn't have what I was expecting. Basically my own fault. Sorry about that!