-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 375 add support for any type #380
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7750771
Add generics type to the AST
ghaith e3fbdc1
Add support for parsing generic types
ghaith 8f03a6b
Use vec instead of map, add generics to index
ghaith 265abfd
Merge remote-tracking branch 'origin/master' into issue-375-Add_suppo…
ghaith ccdb789
Codegen tests
ghaith 560eb00
Add indexing support for generics
ghaith 9098079
Merge remote-tracking branch 'origin/master' into issue-375-Add_suppo…
ghaith 8037f58
Add resolver support for generics
ghaith 43a53a6
Merge remote-tracking branch 'origin/master' into issue-375-Add_suppo…
ghaith 276da9d
Clippy fixes
ghaith 8b7bca3
Code generation for generics
ghaith fe7fdc4
Add natures as keywords, add initial validation scaffold
ghaith 9a54f4c
Merge remote-tracking branch 'origin/master' into issue-375-Add_suppo…
ghaith 71e8a41
Validation wip
ghaith aafb8e8
Add validation for generics
ghaith fd7bb31
Deleted some demo files
ghaith d2b77fe
Add intermdiate index
ghaith 807a3b9
Merge remote-tracking branch 'origin/master' into issue-375-Add_suppo…
ghaith a598e23
Create implementation entries for resolved generics - wip
ghaith c51bfa3
Make sure types are generated, add correctness tests
ghaith 4b8b977
additional tests for generic-parameter annotation
riederm 96ee719
Fix some review comments, missing : Tokens
ghaith a7946cc
Remove the keywords from the lexer and treat type natures as Identifiers
ghaith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
// Copyright (c) 2020 Ghaith Hachem and Mathias Rieder | ||
|
||
use crate::ast::DataTypeDeclaration; | ||
|
||
use super::{ | ||
super::ast::{CompilationUnit, DataType, DataTypeDeclaration, UserTypeDeclaration, Variable}, | ||
super::ast::{CompilationUnit, DataType, UserTypeDeclaration, Variable}, | ||
Pou, SourceRange, | ||
}; | ||
use std::vec; | ||
use std::{collections::HashMap, vec}; | ||
|
||
pub fn pre_process(unit: &mut CompilationUnit) { | ||
//process all local variables from POUs | ||
for mut pou in unit.units.iter_mut() { | ||
//Find all generic types in that pou | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we put this into it's own function?. It kindo'f disturbs the level of detail of the rest of this function |
||
let generic_types = preprocess_generic_structs(&mut pou); | ||
unit.types.extend(generic_types); | ||
|
||
let all_variables = pou | ||
.variable_blocks | ||
.iter_mut() | ||
|
@@ -39,7 +45,9 @@ pub fn pre_process(unit: &mut CompilationUnit) { | |
for dt in unit.types.iter_mut() { | ||
{ | ||
match &mut dt.data_type { | ||
DataType::StructType { name, variables } => { | ||
DataType::StructType { | ||
name, variables, .. | ||
} => { | ||
let name: &str = name.as_ref().map(|it| it.as_str()).unwrap_or("undefined"); | ||
variables | ||
.iter_mut() | ||
|
@@ -88,6 +96,38 @@ pub fn pre_process(unit: &mut CompilationUnit) { | |
unit.types.append(&mut new_types); | ||
} | ||
|
||
fn preprocess_generic_structs(pou: &mut Pou) -> Vec<UserTypeDeclaration> { | ||
let mut generic_types = HashMap::new(); | ||
let mut types = vec![]; | ||
for binding in &pou.generics { | ||
let new_name = format!("__{}__{}", pou.name, binding.name); | ||
//Generate a type for the generic | ||
let data_type = UserTypeDeclaration { | ||
data_type: DataType::GenericType { | ||
name: new_name.clone(), | ||
generic_symbol: binding.name.clone(), | ||
nature: binding.nature, | ||
}, | ||
initializer: None, | ||
scope: Some(pou.name.clone()), | ||
location: pou.location.clone(), | ||
}; | ||
types.push(data_type); | ||
generic_types.insert(binding.name.clone(), new_name); | ||
} | ||
for var in pou | ||
.variable_blocks | ||
.iter_mut() | ||
.flat_map(|it| it.variables.iter_mut()) | ||
{ | ||
replace_generic_type_name(&mut var.data_type, &generic_types); | ||
} | ||
if let Some(datatype) = pou.return_type.as_mut() { | ||
replace_generic_type_name(datatype, &generic_types); | ||
}; | ||
types | ||
} | ||
|
||
fn preprocess_return_type(pou: &mut Pou, types: &mut Vec<UserTypeDeclaration>) { | ||
if let Some(return_type) = &pou.return_type { | ||
if should_generate_implicit(return_type) { | ||
|
@@ -185,3 +225,24 @@ fn add_nested_datatypes( | |
}); | ||
} | ||
} | ||
|
||
fn replace_generic_type_name(dt: &mut DataTypeDeclaration, generics: &HashMap<String, String>) { | ||
match dt { | ||
DataTypeDeclaration::DataTypeDefinition { data_type, .. } => match data_type { | ||
DataType::ArrayType { | ||
referenced_type, .. | ||
} | ||
| DataType::PointerType { | ||
referenced_type, .. | ||
} => replace_generic_type_name(referenced_type.as_mut(), generics), | ||
_ => {} | ||
}, | ||
DataTypeDeclaration::DataTypeReference { | ||
referenced_type, .. | ||
} => { | ||
if let Some(type_name) = generics.get(referenced_type) { | ||
*referenced_type = type_name.clone(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i find it a bit surprising, that typeNatures are note defined in typesystem.rs
I guess you want to avoid the dependency from AST -> typesystem?