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 1 commit
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
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,
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 a little rust trick here, if you rename literal_is_when to just is_when, then you can omit the is_when: literal_is_when al-togehter

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,
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
Loading