Skip to content

Commit

Permalink
⋄ forces arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dzaima committed May 13, 2020
1 parent 6ab8066 commit 29f2cf2
Showing 7 changed files with 49 additions and 28 deletions.
15 changes: 9 additions & 6 deletions src/APL/Exec.java
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import APL.tokenizer.Token;
import APL.tokenizer.types.*;
import APL.types.*;
import APL.types.arrs.DoubleArr;
import APL.types.arrs.*;
import APL.types.dimensions.*;
import APL.types.functions.*;
import APL.types.functions.builtins.AbstractSet;
@@ -684,8 +684,11 @@ private Obj valueOfRaw(Token t) {
int size = ts.size();
if (size == 0) return new StrMap();
LineTok fst = ts.get(0);
if (size == 1 && fst.colonPos() == -1) return Main.exec(ts.get(0), sc);
if (fst.tokens != null && fst.colonPos() != -1) {
if (size==1 && fst.colonPos()==-1) {
if (((ParenTok) t).hasDmd) return new Shape1Arr(Main.vexec(ts.get(0), sc));
return Main.exec(ts.get(0), sc);
}
if (fst.tokens != null && fst.colonPos() != -1) { // map constants
Scope nsc = new Scope(sc);
StrMap res = new StrMap(nsc);
for (LineTok ct : ts) {
@@ -703,17 +706,17 @@ private Obj valueOfRaw(Token t) {
res.setStr(key, val);
}
return res;
} else {
} else { // array
Obj fo = Main.oexec(fst, sc);
if (fo instanceof Value) {
if (fo instanceof Value) { // value array
Value[] vs = new Value[size];
for (int i = 0; i < ts.size(); i++) {
Obj o = Main.oexec(ts.get(i), sc);
if (!(o instanceof Value)) throw new DomainError("⋄-array contained " + o.humanType(true), o);
vs[i] = (Value) o;
}
return Arr.create(vs);
} else if (fo instanceof Fun) {
} else if (fo instanceof Fun) { // function array
Obj[] os = new Obj[size];
for (int i = 0; i < ts.size(); i++) {
Obj o = Main.oexec(ts.get(i), sc);
3 changes: 1 addition & 2 deletions src/APL/Main.java
Original file line number Diff line number Diff line change
@@ -405,10 +405,9 @@ public static ChrArr toAPL(String s) {
}


static String repeat(String s, int l) {
public static String repeat(String s, int l) {
StringBuilder r = new StringBuilder();
for (int i = 0; i < l; i++) r.append(s);
return r.toString();
}

}
15 changes: 8 additions & 7 deletions src/APL/tokenizer/Tokenizer.java
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@ LineTok tok() {
static class Block { // temp storage of multiple lines
final ArrayList<Line> a;
final char b;
boolean hasDmd = false;
private final int pos;

Block(ArrayList<Line> a, char b, int pos) {
@@ -71,7 +72,7 @@ public static BasicLines tokenize(String raw) {
return tokenize(raw, false);
}

public static BasicLines tokenize(String raw, boolean pointless) { // pointless means unevaled things get tokens
public static BasicLines tokenize(String raw, boolean pointless) { // pointless means unevaled things get tokens; mainly for syntax highlighting
int li = 0;
int len = raw.length();

@@ -124,13 +125,13 @@ public static BasicLines tokenize(String raw, boolean pointless) { // pointless
Token r;
switch (c) {
case ')':
r = new ParenTok(raw, closed.pos, i + 1, lineTokens);
r = new ParenTok(raw, closed.pos, i + 1, lineTokens, closed.hasDmd);
break;
case '}':
r = new DfnTok(raw, closed.pos, i + 1, lineTokens);
break;
case ']':
r = new BracketTok(raw, closed.pos, i + 1, lineTokens);
r = new BracketTok(raw, closed.pos, i + 1, lineTokens, closed.hasDmd);
break;
default:
throw new Error("this should really not happen "+c);
@@ -272,8 +273,8 @@ public static BasicLines tokenize(String raw, boolean pointless) { // pointless
i++;
tokens.add(new StrTok(raw, li, i, str.toString()));
} else if (c == '\n' || c == '⋄' || c == '\r' || c == ';') {
if (c == '⋄' && pointless) tokens.add(new DiamondTok(raw, i));

if (c=='⋄' && pointless) tokens.add(new DiamondTok(raw, i));
if (c=='⋄' || c=='\n') expr.hasDmd = true;
if (c == ';') tokens.add(new SemiTok(raw, i, i + 1));

if (tokens.size() > 0) {
@@ -323,13 +324,13 @@ public static BasicLines tokenize(String raw, boolean pointless) { // pointless
Token r;
switch (closed.b) {
case ')':
r = new ParenTok(raw, closed.pos, len, lineTokens);
r = new ParenTok(raw, closed.pos, len, lineTokens, closed.hasDmd);
break;
case '}':
r = new DfnTok(raw, closed.pos, len, lineTokens);
break;
case ']':
r = new BracketTok(raw, closed.pos, len, lineTokens);
r = new BracketTok(raw, closed.pos, len, lineTokens, closed.hasDmd);
break;
default:
throw new Error("this should really not happen "+closed.b);
7 changes: 6 additions & 1 deletion src/APL/tokenizer/types/BracketTok.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package APL.tokenizer.types;

import APL.errors.DomainError;

import java.util.List;

public class BracketTok extends TokArr<LineTok> {
public final boolean array;

public BracketTok(String line, int spos, int epos, List<LineTok> tokens) {
public BracketTok(String line, int spos, int epos, List<LineTok> tokens, boolean hasDmd) {
super(line, spos, tokens);
array = tokens.size()>=2 || hasDmd;
end(epos);
if (tokens.size()==0 && hasDmd) throw new DomainError("[⋄] is not valid syntax", this);
}

@Override public String toRepr() {
5 changes: 4 additions & 1 deletion src/APL/tokenizer/types/ParenTok.java
Original file line number Diff line number Diff line change
@@ -3,8 +3,11 @@
import java.util.List;

public class ParenTok extends TokArr<LineTok> {
public ParenTok(String line, int spos, int epos, List<LineTok> tokens) {
public final boolean hasDmd;

public ParenTok(String line, int spos, int epos, List<LineTok> tokens, boolean hasDmd) {
super(line, spos, tokens);
this.hasDmd = hasDmd;
end(epos);
}

14 changes: 11 additions & 3 deletions src/APL/types/Arr.java
Original file line number Diff line number Diff line change
@@ -48,14 +48,22 @@ public String toString() {
if (rank == 0) return "⊂" + oneliner();
return oneliner();
} else {
if (rank == 0) return "⊂"+first().toString();
if (rank == 0) return "⊂"+first().toString().replace("\n", "\n ");
if (ia==1) {
Value c = get(0);
if (c instanceof Primitive || rank > 2) {
if (rank==1) return "⍮"+c;
String pre = Main.formatAPL(shape);
return pre + "⍴⊂" + c.toString().replace("\n", "\n" + Main.repeat(" ", pre.length()+2));
}
}
if (rank == 1) { // simple vectors
StringBuilder res = new StringBuilder();
var simple = true;
for (Value v : this) {
if (res.length() > 0) res.append(" ");
if (v == null) {
res.append("JAVANULL");
res.append("NULLPTR");
} else {
simple &= v instanceof Primitive;
res.append(v.toString());
@@ -96,7 +104,7 @@ public String toString() {
var simple = true;
int x=0, y=0;
for (Value v : this) {
if (v == null) v = Main.toAPL("JAVANULL");
if (v == null) v = Main.toAPL("NULLPTR");
simple &= v instanceof Primitive;
var c = v.toString().split("\n");
var cw = 0;
18 changes: 10 additions & 8 deletions src/APL/types/dimensions/Brackets.java
Original file line number Diff line number Diff line change
@@ -32,17 +32,19 @@ public String toString() {
}

public static Obj of(BracketTok t, Scope sc) {
if (t.tokens.size() == 0) return new Brackets(null);
if (t.tokens.size() == 1) {
if (t.array) {
Value[] lns = new Value[t.tokens.size()];
for (int i = 0; i < t.tokens.size(); i++) {
LineTok tk = t.tokens.get(i);
lns[i] = Main.vexec(tk, sc);
}
return UpArrowBuiltin.merge(lns, new int[]{lns.length}, new BracketFn(t));
} else {
if (t.tokens.size() == 0) return new Brackets(null);
assert t.tokens.size() == 1; // t.array is true if size>1
Value res = Main.vexec(t.tokens.get(0), sc);
return new Brackets(res);
}
Value[] lns = new Value[t.tokens.size()];
for (int i = 0; i < t.tokens.size(); i++) {
LineTok tk = t.tokens.get(i);
lns[i] = Main.vexec(tk, sc);
}
return UpArrowBuiltin.merge(lns, new int[]{lns.length}, new BracketFn(t));
}

private static class BracketFn extends Callable {

0 comments on commit 29f2cf2

Please sign in to comment.