Merge pull request #54 from chriseth/strings_and_tuples

Support strings and tuples of expressions.
This commit is contained in:
chriseth
2023-02-26 01:10:28 +01:00
committed by GitHub
9 changed files with 31 additions and 0 deletions

View File

@@ -150,6 +150,8 @@ pub enum Expression {
LocalVariableReference(u64),
PublicReference(String),
Number(ConstantNumberType),
String(String),
Tuple(Vec<Expression>),
BinaryOperation(Box<Expression>, BinaryOperator, Box<Expression>),
UnaryOperation(UnaryOperator, Box<Expression>),
/// Call to a non-macro function (like a constant polynomial)

View File

@@ -432,6 +432,8 @@ impl PILContext {
}
ast::Expression::PublicReference(name) => Expression::PublicReference(name.clone()),
ast::Expression::Number(n) => Expression::Number(*n),
ast::Expression::String(value) => Expression::String(value.clone()),
ast::Expression::Tuple(items) => Expression::Tuple(self.process_expressions(items)),
ast::Expression::BinaryOperation(left, op, right) => {
if let Some(value) = self.evaluate_binary_operation(left, op, right) {
Expression::Number(value)
@@ -516,6 +518,8 @@ impl PILContext {
ast::Expression::PolynomialReference(_) => None,
ast::Expression::PublicReference(_) => None,
ast::Expression::Number(n) => Some(*n),
ast::Expression::String(_) => None,
ast::Expression::Tuple(_) => None,
ast::Expression::BinaryOperation(left, op, right) => {
self.evaluate_binary_operation(left, op, right)
}

View File

@@ -239,6 +239,8 @@ impl ASMPILConverter {
)]
}
Expression::Number(value) => vec![(*value, AffineExpressionComponent::Constant)],
Expression::String(_) => panic!(),
Expression::Tuple(_) => panic!(),
Expression::FreeInput(expr) => {
vec![(1, AffineExpressionComponent::FreeInput(*expr.clone()))]
}
@@ -560,9 +562,13 @@ fn substitute(input: &Expression, substitution: &HashMap<String, String>) -> Exp
name.clone(),
args.iter().map(|e| substitute(e, substitution)).collect(),
),
Expression::Tuple(items) => {
Expression::Tuple(items.iter().map(|e| substitute(e, substitution)).collect())
}
Expression::Constant(_)
| Expression::PublicReference(_)
| Expression::Number(_)
| Expression::String(_)
| Expression::FreeInput(_) => input.clone(),
}
}

View File

@@ -155,6 +155,8 @@ impl<'a> Evaluator<'a> {
self.evaluate_binary_operation(left, op, right)
}
Expression::UnaryOperation(op, expr) => self.evaluate_unary_operation(op, expr),
Expression::Tuple(_) => panic!(),
Expression::String(_) => panic!(),
Expression::LocalVariableReference(_) => panic!(),
Expression::PublicReference(_) => panic!(),
Expression::FunctionCall(_, _) => panic!(),

View File

@@ -80,6 +80,8 @@ impl<'a> Evaluator<'a> {
Expression::LocalVariableReference(i) => self.variables[*i as usize],
Expression::PublicReference(_) => todo!(),
Expression::Number(n) => *n,
Expression::String(_) => panic!(),
Expression::Tuple(_) => panic!(),
Expression::BinaryOperation(left, op, right) => {
self.evaluate_binary_operation(left, op, right)
}

View File

@@ -303,6 +303,8 @@ impl<'a> Exporter<'a> {
Expression::FunctionCall(_, _) => {
panic!("No function calls allowed here.")
}
Expression::String(_) => panic!("Strings not allowed here."),
Expression::Tuple(_) => panic!("Tuples not allowed here"),
}
}

View File

@@ -43,6 +43,8 @@ pub enum Expression {
PolynomialReference(PolynomialReference),
PublicReference(String),
Number(ConstantNumberType),
String(String),
Tuple(Vec<Expression>),
BinaryOperation(Box<Expression>, BinaryOperator, Box<Expression>),
UnaryOperation(UnaryOperator, Box<Expression>),
FunctionCall(String, Vec<Expression>),

View File

@@ -122,6 +122,8 @@ impl Display for Expression {
Expression::PolynomialReference(reference) => write!(f, "{reference}"),
Expression::PublicReference(name) => write!(f, "{name}"),
Expression::Number(value) => write!(f, "{value}"),
Expression::String(value) => write!(f, "\"{value}\""), // TODO quote?
Expression::Tuple(items) => write!(f, "({})", format_expressions(items)),
Expression::BinaryOperation(left, op, right) => write!(f, "({left} {op} {right})"),
Expression::UnaryOperation(op, exp) => write!(f, "{op}{exp}"),
Expression::FunctionCall(fun, args) => write!(f, "{fun}({})", format_expressions(args)),
@@ -227,4 +229,11 @@ public out = y(%last_row);"#;
let printed = format!("{}", parser::parse(Some("input"), input).unwrap());
assert_eq!(input.trim(), printed.trim());
}
#[test]
fn reparse_strings_and_tuples() {
let input = r#"constant %N = ("abc", 3);"#;
let printed = format!("{}", parser::parse(Some("input"), input).unwrap());
assert_eq!(input.trim(), printed.trim());
}
}

View File

@@ -290,6 +290,8 @@ Term: Box<Expression> = {
PolynomialReference => Box::new(Expression::PolynomialReference(<>)),
PublicReference => Box::new(Expression::PublicReference(<>)),
Number => Box::new(Expression::Number(<>)),
StringLiteral => Box::new(Expression::String(<>)),
"(" <head:Expression> "," <tail:ExpressionList> ")" => { let mut list = vec![head]; list.extend(tail); Box::new(Expression::Tuple(list)) },
"(" <BoxedExpression> ")",
"${" <BoxedExpression> "}" => Box::new(Expression::FreeInput(<>))
}