Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Implement if/unless/elsif/else. #25

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const const_start = /[A-Z]/,
SHIFT: 9,
ADD: 10,
MULTIPLY: 11,
CONDITIONAL: 99,
};

module.exports = grammar({
Expand Down Expand Up @@ -84,6 +85,8 @@ module.exports = grammar({
$.extend,
$.abstract_def,
$.def,
prec(PREC.CONDITIONAL, $.if),
prec(PREC.CONDITIONAL, $.unless),
$.return,
$._expression,
),
Expand Down Expand Up @@ -178,6 +181,46 @@ module.exports = grammar({
'end',
),

if: $ => seq(
'if',
field('condition', $._expression),
choice($._terminator, field('consequence', $.then)),
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to save the then to a field?

Copy link
Member

@nobodywasishere nobodywasishere Dec 5, 2024

Choose a reason for hiding this comment

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

Ah I guess you're using it to store the body of the if, any terminators, and the then; would recommend not putting all 3 of these together in one field if possible

field('alternative', optional(choice($.else, $.elsif))),
'end',
),

unless: $ => seq(
'unless',
field('condition', $._expression),
choice($._terminator, field('consequence', $.then)),
field('alternative', optional(choice($.else, $.elsif))),
Copy link
Member

Choose a reason for hiding this comment

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

unless doesn't have elsif

'end',
),

elsif: $ => seq(
'elsif',
field('condition', $._expression),
choice($._terminator, field('consequence', $.then)),
field('alternative', optional(choice($.else, $.elsif))),
),

else: $ => seq(
'else',
optional($._terminator),
optional($._statements),
),

then: $ => choice(
seq(
$._terminator,
$._statements,
),
seq(
optional($._terminator),
'then',
optional($._statements),
),
),
return: $ => seq('return', optional($._expression)),

_expression: $ =>
Expand Down
285 changes: 285 additions & 0 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading