Skip to content

Commit

Permalink
Translation for while node
Browse files Browse the repository at this point in the history
  • Loading branch information
egiurleo committed Aug 26, 2024
1 parent 1ebb550 commit 943fa2a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
12 changes: 11 additions & 1 deletion parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,17 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
return make_unique<parser::When>(parser.translateLocation(loc), std::move(sorbetConditions),
std::move(statements));
}
case PM_WHILE_NODE: {
auto whileNode = reinterpret_cast<pm_while_node *>(node);
auto *loc = &whileNode->base.location;

auto inlineIfSingle = true;
auto predicate = translate(whileNode->predicate);
auto statements = translateStatements(whileNode->statements, inlineIfSingle);

return make_unique<parser::While>(parser.translateLocation(loc), std::move(predicate),
std::move(statements));
}
case PM_YIELD_NODE: {
auto yieldNode = reinterpret_cast<pm_yield_node *>(node);
pm_location_t *loc = &yieldNode->base.location;
Expand Down Expand Up @@ -722,7 +733,6 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_SOURCE_FILE_NODE:
case PM_SOURCE_LINE_NODE:
case PM_UNDEF_NODE:
case PM_WHILE_NODE:
case PM_X_STRING_NODE:
case PM_SCOPE_NODE:
auto type_id = PM_NODE_TYPE(node);
Expand Down
53 changes: 53 additions & 0 deletions test/prism_regression/while.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Begin {
stmts = [
DefMethod {
name = <U foo>
args = NULL
body = NULL
}
While {
cond = Send {
receiver = NULL
method = <U foo>
args = [
]
}
body = Integer {
val = "5"
}
}
While {
cond = True {
}
body = Begin {
stmts = [
Assign {
lhs = LVarLhs {
name = <U x>
}
rhs = Send {
receiver = Integer {
val = "3"
}
method = <U +>
args = [
Integer {
val = "2"
}
]
}
}
Send {
receiver = NULL
method = <U puts>
args = [
String {
val = <U hi>
}
]
}
]
}
}
]
}
13 changes: 13 additions & 0 deletions test/prism_regression/while.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# typed: true

def foo; end

while foo
5
end

# multi-statement
while true
x = 3 + 2
puts "hi"
end

0 comments on commit 943fa2a

Please sign in to comment.