Skip to content
Browse files

add write_set_selector_result

  • Loading branch information...
1 parent 20634e3 commit 8135bcaf3950e1c79974636fd3932f793a1af165 @paulhodge paulhodge committed
Showing with 40 additions and 37 deletions.
  1. +29 −12 src/building.cpp
  2. +9 −1 src/building.h
  3. +2 −24 src/parser.cpp
View
41 src/building.cpp
@@ -1218,28 +1218,29 @@ void remap_pointers(Branch* branch, Term* original, Term* replacement)
}
}
-Term* write_selector_for_accessor_expression(Branch* branch, Term* term, Term** headPtr)
+Term* write_selector_for_accessor_expression(Branch* branch, Term* accessor, Term** headPtr)
{
TermList elements;
while (true) {
if (headPtr != NULL)
- *headPtr = term;
+ *headPtr = accessor;
// Stop if we find a term with a name.
- if (term->name != "")
+ if (accessor->name != "")
break;
- if (term->function == FUNCS.get_index) {
- elements.prepend(term->input(1));
- term = term->input(0);
- } else if (term->function == FUNCS.get_field) {
- elements.prepend(term->input(1));
- term = term->input(0);
- } else if (term->function == FUNCS.dynamic_method) {
- Term* fieldName = create_string(branch, term->stringProp("syntax:functionName", ""));
+ if (accessor->function == FUNCS.get_index) {
+ elements.prepend(accessor->input(1));
+ accessor = accessor->input(0);
+ } else if (accessor->function == FUNCS.get_field) {
+ elements.prepend(accessor->input(1));
+ accessor = accessor->input(0);
+ } else if (accessor->function == FUNCS.dynamic_method) {
+ Term* fieldName = create_string(branch,
+ accessor->stringProp("syntax:functionName", ""));
elements.prepend(fieldName);
- term = term->input(0);
+ accessor = accessor->input(0);
} else {
break;
}
@@ -1251,6 +1252,22 @@ Term* write_selector_for_accessor_expression(Branch* branch, Term* term, Term**
return apply(branch, FUNCS.selector, elements);
}
+Term* write_set_selector_result(Branch* branch, Term* accessorExpr, Term* result)
+{
+ Term* head = NULL;
+ Term* selector = write_selector_for_accessor_expression(branch, accessorExpr, &head);
+ if (selector != NULL) {
+ Term* set = apply(branch, FUNCS.set_with_selector, TermList(head, selector, result));
+ change_declared_type(set, declared_type(head));
+ rename(set, head->name);
+ return set;
+ } else {
+ rename(result, accessorExpr->name);
+ result->setBoolProp("syntax:implicitName", true);
+ return result;
+ }
+}
+
bool term_is_nested_in_branch(Term* term, Branch* branch)
{
while (term != NULL) {
View
10 src/building.h
@@ -167,7 +167,15 @@ void remap_pointers(Term* term, TermMap const& map);
void remap_pointers(Term* term, Term* original, Term* replacement);
void remap_pointers(Branch* branch, Term* original, Term* replacement);
-Term* write_selector_for_accessor_expression(Branch* branch, Term* term, Term** headPtr);
+// Examine the 'accessor' term, and walk upwards to construct a selector() expression.
+// Each get_index or get_field expression corresponds to a selector element. This
+// function will return NULL if there are no selector elements.
+Term* write_selector_for_accessor_expression(Branch* branch, Term* accessor, Term** headPtr);
+
+// Write a set_with_selector expression that creates a selector() from 'accessor', and
+// assigns 'result' to the subelement. If the accessor has no selector then we simply
+// rename 'result' to have 'accessor's name.
+Term* write_set_selector_result(Branch* branch, Term* accessor, Term* result);
// Look through the nexted contents of 'term', and find any term references to outer
// terms (terms outside this branch). For every outer reference, add an input to
View
26 src/parser.cpp
@@ -1060,18 +1060,7 @@ ParseResult for_block(Branch* branch, TokenStream& tokens, ParserCxt* context)
// Wrap up the rebound value, if it's a complex lexpr.
if (rebindListName) {
- Term* head = NULL;
- Term* selector = write_selector_for_accessor_expression(branch, listExpr, &head);
-
- if (selector != NULL) {
- Term* set = apply(branch, FUNCS.set_with_selector,
- TermList(head, selector, forTerm));
- change_declared_type(set, declared_type(head));
- rename(set, head->name);
- } else {
- rename(forTerm, listExpr->name);
- forTerm->setBoolProp("syntax:implicitName", true);
- }
+ write_set_selector_result(branch, listExpr, forTerm);
}
return ParseResult(forTerm);
@@ -1773,18 +1762,7 @@ ParseResult method_call(Branch* branch, TokenStream& tokens, ParserCxt* context,
// Possibly rebind the left-hand-side
if (rebindLHS) {
// LHS may be a getter-chain
- Term* head = NULL;
- Term* result = get_extra_output(term, 0);
- Term* selector = write_selector_for_accessor_expression(branch, term->input(0), &head);
-
- if (selector != NULL) {
- Term* set = apply(branch, FUNCS.set_with_selector,
- TermList(head, selector, result));
- change_declared_type(set, declared_type(head));
- rename(set, head->name);
- } else {
- rename(result, term->input(0)->name);
- }
+ write_set_selector_result(branch, term->input(0), get_extra_output(term, 0));
}
inputHints.apply(term);

0 comments on commit 8135bca

Please sign in to comment.
Something went wrong with that request. Please try again.