Skip to content

Commit

Permalink
Added parsing of if-else statements. Flattening is broken for the moment
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Oct 19, 2023
1 parent 5cad090 commit 4208d59
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 70 deletions.
24 changes: 24 additions & 0 deletions multiply_add.sus
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,27 @@ module exists : duplicate a {
module use_other_file : int a -> int r {
r = hello_from_the_other_side(a);
}

//timeline (X -> X) .. (/ -> X) .. (/ -> X) .. (/ -> X)
module Unpack4 : int[4] packed -> int out_stream {
state int st = 0; // Initial value, not a real assignment
state int[3] stored_packed;

if st == 0 {
out_stream = packed[0];
stored_packed[0] = packed[1]; // Shorthand notation is possible here "stored_packed[0:2] = packed[1:3];"
stored_packed[1] = packed[2];
stored_packed[2] = packed[3];
st = 1;
} else if st == 1 {
out_stream = stored_packed[0];
st = 2;
} else if st == 2 {
out_stream = stored_packed[1];
st = 3;
} else if st == 3 {
out_stream = stored_packed[2];
st = 0;
finish; // packet is hereby finished.
}
}
22 changes: 3 additions & 19 deletions resetNormalizer.sus
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
/*
clocked module resetNormalizer :
bool resetn -> bool reg rst, bool reg isInitialized;

int reg cyclesSinceReset;

if(!resetn) {
cyclesSinceReset = 0;
rst = 1;
isInitialized = 0;
} else {
if(cyclesSinceReset > 30) rst = 0;
if(cyclesSinceReset > 512) isInitialized = 1;
cyclesSinceReset = cyclesSinceReset + 1;
}

endmodule
*/

module hello_from_the_other_side : int a -> int result {
result = a;
if true {
result = a;
}
}
11 changes: 9 additions & 2 deletions src/arena_alloc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use std::{ops::{IndexMut, Index}, marker::PhantomData, iter::Enumerate};
use std::{ops::{IndexMut, Index}, marker::PhantomData, iter::Enumerate, fmt};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct UUID<IndexMarker>(usize, PhantomData<IndexMarker>);

impl<IndexMarker> fmt::Debug for UUID<IndexMarker> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("id_")?;
self.0.fmt(f)
}
}

impl<IndexMarker> Default for UUID<IndexMarker> {
fn default() -> Self {
Self::INVALID
Expand Down
52 changes: 29 additions & 23 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use num_bigint::BigUint;

use crate::{tokenizer::TokenTypeIdx, linker::{ValueUUID, FileUUID}};
use crate::{tokenizer::TokenTypeIdx, linker::{ValueUUID, FileUUID}, flattening::FlattenedModule};
use core::ops::Range;
use std::ops::Deref;

Expand Down Expand Up @@ -127,6 +127,7 @@ pub struct CodeBlock {
pub enum Statement {
Declaration{local_id : usize},
Assign{to : Vec<AssignableExpressionWithModifiers>, eq_sign_position : Option<usize>, expr : SpanExpression}, // num_regs v = expr;
If{condition : SpanExpression, then : CodeBlock, els : Option<CodeBlock>},
Block(CodeBlock),
TimelineStage(usize)
}
Expand All @@ -145,7 +146,9 @@ pub struct Module {
pub link_info : LinkInfo,

pub declarations : Vec<SignalDeclaration>,
pub code : CodeBlock
pub code : CodeBlock,

pub flattened : Option<FlattenedModule>
}

impl Module {
Expand Down Expand Up @@ -272,16 +275,29 @@ impl IterIdentifiers for SpanTypeExpression {
}
}

pub fn for_each_assign_in_block<F>(block : &Vec<SpanStatement>, func : &mut F) where F: FnMut(&Vec<AssignableExpressionWithModifiers>, &SpanExpression) {
for (stmt, _span) in block {
match stmt {
Statement::Assign{to, eq_sign_position : _, expr} => {
func(to, expr);
},
Statement::Block(b) => {
for_each_assign_in_block(&b.statements, func);
},
_other => {}
impl IterIdentifiers for CodeBlock {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(LocalOrGlobal, usize) -> () {
for (stmt, _span) in &self.statements {
match stmt {
Statement::Assign{to, eq_sign_position : _, expr} => {
for assign_to in to {
assign_to.expr.for_each_value(func);
}
expr.for_each_value(func);
},
Statement::Block(b) => {
b.for_each_value(func);
},
Statement::Declaration { local_id : _ } => {}
Statement::If { condition, then, els } => {
condition.for_each_value(func);
then.for_each_value(func);
if let Some(e) = &els {
e.for_each_value(func);
}
}
Statement::TimelineStage(_) => {}
}
}
}
}
Expand All @@ -291,16 +307,6 @@ impl IterIdentifiers for Module {
for (pos, decl) in self.declarations.iter().enumerate() {
func(LocalOrGlobal::Local(pos), decl.span.1);
}
for_each_assign_in_block(&self.code.statements, &mut |to, v| {
for assign_to in to {
assign_to.expr.for_each_value(func);
}
v.for_each_value(func);
});
self.code.for_each_value(func);
}
}





9 changes: 9 additions & 0 deletions src/codegen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::linker::Linker;




pub fn gen_code(linker : &mut Linker, ) {

}

11 changes: 6 additions & 5 deletions src/dev_aid/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ impl LoadedFileCache {
}
fn update_text(&mut self, uri : Url, new_file_text : String) {
let file_uuid = self.find_uri(&uri).unwrap();
let (full_parse, parsing_errors) = perform_full_semantic_parse(&new_file_text, file_uuid);
self.linker.relink(file_uuid, new_file_text, full_parse, parsing_errors);
let (full_parse, parsing_errors) = perform_full_semantic_parse(new_file_text, file_uuid);
self.linker.relink(file_uuid, full_parse, parsing_errors);
}
fn ensure_contains_file(&mut self, uri : &Url) -> FileUUID {
if let Some(found) = self.find_uri(uri) {
found
} else {
let file_uuid = self.linker.reserve_file();
let file_text = std::fs::read_to_string(uri.to_file_path().unwrap()).unwrap();
let (full_parse, parsing_errors) = perform_full_semantic_parse(&file_text, file_uuid);
self.linker.add_reserved_file(file_uuid, file_text, full_parse, parsing_errors);
let (full_parse, parsing_errors) = perform_full_semantic_parse(file_text, file_uuid);
self.linker.add_reserved_file(file_uuid, full_parse, parsing_errors);
self.uris.insert(file_uuid, uri.clone());
file_uuid
}
Expand Down Expand Up @@ -347,8 +347,9 @@ fn main_loop(
let mut errors = file_cache.linker.files[uuid].parsing_errors.clone();
file_cache.linker.get_linking_errors(uuid, &mut errors);

file_cache.linker.flatten_all_modules_in_file(uuid, &mut errors);
//file_cache.linker.flatten_all_modules_in_file(uuid, &mut errors);

println!("Errors: {:?}", &errors);
send_errors_warnings(&connection, errors, &token_positions, &file_cache.uris)?;
},
// TODO ...
Expand Down
4 changes: 2 additions & 2 deletions src/dev_aid/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ pub fn syntax_highlight_file(file_paths : Vec<PathBuf>) {
}
};

let (full_parse, errors) = perform_full_semantic_parse(&file_text, uuid);
let (full_parse, errors) = perform_full_semantic_parse(file_text, uuid);

println!("{:?}", full_parse.ast);

prelinker.add_reserved_file(uuid, file_text, full_parse, errors);
prelinker.add_reserved_file(uuid, full_parse, errors);
paths_arena.insert(uuid, file_path);
}

Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn join_expected_list(expected : &[TokenTypeIdx]) -> String {
}

// Class that collects and manages errors and warnings
#[derive(Clone)]
#[derive(Debug,Clone)]
pub struct ErrorCollector {
pub errors : Vec<ParsingError>,
pub file : FileUUID
Expand Down
19 changes: 19 additions & 0 deletions src/flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ impl<'l, 'm, 'e> FlatteningContext<'l, 'm, 'e> {
Statement::Declaration{local_id} => {
// TODO
}
Statement::If { condition, then, els } => {
//todo!()
}
Statement::Assign{to, expr : (Expression::FuncCall(func_and_args), func_span), eq_sign_position} => {
let Some((md, instantiation_idx, output_range)) = self.desugar_func_call(&func_and_args, func_span.1) else {return;};

Expand Down Expand Up @@ -318,3 +321,19 @@ pub struct FlattenedModule {
instantiations : ListAllocator<Instantiation, InstantiationIDMarker>,
connections : Vec<Connection>
}



#[derive(Debug)]
struct InstantiatedWire {
typ : TypeExpression,
latency : i64
}

#[derive(Debug)]
pub struct InstantiatedModule {
wires : ListAllocator<InstantiatedWire, WireIDMarker>,
instantiations : ListAllocator<Instantiation, InstantiationIDMarker>,
connections : Vec<Connection>
}

16 changes: 8 additions & 8 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,15 @@ impl PreLinker {
pub fn reserve_file(&mut self) -> FileUUID {
self.files.reserve()
}
pub fn add_reserved_file(&mut self, file : FileUUID, file_text : String, parse_result : FullParseResult, parsing_errors : ErrorCollector) {
pub fn add_reserved_file(&mut self, file : FileUUID, parse_result : FullParseResult, parsing_errors : ErrorCollector) {
let mut associated_values = Vec::new();
for md in parse_result.ast.modules {
let module_name = &file_text[parse_result.tokens[md.link_info.name_token].get_range()];
let module_name = &parse_result.file_text[parse_result.tokens[md.link_info.name_token].get_range()];
let new_module_uuid = self.links.globals.alloc(Named::Module(md));
associated_values.push(new_module_uuid);
self.links.add_name(module_name, new_module_uuid);
}
self.files.alloc_reservation(file, FileData { file_text, tokens: parse_result.tokens, token_hierarchy: parse_result.token_hierarchy, parsing_errors, associated_values});
self.files.alloc_reservation(file, FileData{file_text : parse_result.file_text, tokens: parse_result.tokens, token_hierarchy: parse_result.token_hierarchy, parsing_errors, associated_values});
}

// This should be called once all modules have been added. Adds errors for globals it couldn't match
Expand Down Expand Up @@ -395,15 +395,15 @@ impl Linker {
self.files.reserve()
}

pub fn add_reserved_file(&mut self, file : FileUUID, file_text : String, parse_result : FullParseResult, parsing_errors : ErrorCollector) {
pub fn add_reserved_file(&mut self, file : FileUUID, parse_result : FullParseResult, parsing_errors : ErrorCollector) {
let mut associated_values = Vec::new();
for md in parse_result.ast.modules {
let module_name = &file_text[parse_result.tokens[md.link_info.name_token].get_range()];
let module_name = &parse_result.file_text[parse_result.tokens[md.link_info.name_token].get_range()];
let new_module_uuid = self.links.globals.alloc(Named::Module(md));
associated_values.push(new_module_uuid);
self.links.add_name(module_name, new_module_uuid);
}
self.files.alloc_reservation(file, FileData { file_text, tokens: parse_result.tokens, token_hierarchy: parse_result.token_hierarchy, parsing_errors, associated_values});
self.files.alloc_reservation(file, FileData { file_text : parse_result.file_text, tokens: parse_result.tokens, token_hierarchy: parse_result.token_hierarchy, parsing_errors, associated_values});

for (_uuid, val_in_file) in &mut self.links.globals {
if let Some(link_info) = val_in_file.get_link_info_mut() {
Expand All @@ -415,10 +415,10 @@ impl Linker {
}
}

pub fn relink(&mut self, file : FileUUID, file_text : String, parse_result : FullParseResult, parsing_errors : ErrorCollector) {
pub fn relink(&mut self, file : FileUUID, parse_result : FullParseResult, parsing_errors : ErrorCollector) {
self.remove_file_datas(&[file]);
self.files.revert_to_reservation(file);
self.add_reserved_file(file, file_text, parse_result, parsing_errors);
self.add_reserved_file(file, parse_result, parsing_errors);
}

pub fn get_constant(&self, GlobalReference(identifier_span, uuid) : GlobalReference, errors : &mut ErrorCollector) -> Option<Value> {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

mod arena_alloc;
mod tokenizer;
mod parser;
mod errors;
mod ast;
mod flattening;
mod arena_alloc;
mod codegen;

mod dev_aid;
mod linker;
Expand Down
Loading

0 comments on commit 4208d59

Please sign in to comment.