CALL is only needed if you reference a SUB before it's declared in your script.
If you put C:\scriptbasic\bin in your environment PATH, you can use scriba from anywhere.
The $ is optional in SB for strings and string functions. It doesn't matter to me if you use them or not.
' ----------------------------------------------
' ========== Declare Global Constants ==========
' ----------------------------------------------
Const BUFSIZE = 256
Const TOKENLEN = 21
Const VARNAME = 33
Const LLEN = 33
Const IOARRAY = 99
Const PATH = 129
Const RECLEN = 128
Const bTRUE = 1
Const bFALSE = 0
I noticed your comment of Global Constants and thought I would clarify what GLOBAL means in ScriptBasic.
By default, all variables are global to the module. MAIN:: is the primary module in SB.
Unless you declare a variable LOCAL in a SUB or FUNCTION, it is a global variable.
FUNCTION test(arg1, arg2)
LOCAL a,b,c
a = "one"
b = "two"
c = "three"
PRINT my_global_var,"\n"
test = arg1 + arg2
END FUNCTION
a = 1
b = 2
my_global_var = "John"
PRINT test(a,b)
END
MODULE Steve
SUB SA()
PRINT a,"\n"
END SUB
END MODULE
a = 1
CALL Steve::SA()
' Test labels
SUB Test1
GOTO IT
PRINT "FYI, I'm in Test1\n"
IT:
END SUB
SUB Test2
GOTO IT
PRINT "FYI, I'm in Test2\n"
IT:
END SUB
TEST1
TEST2
GOTO IT
PRINT "FYI: I'm in MAIN\n"
IT:
END
Ummm..., does that mean that vars declared within a SUB are global in scope, if not declared as LOCAL ?
My assumption (and hope) would be that they would be LOCAL only to that SUB and be destroyed on exiting the SUB, regardless of being declared as LOCAL.
To me SUBs & FUNCTIONs are just routines and if I want isolation, a one line LOCAL statement declaring variables used for that purpose is painless.
globalVar = 1
CALL MySub()
PRINT "\nMAIN:output\n"
PRINT "globalVar=", globalVar, "\n"
PRINT "localVar=", localVar, "\n"
END
SUB MySub
localVar = 2
PRINT "SUB:output\n"
PRINT "globalVar=", globalVar, "\n"
PRINT "localVar=", localVar, "\n"
END SUB
Local localVar
localVar = 2
' CLS
' ----------------------------------------------
' ========== Declare Global Constants ==========
' ----------------------------------------------
Const BUFSIZE = 256
Const TOKENLEN = 21
Const VARNAME = 33
Const LLEN = 33
Const IOARRAY = 99
Const PATH = 129
Const RECLEN = 128
Const bTRUE = 1
Const bFALSE = 0
' ----------------------------------------------
' ========== Declare Global Integers ==========
' ----------------------------------------------
pii = 0
ilen = 0
ix = 0
nrows = 0
ncolumns = BUFSIZE
epos = 0
spos = 0
lineNdx = 0
token = 0
' ========== Declare Global Strings ==========
' ----------------------------------------------
SourceFile$ = ""
SourceTmp$ = ""
pstring$ = ""
xstring$ = ""
sHolder$ = ""
tHolder$ = ""
indent$ = ""
labelOr$ = ""
wlabelBlock$ = ""
' ========== Declare Global Returns ==========
' ----------------------------------------------
ireturn = 0
lreturn = 0
dreturn = 0
sreturn$ = ""
rvarName$ = ""
' ========== Declare Global Flags ==========
' ----------------------------------------------
' ========== Declare Global Arrays ==========
' ----------------------------------------------
array1$[1] = ""
labelNam$[1] = ""
tempProg$[1] = ""
tempLabel$[1] = ""
byteArray[1] = 0
tempByte[1] = 0
FUNCTION RF(arg_num)
IF arg_num <= 1 THEN
RF = 1
ELSE
RF = arg_num * RF(arg_num - 1)
END IF
END FUNCTION
PRINT RF(5),"\n"
factorial( 5 ) = 5 * factorial( 4 )
= 5 * ( 4 * factorial( 3 ) )
= 5 * ( 4 * (3 * factorial( 2 ) ) )
= 5 * ( 4 * (3 * (2 * factorial( 1 ) ) ) )
= 5 * ( 4 * (3 * (2 * ( 1 * factorial( 0 ) ) ) ) )
= 5 * ( 4 * (3 * (2 * ( 1 * 1 ) ) ) )
= 5 * 4 * 3 * 2 * 1 * 1
= 120
FUNCTION ITF(arg_num)
f = 1
FOR c = arg_num TO 1 STEP - 1
f = f * c
NEXT
ITF = f
END FUNCTION
PRINT ITF(5),"\n"
This program was written in the ScriptBasic dialect.
To use this program with ScriptBasic:
1) copy gbasic.bas to: /scriptbasic/bin/
2) copy test.bas to: /scriptbasic/bin/
3) open a Dos/Console (command line) window
5) change directories to: C:.../scriptbasic/bin/
4) at the command line type:
C:> scriba gbasic.bas test.bas
The gbasic program will read the file test.bas and translate it into
a C source file and save it to disk as test.c.
At present, this translator will only translate the following keywords:
(also, block-labels:)Code: [Select]PRINT "quoted string"
LET
END
LABEL:
however, many of the building blocks, needed to take this language
translator further, are already in place.
Due to time constraints, I have not documented the gbasic code much, yet.
Steps for making a language TO language translator:
Load Source:
input: source file
output: byte code
Header:
output: program name
output: copyright information
output: comments
output: destination language headers
Prolog:
scan: global variables
output: declare global constants
output: declare global variables
Parser:
parse: source byte code
output: destination dialect
Epilog:
output: finalize program
output: write functions
Assemble:
output: assemble translation
call: compiler-linker
I'm sure everyone has seen this tutorial. For those that missed it ...
Let's Build a Compiler, by Jack Crenshaw (http://compilers.iecc.com/crenshaw/)
This fifteen-part series, written from 1988 to 1995, is a non-technical introduction to compiler construction.
I'm sure everyone has seen this tutorial. For those that missed it ...
Let's Build a Compiler, by Jack Crenshaw (http://compilers.iecc.com/crenshaw/)
This fifteen-part series, written from 1988 to 1995, is a non-technical introduction to compiler construction.
At QDepartment, I did a complete translation (with commentary) of Jack's tutorial, from Pascal/M68x to C/x86.
http://tech.groups.yahoo.com/group/QDepartment/files/
Unfortunately, it took Jack years to write it and he never finished it.
The over all document is well worth reading, but, is rather fragmented due to the time it took to write it.
Jack's book has greatly influenced me in my work since.