Permalink
Browse files

Lidt smårettelser her og der, fik det til at compile igen, lavede en …

…"ruby.bat" så man kan kalde hotruby fra kommandolinjen.

git-svn-id: https://svn.trifork.com/svn/trifork/projects/hotruby@43 931d27ba-d1ef-0310-baf8-e73a6a934ec9
  • Loading branch information...
ofo
ofo committed Jan 21, 2007
1 parent 04de4ee commit 1a7ee01a520f2d90da7cbaf1566e1db6ac33a8cf
View
Binary file not shown.
@@ -1,5 +1,6 @@
package com.trifork.hotruby.classes;
+import com.trifork.hotruby.callable.PublicMethod0;
import com.trifork.hotruby.objects.IRubyObject;
import com.trifork.hotruby.objects.RubyMatchData;
import com.trifork.hotruby.runtime.MetaClass;
@@ -19,6 +20,13 @@ public IRubyObject call(IRubyObject receiver, IRubyObject[] args, RubyBlock bloc
return ((RubyMatchData)receiver).array_access(args);
}
});
+
+ meta.register_instance_method("to_a", new PublicMethod0() {
+ @Override
+ public IRubyObject call(IRubyObject receiver, RubyBlock block) {
+ return ((RubyMatchData)receiver).to_a();
+ }
+ });
}
}
@@ -2,16 +2,18 @@
import com.trifork.hotruby.callable.PublicMethod0;
import com.trifork.hotruby.callable.PublicMethod1;
import com.trifork.hotruby.objects.IRubyObject;
+import com.trifork.hotruby.objects.RubyArray;
import com.trifork.hotruby.objects.RubyString;
import com.trifork.hotruby.runtime.MetaClass;
import com.trifork.hotruby.runtime.RubyBlock;
+import com.trifork.hotruby.runtime.Selector;
public class RubyClassString
extends RubyBaseClassString
{
@Override
public void init(MetaClass meta) {
super.init(meta);
-
+
meta.register_instance_method("to_s", new PublicMethod0() {
@Override
public IRubyObject call(IRubyObject receiver, RubyBlock block) {
@@ -26,5 +28,13 @@ public IRubyObject call(IRubyObject receiver, IRubyObject arg, RubyBlock block)
}
});
+
+ meta.register_instance_method("==", new PublicMethod1() {
+
+ @Override
+ public IRubyObject call(IRubyObject receiver, IRubyObject arg, RubyBlock block) {
+ return ((RubyString)receiver).op_eq2(arg);
+ }});
+
}
}
@@ -12,11 +12,6 @@
java.math.BigInteger value;
- public IRubyFixnum inverse_cmp(int i) {
- // TODO: Kresten
- throw new UnsupportedOperationException("Not implemented yet");
- }
-
public long longValue() {
return value.longValue();
}
@@ -19,31 +19,29 @@ public IRubyObject array_access(IRubyObject[] args) {
IRubyArray result = LoadedRubyRuntime.instance.newArray();
for (int i = 0; i < args.length; i++) {
- result.add(get_match(args[i]));
+ int val = RubyInteger.mm_induced_from(args[i]).intValue();
+ result.add(get_match(val));
}
return result;
}
+
+ public IRubyObject to_a() {
+ IRubyArray result = LoadedRubyRuntime.instance.newArray();
- private IRubyObject get_match(IRubyObject object) {
-
- int val = RubyInteger.mm_induced_from(object).intValue();
-
- if (val < 0 || val > match.groupCount()) {
- return LoadedRubyRuntime.NIL;
+ for (int i = 0; i <= match.groupCount(); i++) {
+ result.add(get_match(i));
}
- try {
- int start = match.start(val);
- int end = match.end(val);
-
- CharSequence result_sequence = source.subSequence(start, end);
-
- return LoadedRubyRuntime.instance.newString(result_sequence);
-
- } catch (IndexOutOfBoundsException ex) {
+ return result;
+ }
+
+ private IRubyObject get_match(int val) {
+ if (val < 0 || val > match.groupCount())
+ {
return LoadedRubyRuntime.NIL;
}
+ CharSequence result_sequence = match.group(val);
+ return LoadedRubyRuntime.instance.newString(result_sequence);
}
-
}
@@ -10,7 +10,7 @@
import com.trifork.hotruby.runtime.Selector;
import com.trifork.hotruby.runtime.SingletonState;
-public class RubyObject extends RubyBaseObject {
+public class RubyObject extends RubyBaseObject implements IRubyObject {
SingletonState singleton;
public boolean isFrozen() {
@@ -4,36 +4,39 @@
import java.util.regex.Pattern;
import com.trifork.hotruby.runtime.LoadedRubyRuntime;
+import com.trifork.hotruby.util.regexp.RegularExpressionTranslator;
public class RubyRegexp extends RubyBaseRegexp {
- Pattern pattern;
+ String originalExpression;
+ RegularExpressionTranslator translator;
@Override
public String asSymbol() {
- return pattern.toString();
+ return originalExpression;
}
public int flags() {
- return pattern.flags();
+ return translator.getPattern().flags();
}
public RubyRegexp(String string, int flags) {
- pattern = Pattern.compile(string, flags);
+ originalExpression = string;
+ translator = new RegularExpressionTranslator(string);
}
@Override
public String inspect() {
- return "/" + pattern.toString() + "/";
+ return "/" + originalExpression + "/";
}
public IRubyObject match(IRubyObject expr) {
IRubyString string = RubyString.induce_from(expr);
String value = string.asSymbol();
- Matcher match = pattern.matcher(value);
+ Matcher match = translator.getPattern().matcher(value);
- if (!match.matches()) {
+ if (!match.find()) {
return LoadedRubyRuntime.NIL;
}
return new RubyMatchData().initialize(match, value);
@@ -43,9 +46,9 @@ public IRubyObject op_eqmatch(IRubyObject expr) {
IRubyString string = RubyString.induce_from(expr);
String value = string.asSymbol();
- Matcher match = pattern.matcher(value);
+ Matcher match = translator.getPattern().matcher(value);
- if (!match.matches()) {
+ if (!match.find()) {
return LoadedRubyRuntime.NIL;
}
return new RubyFixnum(match.start());
@@ -93,4 +93,9 @@ public static IRubyString induce_from(IRubyObject arg1) {
throw LoadedRubyRuntime.instance.newTypeError("object is not string");
}
+
+ // TODO: Kresten, der må være noget smart man skal gøre her?
+ public IRubyObject op_eq2(IRubyObject arg) {
+ return bool(arg instanceof RubyString && ((RubyString)arg).value.equals(value));
+ }
}
@@ -150,7 +150,7 @@ public static RubyMethod resolve_method(RubyObject object, Selector sel,
}
@Override
- public RuntimeException newArgumentError(String string) {
+ public RaiseException newArgumentError(String string) {
// TODO Auto-generated method stub
return null;
}
@@ -161,7 +161,7 @@ public RubyString newString(String value) {
}
@Override
- public RuntimeException newTypeError(String string) {
+ public RaiseException newTypeError(String string) {
// TODO Auto-generated method stub
return null;
}
@@ -279,7 +279,7 @@ public RuntimeException newLocalJumpError(String string, NonLocalJump ex) {
}
@Override
- public RuntimeException newNoMethodError(String string) {
+ public RaiseException newNoMethodError(String string) {
// TODO Auto-generated method stub
throw new RuntimeException(string);
}
View
@@ -1,33 +1,34 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>hotruby-shared</name>
- <comment></comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.antlr.eclipse.core.antlrbuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.jdt.core.javabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.antlr.eclipse.core.warningcleanerbuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.antlr.eclipse.core.smapbuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.antlr.eclipse.core.antlrnature</nature>
- <nature>org.eclipse.jdt.core.javanature</nature>
- </natures>
-</projectDescription>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>hotruby-shared</name>
+ <comment></comment>
+ <projects>
+ <project>hotruby-loaded</project>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.antlr.eclipse.core.antlrbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.antlr.eclipse.core.warningcleanerbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.antlr.eclipse.core.smapbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.antlr.eclipse.core.antlrnature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
@@ -67,23 +67,21 @@ def File.expand_path(file, dir_string=cwd)
module Kernel
def load(filename, wrap=false)
-
if (File.absolute? filename)
Kernel.eval_file filename, (wrap ? Module.new() : Object::MAIN)
return true
end
- ($:).each {|path_elem|
+ ($:).each do |path_elem|
full_name = File.join(path_elem, filename)
if (File.file? full_name)
Kernel.eval_file full_name, (wrap ? Module.new() : Object::MAIN)
return true
end
- }
+ end
throw LoadError.new "No such file to load -- #{filename}"
-
end
def require(filename)
@@ -95,25 +93,22 @@ def require(filename)
return true
end
- ($:).each {|path_elem|
- full_name = File.join(path_elem, filename)
+ ($:).each do |path_elem|
+ full_name = File.join(path_elem, filename)
- if (File.file? full_name)
- $" << filename
- Kernel.eval_file full_name, (wrap ? Module.new() : Object::MAIN)
- return true
+ if (File.file? full_name)
+ $" << filename
+ Kernel.eval_file full_name, (wrap ? Module.new() : Object::MAIN)
+ return true
+ end
end
- }
- if (/.rb$/ !~ file)
- require(filename + ".rb")
- end
+ if (/.rb$/ !~ file)
+ require(filename + ".rb")
+ end
- throw LoadError.new "No such file to load -- #{filename}"
-
-
+ throw LoadError.new "No such file to load -- #{filename}"
end
-
end
class Array
@@ -9,17 +9,29 @@
public static void main(String[] args) throws Exception
{
RubyConfig cfg = new RubyConfig();
-
cfg.setInputStream(System.in);
cfg.setOutputStream(System.out);
cfg.setErrorStream(System.err);
cfg.setArguments(args);
-
RubyRuntime rr = RubyRuntime.newRuntime(cfg);
+
+ // For quick'n'dirty testing, put "if (true)" here
+ if (true)
+ {
+ IRubyObject obj = rr.load("rb/richards.rb", rr.getObject());
+ System.out.println("=> " + obj.inspect());
+ return;
+ }
- IRubyObject obj = rr.load("rb/richards.rb", rr.getObject());
-
- System.out.println("=> " + obj.inspect());
+ // Can be called from the command line with a file name as argument
+ if (args.length == 0)
+ {
+ System.out.println("No file specified");
+ }
+ else
+ {
+ IRubyObject obj = rr.load(args[0], rr.getObject());
+ System.out.println("=> " + obj.inspect());
+ }
}
-
}
Oops, something went wrong.

0 comments on commit 1a7ee01

Please sign in to comment.