Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

parser for declaring arrays of signals and variables #298

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/parser/ast/statement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fmt::Debug;

use num_bigint::BigInt;

use super::{expression::Expression, DebugSymRef, Identifiable, Identifier};

/// An identifier with the type it is declared with.
Expand All @@ -9,6 +11,8 @@ pub struct TypedIdDecl<V> {
pub id: V,
/// type
pub ty: Option<V>,
/// Array dimension (if any)
pub dimension: Option<Expression<BigInt, V>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -69,15 +73,21 @@ impl<F: Debug> Debug for Statement<F, Identifier> {
f,
"signal {};",
id.iter()
.map(|id| id.id.name())
.map(|id| match &id.dimension {
Some(dim) => format!("{}[{:?}]", id.id.name(), dim),
None => id.id.name().to_string(),
})
.collect::<Vec<_>>()
.join(", ")
),
Statement::WGVarDecl(_, id) => write!(
f,
"var {};",
id.iter()
.map(|id| id.id.name())
.map(|id| match &id.dimension {
Some(dim) => format!("{}[{:?}]", id.id.name(), dim),
None => id.id.name().to_string(),
})
.collect::<Vec<_>>()
.join(", ")
),
Expand Down
16 changes: 14 additions & 2 deletions src/parser/chiquito.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,20 @@ ParseParamDecl: Statement<BigInt, Identifier> = {
}

ParseTypedId: TypedIdDecl<Identifier> = {
<id: Identifier> ":" <ty: Identifier> => TypedIdDecl { id, ty: Some(ty) },
<id: Identifier> => TypedIdDecl { id, ty: None },
<id: Identifier> ":" <ty: Identifier> <dim: ParseArrayDimension?> => TypedIdDecl {
id,
ty: Some(ty),
dimension: dim
},
<id: Identifier> <dim: ParseArrayDimension?> => TypedIdDecl {
id,
ty: None,
dimension: dim
},
}

ParseArrayDimension: Expression<BigInt, Identifier> = {
"[" <expr: Expression> "]" => expr,
}

// Terminals
Expand Down
37 changes: 37 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,43 @@ mod test {
assert_eq!(stmts.len(), 2);
}

#[test]
fn test_parser_declarations() {
let debug_sym_ref_factory = DebugSymRefFactory::new("", "");
let signal_decl = lang::StatementsParser::new()
.parse(&debug_sym_ref_factory, "signal a: field, b, c;")
.unwrap();
assert_eq!(format!("{:?}", signal_decl), r#"[signal a, b, c;]"#);

let typed_signal_decl = lang::StatementsParser::new()
.parse(&debug_sym_ref_factory, "signal a: field;")
.unwrap();
assert_eq!(format!("{:?}", typed_signal_decl), r#"[signal a;]"#);

let var_decl = lang::StatementsParser::new()
.parse(&debug_sym_ref_factory, "var a: field, b, c;")
.unwrap();
assert_eq!(format!("{:?}", var_decl), r#"[var a, b, c;]"#);

let signal_array_decl = lang::StatementsParser::new()
.parse(&debug_sym_ref_factory, "signal a[7];")
.unwrap();
assert_eq!(format!("{:?}", signal_array_decl), r#"[signal a[7];]"#);

let signal_typed_array_decl = lang::StatementsParser::new()
.parse(&debug_sym_ref_factory, "signal a: field[7];")
.unwrap();
assert_eq!(
format!("{:?}", signal_typed_array_decl),
r#"[signal a[7];]"#
);

let var_array_decl = lang::StatementsParser::new()
.parse(&debug_sym_ref_factory, "var a[7];")
.unwrap();
assert_eq!(format!("{:?}", var_array_decl), r#"[var a[7];]"#);
}

#[test]
fn test_parser_tracer() {
let circuit = "
Expand Down
Loading