6666import org .prism .Nodes .*;
6767import org .jruby .prism .parser .ParseResultPrism ;
6868
69- import java .math .BigDecimal ;
69+ import java .math .BigInteger ;
7070import java .util .ArrayList ;
7171import java .util .HashSet ;
7272import java .util .List ;
73+ import java .util .Map ;
7374import java .util .Set ;
7475
7576import static org .jruby .ir .instructions .RuntimeHelperCall .Methods .*;
@@ -351,6 +352,8 @@ protected Operand build(Variable result, Node node) {
351352 return buildReturn ((ReturnNode ) node );
352353 } else if (node instanceof SelfNode ) {
353354 return buildSelf ();
355+ } else if (node instanceof ShareableConstantNode ) {
356+ return buildShareableConstant ((ShareableConstantNode ) node );
354357 } else if (node instanceof SingletonClassNode ) {
355358 return buildSingletonClass ((SingletonClassNode ) node );
356359 } else if (node instanceof SourceEncodingNode ) {
@@ -568,7 +571,7 @@ private Operand buildBegin(BeginNode node) {
568571 RescueNode rescue = node .rescue_clause ;
569572 Node ensureBody = node .ensure_clause != null ? node .ensure_clause .statements : null ;
570573 return buildEnsureInternal (node .statements , node .else_clause , rescue .exceptions , rescue .statements ,
571- rescue .consequent , false , ensureBody , true , rescue .reference );
574+ rescue .subsequent , false , ensureBody , true , rescue .reference );
572575 } else if (node .ensure_clause != null ) {
573576 EnsureNode ensure = node .ensure_clause ;
574577 return buildEnsureInternal (node .statements , null , null , null , null , false , ensure .statements , false , null );
@@ -820,11 +823,11 @@ private Operand buildCallOrWrite(CallOrWriteNode node) {
820823 }
821824
822825 private Operand buildCase (CaseNode node ) {
823- return buildCase (node .predicate , node .conditions , node .consequent );
826+ return buildCase (node .predicate , node .conditions , node .else_clause );
824827 }
825828
826829 private Operand buildCaseMatch (CaseMatchNode node ) {
827- return buildPatternCase (node .predicate , node .conditions , node .consequent );
830+ return buildPatternCase (node .predicate , node .conditions , node .else_clause );
828831 }
829832
830833 private Operand buildClass (ClassNode node ) {
@@ -1397,7 +1400,7 @@ private Operand buildHash(Node[] elements, boolean hasAssignments) {
13971400 }
13981401
13991402 private Operand buildIf (Variable result , IfNode node ) {
1400- return buildConditional (result , node .predicate , node .statements , node .consequent );
1403+ return buildConditional (result , node .predicate , node .statements , node .subsequent );
14011404 }
14021405
14031406 private Operand buildIndexAndWrite (IndexAndWriteNode node ) {
@@ -1467,7 +1470,7 @@ protected Operand buildDRegex(Variable result, Node[] children, RegexpOptions op
14671470 // value of the regexp. Adding an empty string will pick up the encoding from options (this
14681471 // empty string is how legacy parsers do this but it naturally falls out of the parser.
14691472 pieces = new Node [children .length + 1 ];
1470- pieces [0 ] = new StringNode ((short ) 0 , EMPTY .bytes (), 0 , 0 );
1473+ pieces [0 ] = new StringNode (0 , 0 , (short ) 0 , EMPTY .bytes ());
14711474 pieces [1 ] = children [0 ];
14721475 } else {
14731476 pieces = children ;
@@ -1600,7 +1603,7 @@ private Operand buildMatchPredicate(MatchPredicateNode node) {
16001603 }
16011604
16021605 private Operand buildMatchRequired (MatchRequiredNode node ) {
1603- return buildPatternCase (node .value , new Node [] { new InNode (node . pattern , null , 0 , 0 ) }, null );
1606+ return buildPatternCase (node .value , new Node [] { new InNode (0 , 0 , node . pattern , null ) }, null );
16041607 }
16051608
16061609 private Operand buildMatchWrite (Variable result , MatchWriteNode node ) {
@@ -1805,19 +1808,15 @@ private Operand buildPreExecution(PreExecutionNode node) {
18051808 }
18061809
18071810 private Operand buildRational (RationalNode node ) {
1808- if (node .numeric instanceof FloatNode ) {
1809- BigDecimal bd = new BigDecimal (bytelistFrom (node .numeric ).toString ());
1810- BigDecimal denominator = BigDecimal .ONE .scaleByPowerOfTen (bd .scale ());
1811- BigDecimal numerator = bd .multiply (denominator );
1812-
1813- try {
1814- return new Rational (fix (numerator .longValueExact ()), fix (denominator .longValueExact ()));
1815- } catch (ArithmeticException ae ) {
1816- return new Rational (new Bignum (numerator .toBigIntegerExact ()), new Bignum (denominator .toBigIntegerExact ()));
1817- }
1818- }
1811+ ImmutableLiteral num = asRationalValue (node .numerator );
1812+ ImmutableLiteral den = asRationalValue (node .denominator );
1813+ return new Rational (num , den );
1814+ }
18191815
1820- return new Rational ((ImmutableLiteral ) build (node .numeric ), fix (1 ));
1816+ private ImmutableLiteral asRationalValue (Object value ) {
1817+ return value instanceof Integer ? new Fixnum ((int ) value ) :
1818+ value instanceof Long ? new Fixnum ((long ) value ) :
1819+ new Bignum ((BigInteger ) value );
18211820 }
18221821
18231822 private Operand buildRange (RangeNode node ) {
@@ -1891,6 +1890,11 @@ private Operand buildRestKeywordArgs(KeywordHashNode keywordArgs, int[] flags) {
18911890 return splatValue ;
18921891 }
18931892
1893+ private Operand buildShareableConstant (ShareableConstantNode node ) {
1894+ // FIXME: We do not implement shareable constants yet
1895+ return build (node .write );
1896+ }
1897+
18941898 private Operand buildSingletonClass (SingletonClassNode node ) {
18951899 return buildSClass (node .expression , node .body ,
18961900 createStaticScopeFrom (node .locals , StaticScope .Type .LOCAL ), getLine (node ), getEndLine (node ));
@@ -1945,22 +1949,22 @@ private Operand buildUndef(UndefNode node) {
19451949 }
19461950
19471951 private Operand buildUnless (Variable result , UnlessNode node ) {
1948- return buildConditional (result , node .predicate , node .consequent , node .statements );
1952+ return buildConditional (result , node .predicate , node .else_clause , node .statements );
19491953 }
19501954
19511955 private Operand buildUntil (UntilNode node ) {
19521956 return buildConditionalLoop (node .predicate , node .statements , false , !node .isBeginModifier ());
19531957 }
19541958
19551959 private void buildWhenSplatValues (Variable eqqResult , Node node , Operand testValue , Label bodyLabel ,
1956- Set <IRubyObject > seenLiterals ) {
1960+ Set <IRubyObject > seenLiterals , Map < IRubyObject , java . lang . Integer > origLocs ) {
19571961 // FIXME: could see errors since this is missing whatever is YARP args{cat,push}?
19581962 if (node instanceof StatementsNode ) {
1959- buildWhenValues (eqqResult , ((StatementsNode ) node ).body , testValue , bodyLabel , seenLiterals );
1963+ buildWhenValues (eqqResult , ((StatementsNode ) node ).body , testValue , bodyLabel , seenLiterals , origLocs );
19601964 } else if (node instanceof SplatNode ) {
1961- buildWhenValue (eqqResult , testValue , bodyLabel , node , seenLiterals , true );
1965+ buildWhenValue (eqqResult , testValue , bodyLabel , node , seenLiterals , origLocs , true );
19621966 } else {
1963- buildWhenValue (eqqResult , testValue , bodyLabel , node , seenLiterals , true );
1967+ buildWhenValue (eqqResult , testValue , bodyLabel , node , seenLiterals , origLocs , true );
19641968 }
19651969 }
19661970
@@ -1992,7 +1996,7 @@ protected Node bodyFor(RescueNode node) {
19921996
19931997 @ Override
19941998 protected RescueNode optRescueFor (RescueNode node ) {
1995- return node .consequent ;
1999+ return node .subsequent ;
19962000 }
19972001
19982002 // FIXME: Implement
@@ -2228,19 +2232,20 @@ private Operand buildStatements(StatementsNode node) {
22282232 }
22292233
22302234 @ Override
2231- protected void buildWhenArgs (WhenNode whenNode , Operand testValue , Label bodyLabel , Set <IRubyObject > seenLiterals ) {
2235+ protected void buildWhenArgs (WhenNode whenNode , Operand testValue , Label bodyLabel ,
2236+ Set <IRubyObject > seenLiterals , Map <IRubyObject , Integer > origLocs ) {
22322237 Variable eqqResult = temp ();
22332238 Node [] exprNodes = whenNode .conditions ;
22342239
22352240 if (exprNodes .length == 1 ) {
22362241 if (exprNodes [0 ] instanceof SplatNode ) {
2237- buildWhenSplatValues (eqqResult , exprNodes [0 ], testValue , bodyLabel , seenLiterals );
2242+ buildWhenSplatValues (eqqResult , exprNodes [0 ], testValue , bodyLabel , seenLiterals , origLocs );
22382243 } else {
2239- buildWhenValue (eqqResult , testValue , bodyLabel , exprNodes [0 ], seenLiterals , false );
2244+ buildWhenValue (eqqResult , testValue , bodyLabel , exprNodes [0 ], seenLiterals , origLocs , false );
22402245 }
22412246 } else {
22422247 for (Node value : exprNodes ) {
2243- buildWhenValue (eqqResult , testValue , bodyLabel , value , seenLiterals , value instanceof SplatNode );
2248+ buildWhenValue (eqqResult , testValue , bodyLabel , value , seenLiterals , origLocs , value instanceof SplatNode );
22442249 }
22452250 }
22462251 }
@@ -2428,10 +2433,10 @@ protected Variable buildPatternEach(Label testEnd, Variable result, Operand orig
24282433 buildFindPattern (testEnd , result , deconstructed , node .constant , node .left , node .requireds , node .right , value , inAlternation , isSinglePattern , errorString );
24292434 } else if (exprNodes instanceof IfNode ) {
24302435 IfNode node = (IfNode ) exprNodes ;
2431- buildPatternEachIf (result , original , deconstructed , value , node .predicate , node .statements , node .consequent , inAlternation , isSinglePattern , errorString );
2436+ buildPatternEachIf (result , original , deconstructed , value , node .predicate , node .statements , node .subsequent , inAlternation , isSinglePattern , errorString );
24322437 } else if (exprNodes instanceof UnlessNode ) {
24332438 UnlessNode node = (UnlessNode ) exprNodes ;
2434- buildPatternEachIf (result , original , deconstructed , value , node .predicate , node .consequent , node .statements , inAlternation , isSinglePattern , errorString );
2439+ buildPatternEachIf (result , original , deconstructed , value , node .predicate , node .else_clause , node .statements , inAlternation , isSinglePattern , errorString );
24352440 } else if (exprNodes instanceof LocalVariableTargetNode ) {
24362441 buildPatternLocal ((LocalVariableTargetNode ) exprNodes , value , inAlternation );
24372442 } else if (exprNodes instanceof ImplicitNode ) {
@@ -2452,7 +2457,7 @@ protected Variable buildPatternEach(Label testEnd, Variable result, Operand orig
24522457 Operand expression = build (exprNodes );
24532458 boolean needsSplat = exprNodes instanceof AssocSplatNode ; // FIXME: just a guess this is all we need for splat?
24542459
2455- addInstr (new EQQInstr (scope , result , expression , value , needsSplat , scope .maybeUsingRefinements ()));
2460+ addInstr (new EQQInstr (scope , result , expression , value , needsSplat , false , scope .maybeUsingRefinements ()));
24562461 }
24572462
24582463 return result ;
@@ -2509,7 +2514,7 @@ protected void buildAssocs(Label testEnd, Operand original, Variable result, Has
25092514 // FIXME: only build literals (which are guaranteed to build without raising).
25102515 Operand key = build (((AssocNode ) node ).key );
25112516 call (result , d , "key?" , key );
2512- cond_ne (testEnd , result , tru () );
2517+ cond_ne_true (testEnd , result );
25132518
25142519 String method = hasRest ? "delete" : "[]" ;
25152520 Operand value = call (temp (), d , method , key );
@@ -2528,7 +2533,7 @@ protected void buildAssocs(Label testEnd, Operand original, Variable result, Has
25282533 buildPatternEach (testEnd , result , original , copy (nil ()), value , ((AssocNode ) node ).value , inAlteration , isSinglePattern , errorString );
25292534 }
25302535
2531- cond_ne (testEnd , result , tru () );
2536+ cond_ne_true (testEnd , result );
25322537 }
25332538 }
25342539 }
@@ -2784,6 +2789,11 @@ protected Operand putConstant(ConstantPathNode path, Operand value) {
27842789 return putConstant (buildModuleParent (path .parent ), path .name , value );
27852790 }
27862791
2792+ @ Override
2793+ protected Operand putConstant (ConstantPathNode path , CodeBlock valueBuilder ) {
2794+ return putConstant (buildModuleParent (path .parent ), path .name , valueBuilder .run ());
2795+ }
2796+
27872797 protected RubySymbol symbol (SymbolNode node ) {
27882798 short flags = node .flags ;
27892799 Encoding encoding = SymbolFlags .isForcedUsAsciiEncoding (flags ) ? USASCIIEncoding .INSTANCE :
0 commit comments