From fc0e67f5d7d092e5b37d103d2d106733228ddf89 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 18 Dec 2016 14:15:18 -0800 Subject: parser: booleans and arrays --- modelica-parser-lalrpop/src/ast.rs | 21 ++++++++++++++++++++- modelica-parser-lalrpop/src/parser.lalrpop | 6 ++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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>, pub value: Option, pub quantity: Option, pub units: Option, @@ -118,11 +119,13 @@ pub struct SimpleEquation { pub enum Expr { Integer(i64), Float(f64), + Boolean(bool), Ident(String), Der(Box), Sign(Box), MathUnaryExpr(MathUnaryFunc, Box), BinExpr(BinOperator, Box, Box), + Array(Vec), } #[derive(Copy, Clone, PartialEq)] @@ -234,13 +237,20 @@ impl Expr { pub fn identifiers(&self) -> Vec { 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 = 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" "(" ")" => Expr::Der(Box::new(e)), @@ -120,5 +121,10 @@ term: Expr = { "log" "(" ")" => Expr::MathUnaryExpr(MathUnaryFunc::Log, Box::new(e)), "log10" "(" ")" => Expr::MathUnaryExpr(MathUnaryFunc::Log10, Box::new(e)), "(" ")" => e, + // Obviously a hack here, only supporting up to 4 elements in an array + "[" "]" => Expr::Array(vec![e]), + "[" "," "]" => Expr::Array(vec![e1, e2]), + "[" "," "," "]" => Expr::Array(vec![e1, e2, e3]), + "[" "," "," "," "]" => Expr::Array(vec![e1, e2, e3, e4]), }; -- cgit v1.2.3