Skip to content

Commit

Permalink
Compiler can now mostly use tree-sitter based parser 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Apr 5, 2024
1 parent d291673 commit 5890e8c
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 122 deletions.
38 changes: 2 additions & 36 deletions multiply_add.sus
Original file line number Diff line number Diff line change
Expand Up @@ -123,42 +123,6 @@ module multiply_add_old : i32 a, i32 b, i32 c -> i32 result, double double_resul
reg double_result = cvt_to_double(result);
}

/*timeline (a -> /) .. (a -> r)*
module blur : i32'0 a -> i32'1 result {
state int prev = a;
#
loop {
result = @@@@@@(a + prev) / 2; // pipeline stage
prev = a;
# // timeline step
}
}*/

timeline (a -> /) .. (a -> r)*
module my_complex_operation : i32'0 a -> i32'9 result {
state prev'0 = a;
state tmp2'6;
#
loop {
tmp2 = @@@@@@(a + prev) / 2; // pipeline stage
result = @@@(prev + tmp2);
prev = a;
# // timeline step
}
}

timeline (a -> /)* .. (/ -> r)
module seq_adder : i32 a -> i32 result {
state int sum = a;
#
loop {
sum = sum + a;
#
}
result = sum;
}


module exists : hello a -> int result {

}
Expand Down Expand Up @@ -451,6 +415,8 @@ module permute24 : bool[128] mbf, bool[24] valid_permutes, bool start -> bool[12
stored_valid_permutes = stored_valid_permutes & permutes_to_keep;

permuted_out = permute(stored_mbf, current_permutation_idx);

//aaaaa();
}


Expand Down
2 changes: 1 addition & 1 deletion resetNormalizer.sus
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module parallel_mul_add_reg : int a, int b -> int p, int q {

}

module packer : bool[256] data , bool valid -> bool[64] o {
module packer : bool[256] data, bool valid -> bool[64] o {
state bool[192] save = data[64:256];
state int part;
initial part = 3;
Expand Down
7 changes: 7 additions & 0 deletions src/file_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ impl Span {
Span(outer.0, inner.0)
}
#[track_caller]
pub fn shrunk_front(outer : Span, inner : Span) -> Span {
assert!(outer.0 <= inner.0);
assert!(outer.1 >= inner.1);

Span(inner.0, outer.1)
}
#[track_caller]
pub fn difference_right(outer : Span, inner : Span) -> Span {
assert!(outer.0 <= inner.0);
assert!(outer.1 >= inner.1);
Expand Down
269 changes: 201 additions & 68 deletions src/flattening/mod.rs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
if file_paths.len() == 0 {
// Quick debugging
file_paths.push(PathBuf::from("resetNormalizer.sus"));
//file_paths.push(PathBuf::from("multiply_add.sus"));
file_paths.push(PathBuf::from("multiply_add.sus"));
file_paths.push(PathBuf::from("tinyTestFile.sus"));
codegen_all = true;
//codegen = Some("first_bit_idx_6".to_owned());
}
Expand Down
32 changes: 17 additions & 15 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,11 +814,13 @@ pub struct SusTreeSitterSingleton {
pub array_op_kind : u16,
pub func_call_kind : u16,
pub parenthesis_expression_kind : u16,
pub parenthesis_expression_list_kind : u16,
pub array_bracket_expression_kind : u16,
pub block_kind : u16,
pub decl_assign_statement_kind : u16,
pub assign_left_side_kind : u16,
pub assign_to_kind : u16,
pub write_modifiers_kind : u16,
pub if_statement_kind : u16,
pub for_statement_kind : u16,

Expand Down Expand Up @@ -887,11 +889,13 @@ impl SusTreeSitterSingleton {
array_op_kind : node_kind("array_op"),
func_call_kind : node_kind("func_call"),
parenthesis_expression_kind : node_kind("parenthesis_expression"),
parenthesis_expression_list_kind : node_kind("parenthesis_expression_list"),
array_bracket_expression_kind : node_kind("array_bracket_expression"),
block_kind : node_kind("block"),
decl_assign_statement_kind : node_kind("decl_assign_statement"),
assign_left_side_kind : node_kind("assign_left_side"),
assign_to_kind : node_kind("assign_to"),
write_modifiers_kind : node_kind("write_modifiers"),
if_statement_kind : node_kind("if_statement"),
for_statement_kind : node_kind("for_statement"),

Expand Down Expand Up @@ -942,13 +946,13 @@ pub struct Cursor<'t> {
}

impl<'t> Cursor<'t> {
#[track_caller]
pub fn new_for_node(tree : &'t Tree, file_text : &'t FileText, span : Span, kind : u16) -> Self {
let mut cursor = tree.walk();
let _ = cursor.goto_first_child_for_byte(span.into_range().start).unwrap();
let start_node = cursor.node();
assert!(start_node.kind_id() == kind);
assert!(start_node.byte_range() == span.into_range());
assert_eq!(start_node.kind_id(), kind);
// Temprarily comment out, because old parser and new parser are slightly different
//assert_eq!(start_node.byte_range(), span.into_range());

Self{cursor, file_text}
}
Expand Down Expand Up @@ -1081,8 +1085,8 @@ impl<'t> Cursor<'t> {

/// Goes down the current node, checks it's kind, and then iterates through 'item' fields.
#[track_caller]
pub fn list<F : FnMut(&mut Self)>(&mut self, kind : u16, mut func : F) {
self.go_down(kind, |self2| {
pub fn list<F : FnMut(&mut Self)>(&mut self, parent_kind : u16, mut func : F) {
self.go_down(parent_kind, |self2| {
loop {
if self2.cursor.field_id() == Some(SUS.item_field) {
func(self2);
Expand All @@ -1097,26 +1101,24 @@ impl<'t> Cursor<'t> {

/// Goes down the current node, checks it's kind, and then iterates through 'item' fields.
///
/// The function given should return Option<OT>, and from the valid outputs this function constructs a output list
/// The function given should return OT, and from the valid outputs this function constructs a output list
#[track_caller]
pub fn collect_list<OT, F : FnMut(&mut Self) -> Option<OT>>(&mut self, kind : u16, mut func : F) -> Vec<OT> {
pub fn collect_list<OT, F : FnMut(&mut Self) -> OT>(&mut self, parent_kind : u16, mut func : F) -> Vec<OT> {
let mut result = Vec::new();

self.list(kind, |cursor| {
if let Some(item) = func(cursor) {
result.push(item);
}
self.list(parent_kind, |cursor| {
let item = func(cursor);
result.push(item);
});

result
}

/// Goes down the current node, checks it's kind, and then selects the 'content' field. Useful for constructs like seq('[', field('content', $.expr), ']')
#[track_caller]
pub fn go_down_content<OT, F : FnOnce(&mut Self, Span) -> OT>(&mut self, top_kind : u16, func : F) -> OT {
let outer_span = self.span();
self.go_down(top_kind, |self2| {
self2.field(SUS.content_field, |self3| func(self3, outer_span))
pub fn go_down_content<OT, F : FnOnce(&mut Self) -> OT>(&mut self, parent_kind : u16, func : F) -> OT {
self.go_down(parent_kind, |self2| {
self2.field(SUS.content_field, |self3| func(self3))
})
}
}
5 changes: 5 additions & 0 deletions tinyTestFile.sus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

module myMod : int a -> int o {
int x = a;
o = x;
}
2 changes: 1 addition & 1 deletion tree-sitter-sus

0 comments on commit 5890e8c

Please sign in to comment.