Skip to content

Commit

Permalink
Add support for a scoped index
Browse files Browse the repository at this point in the history
The scoped index contains new variables and datatypes that could later
be merged into the main index.

Any ast statetment that could declare new variables or represents a
scope is marked with a new scope.
Further statements would inherit this scope to see new variables.
TODO: Going out of scope would merge the new variables with unique names
back into the index.

Co-authored-by: Michael <[email protected]>
  • Loading branch information
ghaith and mhasel committed Oct 25, 2023
1 parent a036ec9 commit 6dd7ed9
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 125 deletions.
2 changes: 1 addition & 1 deletion compiler/plc_ast/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::{

use crate::ast::AstId;

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct IdProvider {
current_id: Arc<AtomicUsize>,
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use self::{

pub mod const_expressions;
mod instance_iterator;
pub mod scoped_index;
pub mod symbol;
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -734,7 +735,7 @@ impl PouIndexEntry {
/// the TypeIndex carries all types.
/// it is extracted into its seaprate struct so it can be
/// internally borrowed individually from the other maps
#[derive(Debug)]
#[derive(Debug, Clone)]

Check warning on line 738 in src/index.rs

View check run for this annotation

Codecov / codecov/patch

src/index.rs#L738

Added line #L738 was not covered by tests
pub struct TypeIndex {
/// all types (structs, enums, type, POUs, etc.)
types: SymbolMap<String, DataType>,
Expand Down
64 changes: 64 additions & 0 deletions src/index/scoped_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::{collections::HashMap, rc::Rc};

use plc_ast::provider::IdProvider;
use plc_source::source_location::SourceLocation;

use crate::typesystem::DataType;

use super::VariableIndexEntry;

/// A minimal index implementation that can be used for local scopes
#[derive(Debug, Clone)]

Check warning on line 11 in src/index/scoped_index.rs

View check run for this annotation

Codecov / codecov/patch

src/index/scoped_index.rs#L11

Added line #L11 was not covered by tests
pub struct ScopedIndex {
///The scope of the current index, this is usually a POU
scope: String,

/// A unique identifier that new variables in this scope will inherit
suffix_provider: IdProvider,

/// The location that caused this scope to be created
start_location: SourceLocation,

/// New variables defined by this index
variables: HashMap<String, VariableIndexEntry>,

/// Datatypes defined by this index
type_index: HashMap<String, DataType>,

parent: Option<Rc<ScopedIndex>>,
}

impl ScopedIndex {
pub fn merge_into(self, target: &mut Self) {
target.variables.extend(self.variables);
target.type_index.extend(self.type_index);
}

Check warning on line 35 in src/index/scoped_index.rs

View check run for this annotation

Codecov / codecov/patch

src/index/scoped_index.rs#L32-L35

Added lines #L32 - L35 were not covered by tests

pub fn add_variable(&mut self, _name: &str) {}

Check warning on line 37 in src/index/scoped_index.rs

View check run for this annotation

Codecov / codecov/patch

src/index/scoped_index.rs#L37

Added line #L37 was not covered by tests

pub fn add_type(&mut self, _name: &str) {}

Check warning on line 39 in src/index/scoped_index.rs

View check run for this annotation

Codecov / codecov/patch

src/index/scoped_index.rs#L39

Added line #L39 was not covered by tests

pub fn find_variable(&self, _name: &str) -> Option<&VariableIndexEntry> {
todo!()
}

Check warning on line 43 in src/index/scoped_index.rs

View check run for this annotation

Codecov / codecov/patch

src/index/scoped_index.rs#L41-L43

Added lines #L41 - L43 were not covered by tests

pub fn find_type(&self, _name: &str) -> Option<&DataType> {
todo!()
}

Check warning on line 47 in src/index/scoped_index.rs

View check run for this annotation

Codecov / codecov/patch

src/index/scoped_index.rs#L45-L47

Added lines #L45 - L47 were not covered by tests

pub fn new(
parent: Option<Rc<ScopedIndex>>,
location: SourceLocation,
scope: &str,
suffix_provider: IdProvider,
) -> ScopedIndex {
ScopedIndex {
scope: scope.to_string(),
suffix_provider,
start_location: location,
parent,
type_index: Default::default(),
variables: Default::default(),
}
}
}
10 changes: 10 additions & 0 deletions src/index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ impl<K, V> Default for SymbolMap<K, V> {
}
}

impl<K, V> Clone for SymbolMap<K, V>
where
K: Clone,
V: Clone,
{
fn clone(&self) -> Self {
Self { inner_map: self.inner_map.clone() }
}

Check warning on line 28 in src/index/symbol.rs

View check run for this annotation

Codecov / codecov/patch

src/index/symbol.rs#L26-L28

Added lines #L26 - L28 were not covered by tests
}

impl<K, V> SymbolMap<K, V>
where
K: Hash + Eq,
Expand Down
Loading

0 comments on commit 6dd7ed9

Please sign in to comment.