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

Added when keyword #43

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 14 additions & 2 deletions src/flattening/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,18 +1112,30 @@ 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 is_if_literal = cursor.kind() == kw!("if");
let position_if_literal = cursor.span();
cursor.field(field!("condition"));
let (condition, condition_is_generative) = self.flatten_expr(cursor);

let if_id = self.instructions.alloc(Instruction::IfStatement(IfStatement {
condition,
is_generative: condition_is_generative,// TODO `if` vs `when` https://github.com/pc2/sus-compiler/issues/3
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There's TODO's like this one scattered around the codebase. I believe you can get rid of is_generative when you add the warning inline here. (Or even have is_generative get the value of literal_is_when, rather than from its context)

then_start: FlatID::PLACEHOLDER,
then_end_else_start: FlatID::PLACEHOLDER,
else_end: FlatID::PLACEHOLDER,
}));
let then_start = self.instructions.get_next_alloc_id();

match(is_if_literal, condition_is_generative){
(true, false) => {
self.errors.warn(position_if_literal, "Used 'if' in a non generative context");
},
(false, true) => {
self.errors.error(position_if_literal, "Used 'when' in a generative context");
},
(_, _) => ()
}

let then_start = self.instructions.get_next_alloc_id();
cursor.field(field!("then_block"));
self.flatten_code(cursor);

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
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
]
});

17 changes: 15 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.

18 changes: 18 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
Loading