Skip to content

Commit b387402

Browse files
committed
Update to latest prism
1 parent 5fc3192 commit b387402

7 files changed

Lines changed: 2737 additions & 333 deletions

File tree

src/main/java/org/jruby/prism/builder/IRBuilderPrism.java

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.jruby.ir.operands.MutableString;
4242
import org.jruby.ir.operands.NullBlock;
4343
import org.jruby.ir.operands.Operand;
44-
import org.jruby.ir.operands.Rational;
4544
import org.jruby.ir.operands.Regexp;
4645
import org.jruby.ir.operands.Splat;
4746
import org.jruby.ir.operands.Symbol;
@@ -66,7 +65,6 @@
6665
import org.prism.Nodes.*;
6766
import org.jruby.prism.parser.ParseResultPrism;
6867

69-
import java.math.BigDecimal;
7068
import java.util.ArrayList;
7169
import java.util.HashSet;
7270
import java.util.List;
@@ -390,7 +388,7 @@ protected Operand build(Variable result, Node node) {
390388
}
391389

392390
private Operand buildCallOperatorWrite(CallOperatorWriteNode node) {
393-
return buildOpAsgn(node.receiver, node.value, node.read_name, node.write_name, node.operator, node.isSafeNavigation());
391+
return buildOpAsgn(node.receiver, node.value, node.read_name, node.write_name, node.binary_operator, node.isSafeNavigation());
394392
}
395393

396394
private Operand buildImaginary(ImaginaryNode node) {
@@ -507,16 +505,7 @@ protected void buildAssignment(Node node, Operand rhsVal) {
507505
addInstr(new PutClassVariableInstr(classVarDefinitionContainer(), ((ClassVariableTargetNode) node).name, rhsVal));
508506
} else if (node instanceof ConstantPathTargetNode) {
509507
Operand parent = buildModuleParent(((ConstantPathTargetNode) node).parent);
510-
Node child = ((ConstantPathTargetNode) node).child;
511-
RubySymbol name;
512-
if (child instanceof ConstantTargetNode) {
513-
name = ((ConstantTargetNode) child).name;
514-
} else if (child instanceof ConstantReadNode) {
515-
name = ((ConstantReadNode) child).name;
516-
} else {
517-
throwSyntaxError(getLine(child), "Unknown child in ConstantPathTargetNode");
518-
name = null;
519-
}
508+
RubySymbol name = ((ConstantPathTargetNode) node).name;
520509
addInstr(new PutConstInstr(parent, name, rhsVal));
521510
} else if (node instanceof ConstantTargetNode) {
522511
addInstr(new PutConstInstr(getCurrentModuleVariable(), ((ConstantTargetNode) node).name, rhsVal));
@@ -844,15 +833,15 @@ private Operand buildClass(ClassNode node) {
844833
private Operand buildClassVariableOperatorWrite(ClassVariableOperatorWriteNode node) {
845834
Operand lhs = buildClassVar(temp(), node.name);
846835
Operand rhs = build(node.value);
847-
Variable value = call(temp(), lhs, node.operator, rhs);
836+
Variable value = call(temp(), lhs, node.binary_operator, rhs);
848837
addInstr(new PutClassVariableInstr(classVarDefinitionContainer(), node.name, value));
849838
return value;
850839
}
851840

852841
private Operand buildInstanceVariableOperatorWrite(InstanceVariableOperatorWriteNode node) {
853842
Operand lhs = buildInstVar(node.name);
854843
Operand rhs = build(node.value);
855-
Variable value = call(temp(), lhs, node.operator, rhs);
844+
Variable value = call(temp(), lhs, node.binary_operator, rhs);
856845
addInstr(new PutFieldInstr(buildSelf(), node.name, value));
857846
return value;
858847
}
@@ -861,7 +850,7 @@ private Operand buildLocalVariableOperatorWrite(LocalVariableOperatorWriteNode n
861850
int depth = staticScope.isDefined(node.name.idString()) >> 16;
862851
Variable lhs = getLocalVariable(node.name, depth);
863852
Operand rhs = build(node.value);
864-
Variable value = call(lhs, lhs, node.operator, rhs);
853+
Variable value = call(lhs, lhs, node.binary_operator, rhs);
865854
return value;
866855
}
867856

@@ -910,7 +899,7 @@ private Operand buildConstantAndWrite(ConstantAndWriteNode node) {
910899
private Operand buildConstantOperatorWrite(ConstantOperatorWriteNode node) {
911900
Operand lhs = searchConst(temp(), node.name);
912901
Operand rhs = build(node.value);
913-
Variable value = call(temp(), lhs, node.operator, rhs);
902+
Variable value = call(temp(), lhs, node.binary_operator, rhs);
914903
putConstant(buildSelf(), node.name, value);
915904
return value;
916905
}
@@ -923,7 +912,7 @@ private Operand buildConstantOrWrite(ConstantOrWriteNode node) {
923912

924913
private Operand buildConstantOrWritePath(ConstantPathOrWriteNode node) {
925914
// FIXME: unify with AST
926-
RubySymbol name = ((ConstantReadNode) node.target.child).name;
915+
RubySymbol name = ((ConstantPathNode) node.target).name;
927916
Variable result = temp();
928917
Label falseCheck = getNewLabel();
929918
Label done = getNewLabel();
@@ -944,7 +933,7 @@ private Operand buildConstantOrWritePath(ConstantPathOrWriteNode node) {
944933
}
945934

946935
private Operand buildConstantPath(Variable result, ConstantPathNode node) {
947-
return buildConstantPath(result, ((ConstantReadNode) node.child).name, node.parent);
936+
return buildConstantPath(result, node.name, node.parent);
948937
}
949938

950939
private Operand buildConstantPath(Variable result, RubySymbol name, Node parent) {
@@ -957,7 +946,7 @@ private Operand buildConstantPathAndWrite(ConstantPathAndWriteNode node) {
957946
}
958947

959948
private Operand buildConstantPathOperatorWrite(ConstantPathOperatorWriteNode node) {
960-
return buildOpAsgnConstDecl(node.target, node.value, node.operator);
949+
return buildOpAsgnConstDecl(node.target, node.value, node.binary_operator);
961950
}
962951

963952
private Operand buildConstantPathOrWrite(ConstantPathOrWriteNode node) {
@@ -978,7 +967,7 @@ private Operand buildConstantWritePath(ConstantPathWriteNode node) {
978967

979968
// Multiple assignments provide the value otherwise it is grabbed from .value on the node.
980969
private Operand buildConstantWritePath(ConstantPathNode path, Operand value) {
981-
return putConstant(buildModuleParent(path.parent), ((ConstantReadNode) path.child).name, value);
970+
return putConstant(buildModuleParent(path.parent), path.name, value);
982971
}
983972

984973
private Operand buildDef(DefNode node) {
@@ -1242,7 +1231,7 @@ public Operand run() {
12421231

12431232
ConstantPathNode path = (ConstantPathNode) node;
12441233

1245-
final RubySymbol name = ((ConstantReadNode) path.child).name;
1234+
final RubySymbol name = path.name;
12461235
final Variable errInfo = temp();
12471236

12481237
// store previous exception for restoration if we rescue something
@@ -1326,7 +1315,7 @@ private Operand buildGlobalVariableRead(Variable result, GlobalVariableReadNode
13261315
private Operand buildGlobalVariableOperatorWrite(GlobalVariableOperatorWriteNode node) {
13271316
Operand lhs = buildGlobalVar(temp(), node.name);
13281317
Operand rhs = build(node.value);
1329-
Variable value = call(temp(), lhs, node.operator, rhs);
1318+
Variable value = call(temp(), lhs, node.binary_operator, rhs);
13301319
addInstr(new PutGlobalVarInstr(node.name, value));
13311320
return value;
13321321
}
@@ -1414,7 +1403,7 @@ private Operand buildIndexAndWrite(IndexAndWriteNode node) {
14141403
}
14151404

14161405
private Operand buildIndexOperatorWrite(IndexOperatorWriteNode node) {
1417-
return buildOpElementAsgnWithMethod(node.receiver, node.arguments, node.block, node.value, node.operator);
1406+
return buildOpElementAsgnWithMethod(node.receiver, node.arguments, node.block, node.value, node.binary_operator);
14181407
}
14191408

14201409
private Operand buildIndexOrWrite(IndexOrWriteNode node) {
@@ -1814,19 +1803,7 @@ private Operand buildPreExecution(PreExecutionNode node) {
18141803
}
18151804

18161805
private Operand buildRational(RationalNode node) {
1817-
if (node.numeric instanceof FloatNode) {
1818-
BigDecimal bd = new BigDecimal(bytelistFrom(node.numeric).toString());
1819-
BigDecimal denominator = BigDecimal.ONE.scaleByPowerOfTen(bd.scale());
1820-
BigDecimal numerator = bd.multiply(denominator);
1821-
1822-
try {
1823-
return new Rational(fix(numerator.longValueExact()), fix(denominator.longValueExact()));
1824-
} catch (ArithmeticException ae) {
1825-
return new Rational(new Bignum(numerator.toBigIntegerExact()), new Bignum(denominator.toBigIntegerExact()));
1826-
}
1827-
}
1828-
1829-
return new Rational((ImmutableLiteral) build(node.numeric), fix(1));
1806+
return buildRational((Node) node.numerator, (Node) node.denominator);
18301807
}
18311808

18321809
private Operand buildRange(RangeNode node) {
@@ -2429,8 +2406,7 @@ protected Variable buildPatternEach(Label testEnd, Variable result, Operand orig
24292406
} else if (exprNodes instanceof HashPatternNode) {
24302407
HashPatternNode node = (HashPatternNode) exprNodes;
24312408
Node[] keys = getKeys(node);
2432-
Node rest = node.rest != null ? node.rest : getRestFromKeys(node);
2433-
buildHashPattern(testEnd, result, deconstructed, node.constant, node, keys, rest, value, inAlternation, isSinglePattern, errorString);
2409+
buildHashPattern(testEnd, result, deconstructed, node.constant, node, keys, node.rest, value, inAlternation, isSinglePattern, errorString);
24342410
} else if (exprNodes instanceof FindPatternNode) {
24352411
getManager().getRuntime().getWarnings().warnExperimental(getFileName(), getLine(exprNodes) + 1,
24362412
"Find pattern is experimental, and the behavior may change in future versions of Ruby!");
@@ -2482,17 +2458,6 @@ private Encoding getRegexpEncodingFromOptions(short flags) {
24822458
null;
24832459
}
24842460

2485-
private Node getRestFromKeys(HashPatternNode node) {
2486-
int length = node.elements.length;
2487-
2488-
// FIXME: can there be multiple assocsplat and why isn't this rest in HashPatternNode
2489-
for (int i = 0; i < length; i++) {
2490-
if (node.elements[i] instanceof AssocSplatNode) return node.elements[i];
2491-
}
2492-
2493-
return null;
2494-
}
2495-
24962461
private Node[] getKeys(HashPatternNode node) {
24972462
int length = node.elements.length;
24982463
Node[] keys = new Node[length];
@@ -2762,7 +2727,7 @@ private ByteList determineBaseName(Node node) {
27622727
if (node instanceof ConstantReadNode) {
27632728
return ((ConstantReadNode) node).name.getBytes();
27642729
} else if (node instanceof ConstantPathNode) {
2765-
return determineBaseName(((ConstantPathNode) node).child);
2730+
return ((ConstantPathNode) node).name.getBytes();
27662731
}
27672732
throw notCompilable("Unsupported node in module path", node);
27682733
}
@@ -2802,7 +2767,7 @@ private boolean hasOnlyRestKwargs(Node[] elements) {
28022767
}
28032768
@Override
28042769
protected Operand putConstant(ConstantPathNode path, Operand value) {
2805-
return putConstant(buildModuleParent(path.parent), ((ConstantReadNode) path.child).name, value);
2770+
return putConstant(buildModuleParent(path.parent), path.name, value);
28062771
}
28072772

28082773
protected RubySymbol symbol(SymbolNode node) {

src/main/java/org/jruby/prism/parser/ParserPrism.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.jruby.parser.ParserManager;
1414
import org.jruby.parser.ParserType;
1515
import org.jruby.parser.StaticScope;
16+
import org.jruby.runtime.Constants;
1617
import org.jruby.runtime.DynamicScope;
1718
import org.jruby.runtime.ThreadContext;
1819
import org.jruby.runtime.builtin.IRubyObject;
@@ -228,7 +229,11 @@ private byte[] generateMetadata(String fileName, int lineNumber, Encoding encodi
228229
metadata.append(runtime.getInstanceConfig().isFrozenStringLiteral() ? 1 : 0);
229230

230231
// version
231-
metadata.append(ParsingOptions.SyntaxVersion.V3_3_0.getValue());
232+
if (Constants.RUBY_MAJOR_VERSION.equals("3.3")) {
233+
metadata.append(ParsingOptions.SyntaxVersion.V3_3.getValue());
234+
} else {
235+
metadata.append(ParsingOptions.SyntaxVersion.LATEST.getValue());
236+
}
232237

233238
// Eval scopes (or none for normal parses)
234239
if (type == EVAL) {

0 commit comments

Comments
 (0)