Author Topic: C Basic Class Library  (Read 60081 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
Re: C Basic Class Library
« Reply #45 on: November 13, 2013, 03:46:45 AM »
You could always use my trusty Got Here! debugger.  ;D

This is what cint reports when I load the Test.c program from within cint. This tells me that it's not an execution issue but a syntactical problem cint is having with the code. My guess is that your doing something beyond the capabilities of the interpreter.

Code: [Select]
allbasic@c_basic:~/668428/usr/john/cbclass $ cint -p

cint : C/C++ interpreter  (mailing list 'cint@root.cern.ch')
   Copyright(c) : 1995~2010 Masaharu Goto (gotom@hanno.jp)
   revision     : 5.18.00, July 2, 2010 by M.Goto

No main() function found in given source file. Interactive interface started.
'h':help, 'q':quit, '{statements;}' or 'p [expr]' to evaluate

cint> L Test.c
Error: Symbol vo is not defined in current scope  FILE:StringClass.c LINE:39
Error: Failed to evaluate vo->f
Error: class,struct,union or type StringMethods not defined  FILE:StringClass.c LINE:39
Error: Invalid type 'StringMethods' in declaration of 'f' FILE:StringClass.c LINE:39
!!!Error recovered!!!
cint>

This is a simple debugging session using my cb.c C BASIC example. Notice how the #defines are expanded during the debugging session?

Code: [Select]
allbasic@c_basic:~/668428/usr/john/cintbasic $ cint -p

cint : C/C++ interpreter  (mailing list 'cint@root.cern.ch')
   Copyright(c) : 1995~2010 Masaharu Goto (gotom@hanno.jp)
   revision     : 5.18.00, July 2, 2010 by M.Goto

No main() function found in given source file. Interactive interface started.
'h':help, 'q':quit, '{statements;}' or 'p [expr]' to evaluate

cint> L cb.c
cint> p main(0,0);
1
2
3
4
5
x is now 6
My guess is 6
6
Double it up
cint> q
  Bye... (try 'qqq' if still running)
allbasic@c_basic:~/668428/usr/john/cintbasic $

...

cint> p main();

# cb.c
3    main()
4    {
5      int x;
6      for (x = 1 ;
FILE:cb.c LINE:6 cint> s
 x < 6 ; ++ x)
FILE:cb.c LINE:6 cint> s

7      {
8        printf ("%i\n",x);
FILE:cb.c LINE:8 cint> s
1

9      }
7      {
8        printf ("%i\n",x);
FILE:cb.c LINE:8 cint> s
2

9      }
7      {
8        printf ("%i\n",x);
FILE:cb.c LINE:8 cint> s
3

9      }
7      {
8        printf ("%i\n",x);
FILE:cb.c LINE:8 cint> s
4

9      }
7      {
8        printf ("%i\n",x);
FILE:cb.c LINE:8 cint> s
5

9      }
10     if (x == 6)
FILE:cb.c LINE:10 cint> s
 printf ("x is now 6\n");
FILE:cb.c LINE:10 cint> s
x is now 6

11     if (x > 5 && x < 7)
FILE:cb.c LINE:11 cint> s
 {
12       printf ("My guess is 6\n");
FILE:cb.c LINE:12 cint> s
My guess is 6

13     } else
16     int y = 0;
17     do {
18       ++ y;
FILE:cb.c LINE:18 cint> s

19     } while (y < x)
FILE:cb.c LINE:19 cint> s
{
18       ++ y;
FILE:cb.c LINE:18 cint> s

19     }
FILE:cb.c LINE:19 cint> s
{
18       ++ y;
FILE:cb.c LINE:18 cint> s

19     }
FILE:cb.c LINE:19 cint> s
{
18       ++ y;
FILE:cb.c LINE:18 cint> s

19     }
FILE:cb.c LINE:19 cint> s
{
18       ++ y;
FILE:cb.c LINE:18 cint> s

19     }
FILE:cb.c LINE:19 cint> s
{
18       ++ y;
FILE:cb.c LINE:18 cint> s

19     }
FILE:cb.c LINE:19 cint> s
 while (y < x);
20     printf ("%i\n", y);
FILE:cb.c LINE:20 cint> s
6

21     x += y;
FILE:cb.c LINE:21 cint> s

22     switch (x)
FILE:cb.c LINE:22 cint> s

23     {      printf ("Double it up\n");
FILE:cb.c LINE:30 cint> s
Double it up

31         break;
FILE:cb.c LINE:31 cint> s

35   }cint> s

cint>
« Last Edit: November 13, 2013, 02:02:02 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
Re: C Basic Class Library
« Reply #46 on: November 13, 2013, 08:38:30 AM »
I agree that we should put CINT aside and revisit it as an option when time permits. My conCERNs are that cint is being phased out for a C++ only solution. (CLING) As much as I would like to have an interpreter/debugger to compliment the C BASIC offering, it's taking too much time from more important aspects of this project.


« Last Edit: November 13, 2013, 09:25:18 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
C Basic Class Library - BASIC string functions
« Reply #47 on: November 13, 2013, 11:14:15 AM »
Charles,

Can you post a few examples of #define(s) that I can use in my keyword table?

DIM AS STRING w;

#define UCASE
#define MID
#define LEFT
#define INSTR

This will help me understand how strings are created and manipulated with string functions.

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C Basic Class Library
« Reply #48 on: November 13, 2013, 12:03:06 PM »
Hi John,

Bypassing OOP function tables, where A is the target StringObject, and B is the source. (A and B can be the same)

#define UCASE(A,B) StringUcase(&A,B)

similar here

#define MID(A,B,C,D) StringGetMid(&A,B,C,D)


I am embroiled in further strictures to make the library compile with VC10.
:o

Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
Re: C Basic Class Library
« Reply #49 on: November 13, 2013, 01:04:45 PM »
I'm assuming this will work like the object version I did before?

#define UCASE(x) StringUcase(&x, x)

How do I emulate a DIM AS STRING like #define. Is there only one kind of string in C BASIC?
« Last Edit: November 13, 2013, 01:08:26 PM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C Basic Class Library
« Reply #50 on: November 13, 2013, 02:11:18 PM »

yes you can UCASE to self, as with most of the other string functions.



    dim as StringObject  wo=NewString(100);

    #define STRING StringObject

  DIM AS STRING wo=NewString(100) //suitable approx buffer size

Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
Re: C Basic Class Library
« Reply #51 on: November 13, 2013, 05:23:41 PM »
For grins, I thought I would try the C BASIC Test.c with CLING. Interesting results.

Code: [Select]
jrs@laptop:~/C_BASIC/cbclass$ cling

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ .L Test.c
In file included from -:1:
In file included from input_line_4:1:
In file included from ./Test.c:3:
In file included from ./BasicClassLibrary.h:6:
./BasicDefine.h:13:11: error: C++ operator 'and' cannot be used as a macro name
  #define and   &&
          ^
./BasicDefine.h:14:11: error: C++ operator 'or' cannot be used as a macro name
  #define or    ||
          ^
In file included from -:1:
In file included from input_line_4:1:
In file included from ./Test.c:3:
In file included from ./BasicClassLibrary.h:7:
./BasicSpace.c:32:11: error: cannot initialize a variable of type 'char *' with an rvalue of type 'void *'
    char *v =  malloc(count);
          ^    ~~~~~~~~~~~~~
./BasicSpace.c:41:10: error: cannot initialize a variable of type 'char *' with an lvalue of type 'void *'
    char*s=sv;
         ^ ~~
./BasicSpace.c:42:10: error: cannot initialize a variable of type 'char *' with an lvalue of type 'void *'
    char*t=tv;
         ^ ~~
./BasicSpace.c:74:10: error: cannot initialize a variable of type 'char *' with an rvalue of type 'void *'
    char*t=malloc(n);
         ^ ~~~~~~~~~
In file included from -:1:
In file included from input_line_4:1:
In file included from ./Test.c:3:
In file included from ./BasicClassLibrary.h:8:
./GenericClass.c:55:42: error: expected ')'
  sub            SetObjectHeader (Object this,ref f,int le, int wi,int so);
                                         ^
./GenericClass.c:55:34: note: to match this '('
  sub            SetObjectHeader (Object this,ref f,int le, int wi,int so);
                                 ^
./GenericClass.c:73:12: error: cannot initialize a variable of type 'Object' (aka 'ClassStruct *') with an lvalue of type 'void *'
    Object t=*pt;
           ^ ~~~
./GenericClass.c:84:12: error: cannot initialize a variable of type 'Object' (aka 'ClassStruct *') with an lvalue of type 'void *'
    Object t=*pt;
           ^ ~~~
./GenericClass.c:96:12: error: cannot initialize a variable of type 'Object' (aka 'ClassStruct *') with an lvalue of type 'void *'
    Object t=*pt;
           ^ ~~~
./GenericClass.c:106:30: error: expected ')'
  sub SetObjectHeader(Object this,ref f,int le, int wi,int so)
                             ^
./GenericClass.c:106:22: note: to match this '('
  sub SetObjectHeader(Object this,ref f,int le, int wi,int so)
                     ^
./GenericClass.c:108:5: error: invalid use of 'this' outside of a non-static member function
    this->f      = f;
    ^
./GenericClass.c:108:20: error: use of undeclared identifier 'f'
    this->f      = f;
                   ^
./GenericClass.c:109:5: error: invalid use of 'this' outside of a non-static member function
    this->offset = so;
    ^
./GenericClass.c:109:20: error: use of undeclared identifier 'so'
    this->offset = so;
                   ^
./GenericClass.c:110:5: error: invalid use of 'this' outside of a non-static member function
    this->nbytes = le*wi;
    ^
./GenericClass.c:110:20: error: use of undeclared identifier 'le'
    this->nbytes = le*wi;
                   ^
./GenericClass.c:110:23: error: use of undeclared identifier 'wi'
    this->nbytes = le*wi;
                      ^
./GenericClass.c:111:5: error: invalid use of 'this' outside of a non-static member function
    this->count  = le;
    ^
fatal error: too many errors emitted, stopping now
[cling]$

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C Basic Class Library
« Reply #52 on: November 13, 2013, 10:40:57 PM »

I have downgraded the code to make it compatible with VC10, as well as GCC 6.7. It might be more palatable for other compilers too, since it is very plain C, with all variables declared first, no scopes, and no case ranges.

http://www.allbasic.info/forum/index.php?topic=283


Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
Re: C Basic Class Library
« Reply #53 on: November 13, 2013, 10:46:43 PM »
Hi Charles,

Your latest compiled/ran fine on Ubuntu 64 bit but Cling still doesn't like you.  :(

Code: [Select]
jrs@laptop:~/C_BASIC/cbclass$ gcc Test.c -o test4
jrs@laptop:~/C_BASIC/cbclass$ ./test4
LO
HELLO
HELLOHELLOHELLO
9
helloLOhellohello
hellohellohello
42.000000
42.000000
AbcAbcAbcAbcAbcAbcAbc
LOLOLOLOLOLOLO
AbcAbcAbcAbcAbcAbcAbc
<
>
  Hello World!
<HELLO WORLD!>
<<HELLO WORLD!>>
HELLO
HELLOH
abc
       \abc
WORD:       aa
Class Size: 80
Char Width: 1
Word Len:   2
Word Start: 38289618
Word End:   38289620
Text End:   38289624
=
jrs@laptop:~/C_BASIC/cbclass$ cling

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ .L Test.c
In file included from -:1:
In file included from input_line_4:1:
In file included from ./Test.c:2:
In file included from ./BasicClassLibrary.h:6:
./BasicDefine.h:13:11: error: C++ operator 'and' cannot be used as a macro name
  #define and   &&
          ^
./BasicDefine.h:14:11: error: C++ operator 'or' cannot be used as a macro name
  #define or    ||
          ^
In file included from -:1:
In file included from input_line_4:1:
In file included from ./Test.c:2:
In file included from ./BasicClassLibrary.h:7:
./BasicSpace.c:41:10: error: cannot initialize a variable of type 'char *' with an lvalue of type 'void *'
    char*s=sv;
         ^ ~~
./BasicSpace.c:42:10: error: cannot initialize a variable of type 'char *' with an lvalue of type 'void *'
    char*t=tv;
         ^ ~~
In file included from -:1:
In file included from input_line_4:1:
In file included from ./Test.c:2:
In file included from ./BasicClassLibrary.h:8:
./GenericClass.c:55:42: error: expected ')'
  sub            SetObjectHeader (Object this,ref f,int le, int wi,int so);
                                         ^
./GenericClass.c:55:34: note: to match this '('
  sub            SetObjectHeader (Object this,ref f,int le, int wi,int so);
                                 ^
./GenericClass.c:73:12: error: cannot initialize a variable of type 'Object' (aka 'ClassStruct *') with an lvalue of type 'void *'
    Object t=*pt;
           ^ ~~~
./GenericClass.c:85:12: error: cannot initialize a variable of type 'Object' (aka 'ClassStruct *') with an lvalue of type 'void *'
    Object t=*pt;
           ^ ~~~
./GenericClass.c:98:12: error: cannot initialize a variable of type 'Object' (aka 'ClassStruct *') with an lvalue of type 'void *'
    Object t=*pt;
           ^ ~~~
./GenericClass.c:109:30: error: expected ')'
  sub SetObjectHeader(Object this,ref f,int le, int wi,int so)
                             ^
./GenericClass.c:109:22: note: to match this '('
  sub SetObjectHeader(Object this,ref f,int le, int wi,int so)
                     ^
./GenericClass.c:111:5: error: invalid use of 'this' outside of a non-static member function
    this->f      = f;
    ^
./GenericClass.c:111:20: error: use of undeclared identifier 'f'
    this->f      = f;
                   ^
./GenericClass.c:112:5: error: invalid use of 'this' outside of a non-static member function
    this->offset = so;
    ^
./GenericClass.c:112:20: error: use of undeclared identifier 'so'
    this->offset = so;
                   ^
./GenericClass.c:113:5: error: invalid use of 'this' outside of a non-static member function
    this->nbytes = le*wi;
    ^
./GenericClass.c:113:20: error: use of undeclared identifier 'le'
    this->nbytes = le*wi;
                   ^
./GenericClass.c:113:23: error: use of undeclared identifier 'wi'
    this->nbytes = le*wi;
                      ^
./GenericClass.c:114:5: error: invalid use of 'this' outside of a non-static member function
    this->count  = le;
    ^
./GenericClass.c:114:20: error: use of undeclared identifier 'le'
    this->count  = le;
                   ^
./GenericClass.c:115:5: error: invalid use of 'this' outside of a non-static member function
    this->size  = wi;
    ^
fatal error: too many errors emitted, stopping now
[cling]$

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C Basic Class Library
« Reply #54 on: November 13, 2013, 10:55:39 PM »
Yes we intrude on its own C++ namespace.

Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
Re: C Basic Class Library
« Reply #55 on: November 13, 2013, 11:12:39 PM »
To put that in terms I can understand, are you saying that your code is being seen by Cling as C with C++ attributes?

C9 Red Hat

allbasic@c_basic:~/668428/usr/john/cb_class $ gcc Test.c -o test1
allbasic@c_basic:~/668428/usr/john/cb_class $ ./test1
LO
HELLO
HELLOHELLOHELLO
9
helloLOhellohello
hellohellohello
42.000000
42.000000
AbcAbcAbcAbcAbcAbcAbc
LOLOLOLOLOLOLO
AbcAbcAbcAbcAbcAbcAbc
<
>
  Hello World!
<HELLO WORLD!>
<<HELLO WORLD!>>
HELLO
HELLOH
abc
   \abc
WORD:       aa
Class Size: 80
Char Width: 1
Word Len:   2
Word Start: 14966994
Word End:   14966996
Text End:   14967000
=
allbasic@c_basic:~/668428/usr/john/cb_class $

Since Cling thinks it's a hotshot C++ interpreter now, I thought I would test your current code on the old CINT standard.

allbasic@c_basic:~/668428/usr/john/cb_class $ cint -p Test.c
Error: Can't call StringClassTableStruct::SetChars(&uo,"LO") in current scope FILE:Test.c LINE:23
Possible candidates are...
(in StringClassTableStruct)
!!! return from main() function

cint>

I thought :: was C++ style code.
« Last Edit: November 13, 2013, 11:42:17 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
Re: C Basic Class Library
« Reply #56 on: November 14, 2013, 12:45:45 AM »
Dynamic Strings in C

What is the downside of using a dynamic string scheme like THIS?

<string.h> seems to have most of the string manipulation routines in its library.

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C Basic Class Library
« Reply #57 on: November 14, 2013, 03:17:24 AM »
String.h is okay for null terminated ascii strings.

The string object defined in StringClass.h has length and char width attributes, and automatic buffering. - more like a Basic string

http://pubs.opengroup.org/onlinepubs/007908775/xsh/string.h.html

Offline John

  • Forum Support / SB Dev
  • Posts: 3570
    • ScriptBasic Open Source Project
Re: C Basic Class Library
« Reply #58 on: November 14, 2013, 10:00:42 AM »
When I concatenate a string with the new library, do I have to worry about string lengths? Keep in mind I have been spoiled by SB so it's going to be weird having to deal with this again.


Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: C Basic Class Library
« Reply #59 on: November 14, 2013, 10:58:24 AM »
No, the only thing left to do is free the strings before they go out of scope. There isn't an automatic garbage collector. (Yet)