[Swift-commit] cog r3881
swift at ci.uchicago.edu
swift at ci.uchicago.edu
Sat Mar 22 02:05:03 CDT 2014
------------------------------------------------------------------------
r3881 | hategan | 2014-03-22 02:00:54 -0500 (Sat, 22 Mar 2014) | 1 line
re-organized some functions a bit and added join(list, delimiter)
------------------------------------------------------------------------
Index: modules/karajan/src/org/globus/cog/karajan/analyzer/RootScope.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/analyzer/RootScope.java (revision 3880)
+++ modules/karajan/src/org/globus/cog/karajan/analyzer/RootScope.java (working copy)
@@ -23,9 +23,9 @@
import org.globus.cog.karajan.compiled.nodes.SetVarK;
import org.globus.cog.karajan.compiled.nodes.functions.ChannelArg;
import org.globus.cog.karajan.compiled.nodes.functions.List;
-import org.globus.cog.karajan.compiled.nodes.functions.Misc;
import org.globus.cog.karajan.compiled.nodes.functions.Named;
import org.globus.cog.karajan.compiled.nodes.functions.NumericValue;
+import org.globus.cog.karajan.compiled.nodes.functions.Str;
import org.globus.cog.karajan.compiled.nodes.functions.Variable;
import org.globus.cog.karajan.compiled.nodes.user.Function;
import org.globus.cog.karajan.compiled.nodes.user.Lambda;
@@ -52,7 +52,7 @@
addDef("k", "function", new JavaDef(Function.class));
addDef("k", "lambda", new JavaDef(Lambda.class));
addDef("k", "channel", new JavaDef(ChannelArg.class));
- addDef("k", "concat", new JavaDef(Misc.Concat.class));
+ addDef("k", "concat", new JavaDef(Str.Concat.class));
addVar("#context", context);
addVar("#properties", props);
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/Str.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/Str.java (revision 0)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/Str.java (revision 3881)
@@ -0,0 +1,239 @@
+//----------------------------------------------------------------------
+//This code is developed as part of the Java CoG Kit project
+//The terms of the license can be found at http://www.cogkit.org/license
+//This message may not be removed or altered.
+//----------------------------------------------------------------------
+
+/*
+ * Created on Mar 21, 2014
+ */
+package org.globus.cog.karajan.compiled.nodes.functions;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import k.rt.Channel;
+import k.rt.ExecutionException;
+import k.rt.Stack;
+
+import org.globus.cog.karajan.analyzer.ArgRef;
+import org.globus.cog.karajan.analyzer.ChannelRef;
+import org.globus.cog.karajan.analyzer.CompilationException;
+import org.globus.cog.karajan.analyzer.Param;
+import org.globus.cog.karajan.analyzer.Scope;
+import org.globus.cog.karajan.compiled.nodes.Node;
+import org.globus.cog.karajan.parser.WrapperNode;
+import org.globus.cog.karajan.util.TypeUtil;
+
+public class Str {
+ public static class Concat extends AbstractSingleValuedFunction {
+ private ChannelRef<Object> c_vargs;
+
+ @Override
+ protected Param[] getParams() {
+ return params("...");
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ StringBuilder sb = new StringBuilder();
+ for (Object o : c_vargs.get(stack)) {
+ sb.append(TypeUtil.toString(o));
+ }
+ return sb.toString();
+ }
+ }
+
+ public static class Join extends AbstractSingleValuedFunction {
+ private ChannelRef<Object> c_vargs;
+
+ @Override
+ protected Param[] getParams() {
+ return params("...");
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ Channel<Object> args = c_vargs.get(stack);
+ if (args.size() > 2) {
+ throw new ExecutionException("Too many arguments: " + args.size());
+ }
+ if (args.isEmpty()) {
+ throw new ExecutionException("Too few arguments");
+ }
+ @SuppressWarnings("unchecked")
+ Collection<Object> items = (Collection<Object>) args.get(0);
+ String delim = " ";
+ if (args.size() == 2) {
+ delim = (String) args.get(1);
+ }
+ StringBuilder sb = new StringBuilder();
+ Iterator<Object> i = items.iterator();
+ while (i.hasNext()) {
+ sb.append(i.next());
+ if (i.hasNext()) {
+ sb.append(delim);
+ }
+ }
+ return sb.toString();
+ }
+ }
+
+
+ public static class SubString extends AbstractSingleValuedFunction {
+ private ArgRef<String> string;
+ private ArgRef<Number> from;
+ private ArgRef<Number> to;
+
+ @Override
+ protected Param[] getParams() {
+ return params("string", "from", optional("to", Integer.MAX_VALUE));
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ int to = this.to.getValue(stack).intValue();
+ String str = string.getValue(stack);
+ if (to == Integer.MAX_VALUE) {
+ return str.substring(from.getValue(stack).intValue());
+ }
+ else if (to < 0) {
+ to += str.length() - 1;
+ }
+ return str.substring(from.getValue(stack).intValue(), to);
+ }
+ }
+
+ public static class Matches extends AbstractSingleValuedFunction {
+ private ArgRef<Object> string;
+ private ArgRef<String> pattern;
+
+ @Override
+ protected Param[] getParams() {
+ return params("string", "pattern");
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ Object o = string.getValue(stack);
+ String str;
+ if (o instanceof Exception) {
+ str = ((Exception) o).getMessage();
+ }
+ else {
+ str = String.valueOf(o);
+ }
+ if (str == null) {
+ return false;
+ }
+ else {
+ String pat = pattern.getValue(stack);
+ Pattern p = Pattern.compile(pat, Pattern.DOTALL);
+ return p.matcher(str).matches();
+ }
+ }
+ }
+
+ public static class Split extends AbstractSingleValuedFunction {
+ private ArgRef<String> string;
+ private ArgRef<String> separator;
+
+ @Override
+ protected Param[] getParams() {
+ return params("string", "separator");
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ String str = string.getValue(stack);
+ String sep = separator.getValue(stack);
+
+ List<String> list = new ArrayList<String>();
+ int index = -1;
+ int last = 0;
+ do {
+ index = str.indexOf(sep, index + sep.length());
+ if (index > -1) {
+ if (last < index) {
+ list.add(str.substring(last, index));
+ }
+ last = index + sep.length();
+ }
+ } while (index != -1);
+ list.add(str.substring(last, str.length()));
+ return list;
+ }
+ }
+
+ public static class Strip extends AbstractSingleValuedFunction {
+ private ArgRef<String> string;
+
+ @Override
+ protected Param[] getParams() {
+ return params("string");
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ return string.getValue(stack).trim();
+ }
+ }
+
+ public static class Chr extends AbstractSingleValuedFunction {
+ private ArgRef<Number> code;
+
+ @Override
+ protected Param[] getParams() {
+ return params("code");
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ return (char) code.getValue(stack).intValue();
+ }
+ }
+
+ public static class Quote extends AbstractSingleValuedFunction {
+ private ArgRef<String> str;
+
+ @Override
+ protected Param[] getParams() {
+ return params("str");
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ return '"' + str.getValue(stack) + '"';
+ }
+ }
+
+ public static class ParseNumber extends AbstractSingleValuedFunction {
+ private ArgRef<String> str;
+
+ @Override
+ protected Param[] getParams() {
+ return params("str");
+ }
+
+ @Override
+ protected Node compileBody(WrapperNode w, Scope argScope, Scope scope)
+ throws CompilationException {
+ if (str.getValue() != null && staticReturn(scope, Double.parseDouble(str.getValue()))) {
+ return null;
+ }
+ else {
+ return super.compileBody(w, argScope, scope);
+ }
+ }
+
+ @Override
+ public Object function(Stack stack) {
+ String str = this.str.getValue(stack);
+ return Double.parseDouble(str);
+ }
+ }
+
+}
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/Misc.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/Misc.java (revision 3880)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/Misc.java (working copy)
@@ -430,137 +430,7 @@
}
}
}
-
- public static class Concat extends AbstractSingleValuedFunction {
- private ChannelRef<Object> c_vargs;
-
- @Override
- protected Param[] getParams() {
- return params("...");
- }
-
- @Override
- public Object function(Stack stack) {
- StringBuilder sb = new StringBuilder();
- for (Object o : c_vargs.get(stack)) {
- sb.append(TypeUtil.toString(o));
- }
- return sb.toString();
- }
- }
- public static class SubString extends AbstractSingleValuedFunction {
- private ArgRef<String> string;
- private ArgRef<Number> from;
- private ArgRef<Number> to;
-
- @Override
- protected Param[] getParams() {
- return params("string", "from", optional("to", Integer.MAX_VALUE));
- }
-
- @Override
- public Object function(Stack stack) {
- int to = this.to.getValue(stack).intValue();
- String str = string.getValue(stack);
- if (to == Integer.MAX_VALUE) {
- return str.substring(from.getValue(stack).intValue());
- }
- else if (to < 0) {
- to += str.length() - 1;
- }
- return str.substring(from.getValue(stack).intValue(), to);
- }
- }
-
- public static class Matches extends AbstractSingleValuedFunction {
- private ArgRef<Object> string;
- private ArgRef<String> pattern;
-
- @Override
- protected Param[] getParams() {
- return params("string", "pattern");
- }
-
- @Override
- public Object function(Stack stack) {
- Object o = string.getValue(stack);
- String str;
- if (o instanceof Exception) {
- str = ((Exception) o).getMessage();
- }
- else {
- str = String.valueOf(o);
- }
- if (str == null) {
- return false;
- }
- else {
- String pat = pattern.getValue(stack);
- Pattern p = Pattern.compile(pat, Pattern.DOTALL);
- return p.matcher(str).matches();
- }
- }
- }
-
- public static class Split extends AbstractSingleValuedFunction {
- private ArgRef<String> string;
- private ArgRef<String> separator;
-
- @Override
- protected Param[] getParams() {
- return params("string", "separator");
- }
-
- @Override
- public Object function(Stack stack) {
- String str = string.getValue(stack);
- String sep = separator.getValue(stack);
-
- List<String> list = new ArrayList<String>();
- int index = -1;
- int last = 0;
- do {
- index = str.indexOf(sep, index + sep.length());
- if (index > -1) {
- if (last < index) {
- list.add(str.substring(last, index));
- }
- last = index + sep.length();
- }
- } while (index != -1);
- list.add(str.substring(last, str.length()));
- return list;
- }
- }
-
- public static class ParseNumber extends AbstractSingleValuedFunction {
- private ArgRef<String> str;
-
- @Override
- protected Param[] getParams() {
- return params("str");
- }
-
- @Override
- protected Node compileBody(WrapperNode w, Scope argScope, Scope scope)
- throws CompilationException {
- if (str.getValue() != null && staticReturn(scope, Double.parseDouble(str.getValue()))) {
- return null;
- }
- else {
- return super.compileBody(w, argScope, scope);
- }
- }
-
- @Override
- public Object function(Stack stack) {
- String str = this.str.getValue(stack);
- return Double.parseDouble(str);
- }
- }
-
-
public static class Discard extends InternalFunction {
private ChannelRef<Object> c_vargs;
@@ -690,21 +560,7 @@
return c1.compareTo(o2);
}
};
-
- public static class Strip extends AbstractSingleValuedFunction {
- private ArgRef<String> string;
- @Override
- protected Param[] getParams() {
- return params("string");
- }
-
- @Override
- public Object function(Stack stack) {
- return string.getValue(stack).trim();
- }
- }
-
public static class DotIterator implements Iterator<Object> {
private final Iterator<Object>[] its;
private boolean next, nextValid;
@@ -853,34 +709,7 @@
}
}
- public static class Chr extends AbstractSingleValuedFunction {
- private ArgRef<Number> code;
- @Override
- protected Param[] getParams() {
- return params("code");
- }
-
- @Override
- public Object function(Stack stack) {
- return (char) code.getValue(stack).intValue();
- }
- }
-
- public static class Quote extends AbstractSingleValuedFunction {
- private ArgRef<String> str;
-
- @Override
- protected Param[] getParams() {
- return params("str");
- }
-
- @Override
- public Object function(Stack stack) {
- return '"' + str.getValue(stack) + '"';
- }
- }
-
public static class Stats extends AbstractSingleValuedFunction {
private ArgRef<Boolean> asMap;
Index: modules/karajan/resources/sys.k
===================================================================
--- modules/karajan/resources/sys.k (revision 3880)
+++ modules/karajan/resources/sys.k (working copy)
@@ -55,13 +55,14 @@
export(unique, def("org.globus.cog.karajan.compiled.nodes.functions.Unique"))
namespace(str) {
- export(strip, def("org.globus.cog.karajan.compiled.nodes.functions.Misc$Strip"))
- export(split, def("org.globus.cog.karajan.compiled.nodes.functions.Misc$Split"))
- export(matches, def("org.globus.cog.karajan.compiled.nodes.functions.Misc$Matches"))
- export(chr, def("org.globus.cog.karajan.compiled.nodes.functions.Misc$Chr"))
- export(substring, def("org.globus.cog.karajan.compiled.nodes.functions.Misc$SubString"))
- export(quote, def("org.globus.cog.karajan.compiled.nodes.functions.Misc$Quote"))
- export(number, def("org.globus.cog.karajan.compiled.nodes.functions.Misc$ParseNumber"))
+ export(strip, def("org.globus.cog.karajan.compiled.nodes.functions.Str$Strip"))
+ export(split, def("org.globus.cog.karajan.compiled.nodes.functions.Str$Split"))
+ export(matches, def("org.globus.cog.karajan.compiled.nodes.functions.Str$Matches"))
+ export(chr, def("org.globus.cog.karajan.compiled.nodes.functions.Str$Chr"))
+ export(substring, def("org.globus.cog.karajan.compiled.nodes.functions.Str$SubString"))
+ export(quote, def("org.globus.cog.karajan.compiled.nodes.functions.Str$Quote"))
+ export(number, def("org.globus.cog.karajan.compiled.nodes.functions.Str$ParseNumber"))
+ export(join, def("org.globus.cog.karajan.compiled.nodes.functions.Str$Join"))
}
export(time, def("org.globus.cog.karajan.compiled.nodes.functions.Time"))
More information about the Swift-commit
mailing list