Skip to content

Commit a905e54

Browse files
committed
Improve class ast.
Signed-off-by: James Goppert <james.goppert@gmail.com>
1 parent b0404ab commit a905e54

File tree

2 files changed

+102
-61
lines changed

2 files changed

+102
-61
lines changed

src/s1_parser/ast.rs

+51-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use indexmap::IndexMap;
12
use serde::{Deserialize, Serialize};
23
use std::fmt;
4+
use std::hash::{Hash, Hasher};
35

46
derive_alias! {
57
#[derive(CommonTraits!)] = #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)];
@@ -53,7 +55,7 @@ impl NodeData {
5355

5456
impl fmt::Debug for NodeData {
5557
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56-
write!(f, "(id: {:?}, span: {:?})", self.id, self.span)
58+
write!(f, "NodeData {{id: {:?}, span: {:?}}}", self.id, self.span)
5759
}
5860
}
5961

@@ -62,11 +64,11 @@ impl fmt::Debug for NodeData {
6264
#[derive(CommonTraits!, Default)]
6365
pub struct StoredDefinition {
6466
pub node_data: NodeData,
65-
pub classes: Vec<ClassDefinition>,
6667
pub within: Option<Name>,
6768
pub model_md5: String,
6869
pub rumoca_parser_version: String,
6970
pub rumoca_parser_git: String,
71+
pub classes: IndexMap<String, ClassDefinition>,
7072
}
7173

7274
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -109,7 +111,18 @@ pub struct ClassDefinition {
109111
pub flags: ClassFlags,
110112
pub modification: Vec<Argument>,
111113
pub description: DescriptionString,
112-
pub composition: Vec<CompositionPart>,
114+
//pub composition: Vec<CompositionPart>,
115+
pub components: IndexMap<String, ComponentDeclaration>,
116+
pub equations: Vec<Equation>,
117+
pub algorithms: Vec<Vec<Statement>>,
118+
pub initial_equations: Vec<Equation>,
119+
pub initial_algorithms: Vec<Vec<Statement>>,
120+
}
121+
122+
impl Hash for ClassDefinition {
123+
fn hash<H: Hasher>(&self, state: &mut H) {
124+
self.node_data.id.hash(state);
125+
}
113126
}
114127

115128
#[derive(CommonTraits!, Default)]
@@ -399,15 +412,15 @@ pub struct StatementIfBlock {
399412
pub enum Expression {
400413
#[default]
401414
Empty,
402-
Array(ExpressionArray),
403-
Binary(ExpressionBinary),
404-
Boolean(ExpressionBoolean),
415+
Array(Array),
416+
Binary(Binary),
417+
Boolean(Boolean),
405418
FunctionCall(FunctionCall),
406419
If(ExpressionIf),
407420
Ref(ComponentReference),
408-
Unary(ExpressionUnary),
409-
UnsignedInteger(ExpressionUnsignedInteger),
410-
UnsignedReal(ExpressionUnsignedReal),
421+
Unary(Unary),
422+
UnsignedInteger(UnsignedInteger),
423+
UnsignedReal(UnsignedReal),
411424
}
412425

413426
impl_debug_for_enum!(Expression {
@@ -423,20 +436,20 @@ impl_debug_for_enum!(Expression {
423436
});
424437

425438
#[derive(CommonTraits!, Default)]
426-
pub struct ExpressionArray {
439+
pub struct Array {
427440
pub node_data: NodeData,
428441
pub args: Vec<Expression>,
429442
}
430443

431444
#[derive(CommonTraits!, Default)]
432-
pub struct ExpressionUnary {
445+
pub struct Unary {
433446
pub node_data: NodeData,
434447
pub op: UnaryOp,
435448
pub rhs: Box<Expression>,
436449
}
437450

438451
#[derive(CommonTraits!, Default)]
439-
pub struct ExpressionBinary {
452+
pub struct Binary {
440453
pub node_data: NodeData,
441454
pub op: BinaryOp,
442455
pub lhs: Box<Expression>,
@@ -465,19 +478,19 @@ pub struct ExpressionIfBlock {
465478
}
466479

467480
#[derive(CommonTraits!, Default)]
468-
pub struct ExpressionUnsignedInteger {
481+
pub struct UnsignedInteger {
469482
pub node_data: NodeData,
470483
pub val: String,
471484
}
472485

473486
#[derive(CommonTraits!, Default)]
474-
pub struct ExpressionUnsignedReal {
487+
pub struct UnsignedReal {
475488
pub node_data: NodeData,
476489
pub val: String,
477490
}
478491

479492
#[derive(CommonTraits!, Default)]
480-
pub struct ExpressionBoolean {
493+
pub struct Boolean {
481494
pub node_data: NodeData,
482495
pub val: bool,
483496
}
@@ -494,15 +507,16 @@ impl fmt::Debug for ComponentReference {
494507
let parts = self
495508
.parts
496509
.iter()
497-
.map(|part| format!("{:#?}", part))
510+
.map(|part| format!("{:?}", part))
498511
.collect::<Vec<_>>()
499512
.join(".");
500513
write!(
501514
f,
502-
"ComponentReference({}{}: {:#?})",
515+
"ComponentReference {{{}{}, id: {}, span: {:?}}}",
503516
if self.local { "." } else { "" },
504517
parts,
505-
self.node_data,
518+
self.node_data.id,
519+
self.node_data.span,
506520
)
507521
}
508522
}
@@ -517,9 +531,9 @@ pub struct RefPart {
517531
impl fmt::Debug for RefPart {
518532
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
519533
if self.array_subscripts.is_empty() {
520-
write!(f, "{:#?}", self.name)
534+
write!(f, "{:?}", self.name)
521535
} else {
522-
write!(f, "{:#?}{:?}", self.name, self.array_subscripts)
536+
write!(f, "{:?}{:?}", self.name, self.array_subscripts)
523537
}
524538
}
525539
}
@@ -612,13 +626,19 @@ pub struct ModExprBreak {
612626
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
613627
// Common
614628

615-
#[derive(CommonTraits!, Default)]
629+
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
616630
pub struct Description {
617631
pub node_data: NodeData,
618632
pub strings: Vec<String>,
619633
pub annotation: Vec<Argument>,
620634
}
621635

636+
impl fmt::Debug for Description {
637+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
638+
write!(f, "{} {:?}", self.strings.join(" "), self.annotation)
639+
}
640+
}
641+
622642
#[derive(CommonTraits!, Default)]
623643
pub struct TypePrefix {
624644
pub connection: Connection,
@@ -754,7 +774,16 @@ pub enum ClassType {
754774
Type,
755775
}
756776

757-
pub type DescriptionString = Vec<String>;
777+
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
778+
pub struct DescriptionString {
779+
pub parts: Vec<String>,
780+
}
781+
782+
impl fmt::Debug for DescriptionString {
783+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
784+
write!(f, "{}", self.parts.join(" "))
785+
}
786+
}
758787

759788
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
760789
pub struct Name {
@@ -766,21 +795,3 @@ impl fmt::Debug for Name {
766795
write!(f, "{}", self.parts.join("."))
767796
}
768797
}
769-
770-
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
771-
// Unit Testing
772-
#[cfg(test)]
773-
mod tests {
774-
use super::*;
775-
776-
#[test]
777-
fn test_ast() {
778-
let mut def = StoredDefinition::default();
779-
780-
// class ball
781-
let class_ball = ClassDefinition {
782-
..Default::default()
783-
};
784-
def.classes.push(class_ball);
785-
}
786-
}

0 commit comments

Comments
 (0)