Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New IR Operand Complex
  • Loading branch information
enebo committed Oct 29, 2014
1 parent 00ba272 commit d35c59d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyComplex.java
Expand Up @@ -133,7 +133,7 @@ public static RubyComplex newComplexRaw(Ruby runtime, IRubyObject x, IRubyObject
/** rb_complex_raw1
*
*/
static RubyComplex newComplexRaw(Ruby runtime, IRubyObject x) {
public static RubyComplex newComplexRaw(Ruby runtime, IRubyObject x) {
return new RubyComplex(runtime, runtime.getComplex(), x, RubyFixnum.zero(runtime));
}

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/jruby/ast/ComplexNode.java
Expand Up @@ -37,4 +37,8 @@ public List<Node> childNodes() {
public NodeType getNodeType() {
return NodeType.COMPLEXNODE;
}

public Node getNumber() {
return y;
}
}
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Expand Up @@ -376,6 +376,7 @@ private Operand buildOperand(Node node, IRScope s) throws NotCompilableException
case CLASSVARDECLNODE: return buildClassVarDecl((ClassVarDeclNode) node, s);
case COLON2NODE: return buildColon2((Colon2Node) node, s);
case COLON3NODE: return buildColon3((Colon3Node) node, s);
case COMPLEXNODE: return buildComplex((ComplexNode) node, s);
case CONSTDECLNODE: return buildConstDecl((ConstDeclNode) node, s);
case CONSTNODE: return searchConst(s, ((ConstNode) node).getName());
case DASGNNODE: return buildDAsgn((DAsgnNode) node, s);
Expand Down Expand Up @@ -1269,6 +1270,10 @@ public Operand buildColon3(Colon3Node node, IRScope s) {
return searchConstInInheritanceHierarchy(s, new ObjectClass(), node.getName());
}

public Operand buildComplex(ComplexNode node, IRScope s) {
return new Complex((ImmutableLiteral) build(node.getNumber(), s));
}

interface CodeBlock {
public Operand run();
}
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/ir/IRVisitor.java
Expand Up @@ -139,6 +139,7 @@ private void error(Object object) {
public void Boolean(Boolean bool) { error(bool); }
public void UnboxedBoolean(UnboxedBoolean bool) { error(bool); }
public void ClosureLocalVariable(ClosureLocalVariable closurelocalvariable) { error(closurelocalvariable); }
public void Complex(Complex complex) { error(complex); }
public void CurrentScope(CurrentScope currentscope) { error(currentscope); }
public void DynamicSymbol(DynamicSymbol dynamicsymbol) { error(dynamicsymbol); }
public void Fixnum(Fixnum fixnum) { error(fixnum); }
Expand Down
39 changes: 39 additions & 0 deletions core/src/main/java/org/jruby/ir/operands/Complex.java
@@ -0,0 +1,39 @@
package org.jruby.ir.operands;

import org.jruby.RubyComplex;
import org.jruby.ir.IRVisitor;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

/**
* Represents a Complex literal.
*/
public class Complex extends ImmutableLiteral {
private ImmutableLiteral number;

public Complex(ImmutableLiteral number) {
super(OperandType.COMPLEX);

this.number = number;
}

@Override
public Object createCacheObject(ThreadContext context) {
return RubyComplex.newComplexRaw(context.runtime,
context.runtime.newFixnum(0), (IRubyObject) number.cachedObject(context));
}

@Override
public String toString() {
return number + "i";
}

@Override
public void visit(IRVisitor visitor) {
visitor.Complex(this);
}

public Operand getNumber() {
return number;
}
}
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/ir/operands/OperandType.java
Expand Up @@ -14,6 +14,7 @@ public enum OperandType {
BIGNUM((byte) 'B'),
BOOLEAN((byte) 'b'),
LOCAL_VARIABLE((byte) 'l'), // Also applicable for ClosureLocalVariable
COMPLEX((byte) 'C'),
COMPOUND_ARRAY((byte) 'c'),
COMPOUND_STRING((byte) '"'),
CURRENT_SCOPE((byte) 's'),
Expand Down

0 comments on commit d35c59d

Please sign in to comment.