@@ -158,7 +158,7 @@ pub StoredDefinition: ast::StoredDefinition = {
158
158
}
159
159
}
160
160
161
- pub ClassDefinitionWithFinal: ast::ClassDefinition = {
161
+ ClassDefinitionWithFinal: ast::ClassDefinition = {
162
162
<is_final: "final"?> <mut class: ClassDefinition> => {
163
163
class.is_final = is_final.is_some();
164
164
class
@@ -851,7 +851,7 @@ pub ForStatement: ast::Statement = {
851
851
852
852
//✅ for-indices :
853
853
//✅ for-index { "," for-index }
854
- pub ForIndices: Vec<ast::ForIndex> = {
854
+ ForIndices: Vec<ast::ForIndex> = {
855
855
<indices: SeparatedList<ForIndex, ",">> => indices
856
856
}
857
857
@@ -928,7 +928,7 @@ pub ConnectEquation: ast::Equation = {
928
928
//✅ logical-expression
929
929
//🟥 [ ":" logical-expression [ ":" logical-expression ] ]
930
930
pub Expression: ast::Expression = {
931
- <simp: SimpleExpression> => <>,
931
+ <SimpleExpression> => <>,
932
932
"if" <if_cond:SimpleExpression> "then"
933
933
<then_expr:SimpleExpression>
934
934
<else_if_blocks: ElseIfExpressionBlock*>
@@ -966,7 +966,44 @@ pub ElseExpressionBlock: ast::Expression = {
966
966
967
967
pub ParenthesisExpression = SimpleExpression;
968
968
969
- pub SimpleExpression: ast::Expression = {
969
+ //✅ relational-operator :
970
+ //✅ "<" | "<=" | ">" | ">=" | "==" | "<>"
971
+ RelationalOperator : ast::BinaryOp = {
972
+ "<" => ast::BinaryOp::LessThan,
973
+ "<=" => ast::BinaryOp::LessThanOrEqual,
974
+ ">" => ast::BinaryOp::GreaterThan,
975
+ ">=" => ast::BinaryOp::GreaterThanOrEqual,
976
+ "==" => ast::BinaryOp::Equal,
977
+ "<>" => ast::BinaryOp::NotEqual,
978
+ }
979
+
980
+ //✅ add-operator :
981
+ //✅ "+" | "-" | ".+" | ".-"
982
+ AddBinaryOperator : ast::BinaryOp = {
983
+ "+" => ast::BinaryOp::Add,
984
+ "-" => ast::BinaryOp::Sub,
985
+ ".+" => ast::BinaryOp::ElemAdd,
986
+ ".-" => ast::BinaryOp::ElemSub,
987
+ }
988
+ AddUnaryOperator : ast::UnaryOp = {
989
+ "-" => ast::UnaryOp::Negative,
990
+ }
991
+
992
+ FactorOperator : ast::BinaryOp = {
993
+ "^" => ast::BinaryOp::Exp,
994
+ ".^" => ast::BinaryOp::ElemExp,
995
+ }
996
+
997
+ //✅ mul-operator :
998
+ //✅ "*" | "/" | ".*" | "./"
999
+ MulOperator : ast::BinaryOp = {
1000
+ "*" => ast::BinaryOp::Mul,
1001
+ "/" => ast::BinaryOp::Div,
1002
+ ".*" => ast::BinaryOp::ElemMul,
1003
+ "./" => ast::BinaryOp::ElemDiv,
1004
+ }
1005
+
1006
+ SimpleExpression: ast::Expression = {
970
1007
//✅ simple-expression :
971
1008
//✅ logical-expression [ ":" logical-expression [ ":" logical-expression ] ]
972
1009
#[precedence(level="11")] #[assoc(side="left")]
@@ -1008,95 +1045,33 @@ pub SimpleExpression: ast::Expression = {
1008
1045
},
1009
1046
//✅ relation :
1010
1047
//✅ arithmetic-expression [ relational-operator arithmetic-expression ]
1011
- //✅ relational-operator :
1012
- //✅ "<" | "<=" | ">" | ">=" | "==" | "<>"
1013
1048
#[precedence(level="7")] #[assoc(side="left")]
1014
- <lhs:SimpleExpression> "<" <rhs:SimpleExpression> => {
1015
- ast::Expression::Binary {
1016
- op: ast::BinaryOp::LessThan,
1017
- lhs: Box::new(lhs),
1018
- rhs: Box::new(rhs),
1019
- }
1020
- },
1021
- <lhs:SimpleExpression> ">" <rhs:SimpleExpression> => {
1022
- ast::Expression::Binary {
1023
- op: ast::BinaryOp::GreaterThan,
1024
- lhs: Box::new(lhs),
1025
- rhs: Box::new(rhs),
1026
- }
1027
- },
1028
- <lhs:SimpleExpression> "<=" <rhs:SimpleExpression> => {
1049
+ <lhs:SimpleExpression> <op:RelationalOperator> <rhs:SimpleExpression> => {
1029
1050
ast::Expression::Binary {
1030
- op: ast::BinaryOp::LessThanOrEqual ,
1051
+ op,
1031
1052
lhs: Box::new(lhs),
1032
1053
rhs: Box::new(rhs),
1033
1054
}
1034
1055
},
1035
- <lhs:SimpleExpression> ">=" <rhs:SimpleExpression> => {
1036
- ast::Expression::Binary {
1037
- op: ast::BinaryOp::GreaterThanOrEqual,
1038
- lhs: Box::new(lhs),
1039
- rhs: Box::new(rhs),
1040
- }
1041
- },
1042
- <lhs:SimpleExpression> "==" <rhs:SimpleExpression> => {
1043
- ast::Expression::Binary {
1044
- op: ast::BinaryOp::Equal,
1045
- lhs: Box::new(lhs),
1046
- rhs: Box::new(rhs),
1047
- }
1048
- },
1049
- <lhs:SimpleExpression> "<>" <rhs:SimpleExpression> => {
1050
- ast::Expression::Binary {
1051
- op: ast::BinaryOp::NotEqual,
1052
- lhs: Box::new(lhs),
1053
- rhs: Box::new(rhs),
1054
- }
1055
- },
1056
- #[precedence(level="12")] #[assoc(side="left")]
1057
- "-" <rhs:SimpleExpression> => {
1058
- ast::Expression::Unary {
1059
- op: ast::UnaryOp::Negative,
1060
- rhs: Box::new(rhs),
1061
- }
1062
- },
1063
1056
//✅ arithmetic-expression :
1064
1057
//✅ [ add-operator ] term { add-operator term }
1065
- //✅ add-operator :
1066
- //✅ "+" | "-" | ".+" | ".-"
1067
- #[precedence(level="5")] #[assoc(side="left")]
1068
- <lhs:SimpleExpression> "+" <rhs:SimpleExpression> => {
1069
- ast::Expression::Binary {
1070
- op: ast::BinaryOp::Add,
1071
- lhs: Box::new(lhs),
1072
- rhs: Box::new(rhs),
1073
- }
1074
- },
1075
- <lhs:SimpleExpression> ".+" <rhs:SimpleExpression> => {
1076
- ast::Expression::Binary {
1077
- op: ast::BinaryOp::ElemAdd,
1078
- lhs: Box::new(lhs),
1079
- rhs: Box::new(rhs),
1080
- }
1081
- },
1082
- <lhs:SimpleExpression> "-" <rhs:SimpleExpression> => {
1083
- ast::Expression::Binary {
1084
- op: ast::BinaryOp::Sub,
1085
- lhs: Box::new(lhs),
1058
+ #[precedence(level="6")] #[assoc(side="right")]
1059
+ <op:AddUnaryOperator> <rhs:SimpleExpression> => {
1060
+ ast::Expression::Unary {
1061
+ op,
1086
1062
rhs: Box::new(rhs),
1087
1063
}
1088
1064
},
1089
- <lhs:SimpleExpression> ".-" <rhs:SimpleExpression> => {
1065
+ #[precedence(level="5")] #[assoc(side="left")]
1066
+ <lhs:SimpleExpression> <op:AddBinaryOperator> <rhs:SimpleExpression> => {
1090
1067
ast::Expression::Binary {
1091
- op: ast::BinaryOp::ElemSub ,
1068
+ op,
1092
1069
lhs: Box::new(lhs),
1093
1070
rhs: Box::new(rhs),
1094
1071
}
1095
1072
},
1096
1073
//✅ term :
1097
1074
//✅ factor { mul-operator factor }
1098
- //✅ mul-operator :
1099
- //✅ "*" | "/" | ".*" | "./"
1100
1075
#[precedence(level="4")] #[assoc(side="left")]
1101
1076
<lhs:SimpleExpression> "*" <rhs:SimpleExpression> => {
1102
1077
ast::Expression::Binary {
@@ -1129,16 +1104,9 @@ pub SimpleExpression: ast::Expression = {
1129
1104
//✅ factor :
1130
1105
//✅ primary [ ( "^" | ".^" ) primary ]
1131
1106
#[precedence(level="3")] #[assoc(side="left")]
1132
- <lhs:SimpleExpression> "^" <rhs:SimpleExpression> => {
1133
- ast::Expression::Binary {
1134
- op: ast::BinaryOp::Exp,
1135
- lhs: Box::new(lhs),
1136
- rhs: Box::new(rhs),
1137
- }
1138
- },
1139
- <lhs:SimpleExpression> ".^" <rhs:SimpleExpression> => {
1107
+ <lhs:SimpleExpression> <op:FactorOperator> <rhs:SimpleExpression> => {
1140
1108
ast::Expression::Binary {
1141
- op: ast::BinaryOp::ElemExp ,
1109
+ op,
1142
1110
lhs: Box::new(lhs),
1143
1111
rhs: Box::new(rhs),
1144
1112
}
0 commit comments