diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-12-18 14:15:18 -0800 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-12-18 14:15:18 -0800 |
commit | fc0e67f5d7d092e5b37d103d2d106733228ddf89 (patch) | |
tree | 044479c27e226b0aae6db0c4507c738aa9d0bf59 /modelica-parser-lalrpop | |
parent | 643b7ef94bdf507def6cb92575c1af73ba5d691f (diff) | |
download | modelthing-fc0e67f5d7d092e5b37d103d2d106733228ddf89.tar.gz modelthing-fc0e67f5d7d092e5b37d103d2d106733228ddf89.zip |
parser: booleans and arrays
Diffstat (limited to 'modelica-parser-lalrpop')
-rw-r--r-- | modelica-parser-lalrpop/src/ast.rs | 21 | ||||
-rw-r--r-- | modelica-parser-lalrpop/src/parser.lalrpop | 6 |
2 files changed, 26 insertions, 1 deletions
diff --git a/modelica-parser-lalrpop/src/ast.rs b/modelica-parser-lalrpop/src/ast.rs index 3b31072..addb90f 100644 --- a/modelica-parser-lalrpop/src/ast.rs +++ b/modelica-parser-lalrpop/src/ast.rs @@ -85,6 +85,7 @@ pub struct ComponentClause { #[derive(Clone, PartialEq)] pub struct ComponentDeclaration { pub name: String, + pub dimensions: Option<Vec<i64>>, pub value: Option<Expr>, pub quantity: Option<String>, pub units: Option<String>, @@ -118,11 +119,13 @@ pub struct SimpleEquation { pub enum Expr { Integer(i64), Float(f64), + Boolean(bool), Ident(String), Der(Box<Expr>), Sign(Box<Expr>), MathUnaryExpr(MathUnaryFunc, Box<Expr>), BinExpr(BinOperator, Box<Expr>, Box<Expr>), + Array(Vec<Expr>), } #[derive(Copy, Clone, PartialEq)] @@ -234,13 +237,20 @@ impl Expr { pub fn identifiers(&self) -> Vec<String> { use self::Expr::*; match *self { - Integer(_) | Float(_) => vec![], + Integer(_) | Float(_) | Boolean(_) => vec![], Ident(ref s) => vec![s.clone()], Der(ref e) | Sign(ref e) => e.identifiers(), MathUnaryExpr(_, ref e) => e.identifiers(), BinExpr(_, ref e1, ref e2) => { union_strings(&e1.identifiers(), &e2.identifiers()) }, + Array(ref el) => { + let mut all: Vec<String> = vec![]; + for e in el { + all.append(&mut e.identifiers()); + } + all + } } } @@ -331,11 +341,20 @@ impl Debug for Expr { match *self { Integer(e) => write!(fmt, "{}", e), Float(e) => write!(fmt, "{}", e), + Boolean(e) => write!(fmt, "{}", e), Ident(ref e) => write!(fmt, "{}", e), Der(ref e) => write!(fmt, "der({:?})", e), Sign(ref e) => write!(fmt, "sign({:?})", e), MathUnaryExpr(func, ref e) => write!(fmt, "{:?}({:?})", func, e), BinExpr(op, ref l, ref r) => write!(fmt, "({:?} {:?} {:?})", l, op, r), + Array(ref el) => { + try!(write!(fmt, "[")); + for e in el { + // XXX: not last comma + try!(write!(fmt, "{:?},", e)); + } + write!(fmt, "]") + }, } } } diff --git a/modelica-parser-lalrpop/src/parser.lalrpop b/modelica-parser-lalrpop/src/parser.lalrpop index a2c5d44..36aa07d 100644 --- a/modelica-parser-lalrpop/src/parser.lalrpop +++ b/modelica-parser-lalrpop/src/parser.lalrpop @@ -102,6 +102,7 @@ factor: Expr = { term: Expr = { integer => Expr::Integer(<>), + boolean => Expr::Boolean(<>), float => Expr::Float(<>), identifier => Expr::Ident(<>), "der" "(" <e:expr> ")" => Expr::Der(Box::new(e)), @@ -120,5 +121,10 @@ term: Expr = { "log" "(" <e:expr> ")" => Expr::MathUnaryExpr(MathUnaryFunc::Log, Box::new(e)), "log10" "(" <e:expr> ")" => Expr::MathUnaryExpr(MathUnaryFunc::Log10, Box::new(e)), "(" <e:expr> ")" => e, + // Obviously a hack here, only supporting up to 4 elements in an array + "[" <e:expr> "]" => Expr::Array(vec![e]), + "[" <e1:expr> "," <e2:expr> "]" => Expr::Array(vec![e1, e2]), + "[" <e1:expr> "," <e2:expr> "," <e3:expr> "]" => Expr::Array(vec![e1, e2, e3]), + "[" <e1:expr> "," <e2:expr> "," <e3:expr> "," <e4:expr> "]" => Expr::Array(vec![e1, e2, e3, e4]), }; |