Author Topic: C BASIC  (Read 65443 times)

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #30 on: October 29, 2013, 01:04:35 PM »
John, I have added your capitalised symbols to the list. 2 small changes to support pre-increment and pre-decrement. So we have a choice of coding styles.

  #define INCR ++
  #define DECR --

Code: [Select]
  #define function
  #define method
  #define gosub
  #define dim
  #define as
  #define to
  #define limit
  #define step
  #define then
  #define procedure void
  #define sub   void
  #define begin {
  #define end   }
  #define and   &&
  #define or    ||
  #define class typedef struct
  #define types typedef struct
  #define methods
  #define member ->
  #define addressof &
  #define has =
  #define incr ++
  #define decr --
  #define next ++
  #define prior --
  #define byref *
  #define ref   void*
  #define references = &
  #define FreeSpace free

  #define FUNCTION
  #define BEGIN_FUNCTION {
  #define END_FUNCTION }
  #define SUB void
  #define BEGIN_SUB {
  #define END_SUB }
  #define RETURN return
  #define CALL
  #define AND &&
  #define OR ||
  #define MOD %
  #define DIM
  #define AS
  #define LET
  #define INCR ++
  #define DECR --
  #define IF if
  #define BEGIN_IF {
  #define THEN {
  #define THEN_DO
  #define ELSE } else {
  #define END_IF }
  #define FOR for
  #define TO ;
  #define STEP ;
  #define BEGIN_FOR {
  #define NEXT }
  #define SELECT_CASE switch
  #define BEGIN_SELECT {
  #define CASE case
  #define _TO_ ...
  #define END_CASE break;
  #define CASE_ELSE default:
  #define END_SELECT }
  #define DO do {
  #define WHILE } while
« Last Edit: October 30, 2013, 11:13:46 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #31 on: October 29, 2013, 01:15:32 PM »
Great!

BK BASIC Have it your way. Burger King would be proud.   :-*

I think you touched on an importand aspect of C BASIC, that it is merely an illusion with heavy equipment (string lib / SDL) thinking it's BASIC.

Just in time for Halloween.  ;D

« Last Edit: October 30, 2013, 11:14:06 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #32 on: October 29, 2013, 02:03:57 PM »
My vote to keep things consistent and simple is to require encapsulating {} brackets for ALL constructs even though C doesn't require them for single statement constructs. It will help make both Charles's abbreviated version of C BASIC and my full flavored version easier to swap preferred keywords with each other.

@Charles - Sorry for sounding harsh about your abbreviated version as I realized after my post that you are also doing this as part of your C emitter for O2. The more BASiC looking version I'm doing and with hope of Joe's help is targeted at the QB/PB programmer looking for a way to extend that environments life.

« Last Edit: October 30, 2013, 11:14:18 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #33 on: October 29, 2013, 04:35:41 PM »
Well, it's still a very small list, but dramatically improves the readability of C code.

I find it quite easy to code in this way, and I hope to deliver the first of my heavy doughnuts with a further days testing: the StringClass for dynamic strings, which should also serve as a template for other kinds of objects.

Unfortunately, with the limitations of C syntax, you can't disguise dynamic strings to make them look like Basic strings, so we really need translators to reformulate the source and insert all the extras which are hidden from view in Basic.
« Last Edit: October 30, 2013, 11:14:33 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #34 on: October 29, 2013, 04:43:31 PM »
What we can't do in a #define, we should be able to handle with a macro and there is always the .h include closet to hide things in.

« Last Edit: October 30, 2013, 11:14:53 AM by John »

kryton9

  • Guest
C BASIC
« Reply #35 on: October 29, 2013, 06:07:08 PM »
Amazing progress guys, thanks.
« Last Edit: October 30, 2013, 11:15:08 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC - dynamic string functions
« Reply #36 on: October 30, 2013, 05:19:20 PM »
Quote from: Charles Pegge - thinBasic forum
I'm testing out some dynamic string functions:

Code: [Select]
  types StringClassTableStruct
  begin
    method StringObject (byref Redim)   (StringObject*pthis, int n, int wi, int cnt);
    method StringObject (byref Get)     (StringObject*r, StringObject this);
    method StringObject (byref Set)     (StringObject*pthis, StringObject s);
    method char*        (byref GetChars)(char**r, StringObject this, int wi);
    method StringObject (byref SetChars)(StringObject*pthis, char*c, int le, int wi);
    method StringObject (byref GetMid)  (StringObject*r, StringObject this, int s, int le, int wi);
    method StringObject (byref SetMid)  (StringObject*pthis, StringObject s, int i);
    method StringObject (byref Join)    (StringObject*pthis, int n,...);
    method StringObject (byref Ucase)   (StringObject*r, StringObject this);
    method StringObject (byref Lcase)   (StringObject*r, StringObject this);
    method StringObject (byref Ltrim)   (StringObject*r, StringObject this);
    method StringObject (byref Rtrim)   (StringObject*r, StringObject this);
    method StringObject (byref Insert)  (StringObject*pthis, StringObject s, int i);
    method StringObject (byref Delete)  (StringObject*pthis, int i, int le);
    method StringObject (byref Chr)     (StringObject*r, int a, int wi);
    method StringObject (byref Str)     (StringObject*r, double d);
    method int          (byref Show)    (StringObject this);
    method int          (byref Asc)     (StringObject this, int i);   // also supports wide chars
    method int          (byref Instr)   (int i, StringObject this,  StringObject k);
    method double       (byref Val)     (StringObject this);
  end
  StringClassTable,*StringMethods;

@Charles - Can you show an example with our Wetspot2 game in mind of a MID() C BASIC function in actions. Showing how strings are DIM'ed would also be helpful.

« Last Edit: October 30, 2013, 06:16:07 PM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C BASIC
« Reply #37 on: October 31, 2013, 11:36:24 AM »
The full source code attached.

A few test statements:

Code: [Select]
  function TestStrings()
  begin
    dim as int           char8=1, char16=2, all=-1;
    dim as StringObject  uo=NewString(0,char8);
    dim as StringObject  vo=NewString(0,char8);
    dim as StringObject  wo=NewString(100,char8);
    dim as StringMethods sm=vo->StringMethods;
   
    sm->Show(vo);
    sm->SetChars(&uo,"LO",all,char8);
    sm->SetChars(&vo," HellO ",all,char8);
    sm->Ltrim(&vo,vo);
    sm->Rtrim(&vo,vo);
    sm->Ucase(&vo,vo);
    sm->Join(&wo,3,vo,vo,vo);
    sm->Show(uo);
    sm->Show(vo);
    sm->Show(wo);
    printf("%i\n",sm->Instr(6,wo,uo));
    sm->Lcase(&wo,wo);
    sm->Insert(&wo,uo,6);
    sm->Show(wo);
    sm->Delete(&wo,6,2);
    sm->Show(wo);
    sm->Str(&vo,42.0);
    sm->Show(vo);
    double v=sm->Val(vo);
    printf("%f\n",v);
    sm->SetChars(&vo,"Abc",all,char8);
    sm->Repeat(&wo,vo,7);
    sm->Show(wo);
    sm->Replace(&wo,vo,uo);
    sm->Show(wo);
    sm->Replace(&wo,uo,vo);
    sm->Show(wo);
    sm->SetChars(&uo,"<",all,char8);
    sm->SetChars(&vo,">",all,char8);
    sm->SetChars(&wo,"  Hello World! ",all,char8);
    sm->Show(uo);
    sm->Show(vo);
    sm->Show(wo);
    sm->Join(&wo,3,uo,sm->Ltrim(&wo,sm->Rtrim(&wo,sm->Ucase(&wo,wo))),vo);
    sm->Show(wo);
    sm->Join(&wo,3,uo,wo,vo);
    sm->Set(&vo,wo);
    sm->Show(vo);
    sm->GetMid(&vo,wo,3,6,1);
    sm->Show(vo);
    sm->SetMid(&vo,6,vo);
    sm->Show(vo);

    StringFree(&uo);
    StringFree(&vo);
    StringFree(&wo);
  end

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #38 on: October 31, 2013, 12:16:36 PM »
No go on Ubuntu 64.  :-[

jrs@laptop:~/C_BASIC/cbstrlib$ gcc ClassString.c -o cbstrtest
jrs@laptop:~/C_BASIC/cbstrlib$ ./cbstrtest

Segmentation fault (core dumped)
jrs@laptop:~/C_BASIC/cbstrlib$

Windows XP (VirtualBox)

F:\C_BASIC>dir
 Volume in drive F is Dev
 Volume Serial Number is 0C60-620E

 Directory of F:\C_BASIC

10/31/2013  12:20 PM    <DIR>          .
10/31/2013  12:20 PM    <DIR>          ..
10/29/2013  11:10 PM             1,987 Basic.c
10/31/2013  06:23 PM            18,841 ClassString.c
10/31/2013  11:47 AM             5,078 ClassString.zip
09/01/2011  10:48 AM                19 setpath.bat
               4 File(s)         25,925 bytes
               2 Dir(s)  36,116,520,960 bytes free

F:\C_BASIC>gcc ClassString.c -o cbstrtest.exe

F:\C_BASIC>cbstrtest

LO
HELLO
HELLOHELLOHELLO
9
helloLOhellohello
hellohellohello
42.000000
42.000000
AbcAbcAbcAbcAbcAbcAbc
LOLOLOLOLOLOLO
AbcAbcAbcAbcAbcAbcAbc
<
>
  Hello World!
<HELLO WORLD!>
<<HELLO WORLD!>>
HELLO
HELLOH

F:\C_BASIC>
« Last Edit: October 31, 2013, 12:24:32 PM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C BASIC
« Reply #39 on: October 31, 2013, 12:24:30 PM »
Sounds like a pointer problem. Do you have a 32 bit system?

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #40 on: October 31, 2013, 12:26:19 PM »
Sounds like a pointer problem. Do you have a 32 bit system?

I have a Ubuntu 12.04 LTS 32 bit in a VirtualBox. Would that do?

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #41 on: October 31, 2013, 12:32:46 PM »
Using the -m32 switch works.

jrs@laptop:~/C_BASIC/cbstrlib$ gcc -m32 ClassString.c -o cbstrtest
jrs@laptop:~/C_BASIC/cbstrlib$ ./cbstrtest

LO
HELLO
HELLOHELLOHELLO
9
helloLOhellohello
hellohellohello
42.000000
42.000000
AbcAbcAbcAbcAbcAbcAbc
LOLOLOLOLOLOLO
AbcAbcAbcAbcAbcAbcAbc
<
>
  Hello World!
<HELLO WORLD!>
<<HELLO WORLD!>>
HELLO
HELLOH
jrs@laptop:~/C_BASIC/cbstrlib$

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C BASIC
« Reply #42 on: October 31, 2013, 01:36:00 PM »
I installed a windows 64 bit MingW (I selected version 4.7.0) and it works fine - produces quite a hunky binary though. (80k)

This link downloads an interactive installer for many different versions of Mingw:

http://sourceforge.net/projects/mingwbuilds/postdownload?source=dlp

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #43 on: October 31, 2013, 01:40:20 PM »
I guess that means I have to give up on O2 assist and hope AIR will keep us alive. (on 64 bit Linux)

Code: [Select]
jrs@laptop:~/C_BASIC/cbstrlib$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
jrs@laptop:~/C_BASIC/cbstrlib$
« Last Edit: October 31, 2013, 01:48:30 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #44 on: October 31, 2013, 02:06:03 PM »
I manually upgraded gcc to 4.7.3 and I'm still getting the seg. fault.

Code: [Select]
jrs@laptop:~/C_BASIC/cbstrlib$ ./cbstrtest

Segmentation fault (core dumped)
jrs@laptop:~/C_BASIC/cbstrlib$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-2ubuntu1~12.04' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04)
jrs@laptop:~/C_BASIC/cbstrlib$

That sucks, I can't compile using the -m32 switch any longer.  :'(

jrs@laptop:~/C_BASIC/cbstrlib$ gcc -m32 ClassString.c -o cbstrtest
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
jrs@laptop:~/C_BASIC/cbstrlib$
« Last Edit: October 31, 2013, 02:09:49 PM by John »