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

Lox Showcase #162

Closed
wants to merge 37 commits into from
Closed

Lox Showcase #162

wants to merge 37 commits into from

Conversation

emilkrebs
Copy link
Contributor

image
image

Two steps are needed first in order to publish this

  1. Modified the language server so it sends the results of the interpreter TypeFox/langium-lox#3 is needed first and has to be published on npm
  2. Add the package to the dependencies

@@ -0,0 +1,72 @@
export interface LoxMessage {
type: LoxMessageType;
content: unknown;
Copy link
Contributor

Choose a reason for hiding this comment

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

Having unknown type is not so nice. There is a easy pattern I use:

type XType = 'A'|'B'|'C';
interface XBase {
  type: XType;
}

interface A extends XBase {
  type: 'A'; // subset of parent definition!
  content: string;
}

interface B extends XBase {
  type: 'B'; // subset of parent...
  content: number;
}

interface C extends XBase {
  type: 'C';
  content: boolean;
}
type X = A|B|C;

//usage: you can do stuff like this:
const x: X = ... //get from somewhere
if(x.type === 'A') {
   //it asserts then that x is of type A, so accessing x.content will be a string, no casts needed!!!
} else if(x.type === 'B') {
  //...x.content is number
} else if(x.type === 'C') {
  //...x.content is boolean
} else {
  assertUnreachable(x.type); //this code is then unreachable (it will be highlighted red when another type D was added)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The interpreter returns an unknown type, so I could do something like this:

export interface LoxMessage {
    type: LoxMessageType;
    content: MessageContent;
};

type MessageContent = string | number | boolean | null | any;

messages: TerminalMessage[];
}

interface TerminalMessage {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the same trick as above to avoid Union type for content

@spoenemann
Copy link
Contributor

@emilkrebs please squash your commits and rebase to the latest changes on the main branch: we've now switched deployment to GH Pages.

@emilkrebs emilkrebs closed this Oct 17, 2023
@emilkrebs emilkrebs deleted the addLoxShowcase branch October 17, 2023 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants