Need to implement language with type declarations, will be grateful for a hint or an example

Hello!

I am new to MPS and need some help with building custom language.

The desired language should have a possibility to define types  (structs and enums) and define constant values (instances) of user-defined types.

For example, the data in editor should look like

def type Generation : enum {
case generationX
case millenials
case zoomers
}

def type Person : struct {
field generation : Generation
field height : integer
field weight : integer
}

def value John : Person {
generation : millenials
height : 185
weight : 75
}

def value Cameron : Person {
generation : zoomers
height : 170
weight : 60
}

I know how to declare custom types (via named StructDeclaration concept which has children FieldDeclaration concepts), but I have no idea how to instantiate in editor a concrete value of defined type with setup of all fields values (which scheme is described in type def). 

What options and aspects can I use in MPS to achieve this?

Many thanks in advance!

4 comments
Comment actions Permalink

A possibility that comes to mind is to hold a collection of "assignments" as a child of your concept for values:

concept ValueDefinition 

  children:

    valueAssignments: ValueAssignment [0..N]

  references:

    IamInstanceOf: StructDeclaration [1]

These "assignments" refer to the field that they initialize and a value to set:

concept ValueAssignment

  children:

    value: Expression [1]

  references:

    field: FieldDefinition [1]

Constraints or checking rules can be used to report missing as well as duplicate initializations.

Vaclav

 

0
Comment actions Permalink

Hi Vaclav!

Thanks for the idea, it matters.

Is there any way to pre-construct the collection of ValueAssignments in editor/runtime to show the user all fields immediately (without selecting them manually from autocompletion) ?

Or can I create concepts like Person_Instance on the fly and use it in editor?

0
Comment actions Permalink

The BuilderExpression in KernelF (https://github.com/IETS3/iets3.opensource/) is similar to what you want. It uses the model listeners feature from the mbeddr platform, but you could use an auto-applied quickfix instead. Take a look at the usages of the addMissingSetters() method in BuilderExpression_Behavior.

0
Comment actions Permalink

As Sergej has suggested, MPS provides auto-applicable quick-fixes to fix issues detected in code by checking rules (the typesystem aspect).

Alternatively you can offer the user intentions to populate the fields, although these require manual intervention.

My preferred approach, however, would be using node factories - the factory for the reference to "type" in value definition could populate the field initializers.

Vaclav

 

0

Please sign in to leave a comment.