WIP associated function syntax for group::to_*_coordinate

This commit is contained in:
Pranav Gaddamadugu 2023-06-23 14:59:36 -04:00
parent 07d26ea43f
commit 9b7a4ba456
5 changed files with 38 additions and 0 deletions

View File

@ -175,6 +175,9 @@ pub enum CoreFunction {
MappingGet,
MappingGetOrUse,
MappingSet,
GroupToXCoordinate,
GroupToYCoordinate,
}
impl CoreFunction {
@ -338,6 +341,9 @@ impl CoreFunction {
(sym::Mapping, sym::get) => Self::MappingGet,
(sym::Mapping, sym::get_or_use) => Self::MappingGetOrUse,
(sym::Mapping, sym::set) => Self::MappingSet,
(sym::group, sym::to_x_coordinate) => Self::GroupToXCoordinate,
(sym::group, sym::to_y_coordinate) => Self::GroupToYCoordinate,
_ => return None,
})
}
@ -502,6 +508,9 @@ impl CoreFunction {
Self::MappingGet => 2,
Self::MappingGetOrUse => 3,
Self::MappingSet => 3,
Self::GroupToXCoordinate => 1,
Self::GroupToYCoordinate => 1,
}
}
}

View File

@ -357,6 +357,27 @@ impl<'a> CodeGenerator<'a> {
}
_ => unreachable!("The only variants of Mapping are get, get_or, and set"),
},
Type::Group => {
match input.name {
Identifier { name: sym::to_x_coordinate, .. } => {
let mut instruction = " cast".to_string();
let destination_register = get_destination_register();
// Write the argument and the destination register.
writeln!(instruction, " {} into {destination_register} as group.x;", arguments[0],)
.expect("failed to write to string");
(destination_register, instruction)
}
Identifier { name: sym::to_y_coordinate, .. } => {
let mut instruction = " cast".to_string();
let destination_register = get_destination_register();
// Write the argument and the destination register.
writeln!(instruction, " {} into {destination_register} as group.y;", arguments[0],)
.expect("failed to write to string");
(destination_register, instruction)
}
_ => unreachable!("The only associated methods of group are to_x_coordinate and to_y_coordinate"),
}
}
_ => unreachable!("All core functions should be known at this phase of compilation"),
};
// Add the instruction to the list of instructions.

View File

@ -861,6 +861,11 @@ impl<'a> TypeChecker<'a> {
None
}
}
CoreFunction::GroupToXCoordinate | CoreFunction::GroupToYCoordinate => {
// Check that the first argument is a group.
self.assert_scalar_type(&arguments[0].0, arguments[0].1);
Some(Type::Group)
}
}
}

View File

@ -15,6 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
#![forbid(unsafe_code)]
#![recursion_limit = "256"]
pub mod symbol;
pub use symbol::{sym, Symbol};

View File

@ -175,6 +175,8 @@ symbols! {
Poseidon4,
Poseidon8,
set,
to_x_coordinate,
to_y_coordinate,
// types
address,