Compare commits

...

3 Commits

Author SHA1 Message Date
Greg Shuflin dc771fc7ad Working simple tree-sitter grammar 2024-04-23 02:37:01 -07:00
Greg Shuflin 45c4d08fb9 Add justfile 2024-04-23 02:15:26 -07:00
Greg Shuflin 77257d0eb7 Messing with treesitter grammar
doesn't work yet
2024-04-23 02:13:44 -07:00
3 changed files with 41 additions and 16 deletions

View File

@ -9,17 +9,20 @@ module.exports = grammar({
),
function_definition: $ => seq(
"fn",
'fn',
$.identifier,
$.parameter_list,
$._type,
field("return_type", optional($._type)),
$.block,
),
parameter_list: $ => seq("(", /* TODO */ ")"),
block: $ => seq(
"{",
repeat($._statement),
choice(
repeat($._statement),
"",
),
"}"
),
@ -29,9 +32,16 @@ module.exports = grammar({
_return_statement: $ => seq("return", $._expression, ";"),
_expression: $ => choice($.identifier),
_expression: $ => choice($.identifier, $.unary, $.binary),
unary: $ => prec(4, choice(seq("-", $._expression), seq("!", $._expression))),
binary: $ => choice(prec.left(2, seq($._expression, "*", $._expression)), prec.left(1, seq($._expression, "+", $._expression))),
_type: $ => "bool",
_type: $ => choice(
$.primitive_type,
),
primitive_type: $ => choice("bool", "int"),
identifier: $ => /[a-z]+/,
}

View File

@ -0,0 +1,8 @@
_default:
just --list
# Test out the grammar
test-grammar:
#!/usr/bin/env bash
tree-sitter generate
tree-sitter test

View File

@ -1,19 +1,26 @@
=============
Initial test
=============
fn main() {
hello
}
----
(source_file
(function_definition
(identifier)
(parameter_list)
(block)
)
)
====
Another test
====
fn yolo() bool { }
----
(source_file)
=====
Another
====
fn main() { }
------
()
(source_file
(function_definition
(identifier) (parameter_list) (primitive_type) (block)))