Skip to content

Commit

Permalink
Showing 4 changed files with 35 additions and 118 deletions.
71 changes: 13 additions & 58 deletions truffle/src/main/java/org/jruby/truffle/parser/BodyTranslator.java
Original file line number Diff line number Diff line change
@@ -230,6 +230,7 @@
import org.jruby.truffle.parser.ast.ModuleParseNode;
import org.jruby.truffle.parser.ast.MultipleAsgnParseNode;
import org.jruby.truffle.parser.ast.NextParseNode;
import org.jruby.truffle.parser.ast.NilImplicitParseNode;
import org.jruby.truffle.parser.ast.NilParseNode;
import org.jruby.truffle.parser.ast.NthRefParseNode;
import org.jruby.truffle.parser.ast.OpAsgnAndParseNode;
@@ -471,21 +472,11 @@ public RubyNode visitBlockNode(BlockParseNode node) {
int lastLine = firstLine;

for (ParseNode child : node.children()) {
if (child.getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection.push(sourceSection);
} else {
if (child.getPosition() != InvalidSourcePosition.INSTANCE) {
lastLine = Math.max(lastLine, child.getPosition().getLine() + 1);
}

final RubyNode translatedChild;

try {
translatedChild = child.accept(this);
} finally {
if (child.getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection.pop();
}
}
final RubyNode translatedChild = translateNodeOrNil(sourceSection, child);

if (!(translatedChild instanceof DeadNode)) {
translatedChildren.add(translatedChild);
@@ -506,26 +497,12 @@ public RubyNode visitBlockNode(BlockParseNode node) {
@Override
public RubyNode visitBreakNode(BreakParseNode node) {
assert environment.isBlock() || translatingWhile : "The parser did not see an invalid break";

final RubySourceSection sourceSection = translate(node.getPosition());

RubyNode resultNode;

if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection.push(sourceSection);

try {
resultNode = node.getValueNode().accept(this);
} finally {
parentSourceSection.pop();
}
} else {
resultNode = node.getValueNode().accept(this);
}
final RubyNode resultNode = translateNodeOrNil(sourceSection, node.getValueNode());

final RubyNode ret = new BreakNode(environment.getBreakID(), translatingWhile, resultNode);
ret.unsafeSetSourceSection(sourceSection);

return addNewlineIfNeeded(node, ret);
}

@@ -1050,14 +1027,7 @@ private RubyNode openModule(RubySourceSection sourceSection, RubyNode defineOrGe
* </p>
*/
private ModuleBodyDefinitionNode compileClassNode(RubySourceSection sourceSection, String name, ParseNode bodyNode, boolean sclass) {
RubyNode body;

parentSourceSection.push(sourceSection);
try {
body = translateNodeOrNil(sourceSection, bodyNode);
} finally {
parentSourceSection.pop();
}
RubyNode body = translateNodeOrNil(sourceSection, bodyNode);

if (environment.getFlipFlopStates().size() > 0) {
body = sequence(context, source, sourceSection, Arrays.asList(initFlipFlopStates(sourceSection), body));
@@ -2105,17 +2075,7 @@ public RubyNode visitLocalAsgnNode(LocalAsgnParseNode node) {
if (node.getValueNode() == null) {
rhs = new DeadNode(context, sourceSection.toSourceSection(source), new Exception());
} else {
if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection.push(sourceSection);
}

try {
rhs = node.getValueNode().accept(this);
} finally {
if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection.pop();
}
}
rhs = node.getValueNode().accept(this);
}

final RubyNode ret = lhs.makeWriteNode(rhs);
@@ -2543,18 +2503,9 @@ public RubyNode visitNextNode(NextParseNode node) {

final boolean t = translatingNextExpression;
translatingNextExpression = true;

if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection.push(sourceSection);
}

try {
resultNode = node.getValueNode().accept(this);
resultNode = translateNodeOrNil(sourceSection, node.getValueNode());
} finally {
if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection.pop();
}

translatingNextExpression = t;
}

@@ -2565,7 +2516,11 @@ public RubyNode visitNextNode(NextParseNode node) {

@Override
public RubyNode visitNilNode(NilParseNode node) {
if (node.getPosition() == InvalidSourcePosition.INSTANCE && parentSourceSection.peek() == null) {
if (node instanceof NilImplicitParseNode) {
return addNewlineIfNeeded(node, new NilLiteralNode(context, null, true));
}

if (node.getPosition() == InvalidSourcePosition.INSTANCE) {
final RubyNode ret = new DeadNode(context, null, new Exception());
return addNewlineIfNeeded(node, ret);
}
@@ -3074,7 +3029,7 @@ public RubyNode visitRetryNode(RetryParseNode node) {
public RubyNode visitReturnNode(ReturnParseNode node) {
final RubySourceSection sourceSection = translate(node.getPosition());

RubyNode translatedChild = node.getValueNode().accept(this);
RubyNode translatedChild = translateNodeOrNil(sourceSection, node.getValueNode());

final RubyNode ret = new ReturnNode(environment.getReturnID(), translatedChild);
ret.unsafeSetSourceSection(sourceSection);
Original file line number Diff line number Diff line change
@@ -138,24 +138,17 @@ public BlockDefinitionNode compileBlockNode(RubySourceSection sourceSection, Str

final RubyNode preludeLambda = sequence(context, source, sourceSection, Arrays.asList(checkArity, NodeUtil.cloneNode(loadArguments)));

RubyNode body;

parentSourceSection.push(sourceSection);
try {
if (!translatingForStatement) {
// Make sure to declare block-local variables
for (String var : variables) {
environment.declareVar(var);
}
if (!translatingForStatement) {
// Make sure to declare block-local variables
for (String var : variables) {
environment.declareVar(var);
}
}

body = translateNodeOrNil(sourceSection, bodyNode);
RubyNode body = translateNodeOrNil(sourceSection, bodyNode);

if (context.getOptions().CHAOS) {
body = ChaosNodeGen.create(body);
}
} finally {
parentSourceSection.pop();
if (context.getOptions().CHAOS) {
body = ChaosNodeGen.create(body);
}

// Procs
@@ -238,8 +231,6 @@ public RubyNode doCompileMethodBody(RubySourceSection sourceSection, String meth
final LoadArgumentsTranslator loadArgumentsTranslator = new LoadArgumentsTranslator(currentNode, context, source, false, this);
final RubyNode loadArguments = argsNode.accept(loadArgumentsTranslator);

RubyNode body;

boolean isPrimitive = false;
if (bodyNode instanceof BlockParseNode) {
ParseNode[] statements = ((BlockParseNode) bodyNode).children();
@@ -253,15 +244,11 @@ public RubyNode doCompileMethodBody(RubySourceSection sourceSection, String meth
}
}

parentSourceSection.push(sourceSection);
try {
if (isPrimitive) {
body = translateRubiniusPrimitive(sourceSection, (BlockParseNode) bodyNode, loadArguments);
} else {
body = translateNodeOrNil(sourceSection, bodyNode);
}
} finally {
parentSourceSection.pop();
RubyNode body;
if (isPrimitive) {
body = translateRubiniusPrimitive(sourceSection, (BlockParseNode) bodyNode, loadArguments);
} else {
body = translateNodeOrNil(sourceSection, bodyNode);
}

final RubySourceSection bodySourceSection = body.getRubySourceSection();
@@ -477,24 +464,21 @@ private static String indentation(String line) {
* eagerly.
*/
public TranslatorState getCurrentState() {
return new TranslatorState(getEnvironment().unsafeGetLexicalScope(), getEnvironment().isDynamicConstantLookup(), new ArrayDeque<>(parentSourceSection));
return new TranslatorState(getEnvironment().unsafeGetLexicalScope(), getEnvironment().isDynamicConstantLookup());
}

public void restoreState(TranslatorState state) {
getEnvironment().getParseEnvironment().setDynamicConstantLookup(state.dynamicConstantLookup);
getEnvironment().getParseEnvironment().resetLexicalScope(state.scope);
parentSourceSection = state.parentSourceSection;
}

public static class TranslatorState {
private final LexicalScope scope;
private final boolean dynamicConstantLookup;
private final Deque<RubySourceSection> parentSourceSection;

private TranslatorState(LexicalScope scope, boolean dynamicConstantLookup, Deque<RubySourceSection> parentSourceSection) {
private TranslatorState(LexicalScope scope, boolean dynamicConstantLookup) {
this.scope = scope;
this.dynamicConstantLookup = dynamicConstantLookup;
this.parentSourceSection = parentSourceSection;
}
}

22 changes: 6 additions & 16 deletions truffle/src/main/java/org/jruby/truffle/parser/Translator.java
Original file line number Diff line number Diff line change
@@ -25,14 +25,13 @@
import org.jruby.truffle.language.locals.WriteLocalVariableNode;
import org.jruby.truffle.language.methods.Arity;
import org.jruby.truffle.language.objects.SelfNode;
import org.jruby.truffle.parser.ast.NilImplicitParseNode;
import org.jruby.truffle.parser.ast.ParseNode;
import org.jruby.truffle.parser.lexer.ISourcePosition;
import org.jruby.truffle.parser.lexer.InvalidSourcePosition;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -67,8 +66,6 @@ public abstract class Translator extends org.jruby.truffle.parser.ast.visitor.Ab
protected final RubyContext context;
protected final Source source;

protected Deque<RubySourceSection> parentSourceSection = new ArrayDeque<>();

public Translator(Node currentNode, RubyContext context, Source source) {
this.currentNode = currentNode;
this.context = context;
@@ -145,15 +142,8 @@ protected RubySourceSection translate(ISourcePosition sourcePosition) {
}

private RubySourceSection translate(Source source, ISourcePosition sourcePosition) {
if (sourcePosition == InvalidSourcePosition.INSTANCE) {
if (parentSourceSection.peek() == null) {
throw new UnsupportedOperationException("Truffle doesn't want invalid positions - find a way to give me a real position!");
} else {
return parentSourceSection.peek();
}
} else {
return new RubySourceSection(sourcePosition.getLine() + 1);
}
assert sourcePosition != InvalidSourcePosition.INSTANCE;
return new RubySourceSection(sourcePosition.getLine() + 1);
}

protected RubyNode nilNode(Source source, RubySourceSection sourceSection) {
@@ -162,10 +152,10 @@ protected RubyNode nilNode(Source source, RubySourceSection sourceSection) {

protected RubyNode translateNodeOrNil(RubySourceSection sourceSection, ParseNode node) {
final RubyNode rubyNode;
if (node != null) {
rubyNode = node.accept(this);
} else {
if (node == null || node instanceof NilImplicitParseNode) {
rubyNode = nilNode(source, sourceSection);
} else {
rubyNode = node.accept(this);
}
return rubyNode;
}
Original file line number Diff line number Diff line change
@@ -41,7 +41,6 @@
import org.jruby.truffle.language.methods.InternalMethod;
import org.jruby.truffle.language.methods.SharedMethodInfo;
import org.jruby.truffle.language.methods.UnsupportedOperationBehavior;
import org.jruby.truffle.parser.ast.NilParseNode;
import org.jruby.truffle.parser.ast.RootParseNode;
import org.jruby.truffle.parser.parser.ParserConfiguration;
import org.jruby.truffle.parser.scope.DynamicScope;
@@ -176,18 +175,7 @@ public RubyRootNode parse(RubyContext context, Source source, Encoding defaultEn

final BodyTranslator translator = new BodyTranslator(currentNode, context, null, environment, source, topLevel);

RubyNode truffleNode;

if (node.getBodyNode() == null || node.getBodyNode() instanceof NilParseNode) {
translator.parentSourceSection.push(rubySourceSection);
try {
truffleNode = translator.nilNode(source, rubySourceSection);
} finally {
translator.parentSourceSection.pop();
}
} else {
truffleNode = node.getBodyNode().accept(translator);
}
RubyNode truffleNode = translator.translateNodeOrNil(rubySourceSection, node.getBodyNode());

// Load arguments

0 comments on commit c8e4a45

Please sign in to comment.