Jelajahi Sumber

SS compiler advancements

Will 4 bulan lalu
induk
melakukan
93082a41e8
2 mengubah file dengan 172 tambahan dan 33 penghapusan
  1. 94 9
      SS.k
  2. 78 24
      excelpm.k

+ 94 - 9
SS.k

@@ -1,11 +1,11 @@
 \d u
 \l utils.k
 
-\d pat
-\l excelpm.k
-
+\d csv
 \l ngnk-libs/csv/csv.k
+\d .
 
+\l excelpm.k
 \d .
 
 /helpers
@@ -15,6 +15,78 @@ listize: {1_(,/(";",)'{"(",x,")"}'x)};
 split: {[l;di] idx: (-1,&(di~'l)),(#l); {slice[x;y[0]+1;y[1]+1]}[l]'(idx@(2':!#idx))};
 s2n: {[s] `i$s;}; // string to number
 parseidx: {core: slice[x;1;#x]; split[core;";"]};
+gi: {[r;c;rb;cb]
+ i: r#(,!c)
+ i: ((!r)+rb) {x,'y}'(i+cb);
+ :i
+};
+
+// decode excel formula
+// fill these ==========
+mc: {[c;i]
+ i: gi[(#table);i+1;0;c];
+ :i
+};
+
+mr: {[r;i]
+ i: gi[i+1;(#table[0]);r;0];
+ :i
+};
+// =====================
+
+drr: {[s;e]
+ r: $[s[0]<0;mc[s[1];(e[1]-s[1])];mr[s[0];(e[0]-s[0])]];
+ :r
+};
+
+dre: {[s;e]
+ :gi[(e[0]-s[0])+1;(e[1]-s[1])+1;s[0];s[1]]
+};
+
+dr: {[s;e] // decode range
+ ln: (s[0]<0) | (s[1]<0);
+ rn: (e[0]<0) | (e[1]<0);
+
+ :$[ln|rn;drr[s;e];dre[s;e]]
+};
+
+dbit: {[b]
+ ci: (`i$(b@&(pat.testAtZ'b)))-65; // col index
+ ri: (`i$(b@&(pat.test0t9'b)))-49; // row index
+
+ ci: $[(#ci)>0;*ci;-1];
+ ri: $[(#ri)>0;*ri;-1];
+
+ :(ri;ci)
+};
+
+lr: {[f]
+ ci: *&(":"~'f);
+ l: slice[f;0;ci];
+ r: slice[f;ci+1;#f];
+
+ lb: dbit[l];
+ rb: dbit[r];
+
+ :(lb;rb);
+};
+
+de:{[idx;cell]
+ ci: cell[0];
+ cell: cell[1];
+ idx: (*idx);
+ f: slice[cell;idx[0];idx[1]];
+
+ l: slice[cell;0;idx[0]];
+ r: slice[cell;idx[1];#cell];
+
+ (fl;fr): lr[f];
+ deps[ci[0];ci[1]]:: (1;1);
+
+ u.ppr "de: ", f;
+ u.ppr (fl;fr);
+ u.ppr'deps;
+};
 
 /:parseidx["[1;2]"];
 /:split["1;2;3";";"];
@@ -25,12 +97,25 @@ astkey: "Actions"; / key indicating start of actions section
 sectionkeys: (tstkey;cstkey;astkey);
 
 lines: {x,"\n",y}/(0: "SS.csv");
-table: csv.csv[lines];
+table:: csv.csv[lines];
+deps:: (#table;#table[0])#0;
 
 / excel patterns
-pats: *''|''(pat.run''table);
+pats: pat.run''table;
 
-:pats {
- u.ppr (x;y);
- u.ppr $[x[0]>4;slice[y;x[1];x[2]];'$'];
-}''table;
+u.ppr'table;
+/ u.ppr'pats;
+
+// start with last to maintain prior index accuracy
+lasts:{:((#x[0])-1)_x[0]}''pats;
+
+f: pats {
+ :$[x[2]=5;y;0]
+}''lasts;
+
+ti: gi[#table;#table[0];0;0];
+table :ti {(,x),,y}''table;
+
+/ table {
+/  :$[y~0;0;de[y;x]]
+/ }''f

+ 78 - 24
excelpm.k

@@ -1,3 +1,6 @@
+// TODO: clean this up
+\d u
+\l utils.k
 / :`i$"A"; // 65
 / :`i$"Z"; // 90
 / :`i$"0"; // 48
@@ -5,6 +8,7 @@
 / :`i$":"; // 58
 / :`i$"$"; // 36
 
+\d pat
 testAtZ: { i:`i$x; $[(i>64) & (i<91);1;0] };
 test0t9: { i:`i$x; $[(i>47) & (i<58);1;0] };
 testcol: { i:`i$x; $[i=58;1;0] };
@@ -12,35 +16,85 @@ testend: { i:`i$x; $[i=36;1;0] };
 
 s0: ((testAtZ; 1);(test0t9; 2));
 s1: ((test0t9; 2);(testcol; 3));
-s2: ((testend; 7); (testcol;5));
-s3: (,(testAtZ; 5));
+s2: ((testend; 7); (testcol; 3));
+s3: ((test0t9; 5);(testAtZ; 5));
 s4: ((testAtZ; 5); (test0t9; 5));
-s5: (,(testend; 6));
-s7: (({`0:"SUCCESS"};6));
+s5: (,(test0t9; 5));
+
 // 7 is done
-states:: (s0;s1;s2;s3;s4;s5;s7);
+states:: (s0;s1;s2;s3;s4;s5);
 i: 1;
 
+runS: {[c;s]
+ :{
+  :$[x[0][y];x[1];0]
+ }[;c]'states[s]
+};
+
+// blk 1 stuff ===============================
+blk1: {[a;c]
+ / u.ppr "blk1";
+ activeidx: 0;
+ active: (0;a[3]+1);
+ s: 0;
+
+ r: runS[c;s];
+ r: *($[(+/r)>0;r^0;0]);
+
+ a0: $[s>4;s;r];
+ :((,active);activeidx;1;a[3])
+};
+
+// blk 2 stuff ===============================
+e:{[a;c;r]
+ / u.ppr ("edge";r;a;c);
+ // add indices
+ indices: a[0],(,(a[3];a[3]));
+ a[2]: r;
+ a[1] +: 1;
+
+ :(indices; a[1]; a[2]; a[3])
+};
+
+ne:{[a;c;r]
+ / u.ppr "no edge";
+
+ ai: a[1];
+ a[0;ai;0]: $[a[2]>0;a[0;ai;0];a[3]];
+ a[0;ai;1]: a[3]+1;
+
+ a[2]: r;
+
+ :a
+};
+
+blk2: {[a;c]
+ / u.ppr ("blk2"; a);
+
+ s: a[2];
+ r: runS[c;s];
+ r: *($[(+/r)>0;r^0;0]);
+
+ / u.ppr(c;r;a[2]);
+
+ edge: (s=((#states) - 1))&(r=0);
+ a[3] +: 1;
+ :$[edge;e[a;c;r];ne[a;c;r]]
+};
+
 run: {[str]
- // go over each char and run cur statoe
- // cur state; start of pattern; length of pattern    <- args
- idx::0;
- o:(0;(0;0);1) {[a;c]
-  activeidx: (*|a);
-  indices: a[activeidx];
-
-  s: a[0];
-  r:{
-   :$[x[0][y];x[1];0]
-  }[;c]'states[s];
-
-  r: *($[(+/r)>0;r^0;0]);
-
-  idx +: 1;
-  a0: $[s>4;s;r];
-
-  :(a0;($[(s=0)&(r>0);idx-1;indices[0]];$[r=5;idx;indices[1]]);activeidx)
- }\str;
+ / u.ppr ("finding pattern in: ", str);
+ // ((0),-1,0) -> 0: list of start/end indices, 1: active index, 2: state the machine stopped on, 3: idx
+ o:((0),-1,0,0) {[a;c]
+  // if activeidx = -1 => blk1
+  // else blk2
+  / u.ppr "          ";
+  :$[a[1]=-1;blk1[a;c];blk2[a;c]]
+ }/str;
 
  :o
 };
+
+//            01234567890
+/ u.ppr (run["C1:C1-A1:A1"])
+\d .