Author Topic: gBasic  (Read 14909 times)

Steve A.

  • Guest
Re: gBasic
« Reply #15 on: January 28, 2011, 06:29:54 AM »
Quote
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.

Hmmm, my perspective on SUBs and FUNCTIONs is different.
A flat, linear model where Subs and Functions are merely routines, similar to GOSUB/RETURN, would not allow recursion. As every time a SUB was re-entered the pre-existing (local) var values could be corrupted.

Regardless of name spaces, it's been my experience that data contained within Subs and Funcs should be isolated from the rest of the program. Of course, Subs and Funcs can see and access global variables in existance in the main body, (lowest level of the program). Additionally, Subs can access any variables passed to that Sub/Func.

The scope of a variable, when declared and assigned to in a Sub/Func, should be exclusive to that Sub/Func. Otherwise, data could become corrupted.

Code: [Select]
   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


Unfortunately, (using SB) the above illustration displays the same output in both cases.
localvar, created in SUB mysub, is not destroyed upon exiting the SUB.

It remains alive after the Sub has been exited and is visible to the Main Body of the program.

Further tests show that I can force localVar to be exclusively local with:
Code: [Select]
    Local localVar
    localVar = 2
« Last Edit: January 28, 2011, 07:19:29 AM by Steve A. »

JRS

  • Guest
Re: gBasic
« Reply #16 on: January 28, 2011, 11:04:02 AM »
In ScriptBasic you have the choice if variables are defaulted to global or local in functions/subs. To coin a BK phrase "Have it your way!"

declare option DefaultLocal

You can switch back to global by default with the following statement.

declare option DefaultGlobal

The same concept works for declaring variables and can be turned on / off anywhere in your script.

declare option DeclareVars

To return to ScriptBasic default auto global variable mode at anytime, use the following statement.

declare option AutoVars

This should give you the flexibility to setup your script to work like you want it to.

« Last Edit: January 28, 2011, 01:00:08 PM by JRS »

Steve A.

  • Guest
Re: gBasic
« Reply #17 on: January 28, 2011, 12:08:27 PM »
Okay, that should clear up a few things.

JRS

  • Guest
Re: gBasic
« Reply #18 on: January 28, 2011, 04:39:55 PM »
gBasic comments and feedback:

Code: [Select]
'    CLS

Under Windows the SB CIO extension module is used and under Linux curses is used. I would prefer that by default, screen mnemonics aren't used. I use SB in cron jobs, CGI, redirected output utilities and having screen control characters embedded may produce undesirable results.

Code: [Select]
' ----------------------------------------------
' ========== 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

If your going to make your SUBs and FUNCTIONs LOCAL by default, then you will want to prefix these CONSTants and variables with the GLOBAL keyword to allow them to be used in your SUB/FUNCTION routines.

« Last Edit: January 28, 2011, 04:44:05 PM by JRS »

JRS

  • Guest
Re: gBasic
« Reply #19 on: January 28, 2011, 06:49:06 PM »
Here is the Hello World of a recursive function example.

Code: [Select]
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"

jrs@Laptop:~/SB/test$ scriba recursive_function_test.sb
120
jrs@Laptop:~/SB/test$

A factorial of a number is the product of that number with a number 1 less than that number, this multiplication process continues until the number which is being multiplied, eventually becomes equal to 1.

Code: [Select]
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

Here is another example but non-recursive.

Code: [Select]
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"

jrs@Laptop:~/SB/test$ scriba iterative_function_test.sb
120
jrs@Laptop:~/SB/test$
 
« Last Edit: January 28, 2011, 09:27:28 PM by JRS »

Steve A.

  • Guest
Re: gBasic
« Reply #20 on: February 01, 2011, 07:24:48 PM »

JRS

  • Guest
Re: gBasic
« Reply #21 on: February 01, 2011, 08:41:07 PM »
Quote from: README - (Steve)
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.

There is a lot of meat on those gBasic bones. Nice job Steve!
I see this coming together at a rapid pace. I hope you agree doing it in ScriptBasic was the right choice.

Code: [Select]
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

gBasic.bas

Code: Text
  1. '  this is a test
  2. '
  3. Start:
  4.     REM  testing
  5.     PRINT "Hello World!"
  6.     PRINT "My name is Steve"
  7.     LET a = 1 + 1
  8.     LET b = 1
  9.     z = a + b
  10. Done:
  11.     END
  12.  

Code: Text
  1. //      ************* gBasic Compiler *************
  2. //      Copyright: s.arbayo (c) 2011
  3.  
  4. /*--------------------- DECLARE HEADERS ---------------------*/
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. /*------------------- Function Prototypes -------------------*/
  9.  
  10. /*---------------------- BEGIN PROGRAM ----------------------*/
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     int a;
  15.     int b;
  16.     int z;
  17.  
  18. Start:
  19.  
  20.     printf("Hello World!\n");
  21.     printf("My name is Steve\n");
  22.     a = 1 + 1;
  23.     b = 1;
  24.     z = a + b;
  25.  
  26. Done:
  27.     return 0;
  28. }
  29. /*--------- end main ---------*/
  30.  
« Last Edit: February 02, 2011, 09:56:39 PM by JRS »

JRS

  • Guest
Re: gBasic
« Reply #22 on: February 02, 2011, 06:36:32 PM »
I'm sure everyone has seen this tutorial. For those that missed it ...

Let's Build a Compiler, by Jack Crenshaw

This fifteen-part series, written from 1988 to 1995, is a non-technical introduction to compiler construction.

Steve A.

  • Guest
Re: gBasic
« Reply #23 on: February 02, 2011, 08:12:33 PM »
I'm sure everyone has seen this tutorial. For those that missed it ...

Let's Build a Compiler, by Jack 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.
« Last Edit: February 02, 2011, 08:17:54 PM by Steve A. »

JRS

  • Guest
Re: gBasic
« Reply #24 on: February 02, 2011, 08:20:12 PM »
I'm sure everyone has seen this tutorial. For those that missed it ...

Let's Build a Compiler, by Jack 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.


Outstanding!

For those not a member of the QDepartment Yahoo mailing list/portal, I attached a PDF of Steve's translation here.
« Last Edit: February 02, 2011, 09:10:07 PM by JRS »

JRS

  • Guest
Re: gBasic
« Reply #25 on: February 20, 2011, 12:47:43 PM »
Last I heard from Steve was that he was pretty busy and gBasic is something he will work on when he finds the time.

I'm still hoping Steve finds a second wind and puts out another progress release. The closer he gets, the easier it will be for others to contribute.