Working with custom types, parameters and arguments

I'm working on my first DSL in MPS and am struggling a bit with my type system, which should work like a standard Object Oriented language (e.g. Java).

My language needs to allow users to declare:

- custom types
  • methods which return a type and accept a collection of parameters(references to designated types)
  • method calls, which pass typed references or literals to the called method

I have tried setting up a "Type" concept, which has a string property (the type's name). I have then created an interface (ITypeReference), which references a type, and a corresponding comparison rule which considers two ITypeReference nodes equal if their type strings are equivalent.

I have created my method class, which contains a list of parameters and return type, and I can create a method call, which references another method, and allows designation of ITypeReference nodes as arguments.

However, I'm having trouble figuring out the key next steps:

  • How do I enforce that the each argument's type must match the called method's parameter type?

I have tried writing an inference rule, but it's clear that I'm working outside of MPS's native node-typing system, so cannot use type comparison symbols like :==:. Perhaps I'm just going about this whole thing the wrong by instituting my own types based off of string names? Is there some way to plug into MPS's internal typing system for this so that I can allow solution programmers to declare types which will extend MPS's internal type system?

It seems like there would be an example project for this, but I haven't found it yet.


  • Related, how do I restrict the number of arguments which can be passed to a method to the corresponding number of parameters of the method?

For example, if a method accepts 2 parameters, someone calling that method should only be allowed to use 2 arguments. I don't seem to be able to dynamically refer to the number of arguments in the methodCall structure's declaration. Perhaps this is possible via the editor?

Hopefully this makes sense! If not, I'm happy to try providing some example syntax to clarify. Thanks in advance for any assistance!!
1 comment
Hi,

what you describe is a pretty general use case for a typesystem. MPS provides sufficient support for building one that would satisfy your requirements.

I'd suggest you familiarize yourself with how a typesystem works first. I assume you have already looked at the docs, next step is to look at some samples. If you want to have a deeper understanding, I could recommend a book on typesystem design, such as this one.

  How do I enforce that the each argument's type must match the called method's parameter type?


Usually one compares the types of the arguments against the declared types of the formal parameters. You need to ensure that every argument's type is a subtype of the corresponding formal parameter's type.

It seems like there would be an example project for this, but I haven't found it yet.


What's wrong with MPS as an example project?

  Related, how do I restrict the number of arguments which can be passed to a method to the corresponding number of parameters of the method?


You can use a checking rule for that (AKA non-typesystem rule).

I hope this helps you. Unfortunately, without knowing specifics of your problem there's not much that can be said.

Regards,
F.
0

Please sign in to leave a comment.