compiler.k 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /helpers
  2. / e exclusive
  3. slice: {[l;s;e] s_(e-1)#l};
  4. listize: {1_(,/(";",)'{"(",x,")"}'x)};
  5. tstkey: "States/Events"; / key indicating start of transition section
  6. cstkey: "Events"; / key indicating start of events condition section
  7. astkey: "Actions"; / key indicating start of actions section
  8. sectionkeys: (tstkey;cstkey;astkey);
  9. lines: 0: "tempST.csv";
  10. /csv parser
  11. parsel: {[l] comi: &l=",";comi+:1;r: 2':!#comi;r: comi@r;r: (1 2#(0;r[0;0])),r;slice[l].'r};
  12. tempST: parsel'lines;
  13. tname: tempST[0;0];
  14. / st compile
  15. / separate sections
  16. si: (*'&'{(y~)'x}[*'tempST]'sectionkeys),(#tempST-1); / sections line numbers
  17. slices: si@(2':!#si);
  18. (t;c;a):1_'slice[tempST;] .'slices;
  19. / postprocess sections
  20. s:*'t;
  21. t:2_'t;
  22. a:*'a;
  23. c:*'c;
  24. / TODO: make dynamic
  25. args: `isOff `T ! `i `i;
  26. fname: tname,".k";
  27. kcode: "/ ",fname,".csv compiled\n\n";
  28. tstr: "";
  29. /a TODO: make dynamic
  30. avars: ("A",'$((!#a)+1))
  31. a:,[;"\"};\n"]'((,[;":{\""]'avars),'a);
  32. a: ,/a;
  33. avars: 1_,/(";",'avars);
  34. /c
  35. vars: {x,":args["}'$'!args;
  36. cargs:,[;"];"]'(vars,'($!#vars));
  37. cargs: ,/cargs;
  38. c:"{[args]",cargs,"(",listize[c],")}";
  39. /s
  40. s:,[;"\""]'("\"",'s);
  41. s:listize[s];
  42. /at
  43. at: ,/("`",'$:args@!args);
  44. / t
  45. t:listize[listize't];
  46. kcode ,: a;
  47. kcode ,: ("a:(",avars,");\n");
  48. kcode ,: ("c:",c,";\n");
  49. kcode ,: ("s:(",s,");\n");
  50. kcode ,: ("t:(",t,");\n");
  51. kcode ,: ("at:",at,";\n");
  52. / initialize data
  53. f:<`"tempST.k";
  54. f 0: kcode;
  55. / -======== notes ========-
  56. / / how the parser works
  57. / t: lines[0];
  58. / comi: &t=","; / commas idx
  59. / comi+:1; / help exlude commas (later use -1 on upper)
  60. /
  61. / r: 2':!#comi; / ranges of comma separated parts
  62. / r: comi@r; / apply ranges to comma idx
  63. / r: (1 2#(0;r[0;0])),r; / add 0-first comma range
  64. / slice[t].'r