-
Hello, I've been testing things with the playground today, just adding some basic variableDeclarations and if/while/for statements etc. I also have some expressions defined, to be able to handle setting/calculations on my variables, but for now I'm just 'hardcoding it' to be whatever i i want it to be (STRING or INT). In my case i have variables of a specific type (String, Integer, Number, etc), so i need to have specific expressions for each type of variable (for instance multiplication dont make sense for a String type). Last question is if its possible to include from other files? Just to split the .langium file into multiple subfiles which is a little easier to navigate through. Any other example-projects are also much appreciated, preferably a language that has variables, methods and some classes (like mine does). :) //Eivind |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 25 replies
-
Hey @ejfasting,
It might be a little unintuitive, but it's the expected behavior with your grammar. An input such as:
Works as expected. A short explanation on the parser error: Since there's no parser rule that is reachable from the A general statement rule that allows to parse an ID (and then maybe follow up with a
It's generally the best solution for almost all (imperative) programming languages, yes. Yours especially, since you mentioned that your language also has classes. You should also take a look at the class member scoping guide that basically explains what the Lox code is doing under the hood.
I wouldn't solve this in the grammar, as that's either (1) a ton of work or (2) impossible given the context-awareness of most programming languages. I.e. you cannot embed a type system in a programming language grammar. Lox solves this by performing validation on the expressions and checking whether a used operator is valid under the given operands.
Yes, see our requirements example. Note that I would really only recommend splitting up the grammar file in case you have multiple languages. |
Beta Was this translation helpful? Give feedback.
I'm a bit confused. Why are
String
andInteger
keywords in the first place? It should be enough for them to only be defined as classes.Also, a construct like
(type=[Class:StringType])? (type=[Class:IntType])?
would allow for<type-reference> <type-reference> <variable-name>
. Multiple assignments to the same property is a bad idea in general.