basic: doc

This commit is contained in:
minjaesong
2020-12-21 23:10:21 +09:00
parent 666c882b5e
commit 47f5b4d9d0
7 changed files with 57 additions and 9 deletions

View File

@@ -271,7 +271,7 @@ let resolve = function(variable) {
return undefined;
// tail error checking
else
throw Error("BasicIntpError: unknown variable with type "+variable.troType+", with value "+variable.troValue);
throw Error("BasicIntpError: unknown variable/object with type "+variable.troType+", with value "+variable.troValue);
}
let curryDefun = function(inputTree, value) {
let exprTree = cloneObject(inputTree);
@@ -2681,7 +2681,7 @@ bF._executeSyntaxTree = function(lnum, stmtnum, syntaxTree, recDepth) {
// finally assign the function to the variable table
bStatus.vars[defunName] = new BasicVar(exprTree, "usrdefun");
return new SyntaxTreeReturnObj("function", exprTree, [lnum, stmtnum + 1]);
return new SyntaxTreeReturnObj("internal_lambda", exprTree, [lnum, stmtnum + 1]);
}
else if ("ON" == funcName) {
if (syntaxTree.astLeaves.length < 3) throw lang.badFunctionCallFormat();

View File

@@ -12,7 +12,7 @@ Functions are a form of expression that may taks input arguments surrounded by p
Returns inverse cosine of \code{X}.
\subsection{ASN}
\codeline{Y \textbf{= ASN(}X\textbf{)}}\par
Returns inverss sine of \code{X}.
Returns inverse sine of \code{X}.
\subsection{ATN}
\codeline{Y \textbf{= ATN(}X\textbf{)}}\par
Returns inverse tangent of \code{X}.
@@ -37,6 +37,9 @@ Functions are a form of expression that may taks input arguments surrounded by p
\subsection{FLOOR, INT}
\codeline{Y \textbf{=} \{\textbf{FLOOR}|\textbf{INT}\}\textbf{(}X\textbf{)}}\par
Returns integer value of \code{X}, truncated towards negative infinity.
\subsection{LEN}
\codeline{Y \textbf{= LEN(}X\textbf{)}}\par
Returns length of \code{X}. \code{X} can be either a string or an array.
\subsection{LOG}
\codeline{Y \textbf{= LOG(}X\textbf{)}}\par
Returns natural logarithm of \code{X}.
@@ -73,7 +76,7 @@ Functions are a form of expression that may taks input arguments surrounded by p
\subsection{GETKEYSDOWN}
\codeline{\textbf{GETKEYSDOWN} VARIABLE}\par
Stores array that contains keycode of keys held down in \code{VARIABLE}.\par
Actual keycode and the array length depends on the machine: in \thismachine , array length will be fixed to 8. For the list of available keycodes, see \ref{technical}.
Actual keycode and the array length depends on the machine: in \thismachine , array length will be fixed to 8. For the list of available keycodes, see \ref{implementation}.
\subsection{INPUT}
\codeline{\textbf{INPUT} VARIABLE}\par
Prints out \code{? } to the console and waits for user input. Input can be any length and terminated with return key. The input will be stored to given variable.
@@ -130,6 +133,8 @@ Functions are a form of expression that may taks input arguments surrounded by p
\section{Higher-order Function}
\subsection{CURRY}
\subsection{DO}
\subsection{FILTER}
\subsection{FOLD}
\subsection{MAP}

View File

@@ -1,2 +1,2 @@
\label{technical}
\label{implementation}
Keycodes follow LWJGL2 lol

View File

@@ -1,4 +1,4 @@
\tbas{} is a BASIC dialect and its interpreter. \tbas{} emulates most of the common BASIC syntax while adds more advanced and up-to-date concepts gracefully, such as user-defined function that can \emph{actually} recurse, arbitrary list construction using CONS-operator and some of the features in the realm of functional programming such as \code{MAP} and \code{FOLD}.
\tbas{} is a BASIC dialect and its interpreter. \tbas{} emulates most of the common BASIC syntax while adds more advanced and up-to-date concepts gracefully, such as user-defined function that can \emph{actually} recurse, arbitrary list construction using CONS-operator and some of the features in the realm of functional programming from \code{MAP} and \code{FOLD} to cooking a \code{CURRY}\footnote{definitely not a foodstuff\ldots or maybe}.
This is the documentation for \tbas{} \tbasver{}.

View File

@@ -1,3 +1,14 @@
Okay, how do we begin\ldots
\begin{lstlisting}
10 PRINT 2+2
run
4
Ok
\end{lstlisting}
Oh \emph{boy} we just did a computation! It printed out \code{4} which is a correct answer for $2+2$ and it didn't crash!
\begin{lstlisting}
10 DEFUN FAC(N)=IF N==0 THEN 1 ELSE N*FAC(N-1)
20 FOR K=1 TO 6
@@ -11,9 +22,38 @@
30 PRINT K
\end{lstlisting}
\begin{lstlisting}
10 DEFUN FIB(N)=IF N==0 THEN 0 ELSE IF N==1 THEN 1 ELSE FIB(N-1)+FIB(N-2)
20 FOR K=1 TO 12
30 PRINT FIB(K);" ";
40 NEXT
\end{lstlisting}
\section[Currying]{Haskell Curry Wants to Know Your Location}
So what the fsck is currying? Consider the following code:
\begin{lstlisting}
10 DEFUN F(K,T)=ABS(T)==K
20 CF=CURRY(F,32)
30 PRINT CF(24) : REM will print 'false'
40 PRINT CF(-32) : REM will print 'true'
\end{lstlisting}
Here, \code{CF} is a curried function of \code{F}; built-in function \code{CURRY} applies \code{32} to the first parametre of the function \code{F}, which dynamically returns a \code{function} \code{CF(T)=ABS(T)==32}. The fact that \code{CURRY} returns a \emph{function} opens many possibilities, for example, you can create loads of cousin function without making loads of duplicate codes.
\section[Wrapping-Up]{The Grand Unification}
Using all the knowledge we have learned, it should be trivial\footnote{/s} to write a Quicksort algorithm in \tbas, like this:
\begin{lstlisting}
10 DEFUN LESS(P,X)=X<P
11 DEFUN GTEQ(P,X)=X>=P
12 DEFUN QSORT(XS)=IF LEN(XS)<1 THEN NIL ELSE QSORT(FILTER(CURRY(LESS,XS(0)),XS)) # XS(0)!NIL # QSORT(FILTER(CURRY(GTEQ,XS(0)),XS))
100 L=7!9!4!5!2!3!1!8!6!NIL
110 SL=QSORT(L)
120 PRINT L:PRINT SL
\end{lstlisting}
Line 10--12 implements quicksort algorithm. \code{LESS} is a user-function version of less-than operator, and \code{GTEQ} is similar. \code{QSORT} selects a pivot using head-element of array \code{XS}\footnote{stands for \emph{X's}}, then using curried version of \code{LESS} and \code{GTEQ} to move values lesser than pivot to the left and greater to the right, and these two sorted \emph{chunks} are recursively sorted using the same \code{QSORT} function. Currying is used to give comparison functions a pivot-value to compare against, and also because \code{FILTER} wants a \emph{function} and not an \emph{expression}. \code{XS(0)} simply fetches a head-elemement of \code{XS}. \code{XS(0)!NIL} creates a single-element array contains \code{XS(0)}.

View File

@@ -183,6 +183,9 @@
\chapter{Technical Reference}
\input{technical}
\chapter{Implementation Details}
\input{Implementation}
\chapter{Index}
\chapter{Bibliography}

View File

@@ -23,7 +23,7 @@ Typical User Input & TYPEOF(\textbf{Q}) & Instanceof \\
\section{Unresolved Values}
Unresolved variables has JS-object of \code{troType}, with \emph{instanceof} \code{SyntaxTeeReturnObj}. Its properties are defined as follows:
Unresolved variables has JS-object of \code{troType}, with \emph{instanceof} \code{SyntaxTreeReturnObj}. Its properties are defined as follows:
\begin{tabulary}{\textwidth}{RL}
Properties & Description \\
@@ -39,19 +39,19 @@ Following table shows which BASIC object can have which \code{troType}:
BASIC Type & troType \\
\hline
Any Variable & {\ttfamily lit} \\
DEFUN'd Function & {\ttfamily function} \\
Boolean & {\ttfamily bool} \\
Generator & {\ttfamily generator} \\
Array & {\ttfamily array} \\
Number & {\ttfamily num} \\
String & {\ttfamily string} \\
DEFUN'd Function & {\ttfamily internal\_lambda} \\
Array Indexing & {\ttfamily internal\_arrindexing\_lazy} \\
Assignment & {\ttfamily internal\_assignment\_object} \\
\end{tabulary}
\subsection*{Notes}
\begin{itemlist}
\item All type that is not \code{lit} only appear when the statement returns such values, e.g. \code{function} only get returned by DEFUN statements as the statement itself returns defined function as well as assign them to given BASIC variable.
\item All type that is not \code{lit} only appear when the statement returns such values, e.g. \code{internal\_lambda} only get returned by DEFUN statements as the statement itself returns defined function as well as assign them to given BASIC variable.
\item As all variables will have \code{troType} of \code{lit} when they are not resolved, the property must not be used to determine the type of the variable; you must \code{resolve} it first.
\item The type string \code{function} should not appear outside of TRO and \code{astType}; if you do see them in the wild, please check your JS code because you probably meant \code{usrdefun}.
\end{itemlist}