there could be more instances where expressions might need to be matched

This commit is contained in:
gluax 2021-03-10 13:27:06 -05:00
parent 8e0ebd386a
commit 476307fffc

View File

@ -19,30 +19,36 @@
use crate::{ use crate::{
reducer::ReconstructingReducer, reducer::ReconstructingReducer,
Block,
CallExpression,
Circuit, Circuit,
CircuitInitExpression,
CircuitMember, CircuitMember,
CircuitMemberAccessExpression,
CircuitStaticFunctionAccessExpression,
DefinitionStatement,
Expression,
ExpressionStatement,
Function, Function,
FunctionInput, FunctionInput,
FunctionInputVariable, FunctionInputVariable,
Identifier, Identifier,
ReturnStatement,
Statement,
Type, Type,
}; };
pub struct Canonicalizer; pub struct Canonicalizer;
impl Canonicalizer { impl Canonicalizer {
fn is_self(&self, identifier: &Identifier) -> bool { fn _is_self(&self, identifier: &Identifier) -> bool {
match identifier.name.as_str() { matches!(identifier.name.as_str(), "Self")
"Self" => true,
_ => false,
}
} }
fn is_self_keyword(&self, function_inputs: &Vec<FunctionInput>) -> bool { fn _is_self_keyword(&self, function_inputs: &[FunctionInput]) -> bool {
for function_input in function_inputs { for function_input in function_inputs {
match function_input { if let FunctionInput::SelfKeyword(_) = function_input {
FunctionInput::SelfKeyword(_) => return true, return true;
_ => {}
} }
} }
@ -50,16 +56,10 @@ impl Canonicalizer {
} }
fn is_self_type(&self, type_option: Option<&Type>) -> bool { fn is_self_type(&self, type_option: Option<&Type>) -> bool {
match type_option { matches!(type_option, Some(Type::SelfType))
Some(type_) => match type_ {
Type::SelfType => true,
_ => false,
},
None => false,
}
} }
fn canonicalize_function_input(&self, function_input: &FunctionInput, circuit_name: &Identifier) -> FunctionInput { fn _canonicalize_function_input(&self, function_input: &FunctionInput, circuit_name: &Identifier) -> FunctionInput {
match function_input { match function_input {
FunctionInput::SelfKeyword(self_keyword) => { FunctionInput::SelfKeyword(self_keyword) => {
return FunctionInput::Variable(FunctionInputVariable { return FunctionInput::Variable(FunctionInputVariable {
@ -85,12 +85,104 @@ impl Canonicalizer {
function_input.clone() function_input.clone()
} }
fn canonicalize_expression(&self, expression: &Expression, circuit_name: &Identifier) -> Expression {
match expression {
Expression::CircuitInit(circuit_init) => {
return Expression::CircuitInit(CircuitInitExpression {
name: circuit_name.clone(),
members: circuit_init.members.clone(),
span: circuit_init.span.clone(),
});
}
Expression::CircuitMemberAccess(circuit_member_access) => {
return Expression::CircuitMemberAccess(CircuitMemberAccessExpression {
circuit: Box::new(self.canonicalize_expression(&circuit_member_access.circuit, circuit_name)),
name: circuit_member_access.name.clone(),
span: circuit_member_access.span.clone(),
});
}
Expression::CircuitStaticFunctionAccess(circuit_static_func_access) => {
return Expression::CircuitStaticFunctionAccess(CircuitStaticFunctionAccessExpression {
circuit: Box::new(self.canonicalize_expression(&circuit_static_func_access.circuit, circuit_name)),
name: circuit_static_func_access.name.clone(),
span: circuit_static_func_access.span.clone(),
});
}
Expression::Call(call) => {
return Expression::Call(CallExpression {
function: Box::new(self.canonicalize_expression(&call.function, circuit_name)),
arguments: call.arguments.clone(),
span: call.span.clone(),
});
}
_ => {}
}
expression.clone()
}
fn canonicalize_statement(&self, statement: &Statement, circuit_name: &Identifier) -> Statement {
// What Statements could have Self appear
match statement {
Statement::Return(return_statement) => {
return Statement::Return(ReturnStatement {
expression: self.canonicalize_expression(&return_statement.expression, circuit_name),
span: return_statement.span.clone(),
});
}
Statement::Definition(definition) => {
let mut type_ = definition.type_.clone();
if self.is_self_type(type_.as_ref()) {
type_ = Some(Type::Circuit(circuit_name.clone()));
}
return Statement::Definition(DefinitionStatement {
declaration_type: definition.declaration_type.clone(),
variable_names: definition.variable_names.clone(),
type_,
value: self.canonicalize_expression(&definition.value, circuit_name),
span: definition.span.clone(),
});
}
Statement::Expression(expression) => {
return Statement::Expression(ExpressionStatement {
expression: self.canonicalize_expression(&expression.expression, circuit_name),
span: expression.span.clone(),
});
}
Statement::Block(block) => {
return Statement::Block(Block {
statements: block
.statements
.iter()
.map(|block_statement| self.canonicalize_statement(&block_statement, circuit_name))
.collect(),
span: block.span.clone(),
});
}
_ => {}
}
statement.clone()
}
fn canonicalize_circuit_member(&self, circuit_member: &CircuitMember, circuit_name: &Identifier) -> CircuitMember { fn canonicalize_circuit_member(&self, circuit_member: &CircuitMember, circuit_name: &Identifier) -> CircuitMember {
match circuit_member { match circuit_member {
CircuitMember::CircuitVariable(_, _) => {} CircuitMember::CircuitVariable(_, _) => {}
CircuitMember::CircuitFunction(function) => { CircuitMember::CircuitFunction(function) => {
let input = function.input.clone(); let input = function.input.clone();
let mut output = function.output.clone(); let mut output = function.output.clone();
let block = Block {
statements: function
.block
.statements
.iter()
.map(|statement| self.canonicalize_statement(statement, circuit_name))
.collect(),
span: function.block.span.clone(),
};
// probably shouldn't do this its self not Self // probably shouldn't do this its self not Self
// if self.is_self_keyword(&input) { // if self.is_self_keyword(&input) {
@ -109,7 +201,7 @@ impl Canonicalizer {
identifier: function.identifier.clone(), identifier: function.identifier.clone(),
input, input,
output, output,
block: function.block.clone(), block,
span: function.span.clone(), span: function.span.clone(),
}); });
} }