Skip to content

Commit

Permalink
Added when literal
Browse files Browse the repository at this point in the history
- added when literal to grammar
- updated auto generated tree-sitter configs
- added "is_when" bool to if-struct
  • Loading branch information
TomSkarabis committed Dec 18, 2024
1 parent 79895dc commit ceeccc6
Show file tree
Hide file tree
Showing 10 changed files with 2,679 additions and 4,284 deletions.
4 changes: 3 additions & 1 deletion src/flattening/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,11 +1112,13 @@ impl<'l, 'errs> FlatteningContext<'l, 'errs> {

fn flatten_if_statement(&mut self, cursor: &mut Cursor) {
cursor.go_down(kind!("if_statement"), |cursor| {
cursor.field(field!("if_literal"));
let literal_is_when = cursor.kind() == kw!("when");
cursor.field(field!("condition"));
let (condition, condition_is_generative) = self.flatten_expr(cursor);

let if_id = self.instructions.alloc(Instruction::IfStatement(IfStatement {
condition,
is_when: literal_is_when,
is_generative: condition_is_generative,// TODO `if` vs `when` https://github.com/pc2/sus-compiler/issues/3
then_start: FlatID::PLACEHOLDER,
then_end_else_start: FlatID::PLACEHOLDER,
Expand Down
1 change: 1 addition & 0 deletions src/flattening/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl<'linker> InitializationContext<'linker> {

fn gather_ports_in_if_stmt(&mut self, cursor: &mut Cursor) {
cursor.go_down_no_check(|cursor| {
cursor.field(field!("if_literal"));
cursor.field(field!("condition"));
cursor.field(field!("then_block"));
self.gather_all_ports_in_block(cursor);
Expand Down
1 change: 1 addition & 0 deletions src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ impl FuncCallInstruction {
#[derive(Debug)]
pub struct IfStatement {
pub condition: FlatID,
pub is_when: bool,
pub is_generative: bool,
pub then_start: FlatID,
pub then_end_else_start: FlatID,
Expand Down
20 changes: 12 additions & 8 deletions tree-sitter-sus/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = grammar({
'const',
field('const_type', $._type)
),

// Template Declaration

template_declaration_arguments: $ => seq(
Expand All @@ -99,9 +99,9 @@ module.exports = grammar({
newlineSepSeq($, choice(
$.block,
$.decl_assign_statement,
// Decls should only allow a single declaration, and cannot contain expressions,
// but we allow some tolerance in the grammar here, so we can generate better errors after.

// Decls should only allow a single declaration, and cannot contain expressions,
// but we allow some tolerance in the grammar here, so we can generate better errors after.
$.assign_left_side,
$.if_statement,
$.for_statement,
Expand Down Expand Up @@ -130,7 +130,10 @@ module.exports = grammar({
),

if_statement: $ => seq(
'if',
field('if_literal',choice(
'when',
'if'
)),
field('condition', $._expression),
//optional(field('conditional_bindings', $.interface_ports)),
field('then_block', $.block),
Expand Down Expand Up @@ -158,7 +161,7 @@ module.exports = grammar({
'domain',
field('name', $.identifier),
),

interface_statement: $ => seq(
'interface',//field('interface_kind', choice('action', 'query', 'trigger')),
field('name', $.identifier),
Expand Down Expand Up @@ -268,7 +271,7 @@ module.exports = grammar({
'.',
field('name', $.identifier)
)),

parenthesis_expression_list: $ => seq(
'(',
sepSeq($._expression, $._comma),
Expand Down Expand Up @@ -299,7 +302,7 @@ module.exports = grammar({
// Template
optional(field('template_args', $.template_args))
),

template_args: $ => seq(
'#(',
commaSepSeq($, $.template_arg),
Expand Down Expand Up @@ -345,3 +348,4 @@ module.exports = grammar({
$.multi_line_comment
]
});

10 changes: 1 addition & 9 deletions tree-sitter-sus/package.json

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

18 changes: 16 additions & 2 deletions tree-sitter-sus/src/grammar.json

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

19 changes: 19 additions & 0 deletions tree-sitter-sus/src/node-types.json

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

Loading

0 comments on commit ceeccc6

Please sign in to comment.