Author Topic: C BASIC  (Read 35570 times)

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C BASIC
« Reply #60 on: November 01, 2013, 11:05:45 AM »
That's great news, John! I don't think there will be many more 64bit issues.

We can use this code as a template  to build all kinds of objects, though it is quite cumbersome when used directly, compared to Basic String-handling.

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #61 on: November 01, 2013, 11:17:35 AM »
I feel it's time we start introducing macros to seamlessly embedded your BASIC string functions into C code.

FWIW CompileOnline.com

Code: [Select]
$gcc main.c util.c -o demo -lm -pthread -lgmp -lreadline 2>&1
/tmp/cc19kTA2.o: In function `NewSpace':
util.c:(.text+0x0): multiple definition of `NewSpace'
/tmp/ccI4TuYH.o:main.c:(.text+0x0): first defined here
/tmp/cc19kTA2.o: In function `CopyBytes':
util.c:(.text+0x47): multiple definition of `CopyBytes'
/tmp/ccI4TuYH.o:main.c:(.text+0x47): first defined here
collect2: error: ld returned 1 exit status

I wonder if it's the default compiler switches he is using? I will try them on C9 and see if I see the same.

scriptbasic@nimrod:~/639761/projects/John/C_BASIC/libcbasic $ gcc ClassString.c -o demo -lm -pthread -lgmp -lreadline
scriptbasic@nimrod:~/639761/projects/John/C_BASIC/libcbasic $ ./demo

LO
HELLO
HELLOHELLOHELLO
9
helloLOhellohello
hellohellohello
42.000000
42.000000
AbcAbcAbcAbcAbcAbcAbc
LOLOLOLOLOLOLO
AbcAbcAbcAbcAbcAbcAbc
<
>
  Hello World!
<HELLO WORLD!>
<<HELLO WORLD!>>
HELLO
HELLOH
scriptbasic@nimrod:~/639761/projects/John/C_BASIC/libcbasic $
« Last Edit: November 01, 2013, 11:49:57 AM by John »

Offline AIR

  • BASIC Developer
  • Posts: 685
Re: C BASIC
« Reply #62 on: November 01, 2013, 11:44:18 AM »
printf uses ellipsis and pointers for printing strings, so logically speaking, it should work.

In most implementations, it also makes use of stdarg (va_list, va_start, va_end).

In MBC, join is done like this:

Code: [Select]
char * join(int n, ...)
{
  int i = n, tmplen = 0;
  char *strtmp, *s_;

  va_list marker;
  va_start(marker, n); // Initialize variable arguments
  while(i-- > 0)
  {
    s_ = va_arg(marker, char *);
    if(s_) tmplen += strlen(s_);
  }
  strtmp = BCX_TmpStr(tmplen);
  va_end(marker); // Reset variable arguments
  i = n;
  va_start(marker, n); // Initialize variable arguments
  while(i-- > 0)
  {
    s_ = va_arg(marker, char *);
    if(s_) strcat(strtmp, s_);
  }
  va_end(marker); // Reset variable arguments
  return strtmp;
}

kryton9

  • Guest
Re: C BASIC
« Reply #63 on: November 01, 2013, 11:58:44 AM »
Once I get the tutorials finished, I will upload all of these code snippets into the proper branches and hopefully with the tutorials complete, everyone can start using git.
AIR recommends the command line so later today I will make a complete tutorial using the command line too. So users can use Git GUI or Git Bash after today.

Till then the google code site is in a dubious state while I work on it.

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
C BASIC - CompileOnLine.com
« Reply #64 on: November 01, 2013, 12:08:53 PM »
I was able to get Charles's string library compiled on CompileOnLine</>com. The trick with getting it working was combining the two C programs into one. The multi-file feature doesn't seem to work for me.
« Last Edit: November 01, 2013, 02:01:05 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #65 on: November 01, 2013, 12:51:36 PM »
AIR is 100% correct with his comment about using stdarg (va_list, va_start, va_end).  IUP uses it in all of their versions of the code.

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
C BASIC - String Function Macros
« Reply #66 on: November 02, 2013, 12:58:46 AM »
I have added our first set of string related function macros.

Code: [Select]
/* Function MACRO Define's */
  #define LTRIM(x) sm->Ltrim(&x, x)
  #define RTRIM(x) sm->Rtrim(&x, x)
  #define UCASE(x) sm->Ucase(&x, x)
  #define LCASE(x) sm->Lcase(&x, x)

  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);
    LTRIM(vo);
    // sm->Rtrim(&vo,vo);
    RTRIM(vo);
    // sm->Ucase(&vo,vo);
    UCASE(vo);
    StringObject c1[]={vo,vo,vo};
    sm->Join(3,&wo,c1);
    sm->Show(uo);
    sm->Show(vo);
    sm->Show(wo);
    printf("%i\n",sm->Instr(6,wo,uo));
    // sm->Lcase(&wo,wo);
    LCASE(wo);

jrs@laptop:~/C_BASIC/cbstrlib$ ./strlib

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$
« Last Edit: November 02, 2013, 01:00:32 AM by John »

kryton9

  • Guest
Re: C BASIC
« Reply #67 on: November 02, 2013, 01:37:52 AM »
Guys, I think all of this can be done easier once we have abstract data types in place.
You won't need to make StringObject or VectorObject etc.
Also for making classes it will be very handy and having dynamic arrays of any type, linked lists, queses, stacks all that stuff.
Just wanted you to keep that in mind.

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #68 on: November 02, 2013, 01:50:51 AM »
I'm creating the C BASIC overlay to C with #define (static & function) as much as possible. We are not creating a new language, just making the one we know compile as a C program. Feel free to fork C BASIC in whatever direction you wish. That's what makes projects like this so much fun.

« Last Edit: November 02, 2013, 01:52:42 AM by John »

kryton9

  • Guest
Re: C BASIC
« Reply #69 on: November 02, 2013, 01:53:32 AM »
Sounds like the right plan. You guys got the main stuff down already in a very short amount of time!

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #70 on: November 02, 2013, 02:07:57 AM »
Kent,

I don't want to take anything away from the power of C. What Charles has envisioned is to help BASIC programmers get over their initial fear of C and create simple but useful programs. The Wetspot II QuickBASIC game is a test of the theory with a big reward if it works. We will see nursing homes with old BASIC programmer residents using C BASIC to port their QB DOS games supplementing their Medicaid with Google Play checks. It's all good and just relax and have fun working together as a group. There is no pressure to do more than you can give.

John

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C BASIC
« Reply #71 on: November 02, 2013, 12:56:20 PM »
The String class, I think is a very good model for abstraction , to create other classes. Most other classes will be a simplification of the string class, in terms of storage and access. This one supports 16bit and 8bit characters, and I am now adding dynamic string arrays.

The string functions can also be called directly without going via the vm (virtual methods) table, which is a little simpler for non-OOP styles of BASIC.

Another test for you, John: Using the ellipsis StdArg protocol demonstrated by Armando. I created a function called JoinN to test ellipsis. It is only called onece in the test code, but that should be sufficient.

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #72 on: November 02, 2013, 01:06:23 PM »
Code: [Select]
jrs@laptop:~/C_BASIC/cbstrlib$ gcc ClassString.c -o stdargtest
ClassString.c: In function ‘StringJoinN’:
ClassString.c:449:24: error: expected expression before ‘StringObject’
       s=va_arg(marker, StringObject); // get next StringObject
                        ^
ClassString.c:460:24: error: expected expression before ‘StringObject’
       s=va_arg(marker, StringObject); // get next StringObject
                        ^
jrs@laptop:~/C_BASIC/cbstrlib$

Can you join me on chat to work through this?

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: C BASIC
« Reply #73 on: November 02, 2013, 01:18:15 PM »
My screwup. Unzip renamed your copy of Basic.c and left mine. Works GREAT!

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

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 #74 on: November 02, 2013, 01:36:32 PM »
Thanks John. Yes, I put all the <includes> into Basic.c

<StdArg.h> is the only extra, so you can add it to yours. Thank you Armando for resolving this dilemma.

<math.h> will also be useful.