Skip to content

Commit

Permalink
add alias info to ref vlues in json output. Add src file name to src …
Browse files Browse the repository at this point in the history
…info on the parser side.
  • Loading branch information
twoentartian committed Jun 13, 2024
1 parent d3309b2 commit 40a9cf4
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 107 deletions.
22 changes: 19 additions & 3 deletions tydi-lang-json-generator/src/json_representation_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use std::collections::BTreeMap;
use serde::Serialize;

use tydi_lang_parser::tydi_memory_representation::project::ProjectItem;
use tydi_lang_parser::tydi_memory_representation::{self, Project};
use tydi_lang_parser::tydi_memory_representation::{self, Project, scope::GlobalIdentifier, code_location::TraitCodeLocationAccess};
use tydi_lang_parser::trait_common::GetName;

use crate::json_representation_implementation::Implementation;
use crate::json_representation_logic_type::LogicType;
use crate::json_representation_logic_type::{self, LogicType};
use crate::json_representation_streamlet::Streamlet;
use crate::name_conversion;

Expand Down Expand Up @@ -98,7 +99,22 @@ pub fn translate_from_tydi_project(tydi_project: Arc<RwLock<Project>>, target_va
match var_type {
JsonRepresentation_item_type::Unknwon => unreachable!("unknown variable type"),
JsonRepresentation_item_type::LogicType(logic_type_name) => {
output_json_representation.logic_types.insert(target_var_name, Arc::new(RwLock::new(LogicType::Ref(logic_type_name))));
let mut ref_info = json_representation_logic_type::RefInfo::new(logic_type_name);
{
let target_var_parent_scope = target_var.read().unwrap().get_parent_scope();
let target_var_parent_scope_name = match target_var_parent_scope {
Some(v) => {
Some(v.read().unwrap().get_name())
},
None => None
};
let target_var_loc = target_var.read().unwrap().get_code_location();
let loc_begin = target_var_loc.begin.clone();
let loc_end = target_var_loc.end.clone();
let raw_name = target_var.read().unwrap().get_name();
ref_info.add_alias(raw_name, target_var_parent_scope_name, loc_begin, loc_end);
}
output_json_representation.logic_types.insert(target_var_name, Arc::new(RwLock::new(LogicType::Ref(ref_info))));
},
JsonRepresentation_item_type::Streamlet(streamlet_name) => {
todo!()
Expand Down
74 changes: 68 additions & 6 deletions tydi-lang-json-generator/src/json_representation_logic_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,66 @@ pub enum LogicType {
Group(Arc<RwLock<LogicGroup>>),
Union(Arc<RwLock<LogicUnion>>),
Stream(Arc<RwLock<LogicStream>>),
Ref(String),
Ref(RefInfo),
}

#[derive(Clone, Debug)]
pub struct RefInfo {
ref_name: String,
alias: Vec<AliasInfo>,
}

#[derive(Clone, Debug)]
pub struct AliasInfo {
alias_name: String,
declared_in_scope: Option<String>,
location_begin: Option<usize>,
location_end: Option<usize>,
}

impl serde::Serialize for AliasInfo {
fn serialize<S>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
where S: serde::Serializer {
let mut state = serializer.serialize_struct("AliasInfo", 2)?;

state.serialize_field("alias_name", &self.alias_name)?;
if self.declared_in_scope.is_some() {
state.serialize_field("declared_in_scope", &self.declared_in_scope)?;
}
if self.location_begin.is_some() {
state.serialize_field("location_begin", &self.location_begin)?;
}
if self.location_end.is_some() {
state.serialize_field("location_end", &self.location_end)?;
}

state.end()
}

}

impl AliasInfo {
pub fn new(alias: String, scope_name: Option<String>, loc_begin: Option<usize>, loc_end: Option<usize>) -> Self {
return Self {
alias_name: alias,
declared_in_scope: scope_name,
location_begin: loc_begin,
location_end: loc_end,
}
}
}

impl RefInfo {
pub fn new(ref_name: String) -> Self {
return Self {
ref_name: ref_name,
alias: vec![],
};
}

pub fn add_alias(&mut self, alias: String, scope_name: Option<String>, loc_begin: Option<usize>, loc_end: Option<usize>) {
self.alias.push(AliasInfo::new(alias, scope_name, loc_begin, loc_end));
}
}

impl serde::Serialize for LogicType {
Expand All @@ -45,7 +104,8 @@ impl serde::Serialize for LogicType {
state.serialize_field("value", &*v.read().unwrap())?;
},
LogicType::Ref(v) => {
state.serialize_field("value", v)?;
state.serialize_field("value", &v.ref_name)?;
state.serialize_field("alias", &v.alias)?;
},
};

Expand Down Expand Up @@ -74,7 +134,8 @@ impl LogicType {
output_dependency.append(&mut dependencies);
output_dependency.insert(target_var_name.clone(), Arc::new(RwLock::new(output_type.clone())));
// output_types.push(output_type.clone());
output_types.push(LogicType::Ref(target_var_name.clone()));
let ref_info = RefInfo::new(target_var_name.clone());
output_types.push(LogicType::Ref(ref_info));
},
TypedValue::RefToVar(ref_var) => {
let results = LogicType::translate_from_tydi_project(tydi_project.clone(), ref_var.clone());
Expand Down Expand Up @@ -104,7 +165,8 @@ impl LogicType {
output_dependency.append(&mut dependencies);
let element_var_name = format!("{}_for{}", &target_var_name, index);
output_dependency.insert(element_var_name.clone(), Arc::new(RwLock::new(output_type.clone())));
output_types.push(LogicType::Ref(element_var_name));
let ref_info = RefInfo::new(element_var_name);
output_types.push(LogicType::Ref(ref_info));
},
_ => (),
}
Expand Down Expand Up @@ -133,7 +195,7 @@ impl LogicType {
};
output_type = LogicType::Bit(bit_width as usize);
output_dependency.insert(target_var_name.clone(), Arc::new(RwLock::new(output_type.clone())));
//we don't update the target_var_name because for logic bit.
//we don't update the target_var_name because we set alias
},
tydi_memory_representation::LogicType::LogicGroupType(v) => {
let results = LogicGroup::translate_from_tydi_project(tydi_project.clone(), v.clone())?;
Expand All @@ -154,7 +216,7 @@ impl LogicType {
let (logic_stream, mut dependencies) = results;
output_dependency.append(&mut dependencies);
output_type = LogicType::Stream(Arc::new(RwLock::new(logic_stream)));
*target_var_name = get_global_variable_name_with_parent_scope(v.clone());
//we don't update the target_var_name because we set alias
},
}
return Ok((output_type, output_dependency));
Expand Down
4 changes: 2 additions & 2 deletions tydi-lang-parser/src/evaluation/evaluate_logic_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::{RwLock, Arc};
use crate::generate_name::generate_init_value;
use crate::tydi_lang_src_to_memory_representation;
use crate::tydi_memory_representation::{Scope, TypedValue, TypeIndication, LogicType, LogicStream, LogicUnion, LogicGroup, LogicBit, TraitCodeLocationAccess, GetScope, ScopeType};
use crate::tydi_memory_representation::{GetScope, LogicBit, LogicGroup, LogicStream, LogicType, LogicUnion, Scope, ScopeType, SrcInfo, TraitCodeLocationAccess, TypeIndication, TypedValue};
use crate::tydi_parser::*;
use crate::error::TydiLangError;

Expand All @@ -16,7 +16,7 @@ pub fn evaluate_LogicalType(src: Pair<Rule>, scope: Arc<RwLock<Scope>>, evaluato
let rule = element.as_rule();
match rule {
Rule::LogicalType_Basic => {
let logic_type = tydi_lang_src_to_memory_representation::parse_LogicalType_Basic(element, scope.clone(), Arc::new(generate_init_value()))?;
let logic_type = tydi_lang_src_to_memory_representation::parse_LogicalType_Basic(element, scope.clone(), SrcInfo::new_init())?;
match &logic_type {
TypeIndication::LogicNull => {
output = TypedValue::LogicTypeValue(Arc::new(RwLock::new(LogicType::LogicNullType)));
Expand Down
53 changes: 26 additions & 27 deletions tydi-lang-parser/src/tydi_lang_src_to_memory_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ use parse_logic_flow::*;
use crate::error::TydiLangError;
use crate::generate_name::generate_init_value;
use crate::tydi_parser::*;
use crate::tydi_memory_representation::{CodeLocation, Package, TraitCodeLocationAccess, GetScope};
use crate::tydi_memory_representation::{CodeLocation, GetScope, Package, SrcInfo, TraitCodeLocationAccess};

pub fn tydi_lang_src_to_memory_representation(src: String) -> Result<Arc<RwLock<Package>>, TydiLangError> {
let src_arc = Arc::new(src.clone());
pub fn tydi_lang_src_to_memory_representation(src: String, src_info: Arc<SrcInfo>) -> Result<Arc<RwLock<Package>>, TydiLangError> {
let parse_result = TydiLangSrc::parse(Rule::TydiFile,&src);
if parse_result.is_err() {
let parse_result = parse_result.err().unwrap();
match parse_result.variant {
pest::error::ErrorVariant::ParsingError { positives, negatives } => {
let error_location = match parse_result.location {
pest::error::InputLocation::Pos(begin) => CodeLocation::new_only_begin(begin, src_arc),
pest::error::InputLocation::Span((begin, end)) => CodeLocation::new(begin, end, src_arc),
pest::error::InputLocation::Pos(begin) => CodeLocation::new_only_begin(begin, src_info.clone()),
pest::error::InputLocation::Span((begin, end)) => CodeLocation::new(begin, end, src_info.clone()),
};
let message_from_parser = format!("Expected: {:?}, found: {:?}", positives, negatives);
return Err(TydiLangError::new(format!("cannot parse the source code, message from parser: {}", message_from_parser), error_location));
Expand All @@ -69,10 +68,10 @@ pub fn tydi_lang_src_to_memory_representation(src: String) -> Result<Arc<RwLock<
for element in parse_result.clone().into_iter() {
match element.as_rule() {
Rule::PackageStatement => {
parse_PackageStatement(element, output_package.clone(), src_arc.clone())?;
parse_PackageStatement(element, output_package.clone(), src_info.clone())?;
}
Rule::Scope_WithoutBracket => {
parse_Scope_WithoutBracket(element, output_package.read().unwrap().get_scope(), src_arc.clone())?;
parse_Scope_WithoutBracket(element, output_package.read().unwrap().get_scope(), src_info.clone())?;
}
Rule::EOI => {
//do nothing
Expand All @@ -82,7 +81,7 @@ pub fn tydi_lang_src_to_memory_representation(src: String) -> Result<Arc<RwLock<
}
{
let mut output_package_write = output_package.write().unwrap();
let loc = CodeLocation::new(0, src.len(), src_arc);
let loc = CodeLocation::new(0, src.len(), src_info.clone());
output_package_write.set_code_location(loc);
}

Expand All @@ -107,8 +106,8 @@ mod test_tydi_lang_src_to_memory_representation {
let src = String::from(r#"
package test;
"#);
let src_ptr = Some(Arc::new(src.clone()));
let result = tydi_lang_src_to_memory_representation(src);
let src_ptr = Some(SrcInfo::new(format!("test.td"), src.clone()));
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand All @@ -126,7 +125,7 @@ mod test_tydi_lang_src_to_memory_representation {
package test;
i = 10;
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand All @@ -152,7 +151,7 @@ mod test_tydi_lang_src_to_memory_representation {
package test;
i:int = 10;
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand All @@ -179,7 +178,7 @@ mod test_tydi_lang_src_to_memory_representation {
i:[int] = {10, 20, 30};
i2 = {10, 20, 30};
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -212,7 +211,7 @@ mod test_tydi_lang_src_to_memory_representation {
package test;
i = {10, 20, 30.0};
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand All @@ -239,7 +238,7 @@ mod test_tydi_lang_src_to_memory_representation {
i:[int] = {10, 20, 30};
i0 = i[0] + func(i);
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -270,7 +269,7 @@ mod test_tydi_lang_src_to_memory_representation {
package test;
type_null: type = Null;
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand All @@ -296,7 +295,7 @@ mod test_tydi_lang_src_to_memory_representation {
package test;
type_null: [type] = {Null, Null, Null};
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -324,7 +323,7 @@ mod test_tydi_lang_src_to_memory_representation {
bit_8_type0: Bit(8);
bit_8_type1: Bit(x);
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -363,7 +362,7 @@ mod test_tydi_lang_src_to_memory_representation {
bit_8_type1: Bit(8);
}
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -395,7 +394,7 @@ mod test_tydi_lang_src_to_memory_representation {
bit_8_type1: Bit(8);
}
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -425,7 +424,7 @@ mod test_tydi_lang_src_to_memory_representation {
bit8 = Bit(8);
bit8_stream : Stream(Bit(8), d=2, throughput=2.0, s="Sync");
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand All @@ -450,7 +449,7 @@ mod test_tydi_lang_src_to_memory_representation {
use test1;
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -483,7 +482,7 @@ mod test_tydi_lang_src_to_memory_representation {
port_1: bit_8_stream out;
}
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -514,7 +513,7 @@ mod test_tydi_lang_src_to_memory_representation {
}
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down Expand Up @@ -548,7 +547,7 @@ mod test_tydi_lang_src_to_memory_representation {
}
}
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand All @@ -573,7 +572,7 @@ mod test_tydi_lang_src_to_memory_representation {
}
}
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand All @@ -597,7 +596,7 @@ mod test_tydi_lang_src_to_memory_representation {
function(0, Bit(1));
"#);
let result = tydi_lang_src_to_memory_representation(src);
let result = tydi_lang_src_to_memory_representation(src, SrcInfo::new_init());
if result.is_err() {
let result = result.err().unwrap();
println!("{}", result.print());
Expand Down
Loading

0 comments on commit 40a9cf4

Please sign in to comment.