Skip to content

Commit

Permalink
arbitrary nested arrays working
Browse files Browse the repository at this point in the history
  • Loading branch information
neotheprogramist committed Dec 26, 2023
1 parent 6d48f63 commit 8d8f16c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
8 changes: 4 additions & 4 deletions runner/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use std::{
#[derive(Debug, Clone)]
pub enum Expr {
Value(String),
Array(Vec<String>),
Array(Vec<Expr>),
}

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),
Expr::Value(v) => write!(f, "{v}"),
Expr::Array(v) => write!(f, "{v:?}"),
}
}
}
Expand All @@ -29,7 +29,7 @@ impl Display for Exprs {
if i != 0 {
write!(f, ", ")?;
}
write!(f, "{}", expr)?;
write!(f, "{expr}")?;
}

write!(f, "]")?;
Expand Down
17 changes: 6 additions & 11 deletions runner/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ pub CairoParserOutput: Exprs = {
StructName "(" <n:Comma<Arg>> ")" => Exprs(n.iter().flat_map(|x| x.iter().cloned()).collect()),
};

FlatExpr: Vec<String> = {
<n:Num> => vec![n],
StructName "()" => Vec::new(),
StructName "(" <n:Comma<FlatArg>> ")" => n.iter().flat_map(|x| x.iter().cloned()).collect(),
};

FlatArg: Vec<String> = {
ArgName "=" <n:FlatExpr> => n,
CairoParserOutputInner: Exprs = {
<n:Num> => Exprs(vec![Expr::Value(n)]),
"[" <n:Comma<CairoParserOutputInner>> "]" => Exprs(vec![Expr::Array(n.iter().flat_map(|x| x.iter().cloned()).collect())]),
StructName "()" => Exprs(Vec::new()),
StructName "(" <n:Comma<Arg>> ")" => Exprs(n.iter().flat_map(|x| x.iter().cloned()).collect()),
};

Arg: Exprs = {
ArgName "=" <n:Num> => Exprs(vec![Expr::Value(n)]),
ArgName "=" "[" <n:Comma<FlatExpr>> "]" => Exprs(vec![Expr::Array(n.iter().flat_map(|x| x.iter().cloned()).collect())]),
ArgName "=" <n:CairoParserOutput> => n,
ArgName "=" <n:CairoParserOutputInner> => n,
};

Comma<T>: Vec<T> = {
Expand Down

0 comments on commit 8d8f16c

Please sign in to comment.