big self canonicalization fixes

This commit is contained in:
gluaxspeed 2021-08-25 08:55:26 -07:00
parent 7a5979660b
commit a5b6780c3b
4 changed files with 68 additions and 21 deletions

View File

@ -125,6 +125,19 @@ impl Canonicalizer {
} }
} }
fn canonicalize_circuit_implied_variable_definition(
&mut self,
member: &CircuitImpliedVariableDefinition,
) -> CircuitImpliedVariableDefinition {
CircuitImpliedVariableDefinition {
identifier: member.identifier.clone(),
expression: member
.expression
.as_ref()
.map(|expr| self.canonicalize_expression(expr)),
}
}
fn canonicalize_expression(&mut self, expression: &Expression) -> Expression { fn canonicalize_expression(&mut self, expression: &Expression) -> Expression {
match expression { match expression {
Expression::Unary(unary) => { Expression::Unary(unary) => {
@ -262,7 +275,11 @@ impl Canonicalizer {
return Expression::CircuitInit(CircuitInitExpression { return Expression::CircuitInit(CircuitInitExpression {
name, name,
members: circuit_init.members.clone(), members: circuit_init
.members
.iter()
.map(|member| self.canonicalize_circuit_implied_variable_definition(member))
.collect(),
span: circuit_init.span.clone(), span: circuit_init.span.clone(),
}); });
} }
@ -284,7 +301,11 @@ impl Canonicalizer {
Expression::Call(call) => { Expression::Call(call) => {
return Expression::Call(CallExpression { return Expression::Call(CallExpression {
function: Box::new(self.canonicalize_expression(&call.function)), function: Box::new(self.canonicalize_expression(&call.function)),
arguments: call.arguments.clone(), arguments: call
.arguments
.iter()
.map(|arg| self.canonicalize_expression(dbg!(arg)))
.collect(),
span: call.span.clone(), span: call.span.clone(),
}); });
} }
@ -439,11 +460,31 @@ impl Canonicalizer {
} }
} }
fn canonicalize_function_input(&mut self, input: &FunctionInput) -> FunctionInput {
if let FunctionInput::Variable(variable) = input {
if variable.type_.is_self() {
return FunctionInput::Variable(FunctionInputVariable {
identifier: variable.identifier.clone(),
const_: variable.const_,
mutable: variable.mutable,
type_: Type::Circuit(self.circuit_name.as_ref().unwrap().clone()),
span: variable.span.clone(),
});
}
}
input.clone()
}
fn canonicalize_circuit_member(&mut self, circuit_member: &CircuitMember) -> CircuitMember { fn canonicalize_circuit_member(&mut self, circuit_member: &CircuitMember) -> 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
.iter()
.map(|input| self.canonicalize_function_input(input))
.collect();
let output = self.canonicalize_self_type(function.output.as_ref()); let output = self.canonicalize_self_type(function.output.as_ref());
let block = self.canonicalize_block(&function.block); let block = self.canonicalize_block(&function.block);

View File

@ -107,7 +107,7 @@ fn run_with_args(opt: Opt) -> Result<(), Box<dyn Error>> {
cwd.pop(); cwd.pop();
cwd.join(&val.as_str().unwrap()) cwd.join(&val.as_str().unwrap())
}) })
.unwrap_or(PathBuf::from(path)); .unwrap_or_else(|| PathBuf::from(path));
// Write all files into the directory. // Write all files into the directory.
let (initial, canonicalized, type_inferenced) = generate_asts(cwd, text)?; let (initial, canonicalized, type_inferenced) = generate_asts(cwd, text)?;
@ -129,7 +129,7 @@ fn run_with_args(opt: Opt) -> Result<(), Box<dyn Error>> {
} }
/// Do what Compiler does - prepare 3 stages of AST: initial, canonicalized and type_inferenced /// Do what Compiler does - prepare 3 stages of AST: initial, canonicalized and type_inferenced
fn generate_asts(path: PathBuf, text: &String) -> Result<(String, String, String), Box<dyn Error>> { fn generate_asts(path: PathBuf, text: &str) -> Result<(String, String, String), Box<dyn Error>> {
let mut ast = leo_parser::parse_ast(path.clone().into_os_string().into_string().unwrap(), text)?; let mut ast = leo_parser::parse_ast(path.clone().into_os_string().into_string().unwrap(), text)?;
let initial = ast.to_json_string()?; let initial = ast.to_json_string()?;

View File

@ -5,23 +5,29 @@ input_file: input/dummy.in
*/ */
circuit Foo { circuit Foo {
x: u32; x: u32;
function new() -> Self { function new() -> Self {
let new: Self = Self { let new: Self = Self {
x: 1u32 x: 1u32
}; };
return new; return new;
} }
function etc() { function etc() {
let y = [0u32, 1, 2, 3]; let y = [0u32, 1, 2, 3];
y[Self {x: 0}.x] += 2; y[Self {x: 0}.x] += 2;
} }
function func() {
const x: Self = Foo {x: Self {x: 1}.x};
}
function self_circ(x: Self) {}
} }
function main(y: bool) -> bool { function main(y: bool) -> bool {
let foo: Foo = Foo::new(); let foo: Foo = Foo::new();
return foo.x == 1u32 && y; return foo.x == 1u32 && y;
} }

View File

@ -16,6 +16,6 @@ outputs:
r0: r0:
type: bool type: bool
value: "true" value: "true"
initial_ast: bc330763338677f23601b03f5665bd8f42f8143f59cc9b4803fb693b3cfa0311 initial_ast: f5211a78cc2ceca171cc594466a95d12e9240d552dbdb0c65ea718eb07a756fc
canonicalized_ast: fbff610ef772ee7f997b4bc4cd7c2a3f2024d70af35b94a966ca6a0f19f15194 canonicalized_ast: 787dffcc703941dd35266c066dc5fdf5d15490ccc60c95ade650366463390239
type_inferenced_ast: f6b0159f6bffeff8e3cde7f13c97ac5d537b40855271a4a13d07a84d24d78504 type_inferenced_ast: 96dba011f40d5e05148f41d78de45801277a54fea727fc476b5d8cefae6e3679