Skip to content

Add a simple example without Langium#59

Merged
insafuhrmann merged 15 commits intomainfrom
lotes/39-simple-readme-example
Jun 23, 2025
Merged

Add a simple example without Langium#59
insafuhrmann merged 15 commits intomainfrom
lotes/39-simple-readme-example

Conversation

@Lotes
Copy link
Collaborator

@Lotes Lotes commented Feb 4, 2025

I wrote a simple handwritten expression parser.

It has:

  • print outs like print 123;
  • variable declaration like var index = 123;
  • variable usage like print index+1;
  • binary expressions, like +, -, *, / and %
  • unary expressions, like + 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.

Copy link
Collaborator

@JohannesMeierSE JohannesMeierSE left a 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!)

Copy link
Collaborator

@JohannesMeierSE JohannesMeierSE left a 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!


typir.validation.Collector.addValidationRule((node) => {
if(isAssignment(node)) {
const left = typir.Inference.inferType(node.variable);
Copy link
Collaborator

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 @@
/******************************************************************************
Copy link
Collaborator

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?

@JohannesMeierSE JohannesMeierSE modified the milestones: v0.2, v0.3 Mar 31, 2025
@JohannesMeierSE JohannesMeierSE force-pushed the lotes/39-simple-readme-example branch 2 times, most recently from 9f2257f to 5c952df Compare May 26, 2025 14:21
@JohannesMeierSE
Copy link
Collaborator

@insafuhrmann As discussed, I updated this PR according my own comments above. Could you review the update therefore?

@JohannesMeierSE JohannesMeierSE force-pushed the lotes/39-simple-readme-example branch from 5c952df to d84f7da Compare May 28, 2025 14:45
Copy link
Member

@insafuhrmann insafuhrmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the additions and adjustments @JohannesMeierSE! They look good and helpful to me.
I think this is ready to be merged now.
Thanks @Lotes for the contribution of this great new example!

@insafuhrmann insafuhrmann merged commit 557cd28 into main Jun 23, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants