elif
This commit is contained in:
parent
307c51539c
commit
c0244b5822
3 changed files with 35 additions and 6 deletions
20
src/parser.y
20
src/parser.y
|
|
@ -61,6 +61,7 @@
|
||||||
%type <std::vector<std::unique_ptr<Expr>>> tuple
|
%type <std::vector<std::unique_ptr<Expr>>> tuple
|
||||||
%type <std::vector<std::unique_ptr<Stmt>>> stmt_list
|
%type <std::vector<std::unique_ptr<Stmt>>> stmt_list
|
||||||
%type <std::vector<std::string>> params
|
%type <std::vector<std::string>> params
|
||||||
|
%type <std::vector<std::unique_ptr<Stmt>>> elif_chain
|
||||||
|
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
@ -80,11 +81,8 @@ stmt:
|
||||||
| expr ASSIGN expr NEWLINE {
|
| expr ASSIGN expr NEWLINE {
|
||||||
$$ = std::make_unique<Assign>(std::move($1), std::move($3));
|
$$ = std::make_unique<Assign>(std::move($1), std::move($3));
|
||||||
}
|
}
|
||||||
| KW_IF expr COLON NEWLINE INDENT stmt_list DEDENT {
|
| KW_IF expr COLON NEWLINE INDENT stmt_list DEDENT elif_chain{
|
||||||
$$ = std::make_unique<If>(std::move($2), std::move($6), std::vector<std::unique_ptr<Stmt>>());
|
$$ = std::make_unique<If>(std::move($2), std::move($6), std::move($8));
|
||||||
}
|
|
||||||
| KW_IF expr COLON NEWLINE INDENT stmt_list DEDENT KW_ELSE COLON NEWLINE INDENT stmt_list DEDENT {
|
|
||||||
$$ = std::make_unique<If>(std::move($2), std::move($6), std::move($12));
|
|
||||||
}
|
}
|
||||||
| KW_FOR IDENTIFIER KW_IN expr COLON NEWLINE INDENT stmt_list DEDENT {
|
| KW_FOR IDENTIFIER KW_IN expr COLON NEWLINE INDENT stmt_list DEDENT {
|
||||||
$$ = std::make_unique<For> ($2, std::move($4), std::move($8));
|
$$ = std::make_unique<For> ($2, std::move($4), std::move($8));
|
||||||
|
|
@ -168,6 +166,18 @@ params:
|
||||||
{ $$ = std::vector<std::string>();}
|
{ $$ = std::vector<std::string>();}
|
||||||
| IDENTIFIER { $$ = std::vector<std::string>(); $$.push_back($1); }
|
| IDENTIFIER { $$ = std::vector<std::string>(); $$.push_back($1); }
|
||||||
| params COMMA IDENTIFIER {$1.push_back($3); $$ = std::move($1);}
|
| params COMMA IDENTIFIER {$1.push_back($3); $$ = std::move($1);}
|
||||||
|
|
||||||
|
elif_chain:
|
||||||
|
/* empty */ { $$ = std::vector<std::unique_ptr<Stmt>>(); }
|
||||||
|
| KW_ELIF expr COLON NEWLINE INDENT stmt_list DEDENT elif_chain {
|
||||||
|
std::vector<std::unique_ptr<Stmt>> output;
|
||||||
|
output.push_back(std::make_unique<If>(std::move($2), std::move($6), std::move($8)));
|
||||||
|
$$ = std::move(output);
|
||||||
|
}
|
||||||
|
| KW_ELSE COLON NEWLINE INDENT stmt_list DEDENT {
|
||||||
|
$$ = std::move($5);
|
||||||
|
}
|
||||||
|
;
|
||||||
%%
|
%%
|
||||||
namespace yy {
|
namespace yy {
|
||||||
void parser::error(const std::string& msg)
|
void parser::error(const std::string& msg)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
# test_arithmetic.py
|
|
||||||
def calculate_bmi(weight, height):
|
def calculate_bmi(weight, height):
|
||||||
bmi = weight / (height ** 2)
|
bmi = weight / (height ** 2)
|
||||||
|
|
||||||
|
|
|
||||||
20
test/ruby/25.out
Normal file
20
test/ruby/25.out
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
def calculate_bmi(weight, height)
|
||||||
|
bmi = weight / height ** 2
|
||||||
|
if bmi < 18.5
|
||||||
|
category = "Underweight"
|
||||||
|
else
|
||||||
|
if bmi < 25
|
||||||
|
category = "Normal"
|
||||||
|
else
|
||||||
|
if bmi < 30
|
||||||
|
category = "Overweight"
|
||||||
|
else
|
||||||
|
category = "Obese"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return "BMI: #{bmi}, Category: #{category}"
|
||||||
|
end
|
||||||
|
print(calculate_bmi(70, 1.75), "\n")
|
||||||
|
print(calculate_bmi(90, 1.80), "\n")
|
||||||
|
print(calculate_bmi(50, 1.65), "\n")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue