Skip to content

Commit

Permalink
Array basic Array support, fix Fixnum#<=>.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Dec 11, 2012
1 parent 8a1f2b1 commit 5003157
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 4 deletions.
140 changes: 140 additions & 0 deletions src/main/java/RArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class RArray extends RObject implements List<RObject> {
public final ArrayList<RObject> impl;

public RArray() {
impl = new ArrayList<RObject>();
}

public RArray(RObject... args) {
impl = new ArrayList<RObject>(Arrays.asList(args));
}

public RArray(List<RObject> impl) {
this.impl = new ArrayList<RObject>(impl);
}

public RObject $less$less(RObject what) {
add(what);

return this;
}

public RObject $lbrack$rbrack(RObject where) {
int index = (int)((RFixnum)where.to_i()).fix;

if (index < size()) {
return get(index);
}

return RNil;
}

public RObject $lbrack$rbrack$equal(RObject where, RObject what) {
int index = (int)((RFixnum)where.to_i()).fix;

// TODO index >= size
impl.set(index, what);

return this;
}

public int size() {
return impl.size();
}

public boolean isEmpty() {
return impl.isEmpty();
}

public boolean contains(Object o) {
return impl.contains(o);
}

public Iterator<RObject> iterator() {
return impl.iterator();
}

public Object[] toArray() {
return impl.toArray();
}

public <T> T[] toArray(T[] a) {
return impl.toArray(a);
}

public boolean add(RObject e) {
return impl.add(e);
}

public boolean remove(Object o) {
return impl.remove(o);
}

public boolean containsAll(Collection<?> c) {
return impl.containsAll(c);
}

public boolean addAll(Collection<? extends RObject> c) {
return impl.addAll(c);
}

public boolean addAll(int index, Collection<? extends RObject> c) {
return impl.addAll(index, c);
}

public boolean removeAll(Collection<?> c) {
return impl.removeAll(c);
}

public boolean retainAll(Collection<?> c) {
return impl.retainAll(c);
}

public void clear() {
impl.clear();
}

public RObject get(int index) {
return impl.get(index);
}

public RObject set(int index, RObject element) {
return impl.set(index, element);
}

public void add(int index, RObject element) {
impl.add(index, element);
}

public RObject remove(int index) {
return impl.remove(index);
}

public int indexOf(Object o) {
return impl.indexOf(o);
}

public int lastIndexOf(Object o) {
return impl.lastIndexOf(o);
}

public ListIterator<RObject> listIterator() {
return impl.listIterator();
}

public ListIterator<RObject> listIterator(int index) {
return impl.listIterator(index);
}

public List<RObject> subList(int fromIndex, int toIndex) {
return new RArray(impl.subList(fromIndex, toIndex));
}
}
4 changes: 2 additions & 2 deletions src/main/java/RFixnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public RObject to_i() {

public RObject $less$equal$greater(RObject other) {
if (other instanceof RFloat) {
return new RFixnum(Long.compare(fix, (long)((RFloat)other).flo));
return new RFixnum(Long.valueOf(fix).compareTo((long)((RFloat)other).flo));
} else {
return new RFixnum(Long.compare(fix, ((RFixnum)other.to_i()).fix));
return new RFixnum(Long.valueOf(fix).compareTo(((RFixnum)other.to_i()).fix));
}
}

Expand Down
22 changes: 20 additions & 2 deletions src/main/ruby/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module FastRuby
RBoolean.java
RString.java
RFloat.java
RArray.java
]

module JDTUtils
Expand Down Expand Up @@ -521,6 +522,20 @@ def visitIfNode(node)
nil
end

def visitZArrayNode(node)
ast.new_class_instance_creation.tap do |ary|
ary.type = ast.new_simple_type(ast.new_simple_name('RArray'))
end
end

def visitArrayNode(node)
visitZArrayNode(node).tap do |ary|
node.child_nodes.each do |element|
ary.arguments << ExpressionCompiler.new(ast, body_compiler, element).start
end
end
end

=begin
def visitConstDeclNode(node)
#print " #{node.name} = "
Expand All @@ -534,8 +549,9 @@ def visitConstNode(node)

def safe_name(name)
new_name = ''

name.chars.each do |ch|
new_name << case name
new_name << case ch
when '+'; '$plus'
when '-'; '$minus'
when '*'; '$times'
Expand All @@ -549,7 +565,9 @@ def safe_name(name)
when '^'; '$up'
when '?'; '$qmark'
when '|'; '$bar'
else; ch
when '['; '$lbrack'
when ']'; '$rbrack'
else; ch;
end
end

Expand Down

0 comments on commit 5003157

Please sign in to comment.