Skip to content

Commit 5518bf3

Browse files
authored
Merge pull request #12 from flodavid/compilateur
Parenthesis available in conditionnalExpression and added in BooleanE…
2 parents 413c00e + 2f3f73a commit 5518bf3

6 files changed

Lines changed: 27 additions & 19 deletions

File tree

src/EZ_language_compiler.ypp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,7 @@ conditionalExpression:
861861
debugYacc("BooleanExpression from variable "+ $1, AT);
862862
$$ = new BooleanExpression($1);
863863
}
864-
// | LEFT_PARENTHESIS conditionalExpression RIGHT_PARENTHESIS {
865-
// TranslatedNode* parenthCond= new TranslatedNode("("+ $2->translate() +")");
866-
// delete $2;
867-
// $$ = parenthCond;
868-
// }
864+
| LEFT_PARENTHESIS conditionalExpression RIGHT_PARENTHESIS { $$ = $2; }
869865
| conditionalExpression logicalOperator conditionalExpression {
870866
BooleanExpression* expr= new BooleanExpression($2,$1,$3);
871867
$$ = expr;
@@ -1015,12 +1011,6 @@ integerExpression:
10151011
| NAME { $$ = new TranslatedNode($1); }
10161012
;
10171013

1018-
// numberExpression:
1019-
// NUM_INTEGER { /* $$ = Integer(to_string($1)); */ }
1020-
// | NUM_REAL { /* $$ = Real($1); */ }
1021-
// | NAME { /* $$ = String($1); */ }
1022-
// ;
1023-
10241014
/* ******************
10251015
* OPERATORS *
10261016
* ******************/

src/instructions/If.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ If::~If()
1818

1919
string If::preTranslate() const
2020
{
21-
string res="if(" + mCondition->translate() + ") ";
22-
23-
res+= "{\n";
21+
string res="if " + mCondition->translate() + " {\n";
2422

2523
// the instructions are in the left_son (the first one IS the left son)
2624

src/instructions/Repeat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ string Repeat::preTranslate() const
1919

2020
string Repeat::postTranslate() const
2121
{
22-
return "} while(" + mCondition->translate() + ");\n";
22+
return "} while " + mCondition->translate() + ";\n";
2323
}

src/instructions/While.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ While::~While()
1414

1515

1616
string While::preTranslate() const {
17-
return "while (" + mCondition->translate() + ") {\n";
17+
return "while " + mCondition->translate() + " {\n";
1818
}
1919

2020
string While::postTranslate() const

src/modules/BooleanExpression.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,27 @@ BooleanExpression::~BooleanExpression()
2929
}
3030

3131
string BooleanExpression::preTranslate() const {
32-
string res= "";
32+
string res= "( ";
3333

3434
if (value != "") {
3535
debug("Cond. Expr., just value", AT);
3636
res+= value;
3737
} else {
3838
// Two operandes case
3939
if(left_part != nullptr) {
40-
debug("Cond. Expr.: binary operator", AT);
4140
res+= left_part->translate() +" ";
4241
}
43-
if(right_part != nullptr && mOperator != nullptr) res+= mOperator->translate() + " "+ right_part->translate();
42+
if(right_part != nullptr && mOperator != nullptr) {
43+
debug("Cond. Expr.: binary operator", AT);
44+
res+= mOperator->translate() + " "+ right_part->translate();
45+
}
4446
// There is no right part or operator whereas there is a left part
4547
else error("BooleanExpression is not correctly initialized (no operator or right part)", AT);
48+
4649
}
50+
51+
res+= " )";
52+
4753
return res;
4854

4955
}

tests/1_main_simple.ez

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,18 @@ a multiple lines comment
6464
m is map of <string, integer>
6565
s is set of string
6666

67+
68+
if true or true and false
69+
then print "Cond expr with no parenthesis priority working"
70+
else print "Cond expr with no parenthesis priority not working"
71+
end if
72+
73+
if (false and true) or true
74+
then print "Cond expr with parenthesis left first working"
75+
end if
76+
77+
if true or (true and false)
78+
then print "Cond expr with parenthesis right first working"
79+
end if
80+
6781
end

0 commit comments

Comments
 (0)