Remove SymbolTable functionality associated with flattening

This commit is contained in:
Pranav Gaddamadugu 2022-07-01 20:01:39 -07:00
parent 24f38721d6
commit bd1b8251c2
3 changed files with 21 additions and 103 deletions

View File

@ -22,7 +22,7 @@ use crate::SymbolTable;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct FunctionSymbol { pub struct FunctionSymbol {
pub(crate) id: usize, pub(crate) id: usize,
pub(crate) type_: Type, pub(crate) output: Type,
pub(crate) span: Span, pub(crate) span: Span,
pub(crate) input: Vec<FunctionInput>, pub(crate) input: Vec<FunctionInput>,
} }
@ -31,7 +31,7 @@ impl SymbolTable {
pub(crate) fn new_function_symbol(id: usize, func: &Function) -> FunctionSymbol { pub(crate) fn new_function_symbol(id: usize, func: &Function) -> FunctionSymbol {
FunctionSymbol { FunctionSymbol {
id, id,
type_: func.output, output: func.output,
span: func.span, span: func.span,
input: func.input.clone(), input: func.input.clone(),
} }

View File

@ -22,7 +22,7 @@ use leo_span::{Span, Symbol};
use indexmap::IndexMap; use indexmap::IndexMap;
use crate::{Declaration, FunctionSymbol, Value, VariableSymbol}; use crate::{FunctionSymbol, VariableSymbol};
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct SymbolTable { pub struct SymbolTable {
@ -40,8 +40,6 @@ pub struct SymbolTable {
pub(crate) variables: IndexMap<Symbol, VariableSymbol>, pub(crate) variables: IndexMap<Symbol, VariableSymbol>,
/// The index of the current scope /// The index of the current scope
pub(crate) scope_index: usize, pub(crate) scope_index: usize,
/// If the block will always be executed when the parent block is executed
pub(crate) is_locally_non_const: bool,
/// The subscopes of this scope /// The subscopes of this scope
pub(crate) scopes: Vec<RefCell<SymbolTable>>, pub(crate) scopes: Vec<RefCell<SymbolTable>>,
} }
@ -90,11 +88,8 @@ impl SymbolTable {
Ok(()) Ok(())
} }
pub fn insert_block(&mut self, is_locally_non_const: bool) -> usize { pub fn insert_block(&mut self) -> usize {
self.scopes.push(RefCell::new(SymbolTable { self.scopes.push(RefCell::new(Default::default()));
is_locally_non_const,
..Default::default()
}));
self.scope_index() self.scope_index()
} }
@ -156,62 +151,6 @@ impl SymbolTable {
} }
} }
/// Finds the variable in the parent scope, then SHADOWS it in most recent non-const scope with a mut const value.
/// returns a boolean for if the variable should be slated for deconstification
pub fn locally_constify_variable(&mut self, symbol: Symbol, value: Value) -> bool {
let mut var = self
.lookup_variable(&symbol)
.unwrap_or_else(|| panic!("attempting to constify non-existent variable `{symbol}`"))
.clone();
var.declaration = Declaration::Mut(Some(value));
let mut st = self;
loop {
let is_in_parent = st.variable_in_parent_scope(&symbol);
let is_in_local = st.variable_in_local_scope(&symbol);
if is_in_local && is_in_parent {
st.variables.insert(symbol, var.clone());
} else if st.is_locally_non_const && !is_in_parent {
st.variables.insert(symbol, var);
break false;
} else if st.is_locally_non_const && is_in_parent {
st.variables.insert(symbol, var);
break true;
}
if !st.is_locally_non_const && st.parent.is_some() && is_in_parent {
st = st.parent.as_mut().unwrap()
} else {
break true;
}
}
}
pub fn set_variable(&mut self, symbol: &Symbol, value: Value) -> bool {
if let Some(var) = self.variables.get_mut(symbol) {
var.declaration = match &var.declaration {
Declaration::Const(_) => Declaration::Const(Some(value)),
Declaration::Mut(_) => Declaration::Mut(Some(value)),
other => other.clone(),
};
true
} else if let Some(parent) = &mut self.parent {
parent.set_variable(symbol, value)
} else {
false
}
}
/// Finds all previous occurrences of the variable and replaces it with a non-const mutable value
pub fn deconstify_variable(&mut self, symbol: &Symbol) {
if let Some(var) = self.variables.get_mut(symbol) {
var.declaration = Declaration::Mut(None);
}
if let Some(parent) = &mut self.parent {
parent.deconstify_variable(symbol)
}
}
pub fn get_fn_scope(&self, symbol: &Symbol) -> Option<&RefCell<Self>> { pub fn get_fn_scope(&self, symbol: &Symbol) -> Option<&RefCell<Self>> {
if let Some(func) = self.functions.get(symbol) { if let Some(func) = self.functions.get(symbol) {
self.scopes.get(func.id) self.scopes.get(func.id)

View File

@ -14,61 +14,40 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use leo_ast::{Identifier, ParamMode, Type}; use std::fmt::Display;
use leo_errors::Result;
use leo_span::Span;
use crate::Value; use leo_ast::{ParamMode, Type};
use leo_span::Span;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum Declaration { pub enum Declaration {
Const(Option<Value>), Const,
Input(Type, ParamMode), Input(ParamMode),
Mut(Option<Value>), Mut,
} }
impl Declaration { impl Display for Declaration {
pub fn get_as_u128(&self) -> Result<Option<u128>> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Declaration::*; use Declaration::*;
match self { match self {
Const(Some(value)) => Ok(Some(value.try_into()?)), Const => write!(f, "const var"),
Input(_, _) => Ok(None), Input(m) => write!(f, "{m} input"),
_ => Ok(None), Mut => write!(f, "mut var"),
}
}
pub fn get_type(&self) -> Option<Type> {
use Declaration::*;
match self {
Const(Some(value)) => Some(value.into()),
Input(type_, _) => Some(*type_),
_ => None,
} }
} }
} }
impl AsRef<Self> for Declaration { #[derive(Clone, Debug, Eq, PartialEq)]
fn as_ref(&self) -> &Self {
self
}
}
#[derive(Clone, Debug)]
pub struct VariableSymbol { pub struct VariableSymbol {
pub type_: Type, pub type_: Type,
pub span: Span, pub span: Span,
pub declaration: Declaration, pub declaration: Declaration,
} }
impl VariableSymbol { impl Display for VariableSymbol {
pub fn get_const_value(&self, ident: Identifier) -> Option<Value> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Declaration::*; write!(f, "{}: {}", self.declaration, self.type_)?;
match &self.declaration { Ok(())
Const(Some(v)) | Mut(Some(v)) => Some(v.clone()),
Input(type_, ParamMode::Const) => Some(Value::Input(*type_, ident)),
_ => None,
}
} }
} }