Skip to content

Commit 661776f

Browse files
committed
Co-authored-by: Michael <[email protected]>
1 parent a036ec9 commit 661776f

File tree

5 files changed

+211
-76
lines changed

5 files changed

+211
-76
lines changed

src/index.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod symbol;
2626
#[cfg(test)]
2727
mod tests;
2828
pub mod visitor;
29+
pub mod scoped_index;
2930

3031
/// A label represents a possible jump point in the source.
3132
/// It can be referenced by jump elements in the same unit
@@ -734,7 +735,7 @@ impl PouIndexEntry {
734735
/// the TypeIndex carries all types.
735736
/// it is extracted into its seaprate struct so it can be
736737
/// internally borrowed individually from the other maps
737-
#[derive(Debug)]
738+
#[derive(Debug, Clone)]
738739
pub struct TypeIndex {
739740
/// all types (structs, enums, type, POUs, etc.)
740741
types: SymbolMap<String, DataType>,

src/index/scoped_index.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::{collections::HashMap, rc::Rc};
2+
3+
use plc_source::source_location::SourceLocation;
4+
5+
use crate::typesystem::DataType;
6+
7+
use super::VariableIndexEntry;
8+
9+
/// A minimal index implementation that can be used for local scopes
10+
#[derive(Debug, Clone)]
11+
pub struct ScopedIndex {
12+
///The scope of the current index, this is usually a POU
13+
scope: String,
14+
15+
/// A unique identifier that new variables in this scope will inherit
16+
scope_suffix: usize,
17+
18+
/// The location that caused this scope to be created
19+
start_location: SourceLocation,
20+
21+
/// New variables defined by this index
22+
variables: HashMap<String, VariableIndexEntry>,
23+
24+
/// Datatypes defined by this index
25+
type_index: HashMap<String, DataType>,
26+
27+
parent : Option<Rc<ScopedIndex>>
28+
29+
}
30+
31+
impl ScopedIndex {
32+
pub fn merge_into(self, target: &mut Self) {
33+
target.variables.extend(self.variables);
34+
target.type_index.extend(self.type_index);
35+
}
36+
37+
pub fn add_variable(&mut self, name: &str) {
38+
39+
}
40+
41+
pub fn add_type(&mut self, name: &str) {
42+
43+
}
44+
45+
pub fn find_variable(&self, name: &str) -> Option<&VariableIndexEntry> {
46+
todo!()
47+
}
48+
49+
pub fn find_type(&self, name: &str) -> Option<&DataType> {
50+
todo!()
51+
}
52+
53+
pub fn new(parent: Option<Rc<ScopedIndex>>, location: SourceLocation, scope: &str, suffix: usize) -> ScopedIndex {
54+
ScopedIndex { scope: scope.to_string(), scope_suffix: suffix, start_location: location, parent, type_index: Default::default(), variables: Default::default() }
55+
}
56+
57+
}

src/index/symbol.rs

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ impl<K, V> Default for SymbolMap<K, V> {
1818
}
1919
}
2020

21+
impl<K, V> Clone for SymbolMap<K, V>
22+
where
23+
K: Clone,
24+
V: Clone,
25+
{
26+
fn clone(&self) -> Self {
27+
Self {
28+
inner_map: self.inner_map.clone()
29+
}
30+
}
31+
}
32+
2133
impl<K, V> SymbolMap<K, V>
2234
where
2335
K: Hash + Eq,

0 commit comments

Comments
 (0)