tuples
This commit is contained in:
parent
c0244b5822
commit
61dc873489
5 changed files with 60 additions and 5 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
- string processing is very primitive, it just checks for f at the beggining, and then adds #, before { if so
|
- string processing is very primitive, it just checks for f at the beggining, and then adds #, before { if so
|
||||||
- no elif
|
- no elif
|
||||||
- no lambdas
|
- no lambdas
|
||||||
|
- tuples need to be in ()
|
||||||
|
- converts tuples into lists
|
||||||
|
|
@ -67,7 +67,29 @@ private:
|
||||||
else if (auto *assign = dynamic_cast<const Assign *>(&stmt))
|
else if (auto *assign = dynamic_cast<const Assign *>(&stmt))
|
||||||
{
|
{
|
||||||
write_indent();
|
write_indent();
|
||||||
|
if (auto *tuple = dynamic_cast<const Tuple *>(assign->target.get()))
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < tuple->elements.size(); ++i)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
write(", ");
|
||||||
|
generate_expr(*tuple->elements[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (auto *list = dynamic_cast<const List *>(assign->target.get()))
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < list->elements.size(); ++i)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
write(", ");
|
||||||
|
generate_expr(*list->elements[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
generate_expr(*assign->target);
|
generate_expr(*assign->target);
|
||||||
|
}
|
||||||
write(" = ");
|
write(" = ");
|
||||||
generate_expr(*assign->value);
|
generate_expr(*assign->value);
|
||||||
write("\n");
|
write("\n");
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,8 @@ expr:
|
||||||
| expr MULTIPLY expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::MUL), std::move($1), std::move($3)); }
|
| expr MULTIPLY expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::MUL), std::move($1), std::move($3)); }
|
||||||
| expr MINUS expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::SUB), std::move($1), std::move($3)); }
|
| expr MINUS expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::SUB), std::move($1), std::move($3)); }
|
||||||
| expr LT expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::LT) , std::move($1), std::move($3)); }
|
| expr LT expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::LT) , std::move($1), std::move($3)); }
|
||||||
|
| expr GT expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::GT) , std::move($1), std::move($3)); }
|
||||||
|
| expr GTE expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::GTE) , std::move($1), std::move($3)); }
|
||||||
| expr KW_AND expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::AND) , std::move($1), std::move($3)); }
|
| expr KW_AND expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::AND) , std::move($1), std::move($3)); }
|
||||||
| expr KW_OR expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::OR) , std::move($1), std::move($3)); }
|
| expr KW_OR expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::OR) , std::move($1), std::move($3)); }
|
||||||
| expr LTE expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::LTE) , std::move($1), std::move($3)); }
|
| expr LTE expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::LTE) , std::move($1), std::move($3)); }
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
def find_max_min(numbers):
|
def find_max_min(numbers):
|
||||||
if not numbers:
|
if not numbers:
|
||||||
return None, None
|
return (None, None)
|
||||||
|
|
||||||
max_num = numbers[0]
|
max_num = numbers[0]
|
||||||
min_num = numbers[0]
|
min_num = numbers[0]
|
||||||
|
|
@ -11,7 +11,7 @@ def find_max_min(numbers):
|
||||||
if num < min_num:
|
if num < min_num:
|
||||||
min_num = num
|
min_num = num
|
||||||
|
|
||||||
return max_num, min_num
|
return (max_num, min_num)
|
||||||
|
|
||||||
def reverse_list(items):
|
def reverse_list(items):
|
||||||
reversed_items = []
|
reversed_items = []
|
||||||
|
|
@ -20,7 +20,7 @@ def reverse_list(items):
|
||||||
return reversed_items
|
return reversed_items
|
||||||
|
|
||||||
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
|
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
|
||||||
max_val, min_val = find_max_min(numbers)
|
(max_val, min_val) = find_max_min(numbers)
|
||||||
print(f"List: {numbers}")
|
print(f"List: {numbers}")
|
||||||
print(f"Max: {max_val}, Min: {min_val}")
|
print(f"Max: {max_val}, Min: {min_val}")
|
||||||
print(f"Reversed: {reverse_list(numbers)}")
|
print(f"Reversed: {reverse_list(numbers)}")
|
||||||
|
|
|
||||||
29
test/ruby/26.out
Normal file
29
test/ruby/26.out
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
def find_max_min(numbers)
|
||||||
|
if not numbers
|
||||||
|
return [None, None]
|
||||||
|
end
|
||||||
|
max_num = numbers[0]
|
||||||
|
min_num = numbers[0]
|
||||||
|
for num in numbers[1...-1]
|
||||||
|
if num > max_num
|
||||||
|
max_num = num
|
||||||
|
end
|
||||||
|
if num < min_num
|
||||||
|
min_num = num
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return [max_num, min_num]
|
||||||
|
end
|
||||||
|
def reverse_list(items)
|
||||||
|
reversed_items = []
|
||||||
|
for i in (items.length - 1...- 1).step(- 1)
|
||||||
|
reversed_items.append(items[i])
|
||||||
|
end
|
||||||
|
return reversed_items
|
||||||
|
end
|
||||||
|
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
|
||||||
|
max_val, min_val = find_max_min(numbers)
|
||||||
|
print("List: #{numbers}", "\n")
|
||||||
|
print("Max: #{max_val}, Min: #{min_val}", "\n")
|
||||||
|
print("Reversed: #{reverse_list(numbers)}", "\n")
|
||||||
|
print("Original list unchanged: #{numbers}", "\n")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue