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)]
pub struct FunctionSymbol {
pub(crate) id: usize,
pub(crate) type_: Type,
pub(crate) output: Type,
pub(crate) span: Span,
pub(crate) input: Vec<FunctionInput>,
}
@ -31,7 +31,7 @@ impl SymbolTable {
pub(crate) fn new_function_symbol(id: usize, func: &Function) -> FunctionSymbol {
FunctionSymbol {
id,
type_: func.output,
output: func.output,
span: func.span,
input: func.input.clone(),
}

View File

@ -22,7 +22,7 @@ use leo_span::{Span, Symbol};
use indexmap::IndexMap;
use crate::{Declaration, FunctionSymbol, Value, VariableSymbol};
use crate::{FunctionSymbol, VariableSymbol};
#[derive(Clone, Debug, Default)]
pub struct SymbolTable {
@ -40,8 +40,6 @@ pub struct SymbolTable {
pub(crate) variables: IndexMap<Symbol, VariableSymbol>,
/// The index of the current scope
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
pub(crate) scopes: Vec<RefCell<SymbolTable>>,
}
@ -90,11 +88,8 @@ impl SymbolTable {
Ok(())
}
pub fn insert_block(&mut self, is_locally_non_const: bool) -> usize {
self.scopes.push(RefCell::new(SymbolTable {
is_locally_non_const,
..Default::default()
}));
pub fn insert_block(&mut self) -> usize {
self.scopes.push(RefCell::new(Default::default()));
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>> {
if let Some(func) = self.functions.get(symbol) {
self.scopes.get(func.id)

View File

@ -14,61 +14,40 @@
// 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/>.
use leo_ast::{Identifier, ParamMode, Type};
use leo_errors::Result;
use leo_span::Span;
use std::fmt::Display;
use crate::Value;
use leo_ast::{ParamMode, Type};
use leo_span::Span;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Declaration {
Const(Option<Value>),
Input(Type, ParamMode),
Mut(Option<Value>),
Const,
Input(ParamMode),
Mut,
}
impl Declaration {
pub fn get_as_u128(&self) -> Result<Option<u128>> {
impl Display for Declaration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Declaration::*;
match self {
Const(Some(value)) => Ok(Some(value.try_into()?)),
Input(_, _) => Ok(None),
_ => Ok(None),
}
}
pub fn get_type(&self) -> Option<Type> {
use Declaration::*;
match self {
Const(Some(value)) => Some(value.into()),
Input(type_, _) => Some(*type_),
_ => None,
Const => write!(f, "const var"),
Input(m) => write!(f, "{m} input"),
Mut => write!(f, "mut var"),
}
}
}
impl AsRef<Self> for Declaration {
fn as_ref(&self) -> &Self {
self
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VariableSymbol {
pub type_: Type,
pub span: Span,
pub declaration: Declaration,
}
impl VariableSymbol {
pub fn get_const_value(&self, ident: Identifier) -> Option<Value> {
use Declaration::*;
match &self.declaration {
Const(Some(v)) | Mut(Some(v)) => Some(v.clone()),
Input(type_, ParamMode::Const) => Some(Value::Input(*type_, ident)),
_ => None,
}
impl Display for VariableSymbol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.declaration, self.type_)?;
Ok(())
}
}