Skip to content
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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

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!)

Lotes added 4 commits March 7, 2025 17:29
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]>
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!

- Print statements
- Expressions, like basic arithmetic operations, string concatenation, variable references, literals and parentheses

## How does it work?
Copy link
Collaborator

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);
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?

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.

2 participants