How to constraint a subset of baselanguages types and expression

Hey,
I've just started my first MPS project. I want to create a new language, which borrows concepts of the baselanguage. This means I have my own root elements and my own statements which uses some types and expressions of java. But I don't want to have all java types and expressions. Because types and expressions can occur in many places in my language I'm not sure where and how to constrain which of the concepts are allowed.

Has any body an advice how to do this?
Thanks
Fabian
3 comments
The best way is to implement 'can be ancestor' constraint.

Example: your concept contains node<Expression>, but supports only + and integer literals

can be ancestor
   (operationContext, scope, node, childConcept)->boolean {
      childConcept.isExactly(PlusExpression) || childConcept.isInstanceOf(IntegerLiteral)
   }
0
Thanks for your answer!
This was the solution I had in mind, too. But there are several places where I can use expression, and I don't want to constrain it in all places. Of course I can create a method here. But it would be nicer to have some kind of wrapper concept for my own type.

And if I look to your example, I think there will be a problem. Because PlusExpression is a BinaryOperation it has to subexpressions, which could be any kind of Expression. So how can I constrain, that this ones could be only PlusExpressions or IntegerLiterals, too?
There is the same problem with the types, if I want to reuse the ArrayType. Is there any recommended way to reuse these concepts?
0
But there are several places where I can use expression, and I don't want to constrain it in all places. Of course I can create a method here. But it would be nicer to have some kind of wrapper concept for my own type.


Add constraints only in certain places where baseLanguage expressions are used.

If you need a wrapper concept for Expression, create also a 'Paste Wrapper' (in the actions aspect), to be able to paste expressions into roles where the wrapper concept is expected.

Because PlusExpression is a BinaryOperation it has to subexpressions, which could be any kind of Expression. So how can I constrain, that this ones could be only PlusExpressions or IntegerLiterals, too?


If you used 'can be parent' constraint, there would be the problem. But 'can be ancestor' checks all descendant nodes.

There is the same problem with the types, if I want to reuse the ArrayType. Is there any recommended way to reuse these concepts?


When you reuse type concept, you inherit its subtyping relations. If it doesn't perfectly match your needs, choose another one, or create new.

If you reuse baseLanguage expressions, you inherit their type rules. You cannot change the behavior of existing concept, so to customize its type you have to extend it. Create a new type Inference rule for the sub-concept with 'overrides = true'.
0

Please sign in to leave a comment.