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
|
||||
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
|
||||
let compile = vec![
|
||||
"../target/release/zokrates",
|
||||
|
@ -130,7 +141,7 @@ mod integration {
|
|||
|
||||
let abi: Abi = from_reader(&mut reader)
|
||||
.map_err(|why| why.to_string())
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
|
||||
let signature = abi.signature().clone();
|
||||
|
||||
|
|
|
@ -29,15 +29,15 @@ pub struct Flattener<'ast, T: Field> {
|
|||
}
|
||||
|
||||
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>>;
|
||||
}
|
||||
|
||||
impl<T: Field> FlattenOutput<T> for FlatExpression<T> {
|
||||
fn branches(self, other: Self) -> (Self, Self) {
|
||||
(self, other)
|
||||
}
|
||||
// fn branches(self, other: Self) -> (Self, Self) {
|
||||
// (self, other)
|
||||
// }
|
||||
|
||||
fn flat(&self) -> Vec<FlatExpression<T>> {
|
||||
vec![self.clone()]
|
||||
|
@ -45,31 +45,31 @@ impl<T: Field> FlattenOutput<T> for FlatExpression<T> {
|
|||
}
|
||||
|
||||
impl<T: Field> FlattenOutput<T> for FlatUExpression<T> {
|
||||
fn branches(self, other: Self) -> (Self, Self) {
|
||||
let left_bits = self.bits.unwrap();
|
||||
let right_bits = other.bits.unwrap();
|
||||
let size = std::cmp::max(left_bits.len(), right_bits.len());
|
||||
// fn branches(self, other: Self) -> (Self, Self) {
|
||||
// let left_bits = self.bits.unwrap();
|
||||
// let right_bits = other.bits.unwrap();
|
||||
// let size = std::cmp::max(left_bits.len(), right_bits.len());
|
||||
|
||||
let left_bits = (0..size - left_bits.len())
|
||||
.map(|_| FlatExpression::Number(T::from(0)))
|
||||
.chain(left_bits)
|
||||
.collect();
|
||||
let right_bits = (0..size - right_bits.len())
|
||||
.map(|_| FlatExpression::Number(T::from(0)))
|
||||
.chain(right_bits)
|
||||
.collect();
|
||||
// let left_bits = (0..size - left_bits.len())
|
||||
// .map(|_| FlatExpression::Number(T::from(0)))
|
||||
// .chain(left_bits)
|
||||
// .collect();
|
||||
// let right_bits = (0..size - right_bits.len())
|
||||
// .map(|_| FlatExpression::Number(T::from(0)))
|
||||
// .chain(right_bits)
|
||||
// .collect();
|
||||
|
||||
(
|
||||
FlatUExpression {
|
||||
bits: Some(left_bits),
|
||||
..self
|
||||
},
|
||||
FlatUExpression {
|
||||
bits: Some(right_bits),
|
||||
..other
|
||||
},
|
||||
)
|
||||
}
|
||||
// (
|
||||
// FlatUExpression {
|
||||
// bits: Some(left_bits),
|
||||
// ..self
|
||||
// },
|
||||
// FlatUExpression {
|
||||
// bits: Some(right_bits),
|
||||
// ..other
|
||||
// },
|
||||
// )
|
||||
// }
|
||||
|
||||
fn flat(&self) -> Vec<FlatExpression<T>> {
|
||||
self.bits
|
||||
|
|
|
@ -1366,6 +1366,10 @@ impl<'ast> Checker<'ast> {
|
|||
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")
|
||||
}
|
||||
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