-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a simple example without Langium #59
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @Lotes for this nice application example without Langium!
A general remark: Since this example is for demonstration, I suggest to add more comments:
- Add a
README.md
which describes the purpose and the interesting parts of this application of Typir. - Document the features/concepts of your DSL (at the moment you need to look into the implementation
- Give readers a hint, how they can experiment with your DSL, e.g. point them to your test cases.
- Add one or two sentences as comments into each of the five
src/*.ts
files to describe their purpose. That will help people who never wrote a parser themselves to get the ideas of the files.
Ideas for more validations:
VAR X = 2 * "hello";
should fail- Introduce an AssignmentStatement and check asignability of left- and right-hand side
If you plan to add a generator, maybe these ideas are helpful:
- Keep it as simple as possible, I like the slim design of the current state of your example!
- Make the implicit conversions (number as string) in the generated code explicit, since here the type system is very helpful (the assignability service returns the conversion path now!)
Signed-off-by: Markus Rudolph <[email protected]>
Signed-off-by: Markus Rudolph <[email protected]>
Signed-off-by: Markus Rudolph <[email protected]>
Signed-off-by: Markus Rudolph <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @Lotes for your example! The additional comments, diagrams and test cases are really helpful!
- Print statements | ||
- Expressions, like basic arithmetic operations, string concatenation, variable references, literals and parentheses | ||
|
||
## How does it work? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice graphics, thanks a lot!
|
||
typir.validation.Collector.addValidationRule((node) => { | ||
if(isAssignment(node)) { | ||
const left = typir.Inference.inferType(node.variable); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could write this shorter in this way:
return typir.validation.Constraints.ensureNodeIsAssignable(node.value, node.variable, (actual, expected) => <ValidationMessageDetails>{
languageNode: node, severity: 'error', message: `'${actual.name}' is not assignable to '${expected.name}'.`,
});
But it is also interesting to the see the implementation behind 🙂
@@ -0,0 +1,141 @@ | |||
/****************************************************************************** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An idea: For the examples OX and LOX, we use the prefixes ox-
and lox-
for the typescript files. Does it makes sense to have a similar prefix (maybe expr-
or expressions-
) for your expressions examples as well?
I wrote a simple handwritten expression parser.
It has:
print 123;
var index = 123;
print index+1;
+
,-
,*
,/
and%
+
and-
It worked good so far, even the desired validations I get for free :-) .
Currently there is not much, that can go wrong. Only operators that are not overloaded like
string * string
cause troubles.Maybe one of you has more ideas, what to add on top for testing or for language features.
For sure I can add parentheses expressions or assignments in order to make the language "round & sound".
In the end I just wanted to have something framework-agnostic and I think this was proven.