diff --git a/run.sh b/run.sh new file mode 100755 index 000000000..8ec50f923 --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +scarb build +cd runner +cargo run -- ../target/dev/cairo_verifier.sierra < resources/parserin.txt diff --git a/runner/Cargo.toml b/runner/Cargo.toml index 19b05eaed..707a80401 100644 --- a/runner/Cargo.toml +++ b/runner/Cargo.toml @@ -7,6 +7,11 @@ edition = "2021" [dependencies] anyhow = "1.0.76" +cairo-args-runner = "0.1.1" clap = { version = "4.4.11", features = ["derive"] } +lalrpop-util = { version = "0.20.0", features = ["lexer", "unicode"] } serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.108" + +[build-dependencies] +lalrpop = "0.20.0" diff --git a/runner/build.rs b/runner/build.rs new file mode 100644 index 000000000..ca5c2836d --- /dev/null +++ b/runner/build.rs @@ -0,0 +1,3 @@ +fn main() { + lalrpop::process_root().unwrap(); +} diff --git a/runner/resources/parserin.txt b/runner/resources/parserin.txt new file mode 100644 index 000000000..82f6a0c2d --- /dev/null +++ b/runner/resources/parserin.txt @@ -0,0 +1 @@ +StarkProof(config=StarkConfig(traces=TracesConfig(original=TableCommitmentConfig(n_columns=12, vector=VectorCommitmentConfig(height=24, n_verifier_friendly_commitment_layers=9)), interaction=TableCommitmentConfig(n_columns=3, vector=VectorCommitmentConfig(height=24, n_verifier_friendly_commitment_layers=9))), composition=TableCommitmentConfig(n_columns=2, vector=VectorCommitmentConfig(height=24, n_verifier_friendly_commitment_layers=9)))) diff --git a/runner/src/ast.rs b/runner/src/ast.rs new file mode 100644 index 000000000..f0ad2891f --- /dev/null +++ b/runner/src/ast.rs @@ -0,0 +1,53 @@ +use std::{ + fmt::Display, + ops::{Deref, DerefMut}, +}; + +#[derive(Debug, Clone)] +pub enum Expr { + Value(String), + Array(Vec), +} + +impl Display for Expr { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Expr::Value(v) => write!(f, "{}", v), + Expr::Array(v) => write!(f, "{:?}", v), + } + } +} + +#[derive(Debug, Clone)] +pub struct Exprs(pub Vec); + +impl Display for Exprs { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[")?; + + for (i, expr) in self.iter().enumerate() { + if i != 0 { + write!(f, ", ")?; + } + write!(f, "{}", expr)?; + } + + write!(f, "]")?; + + Ok(()) + } +} + +impl Deref for Exprs { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Exprs { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} diff --git a/runner/src/main.rs b/runner/src/main.rs index 6aa2d77ce..4aa3caeb2 100644 --- a/runner/src/main.rs +++ b/runner/src/main.rs @@ -1,35 +1,36 @@ -use std::fs::File; -use std::io::BufReader; +use std::io::{stdin, Read}; +use cairo_args_runner::{run, WrappedArg}; use clap::Parser; -use serde_json::Value; +use lalrpop_util::lalrpop_mod; + +mod ast; + +lalrpop_mod!(pub parser); #[derive(Parser)] #[command(author, version, about, long_about = None)] struct Cli { - /// Path to the input file - #[clap(default_value = "resources/input.json")] - path: String, + /// Path to compiled sierra file + target: String, } fn main() -> anyhow::Result<()> { - let args = Cli::parse(); - let file = File::open(args.path)?; - let reader = BufReader::new(file); + let cli = Cli::parse(); + let mut input = String::new(); + stdin().read_to_string(&mut input)?; - let v: Value = serde_json::from_reader(reader)?; + let parsed = parser::ExprParser::new() + .parse(&input) + .map_err(|e| anyhow::anyhow!("{}", e))?; + let result = format!("{parsed}"); - let flattened = flatten_recursively(&v); + let target = cli.target; + let function = "main"; + let args: WrappedArg = serde_json::from_str(&result).unwrap(); - println!("{:?}", flattened.collect::>()); + let result = run(&target, &function, &args)?; + println!("{result:?}"); Ok(()) } - -fn flatten_recursively(v: &Value) -> Box + '_> { - match v { - Value::Array(array) => Box::new(array.iter().flat_map(flatten_recursively)), - Value::Number(v) => Box::new(std::iter::once(v.as_u64().unwrap())), - _ => Box::new(std::iter::empty()), - } -} diff --git a/runner/src/parser.lalrpop b/runner/src/parser.lalrpop new file mode 100644 index 000000000..91369ddb8 --- /dev/null +++ b/runner/src/parser.lalrpop @@ -0,0 +1,28 @@ +use crate::ast::{Expr, Exprs}; + +grammar; + +pub Expr: Exprs = { + StructName "()" => Exprs(Vec::new()), + StructName "(" > ")" => Exprs(n.iter().flat_map(|x| x.iter().cloned()).collect()), +}; + +Arg: Exprs = { + ArgName "=" => n, + ArgName "=" => Exprs(vec![Expr::Value(n)]), + ArgName "=" "[" > "]" => Exprs(vec![Expr::Array(n)]) +}; + +Comma: Vec = { + ",")*> => match e { + None => v, + Some(e) => { + v.push(e); + v + } + } +}; + +StructName: String = => s.to_string(); +ArgName: String = => s.to_string(); +Num: String = => s.to_string(); diff --git a/src/input_structs/stark_config.cairo b/src/input_structs/stark_config.cairo index ca3282c83..4e4c27c67 100644 --- a/src/input_structs/stark_config.cairo +++ b/src/input_structs/stark_config.cairo @@ -1,7 +1,7 @@ #[derive(Copy, Drop)] struct StarkConfig { traces: TracesConfig, -// composition: TableCommitmentConfig, + composition: TableCommitmentConfig, // fri: FriConfig, // proof_of_work: ProofOfWorkConfig, // // Log2 of the trace domain size.