Skip to content

Commit

Permalink
Fix method cascade support when the receiver evaluation has side effects
Browse files Browse the repository at this point in the history
  • Loading branch information
gefarion committed Jul 2, 2016
1 parent d861f21 commit cb28ee4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
@@ -1,6 +1,6 @@
[submodule "core-lib"]
path = core-lib
url = https://github.com/charig/SOM.git
url = https://github.com/gefarion/SOM.git
[submodule "libs/truffle"]
path = libs/truffle
url = https://github.com/smarr/Truffle.git
Expand Down
20 changes: 19 additions & 1 deletion src/som/compiler/Parser.java
Expand Up @@ -74,6 +74,7 @@
import som.interpreter.nodes.FieldNode.FieldReadNode;
import som.interpreter.nodes.FieldNode.FieldWriteNode;
import som.interpreter.nodes.MessageSendNode.AbstractMessageSendNode;
import som.interpreter.nodes.MessageSendNode;
import som.interpreter.nodes.literals.ArrayLiteralNode;
import som.interpreter.nodes.literals.BigIntegerLiteralNode;
import som.interpreter.nodes.literals.BlockNode;
Expand Down Expand Up @@ -541,26 +542,43 @@ private String assignment() throws ParseError {
return v;
}

private String makeTempVariableName() {
SourceCoordinate coord = getCoordinate();
return "!" + coord.startLine + "!" + coord.startColumn;

This comment has been minimized.

Copy link
@smarr

smarr Jul 3, 2016

hmmm, could that be a more useful name? perhaps including the receiver expression's code? could help when debugging the interpreter.

}

private ExpressionNode evaluation(final MethodGenerationContext mgenc) throws ParseError {
ExpressionNode exp = primary(mgenc);
if (isIdentifier(sym) || sym == Keyword || sym == OperatorSequence
|| symIn(binaryOpSyms)) {

ExpressionNode receiver = exp;
SourceCoordinate coord = getCoordinate();
SourceSection section = getSource(coord);

exp = messages(mgenc, receiver);

if (SemiColon == sym) {
// Method Cascade

String varname = makeTempVariableName();
mgenc.addLocalIfAbsent(varname);

exp.adoptChildren();
receiver.replace(variableRead(mgenc, varname, section));

List<ExpressionNode> expressions = new ArrayList<ExpressionNode>();

expressions.add(variableWrite(mgenc, varname, receiver, section));
expressions.add(exp);

while (accept(SemiColon)) {
expressions.add(messages(mgenc, receiver));
expressions.add(messages(mgenc, variableRead(mgenc, varname, section)));
}

return createSequenceNode(coord, expressions);
}

}
return exp;
}
Expand Down

3 comments on commit cb28ee4

@smarr
Copy link

@smarr smarr commented on cb28ee4 Jul 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just out of interest, why did you decide to introduce a temp variable instead of introducing an interpreter node? Seems actually like a nice solution.

@smarr
Copy link

@smarr smarr commented on cb28ee4 Jul 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the tests are this SOM-st/SOM@09b275b...c6507b1, right?

Looks good to me, too. 👍

@gefarion
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smarr, I tried this simple implementation before code a interpreter node. I am still experimenting and learning about TruffleSOM object model.

Please sign in to comment.