Forráskód Böngészése

SS compiler final touches

Will 1 hónapja
szülő
commit
9db7d683cb
2 módosított fájl, 209 hozzáadás és 27 törlés
  1. 67 27
      SS.k
  2. 142 0
      fb.k

+ 67 - 27
SS.k

@@ -1,54 +1,68 @@
+/ utils (ppr -> pretty print)
 \d u
 \l utils.k
 
+/ csv parser
 \d csv
 \l ngnk-libs/csv/csv.k
 \d .
 
+/ excel pattern finder
 \l excelpm.k
 \d .
 
-/helpers
-/ e exclusive
-slice: {[l;s;e] s_(e)#l};
-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]
+/ ====================================================================================
+/ helpers ============================================================================
+/ ====================================================================================
+slice: {[l;s;e] s_(e)#l}; / slice a list (l) from start (s) til end (e)
+listize: {1_(,/(";",)'{"(",x,")"}'x)}; / turn a list(l) of strings into a string representation of a k list
+split: {[l;di] idx: (-1,&(di~'l)),(#l); {slice[x;y[0]+1;y[1]+1]}[l]'(idx@(2':!#idx))}; / split a list(l) by delimiter (di)
+s2n: {[s] `i$s;}; / string to number
+parseidx: {core: slice[x;1;#x]; split[core;";"]}; / parse a string containing k index
+str: `k@;
+gi: {[r;c;rb;cb] / generate indices of a rowxcol table starting from row=rb and col=cb
  i: r#(,!c)
  i: ((!r)+rb) {x,'y}'(i+cb);
  :i
 };
+hasloops: {deps:&'x; d:deps; #,/((#deps)(,/'deps@)/d)>0}; / determine if dependency table contains loops
+order: { / come up with a order of calculations for a dependency graph
+ deps: {$[+/x>0;&x;()]}'x;
+ d: ?,/deps;
+ all: !(#deps);
+ :d,(all^d)
+};
 
-// decode excel formula
-mc: {[c;i]
+/ ====================================================================================
+/ excel formula handler ==============================================================
+/ ====================================================================================
+mc: {[c;i] / generate indices for column-based indexing
  i: gi[(#table);i+1;0;c];
  :i
 };
 
-mr: {[r;i]
+mr: {[r;i] / generate indices for row-based indexing
  i: gi[i+1;(#table[0]);r;0];
  :i
 };
 
-drr: {[s;e]
+drr: {[s;e] / decode range of indices from start(s) to end(e) with one indicator missing (full rows or full column selection)
  r: $[s[0]<0;mc[s[1];(e[1]-s[1])];mr[s[0];(e[0]-s[0])]];
  :r
 };
 
-dre: {[s;e]
+dre: {[s;e] / decode range of indices from start(s) to end(e) with both indices present
  :gi[(e[0]-s[0])+1;(e[1]-s[1])+1;s[0];s[1]]
 };
 
-dr: {[s;e] // decode range
+dr: {[s;e] / decode range from start(s) to end(e)
  ln: (s[0]<0) | (s[1]<0);
  rn: (e[0]<0) | (e[1]<0);
 
  :$[ln|rn;drr[s;e];dre[s;e]]
 };
 
-dbit: {[b]
+dbit: {[b] / decode bit - refering to A3 or C5 part of A3:C5
  ci: (`i$(b@&(pat.testAtZ'b)))-65; // col index
  ri: (`i$(b@&(pat.test0t9'b)))-49; // row index
 
@@ -58,7 +72,7 @@ dbit: {[b]
  :(ri;ci)
 };
 
-lr: {[f]
+lr: {[f] / turn A3:C5 into left and right bits
  ci: *&(":"~'f);
  l: slice[f;0;ci];
  r: slice[f;ci+1;#f];
@@ -69,7 +83,7 @@ lr: {[f]
  :(lb;rb);
 };
 
-de:{[idx;cell]
+de:{[idx;cell] / decode excel string in a cell
  ci: cell[0];
  cell: cell[1];
  idx: (*idx);
@@ -77,38 +91,42 @@ de:{[idx;cell]
  f: slice[cell;idx[0];idx[1]];
  l: slice[cell;0;idx[0]];
  r: slice[cell;idx[1];#cell];
- u.ppr (l;r);
- u.ppr table[ci[0];ci[1]];
 
  (fl;fr): lr[f];
- r: dr[fl;fr];
+ range: dr[fl;fr];
 
  i: (ci[0]*3) + ci[1];
  {
   i:(*y);
   i: (i[0]*3)+i[1];
   deps[x;i]: 1;
- }[(ci[0]*3)+ci[1];]'r;
+ }[(ci[0]*3)+ci[1];]'range;
+
+ / compiled list
+ cl: {i:(*x); "self[",(`k@x[0;0]),";",(`k@x[0;1]),"]"}'range;
+ cl: l,"(",({x,";",y}/cl),")",r;
+ table[ci[0];ci[1];1]:: cl;
 };
 
-/:parseidx["[1;2]"];
-/:split["1;2;3";";"];
+/ ====================================================================================
+/ Compiler ===========================================================================
+/ ====================================================================================
 
 tstkey: "States/Events"; / key indicating start of transition section
 cstkey: "Events"; / key indicating start of events condition section
 astkey: "Actions"; / key indicating start of actions section
 sectionkeys: (tstkey;cstkey;astkey);
 
-lines: {x,"\n",y}/(0: "SS.csv");
+fname: "SS.csv";
+tname: "SS";
+lines: {x,"\n",y}/(0: fname);
 table:: csv.csv[lines];
 d: (#table)*(#table[0]);
 deps:: (d;d)#0;
 
 / excel patterns
 :pats: pat.run''table;
-
 u.ppr'table;
-/ u.ppr'pats;
 
 // start with last to maintain prior index accuracy
 lasts:{:((#x[0])-1)_x[0]}''pats;
@@ -119,6 +137,7 @@ f: pats {
 }''lasts;
 
 ti: gi[#table;#table[0];0;0];
+ptable:: table;
 table::ti {(,x),,y}''table;
 
 table {
@@ -129,4 +148,25 @@ table {
  :$[y~0;0;de[y;x]]
 }''firsts;
 
-:deps
+ord:order[deps];
+
+/ ====================================================================================
+/ Generator ==========================================================================
+/ ====================================================================================
+compstr:: "/ compiled ",fname,"\n\n";
+al: {[l;indt] tab: "" {[x;y] x,"\t"}/(indt#0); compstr:: compstr,tab,l,"\n"}; // add line with indentation
+
+al[(tname,":{[]");0];
+al["self:(", (str (#table)),";", (str (#table[0])), ")#0;\n";1];
+
+{
+ d: #table;
+ i0: `i$(_(x%d));
+ i1: x-(i0*d);
+ al["self[",(str(i0)),";",(str(i1)),"]: ", table[i0;i1;1], ";";1];
+}'ord;
+
+al[":self";1];
+al[("};");0];
+
+`0: compstr;

+ 142 - 0
fb.k

@@ -0,0 +1,142 @@
+// The code is difficult to read; 
+// feels like there is a lot of scalar indexing code, but perhaps not
+// Every function should have a comment describing it
+// and ideally illusrate a simple input and output
+// Every variable which isn't obvious should be described by a comment 
+// such as \\ c is circumference of a cirlce radius r
+// Given SS are described as sheet of rows by cols of cells likely sheet or cells is a better name
+// than table? 
+// Functions which are long should be split as separate statements 
+
+\d u // What is this??? 
+\l utils.k
+
+\d csv // what is this if comment those things which people may not know if not on the same line on a previous line
+\l ngnk-libs/csv/csv.k
+\d .
+
+\l excelpm.k // what functions are you using 
+\d .
+
+/helpers can describe them in comments here or have comment with each helper
+/ e exclusive // what is "exclusive" 
+slice: {[l;s;e] s_(e)#l}; / brief comment
+listize: {1_(,/(";",)'{"(",x,")"}'x)}; // brief comment
+split: {[l;di] 
+        idx: (-1,&(di~'l)),(#l); // brief comment
+        {slice[x;y[0]+1;y[1]+1]}[l]'(idx@(2':!#idx))}; // brief comment
+s2n: {[s] `i$s;}; // string to number`
+parseidx: {core: slice[x;1;#x]; split[core;";"]}; // brief comment what is core?
+gi: {[r;c;rb;cb] // what is gi? rb, cb
+ i: r#(,!c)
+ i: ((!r)+rb) {x,'y}'(i+cb);
+ :i
+}; // brief comment I don't think functions require a closing ; unless ngnk does?
+
+// decode excel formula
+mc: {[c;i] // brief comment
+
+ i: gi[(#table);i+1;0;c];
+ :i
+}; 
+mr: {[r;i] // brief comment
+
+ i: gi[i+1;(#table[0]);r;0];
+ :i
+}; 
+drr: {[s;e] // brief comment
+ r: $[s[0]<0;mc[s[1];(e[1]-s[1])];mr[s[0];(e[0]-s[0])]];
+ :r
+}; 
+
+dre: {[s;e] // brief comment
+ :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]] // brief comment
+};
+
+dbit: {[b] // brief comment
+ 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] // brief comment
+ ci: *&(":"~'f);
+ l: slice[f;0;ci];
+ r: slice[f;ci+1;#f];
+
+ lb: dbit[l];
+ rb: dbit[r];
+
+ :(lb;rb);
+};
+
+de:{[idx;cell] // brief comment
+ ci: cell[0];
+ cell: cell[1];
+ idx: (*idx); // what is ix
+
+ f: slice[cell;idx[0];idx[1]]; // What is f
+ l: slice[cell;0;idx[0]]; // What is l 
+ r: slice[cell;idx[1];#cell];
+ u.ppr (l;r); // what is u.ppr
+ u.ppr table[ci[0];ci[1]];
+
+ (fl;fr): lr[f]; // brief comment
+ r: dr[fl;fr];
+
+ i: (ci[0]*3) + ci[1];
+ {
+  i:(*y);
+  i: (i[0]*3)+i[1];
+  deps[x;i]: 1;
+ }[(ci[0]*3)+ci[1];]'r;
+}; // brief comment
+
+/:parseidx["[1;2]"];
+/:split["1;2;3";";"];
+
+tstkey: "States/Events"; / key indicating start of transition section // SS dpn't have States/Events"
+cstkey: "Events"; / key indicating start of events condition section 
+astkey: "Actions"; / key indicating start of actions section
+sectionkeys: (tstkey;cstkey;astkey);
+
+lines: {x,"\n",y}/(0: "SS.csv");
+table:: csv.csv[lines];
+d: (#table)*(#table[0]);
+deps:: (d;d)#0; / brief comment
+
+/ excel patterns
+:pats: pat.run''table;
+
+u.ppr'table;
+/ u.ppr'pats;
+
+// start with last to maintain prior index accuracy
+lasts:{:((#x[0])-1)_x[0]}''pats;
+firsts:{:$[(#x[0])>1;((#x[0])-1)#x[0];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; // brief comment
+
+table {
+ :$[y~0;0;de[y;x]]
+}''firsts; // brief comment