Skip to content

Commit

Permalink
Include tree-sitter-sus in main repo
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Jan 31, 2024
1 parent 143cbc0 commit 7102f2f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "0.0.1"
authors = ["VonTum <[email protected]>"]
edition = "2021"
repository = "https://github.com/pc2/sus-compiler"
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -14,6 +13,10 @@ console = "0.15.7" # for terminal colors
ariadne = "0.3.0" # for nice errors
num = "*"

# Tree sitter
tree-sitter = "0.20.10"
tree-sitter-sus = {path = "tree-sitter-sus"}

# calyx-ir = {version = "0.6.1", optional = true}
# calyx-opt = {version = "0.6.1", optional = true}
# calyx-backend = {version = "0.6.1", optional = true}
Expand Down
43 changes: 41 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ mod typing;
mod dev_aid;
mod linker;

use std::process::Stdio;
use std::{env, ops::Deref};
use std::error::Error;
use std::fs::File;
use std::fs::{read_to_string, File};
use std::io::Write;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use ast::Module;
use codegen_fallback::gen_verilog_code;
use dev_aid::syntax_highlighting::*;
Expand All @@ -47,6 +48,33 @@ fn codegen_to_file(linker : &Linker, id : ModuleUUID, md : &Module) -> Option<()
Some(())
}

fn test_tree_sitter(path : &Path) {
let code = read_to_string(path).unwrap();
let mut parser = tree_sitter::Parser::new();
parser.set_language(tree_sitter_sus::language()).expect("Error loading sus grammar");
let tree = parser.parse(code, None).unwrap();

let mut dot_cmd = std::process::Command::new("dot");
dot_cmd.arg("-Tsvg");
dot_cmd.arg("-Gcharset=latin1");
dot_cmd.stdin(Stdio::piped());
dot_cmd.stdout(Stdio::piped());
let dot_proc = dot_cmd.spawn().unwrap();
tree.print_dot_graph(dot_proc.stdin.as_ref().unwrap());
let out = dot_proc.wait_with_output().unwrap();
let mut out_file = File::create(format!("{}.svg", path.file_stem().unwrap().to_str().unwrap())).unwrap();
out_file.write(&out.stdout).unwrap();

/*
let root = tree.root_node();
let mut cursor = root.walk();
for c in root.children(&mut cursor) {
println!("{c:?}");
}
println!("{root:?}");*/
}


fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
let mut args = env::args();

Expand All @@ -56,6 +84,7 @@ fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
let mut is_lsp = false;
let mut codegen = None;
let mut codegen_all = false;
let mut test_sus_sitter = false;
let mut settings = SyntaxHighlightSettings{
show_tokens : false
};
Expand All @@ -74,6 +103,9 @@ fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
"--tokens" => {
settings.show_tokens = true;
}
"--tree" => {
test_sus_sitter = true;
}
other => {
file_paths.push(PathBuf::from(other));
}
Expand All @@ -92,6 +124,13 @@ fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
//codegen = Some("first_bit_idx_6".to_owned());
}

if test_sus_sitter {
for path in &file_paths {
test_tree_sitter(&path);
}
return Ok(())
}

let (linker, paths_arena) = compile_all(file_paths);
print_all_errors(&linker, &paths_arena);
for (id, path) in &paths_arena {
Expand Down
2 changes: 1 addition & 1 deletion tree-sitter-sus

0 comments on commit 7102f2f

Please sign in to comment.