start if_else
This commit is contained in:
parent
0951d7d14c
commit
092f74b2f8
5 changed files with 71 additions and 28 deletions
|
@ -80,6 +80,17 @@ mod integration {
|
||||||
// create a tmp folder to store artifacts
|
// create a tmp folder to store artifacts
|
||||||
fs::create_dir(test_case_path).unwrap();
|
fs::create_dir(test_case_path).unwrap();
|
||||||
|
|
||||||
|
// prepare compile arguments
|
||||||
|
let check = vec![
|
||||||
|
"../target/release/zokrates",
|
||||||
|
"check",
|
||||||
|
"-i",
|
||||||
|
program_path.to_str().unwrap(),
|
||||||
|
];
|
||||||
|
|
||||||
|
// check
|
||||||
|
assert_cli::Assert::command(&check).succeeds().unwrap();
|
||||||
|
|
||||||
// prepare compile arguments
|
// prepare compile arguments
|
||||||
let compile = vec![
|
let compile = vec![
|
||||||
"../target/release/zokrates",
|
"../target/release/zokrates",
|
||||||
|
@ -130,7 +141,7 @@ mod integration {
|
||||||
|
|
||||||
let abi: Abi = from_reader(&mut reader)
|
let abi: Abi = from_reader(&mut reader)
|
||||||
.map_err(|why| why.to_string())
|
.map_err(|why| why.to_string())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let signature = abi.signature().clone();
|
let signature = abi.signature().clone();
|
||||||
|
|
||||||
|
|
|
@ -29,15 +29,15 @@ pub struct Flattener<'ast, T: Field> {
|
||||||
}
|
}
|
||||||
|
|
||||||
trait FlattenOutput<T: Field>: Sized {
|
trait FlattenOutput<T: Field>: Sized {
|
||||||
fn branches(self, other: Self) -> (Self, Self);
|
// fn branches(self, other: Self) -> (Self, Self);
|
||||||
|
|
||||||
fn flat(&self) -> Vec<FlatExpression<T>>;
|
fn flat(&self) -> Vec<FlatExpression<T>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Field> FlattenOutput<T> for FlatExpression<T> {
|
impl<T: Field> FlattenOutput<T> for FlatExpression<T> {
|
||||||
fn branches(self, other: Self) -> (Self, Self) {
|
// fn branches(self, other: Self) -> (Self, Self) {
|
||||||
(self, other)
|
// (self, other)
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn flat(&self) -> Vec<FlatExpression<T>> {
|
fn flat(&self) -> Vec<FlatExpression<T>> {
|
||||||
vec![self.clone()]
|
vec![self.clone()]
|
||||||
|
@ -45,31 +45,31 @@ impl<T: Field> FlattenOutput<T> for FlatExpression<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Field> FlattenOutput<T> for FlatUExpression<T> {
|
impl<T: Field> FlattenOutput<T> for FlatUExpression<T> {
|
||||||
fn branches(self, other: Self) -> (Self, Self) {
|
// fn branches(self, other: Self) -> (Self, Self) {
|
||||||
let left_bits = self.bits.unwrap();
|
// let left_bits = self.bits.unwrap();
|
||||||
let right_bits = other.bits.unwrap();
|
// let right_bits = other.bits.unwrap();
|
||||||
let size = std::cmp::max(left_bits.len(), right_bits.len());
|
// let size = std::cmp::max(left_bits.len(), right_bits.len());
|
||||||
|
|
||||||
let left_bits = (0..size - left_bits.len())
|
// let left_bits = (0..size - left_bits.len())
|
||||||
.map(|_| FlatExpression::Number(T::from(0)))
|
// .map(|_| FlatExpression::Number(T::from(0)))
|
||||||
.chain(left_bits)
|
// .chain(left_bits)
|
||||||
.collect();
|
// .collect();
|
||||||
let right_bits = (0..size - right_bits.len())
|
// let right_bits = (0..size - right_bits.len())
|
||||||
.map(|_| FlatExpression::Number(T::from(0)))
|
// .map(|_| FlatExpression::Number(T::from(0)))
|
||||||
.chain(right_bits)
|
// .chain(right_bits)
|
||||||
.collect();
|
// .collect();
|
||||||
|
|
||||||
(
|
// (
|
||||||
FlatUExpression {
|
// FlatUExpression {
|
||||||
bits: Some(left_bits),
|
// bits: Some(left_bits),
|
||||||
..self
|
// ..self
|
||||||
},
|
// },
|
||||||
FlatUExpression {
|
// FlatUExpression {
|
||||||
bits: Some(right_bits),
|
// bits: Some(right_bits),
|
||||||
..other
|
// ..other
|
||||||
},
|
// },
|
||||||
)
|
// )
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn flat(&self) -> Vec<FlatExpression<T>> {
|
fn flat(&self) -> Vec<FlatExpression<T>> {
|
||||||
self.bits
|
self.bits
|
||||||
|
|
|
@ -1366,6 +1366,10 @@ impl<'ast> Checker<'ast> {
|
||||||
unimplemented!("handle consequence alternative inner type mismatch")
|
unimplemented!("handle consequence alternative inner type mismatch")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
(TypedExpression::Uint(consequence), TypedExpression::Uint(alternative)) => {
|
||||||
|
let bitwidth = consequence.bitwidth();
|
||||||
|
Ok(UExpressionInner::IfElse(box condition, box consequence, box alternative).annotate(bitwidth).into())
|
||||||
|
},
|
||||||
_ => unreachable!("types should match here as we checked them explicitly")
|
_ => unreachable!("types should match here as we checked them explicitly")
|
||||||
}
|
}
|
||||||
false => Err(ErrorInner {
|
false => Err(ErrorInner {
|
||||||
|
|
26
zokrates_core_test/tests/tests/uint/if_else.json
Normal file
26
zokrates_core_test/tests/tests/uint/if_else.json
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
"entry_point": "./tests/tests/uint/if_else.zok",
|
||||||
|
"max_constraint_count": 28,
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"values": ["1", "0x00", "0xff"]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"Ok": {
|
||||||
|
"values": ["0xff"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"values": ["0", "0x00", "0xff"]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"Ok": {
|
||||||
|
"values": ["0x00"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
2
zokrates_core_test/tests/tests/uint/if_else.zok
Normal file
2
zokrates_core_test/tests/tests/uint/if_else.zok
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
def main(bool condition, u8 consequence, u8 alternative) -> (u8):
|
||||||
|
return if condition then consequence else alternative fi
|
Loading…
Reference in a new issue