Skip to content

Comma operator solution (Ch 6 Ex 1)#5

Open
chelseatroy wants to merge 2 commits intomasterfrom
comma-operator-solution
Open

Comma operator solution (Ch 6 Ex 1)#5
chelseatroy wants to merge 2 commits intomasterfrom
comma-operator-solution

Conversation

@chelseatroy
Copy link
Copy Markdown
Owner

No description provided.

+ In C, the comma operator has the LOWEST precedence, and we are instructed to give it the same precedence in Lox. That means that it must be parsed BEFORE the thing that PREVIOUSLY had lowest precedence—in our case, the OR operator.
+ We have the commaOperator() function in the parser return a new kind of expression—a CommaCollection (yeah, I know, I'm a verbose namer). We define that type of Expression in GenerateAst, and each time we build the expression type itself gets generated in Expr.java. (Don't change that file directly—it just gets overwritten).
+ That expression type receives a method in the Visitor Interface. So all of the interface's implementors (Resolver and Interpreter) must deal with comma collections. The interpreter evaluates the expression before the comma and the expression after the comma, and returns only the one after the comma. The resolver resolves both halves (maybe it doesn't need to resolve the left half if we're discarding the evaluation, but whatever).
+ FINALLY, we have to nudge over the code path for argument lists in functions. Like comma collections, the arguments of a function are delimited with commas. UNLIKE comma collections, the arguments of a function are all needed in the function and we can't just discard all but the last one. So instead of calling expression(), which now includes commaOperator() in its recursive descent path, we make a new function in the Parser called assignArg() that follows the same recursive descent path expression() followed BEFORE we added commaOperator().
@chelseatroy chelseatroy changed the title Comma operator solution Comma operator solution (Ch 6 Ex 1) Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant