Skip to content

Commit

Permalink
feat: loops
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed Jul 6, 2024
1 parent 83668af commit 334fc4c
Show file tree
Hide file tree
Showing 5 changed files with 3,861 additions and 2,243 deletions.
35 changes: 34 additions & 1 deletion tree-sitter-aoc/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ module.exports = grammar({
rules: {
source_file: ($) => repeat(seq($._rules, terminator)),

_rules: ($) => choice($._expression, $.assignment),
_rules: ($) =>
choice(
$._expression,
$.assignment,
$.for_loop,
$.while_loop,
$.continue,
$.break,
),

_expression: ($) =>
choice(
Expand Down Expand Up @@ -132,6 +140,28 @@ module.exports = grammar({
seq(choice($.identifier, $.index, $.array), "=", $._expression),
),

while_loop: ($) =>
seq(
"while",
"(",
field("condition", $._expression),
")",
field("body", $.block),
),

for_loop: ($) =>
seq(
"for",
"(",
field("initial", $._rules),
";",
field("condition", $._expression),
";",
field("after", $._rules),
")",
field("body", $.block),
),

block: ($) =>
choice(
// emtpy block
Expand Down Expand Up @@ -186,6 +216,9 @@ module.exports = grammar({
_string_basic_content: () => token.immediate(prec(1, /[^"\n\\]+/)),
escape_sequence: () => token.immediate(/\\./),

continue: () => "continue",
break: () => "break",

identifier: () => /[a-zA-Z][a-zA-Z_\d]*/,
integer: () => /\d+/,
float: () => /\d+\.\d+/,
Expand Down
114 changes: 114 additions & 0 deletions tree-sitter-aoc/src/grammar.json

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

Loading

0 comments on commit 334fc4c

Please sign in to comment.