Permalink
Browse files

explicit dyad support

  • Loading branch information...
joebo committed Nov 10, 2015
1 parent 974ab06 commit 3e3e5bdb1fba3b4e4de681150d4d05f907fe2991
Showing with 37 additions and 8 deletions.
  1. +31 −6 primitives.cs
  2. +1 −1 repl.cs
  3. +5 −1 tests.ijs
View
@@ -1233,6 +1233,10 @@ public class Verbs {
var verb = ((A<Verb>)method).Ravel[0];
var verbs = (A<Verb>)method;
if (verb.explicitDef != null) {
return runExplicit(verb.explicitDef, y, x);
}
if (verb.childVerb != null) {
var v = verb.childVerb as A<Verb>;
if (v != null) {
@@ -1578,36 +1582,39 @@ public class Verbs {
}
public AType runExplicit(string def, AType y) {
public AType runExplicit(string def, AType y, AType x = null) {
var oldLocals = Conjunctions.Parser.LocalNames;
//ensure locals are restored after running an explicit (multiple depths of explicit)
try {
if (oldLocals != null) { Conjunctions.Parser.LocalNames = new Dictionary<string, AType>(oldLocals); }
return _runExplicit(def, y);
return _runExplicit(def, y, x);
}
finally {
Conjunctions.Parser.LocalNames = oldLocals;
}
}
private AType _runExplicit(string def, AType y) {
private AType _runExplicit(string def, AType y, AType x) {
var lines = def.Split('\n').SelectMany(x => {
var line = x.Trim(new char[] { ' ', '\t' });
var lines = def.Split('\n').SelectMany(xv => {
var line = xv.Trim(new char[] { ' ', '\t' });
if (line.StartsWith("if.")) {
//normalize def so if/elseif/else start on its own line to simplify parsing
line = line.Replace(" if.", "\nif.").Replace(" elseif.", "\nelseif.").Replace(" else.", "\nelse.");
}
return line.Split('\n');
}).Where(x => x.Length > 0 && !x.StartsWith("NB.")).ToArray();
}).Where(xv => xv.Length > 0 && !xv.StartsWith("NB.")).ToArray();
var parser = Conjunctions.Parser;
if (parser.LocalNames == null) {
parser.LocalNames = new Dictionary<string, AType>();
}
parser.LocalNames["y"] = y;
if (x != null) {
parser.LocalNames["x"] = x;
}
AType ret = null;
for (var i = 0; i < lines.Length; i++) {
var line = lines[i];
@@ -2985,6 +2992,13 @@ public AType rank1ex<T>(AType method, A<T> y)
return Verbs.runExplicit(verb.rhs.TrimStart('\'').TrimEnd('\''), y);
}
}
else if (verb.conj == ":" && verb.op == "4") {
if (verb.rhs == "0") {
var v = new A<Verb>(0);
v.Ravel[0] = new Verb { explicitDef = y.ToString() };
return v;
}
}
else if (verb.conj == "!:" && verb.op == "0") {
return runfile((A<Box>) y,verb);
}
@@ -3077,6 +3091,16 @@ public AType rank1ex<T>(AType method, A<T> y)
}
return Verbs.InvokeExpression("rank2ex", x, y, 2, this,method);
}
else if (verb.conj == ":" && verb.op == "4") {
if (verb.rhs == "0") {
var v = new A<Verb>(0);
v.Ravel[0] = new Verb { explicitDef = y.ToString() };
return v;
}
else {
return Verbs.runExplicit(verb.rhs.TrimStart('\'').TrimEnd('\''), y, x);
}
}
else if (verb.conj == "!:" && verb.op == "151" && verb.rhs == "0") {
return readmmap((A<Box>)x, (A<Box>)y, verb);
}
@@ -3095,6 +3119,7 @@ public AType rank1ex<T>(AType method, A<T> y)
else if (verb.conj == "!:" && verb.op == "151" && verb.rhs == "5") {
return readTableKey((A<Box>)x, (A<Box>)y);
}
throw new NotImplementedException(verb + " on y:" + y + " type: " + y.GetType());
}
View
@@ -125,7 +125,7 @@ public static class Program
};
var ret = repl.parse(line).ToString();
if (testMode && ret != "1" && !line.EndsWith("3 : 0")) {
if (testMode && ret != "1" && !line.EndsWith("3 : 0") && !line.EndsWith("4 : 0")) {
var eqIdx = line.IndexOf("=");
var rerun = "";
if (eqIdx > -1) {
View
@@ -96,7 +96,11 @@ NB. 0 conjunction
'abc' -: (0 : 'abc')
NB. explicit verb
NB. 'abc' -: (3 : '1+1') ''
((3 : '1+1') 0) -: 2
((3 : 'y+1') 1) -: 2
(1 (4 : 'y+x') 1) -: 2
(_1 p: 10 20 50 100) -: 4 8 15 25 NB. Primes less than
(_1 p: 17 37 79 101) -: 6 11 21 25 NB. Primes less than (prime arguments)

0 comments on commit 3e3e5bd

Please sign in to comment.