User:
JT
Date: 3/18/2007 3:19 pm
Views: 1306
Rating: 3
This macro brings all the power of the 1980's BASIC programming language to your WebGUI site!
Syntax
DATA
- DATA 1,2,"HI". These will be read sequentially by READ statements. Note that currently all string constants must be quoted.
DEF
DIM
- DIM A(20), B(10,10). Arrays default to size 10 (or actually 11 since they start at zero.)
END
FOR
- FOR I = 1 TO 10 STEP 3. STEP defaults to 1 if not given, and may be negative. (For loops are always implemented at least once.)
GOTO
- GOTO 30. Note that GOTO 30+(X*3) is also supported.
GOSUB
- GOSUB 10+X. Gosub is just like GOTO, except that when the programgets to a RETURN statement, it will come back to the statement justafter the GOSUB.
IF
- IF X > Y THEN 30 ELSE X = X + 1. ELSE is not required. In a THENor ELSE, a lone number means GOTO that number (also known as an impliedGOTO).
LET
- LET X=4. The word "LET" isn't required; i.e. X=4 is just like LET X=4.
NEXT
- NEXT I. Increment I by STEP, test against its limit, go back to theFOR statement if it's not over (or under, for a descending loop) itslimit.
ON
- ON X-3 GOSUB 10,20. This is equivalent to: IF X-3 = 1 THEN GOSUB 10 IF X-3 = 2 THEN GOSUB 20
- ON ... GOTO is also allowed.
PRINT
- PRINT FOO; BAR$, 6*BLAH. semicolon means no space (or one spaceafter printing numbers!), comma is like a 14-character tab (or \n pastcolumn 56). Print \n after the last expression unless there's asemicolon after it.
READ
- READ A, B(I), C$. Reads data from DATA statements into variables
REM
- REM WHATEVER. Anything after the REM is ignored (including colons and succeeding statements!)
RETURN
- RETURN. Return to the statement after the last GOSUB.
Example Program
10 FOR J=1 TO 10
20 PRINT "Hello World!<br />"
30 NEXT J
Installation
You're going to need the Language::Basic perl module installed on your server.
perl -MCPAN -e shell
install Language::Basic
exit
Then copy Basic.pm into your lib/WebGUI/Macro folder, add it to your WebGUI config file, and restart your web server.
Usage
To use this module simply create a Snippet asset with your program in it. Then use the URL of the snippet in combination with the Basic macro to execute your program:
^Basic(programs/helloworld.bas);
Caveats
Do not do anything that would cause a loop to not exit. The macro currently has no way to detect infinite loops and therefore the only way to kill a program in an infinite loop is to restart your web server. For this reason I recommend testing your program using a command line interpreter first. And it just so happens that I've included a command line interpreter as an attachment to this post. To use it just type:
perl basic.pl programname.bas
Versions
1.0.0 - Initial release
1.0.1 - Bugfix in output redirect thanks to Martin Kamerbeek