Skip to content

Commit

Permalink
add support for [] in Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
doichanj committed Jan 7, 2025
1 parent 2fbc9a7 commit 6c448d7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
21 changes: 19 additions & 2 deletions crates/circuit/src/symbol_expr_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// that they have been altered from the originals.


use crate::symbol_expr::{SymbolExpr, Value};
use crate::symbol_expr::{SymbolExpr, Value, Symbol};
use crate::symbol_parser::parse_expression;

use num_complex::Complex64;
Expand Down Expand Up @@ -65,10 +65,27 @@ impl PySymbolExpr {
#[staticmethod]
pub fn Symbol(name: String) -> Self {
PySymbolExpr {
expr : parse_expression(&name),
expr : SymbolExpr::Symbol(Symbol::new(&name)),
}
}

#[staticmethod]
pub fn Value(value: ParameterValue) -> Self {
match value {
ParameterValue::Real(r) => PySymbolExpr { expr: SymbolExpr::Value( Value::Real(r.clone()))},
ParameterValue::Complex(c) => PySymbolExpr { expr: SymbolExpr::Value( Value::Complex(c.clone()))},
ParameterValue::Int(r) => PySymbolExpr { expr: SymbolExpr::Value( Value::Real(r.clone().into()))},
ParameterValue::Str(s) => PySymbolExpr { expr: parse_expression(&s)},
ParameterValue::Expr(e) => PySymbolExpr { expr: e.expr},
}
}

#[staticmethod]
pub fn Expression(name: String) -> Self {
PySymbolExpr {
expr : parse_expression(&name),
}
}

pub fn sin(&self) -> Self {
Self {
Expand Down
28 changes: 23 additions & 5 deletions crates/circuit/src/symbol_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ extern crate nom;
extern crate nom_unicode;
use nom::IResult;
use nom::Parser;
use nom::character::complete::{char, multispace0};
use nom::character::complete::{char, multispace0, digit1};
use nom::bytes::complete::tag;
use nom::combinator::{all_consuming, map_res, recognize};
use nom::combinator::{all_consuming, map_res, recognize, opt};
use nom::branch::{alt, permutation};
use nom::sequence::{delimited, pair, tuple};
use nom::multi::{many0, many0_count};
Expand Down Expand Up @@ -85,9 +85,27 @@ fn parse_symbol_string(s: &str) -> IResult<&str, &str> {

fn parse_symbol(s: &str) -> IResult<&str, BinaryOpContainer> {
map_res(
parse_symbol_string,
|v: &str| -> Result<BinaryOpContainer, &str> {
Ok(BinaryOpContainer{op: BinaryOps::Add, expr: SymbolExpr::Symbol( Symbol::new(v))})
tuple((
parse_symbol_string,
opt(
delimited(
char('['),
digit1,
char(']'),
),
),
)),
|(v, array_idx)| -> Result<BinaryOpContainer, &str> {
match array_idx {
Some(i) => {
// currently array index is stored as string
// if array indexing is required in the future
// add indexing in Symbol struct
let s = format!("{}[{}]",v,i);
return Ok(BinaryOpContainer{op: BinaryOps::Add, expr: SymbolExpr::Symbol( Symbol::new(&s))});
},
None => Ok(BinaryOpContainer{op: BinaryOps::Add, expr: SymbolExpr::Symbol( Symbol::new(v))}),
}
}
)(s)
}
Expand Down

0 comments on commit 6c448d7

Please sign in to comment.