mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-07 19:51:51 +09:00
basic: rudimentary auto-currying
This commit is contained in:
6
assets/disk0/home/basic/autocurrytest.bas
Normal file
6
assets/disk0/home/basic/autocurrytest.bas
Normal file
@@ -0,0 +1,6 @@
|
||||
10 DEFUN SINC(P)=SIN(P)/P
|
||||
20 DEFUN OTHERFUN(A,B)=A+"|"+B
|
||||
30 DEFUN PLOTLINE(F,X)=MAP(OTHERFUN<~F(X),1 TO 5):REM needs auto-currying
|
||||
31 DEFUN PLOTLINE2(F,X)=MAP(OTHERFUN<~F<~X,1 TO 5):REM WORKS!
|
||||
100 PRINT PLOTLINE(SINC,333)
|
||||
101 PRINT PLOTLINE2(SINC,333)
|
||||
@@ -3,14 +3,7 @@
|
||||
10 DEFUN SINC(P)=IF P==0 THEN 1.0 ELSE SIN(P)/P
|
||||
20 DEFUN TOCHAR(P,X)=IF (X==ROUND(ZEROLINE+P*AMP)) THEN "@" ELSE IF (X==ZEROLINE) THEN "|" ELSE CHR(250)
|
||||
30 DEFUN SCONCAT(ACC,S)=ACC+S
|
||||
40 REM DEFUN PLOTLINE(X)=FOLD(SCONCAT,"",MAP(TOCHAR<~X,1 TO ZEROLINE+AMP))
|
||||
41 REM PLOTLINE(F,X)=FOLD(SCONCAT,"",MAP(TOCHAR<~F(X),1 TO ZEROLINE+AMP)):REM does not work
|
||||
42 DEFUN PLOTLINE(F,X)=FOLD(SCONCAT,"",MAP(TOCHAR<~F<~X,1 TO ZEROLINE+AMP)):REM WORKS!
|
||||
40 DEFUN PLOTLINE(F,X)=FOLD(SCONCAT,"",MAP(TOCHAR<~F<~X,1 TO ZEROLINE+AMP))
|
||||
100 FOR I=-40 TO 40
|
||||
110 PRINT PLOTLINE(SINC,I)
|
||||
120 NEXT
|
||||
999 END
|
||||
|
||||
1000 REM Known bugs
|
||||
1010 DEFUN PLOTLINE(F,X)=FOLD(SCONCAT,CHR(0),MAP(TOCHAR<~F(X),1 TO ZEROLINE+AMP))
|
||||
1011 REM calling user-defined function within DEFUN is not working
|
||||
120 NEXT
|
||||
@@ -2858,6 +2858,7 @@ bF._interpretLine = function(lnum, cmd) {
|
||||
let syntaxTrees = bF._parseTokens(lnum, tokens, states);
|
||||
|
||||
// syntax tree pruning
|
||||
|
||||
// turn UNARYMINUS(num) to -num
|
||||
syntaxTrees.forEach(syntaxTree => {
|
||||
if (syntaxTree !== undefined) {
|
||||
@@ -2880,6 +2881,24 @@ bF._interpretLine = function(lnum, cmd) {
|
||||
}
|
||||
});
|
||||
|
||||
// automatically apply currying
|
||||
// '(<~) FNQ (P X Y)' into '(<~) FNQ ((<~) P ((<~) X Y))'
|
||||
// FIXME this autocurrying is very likely to be a janky-ass-shit
|
||||
syntaxTrees.forEach(syntaxTree => {
|
||||
if (syntaxTree !== undefined) {
|
||||
bF._recurseApplyAST(syntaxTree, tree => {
|
||||
if (tree.astValue == "<~" && tree.astLeaves[1] !== undefined && tree.astLeaves[1].astLeaves[0] !== undefined) {
|
||||
let subTree = new BasicAST();
|
||||
subTree.astValue = "<~";
|
||||
subTree.astType = "op";
|
||||
subTree.astLnum = tree.astLnum;
|
||||
subTree.astLeaves = [tree.astLeaves[1], tree.astLeaves[1].astLeaves[0]];
|
||||
|
||||
tree.astLeaves[1] = subTree;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
if (_debugprintHighestLevel) {
|
||||
syntaxTrees.forEach((t,i) => {
|
||||
serial.println("\nParsed Statement #"+(i+1));
|
||||
|
||||
Reference in New Issue
Block a user