简体   繁体   中英

How to check return type in xtext using xbase

With xtext I'm trying to develop a small language.

def sum(Integer a, Integer b):Integer {
  return (a+b)
}

This is the grammar I use for this:

Function:
  'def' name=ValidID
  '('(params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
   ':' type=JvmTypeReference
   body=XBlockExpression;

For reasons obvious to me it complains that "Void functions cannot return a value". How do I link the type of the return expression with the type in my function declaration?

You have to put the expression into the context of a JvmOperation. Please refer to the domain model example, the docs and the 7-languages if you want to learn more about the inferred JVM model.

Basically, what you have to do is something along these lines:

def dispatch infer(MyLanguageConcept concept, IJvmDeclaredTypeAcceptor acceptor, boolean prelinking) {
    acceptor.accept(
        concept.toClass( concept.fullyQualifiedName )
    ).initializeLater [
        for ( definition : concept.getDefinitions ) {
            members += definition.toMethod(
                    definition.name,
                    definition.type) [
                for (p : definition.params) {
                    parameters += p.toParameter(p.name, p.parameterType)
                }
                body = definition.body
            ]
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM