Author Topic: C BASIC  (Read 65441 times)

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« on: October 23, 2013, 02:53:24 AM »
This might be useful for a C emitter  :)

Code: [Select]

  #include <stdio.h>
  #define function
  #define gosub
  #define dim
  #define as
  #define then
  #define sub   void
  #define begin {
  #define end   }
  #define endif }
  #define and   &&
  #define or    ||

  function int main()
  begin
    dim as int a=2;
    sub mysubroutine()
    begin
     if ((a==2)and(a!=1)) then a+=40; else a=0;
    end
    gosub mysubroutine();
    printf("%i",a);
  end

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #1 on: October 23, 2013, 10:05:11 AM »
That is very cool Charles. The C #define and macros can make C digestible. Thanks for the interesting perspective.
« Last Edit: October 30, 2013, 11:19:00 AM by John »

kryton9

  • Guest
C BASIC
« Reply #2 on: October 23, 2013, 07:13:26 PM »
That's excellent Charles. I did the brace defines with begin & and, but never thought of the other defines.
Syntax coloring, can make it even more like BASIC
« Last Edit: October 30, 2013, 11:19:10 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #3 on: October 23, 2013, 07:23:31 PM »
I like that you can define syntax that is nothing more than decorations. (no-op place holders)
« Last Edit: October 30, 2013, 11:19:23 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #4 on: October 24, 2013, 01:46:17 PM »
Okay lets go hardcore  ;D

This is an exercise in modelling OOP

Code: [Select]

  #include <stdlib.h>
  #include <stdio.h>
  #define function
  #define method
  #define gosub
  #define dim
  #define as
  #define then
  #define sub   void
  #define begin {
  #define end   }
  #define endif }
  #define and   &&
  #define or    ||
  #define class typedef struct
  #define types typedef struct
  #define methods
  #define member ->
  #define addressof &
  #define has =
  #define NewSpace  malloc
  #define FreeSpace free

  #define create(A,B,C) A B=malloc((C)*sizeof(*B))
  #define copy(A,B,C) CopyInts((int*)A,(int*)B,C);

  /*

  ENTITIES
  ========

  Using Vector as a generic model for OOP

  VectorType
  VectorArray
  VectorClass
  VectorClassTable
  VectorMethods
  VectorObject

  NewVector
  FreeVector

  */

  sub CopyInts(int*d,int*s,int n)
  begin
    n=n/sizeof(int);
    int i;
    for (i=0;i<n;i++)
    begin
      *d=*s;
      d++;
      s++;
    end;
  end;


  types VectorStruct
  begin
    float x,y,z;
  end
  VectorType,*VectorArray;


  class VectorClassStruct
  begin
    void*vft;
    int count;
    VectorArray v;
  end
  *VectorObject;


  types VectorClassTableStruct
  begin
    method void  (*Redim)  (VectorObject vv,int n);
    method float (*Assign) (VectorObject vv, int i, float x,float y,float z);
    method float (*Scale)  (VectorObject vv, int i, float s);
  end
  VectorClassTable,*VectorMethods;


  methods

  method void VectorRedim(VectorObject vv,int n)
  begin
    VectorArray va has NewSpace(n*sizeof(VectorType));
    copy(va,vv member v,vv member count*sizeof(VectorType));
    free(vv member v);
    vv member v = va;
    vv member count = n;
  end;

  method float VectorAssign(VectorObject vv, int i, float x,float y,float z)
  begin
    VectorArray w has addressof vv member v[i];
    w member x = x;
    w member y = y;
    w member z = z;
  end;

  method float VectorScale(VectorObject vv,int i, float s)
  begin
    if (vv member count <= i) then VectorRedim(vv,i+16);
    VectorArray w has addressof vv member v[i];
    w member x *= s;
    w member y *= s;
    w member z *= s;
  end;

  function VectorMethods VectorClassTableBuild(void)
  begin
    static VectorClassTable sct;
    VectorMethods vm has addressof sct;
    vm member Redim  has addressof VectorRedim;
    vm member Assign has addressof VectorAssign;
    vm member Scale  has addressof VectorScale;
    return vm;
  end

  function VectorObject NewVector()
  begin
    static VectorMethods vm=0;
    if (vm==0) then vm = VectorClassTableBuild();
    create(VectorObject,vv,1);
    vv member vft = vm;
    vv member count = 16;
    vv member v has NewSpace(16*sizeof(VectorType));
    return vv;
  end

  sub FreeVector(VectorObject vv)
  begin
    FreeSpace(vv member v);
    FreeSpace(vv);
  end


  function int main()
  begin
    VectorObject vo = NewVector();
    printf("%i\n",vo member count);
    VectorMethods vm=vo member vft;
    vm->Assign(vo, 0, 1.0, 2.0, 3.0);
    printf("%f,%f,%f\n", vo member v[0].x, vo member v[0].y, vo member v[0].z);
    FreeVector(vo);
  end;
« Last Edit: October 30, 2013, 11:19:33 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #5 on: October 24, 2013, 03:16:27 PM »
Output of the above C BASIC program

Code: [Select]
jrs@laptop:~/C_BASIC$ gcc cbas_oop.c
jrs@laptop:~/C_BASIC$ ./a.out
16
1.000000,2.000000,3.000000
jrs@laptop:~/C_BASIC$



Charles,

I have created this child board for your C BASIC project. Thanks for your contributions!

I guess it would be a good time for me to start digging through Peter's SB macros for hidden treasures.  :o

John
« Last Edit: October 30, 2013, 11:19:44 AM by John »

kryton9

  • Guest
C BASIC
« Reply #6 on: October 24, 2013, 07:13:16 PM »
Look how awesome this is and looks! 
Charles you are amazing!

Now if you can figure out how to use abstract data types, then you only need to make one header file to handle
any type. C++ makes this easy with templates.

I have not dived in depth, but know it can be done in C.

Here is a start (ADT: Abstract Data Type):
http://inst.eecs.berkeley.edu/~selfpace/studyguide/9C.sg/Output/ADTs.in.C.html
« Last Edit: October 30, 2013, 11:19:54 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #7 on: October 24, 2013, 07:18:04 PM »
How about putting all the BASIC defines in a .h file and keep the application code as close to BASIC as possible?
« Last Edit: October 30, 2013, 11:20:06 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #8 on: October 24, 2013, 08:32:40 PM »
Sure.

We will also need a set of string functions. Unlike regular Basic, String concatenation will have to be done in an Ellipsis function. Garbage collection will also have to be explicit, as well as OOP setup.

I don't know how far we can go with formal templates but the code is highly adaptable by various substitutions.

I think we can use this as a high-level C emitter for OxygenBasic, so all the hidden operations are made explicit.

Here is mark 3. There are very few traces of C left :)

Code: [Select]

  //#include <stdlib.h>
  #include <stdio.h>

  #define function
  #define method
  #define gosub
  #define dim
  #define as
  #define to
  #define limit
  #define step
  #define then
  #define sub   void
  #define begin {
  #define end   }
  #define endif }
  #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 NewSpace  malloc
  #define FreeSpace free

  #define create(A,B,C) A B=malloc((C)*sizeof(*B))
  #define copy(A,B,C) CopyInts((int*)A,(int*)B,C);

  /*

  ENTITIES
  ========

  Using Vector as a generic model for OOP

  VectorType
  VectorArray
  VectorClass
  VectorClassTable
  VectorMethods
  VectorObject

  NewVector
  FreeVector

  */

  sub CopyInts(int*d,int*s,int count)
  begin
    count /= sizeof(int); //itr beware packed types
    int i;
    for (i=0; limit i<count; step incr i)
    begin
      *d=*s;
      next d;
      next s;
    end;
  end;


  types VectorStruct
  begin
    float x,y,z;
  end
  VectorType,*VectorArray;


  class VectorClassStruct
  begin
    ref         vft;
    int         count;
    VectorArray v;
  end
  *VectorObject;


  types VectorClassTableStruct
  begin
    method void  (byref Redim)  (VectorObject this,int n);
    method float (byref Assign) (VectorObject this, int i, float x,float y,float z);
    method float (byref Scale)  (VectorObject this, int i, float s);
  end
  VectorClassTable,*VectorMethods;


  methods

  method void VectorRedim(VectorObject this, int n)
  begin
    create(VectorArray,va,n);
    copy(to va, this member v, this member count*sizeof(VectorType));
    free(this member v);
    this member v = va;
    this member count = n;
  end;

  method float VectorAssign(VectorObject this, int i, float x,float y,float z)
  begin
    if (this member count <= i) then VectorRedim(this, i+16);
    VectorArray w references this member v[i];
    w member x = x;
    w member y = y;
    w member z = z;
  end;

  method float VectorScale(VectorObject this, int i, float s)
  begin
    if (this member count <= i) then VectorRedim(this, i+16);
    VectorArray w references this member v[i];
    w member x *= s;
    w member y *= s;
    w member z *= s;
  end;

  function VectorMethods VectorClassTableBuild()
  begin
    static VectorClassTable t;
    VectorMethods vm references t;
    vm member Redim  references method VectorRedim;
    vm member Assign references method VectorAssign;
    vm member Scale  references method VectorScale;
    return vm;
  end

  function VectorObject NewVector()
  begin
    static VectorMethods vm;
    if (vm==0) then vm = VectorClassTableBuild();
    create(VectorObject,this,1);
    this member vft = vm;
    this member count = 16;
    this member v has NewSpace(16*sizeof(VectorType));
    return this;
  end

  sub FreeVector(VectorObject this)
  begin
    FreeSpace(this member v);
    FreeSpace(this);
  end


  function int main()
  begin
    VectorObject vo = NewVector();
    VectorMethods vm=vo member vft;
    vm->Assign(vo, 20, 1.0, 2.0, 3.0);
    vm->Scale(vo, 20, 4.0);
    printf("%i\n",vo member count);
    int i=20;
    printf("%f,%f,%f\n", vo member v[i].x, vo member v[i].y, vo member v[i].z);
    FreeVector(vo);
  end;
« Last Edit: October 30, 2013, 11:20:15 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #9 on: October 25, 2013, 09:29:17 AM »
Charles,

I'm now getting warnings with this version.

jrs@laptop:~/C_BASIC$ gcc cbas_oop2.c
cbas_oop2.c: In function ‘VectorRedim’:
cbas_oop2.c:100:5: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
cbas_oop2.c:102:5: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
cbas_oop2.c: In function ‘NewVector’:
cbas_oop2.c:139:5: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
cbas_oop2.c: In function ‘FreeVector’:
cbas_oop2.c:148:5: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
jrs@laptop:~/C_BASIC$ ./a.out
36
4.000000,8.000000,12.000000
jrs@laptop:~/C_BASIC$

« Last Edit: October 30, 2013, 11:20:28 AM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
C BASIC
« Reply #10 on: October 25, 2013, 10:28:30 AM »
Uncomment stdlib line.
« Last Edit: October 30, 2013, 11:20:38 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Re: GTK BASIC
« Reply #11 on: October 25, 2013, 11:02:15 AM »
Thanks AIR!

jrs@laptop:~/C_BASIC$ gcc cbas_oop2.c
jrs@laptop:~/C_BASIC$ ./a.out
36
4.000000,8.000000,12.000000
jrs@laptop:~/C_BASIC$

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #12 on: October 25, 2013, 12:53:39 PM »
I installed the latest MingW, and to my horror I get this when I try to fire up GCC or any of the other apps. My 2009 version presented no problems.

Any ideas?



« Last Edit: October 30, 2013, 11:20:49 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #13 on: October 25, 2013, 01:07:25 PM »
I would use MinGW from the Nimrod for Windows 32 I posted. That would give you everything you need for C BASIC and allow you to mentor us along on the Nimrod piece as well.

Try adding something more than just gcc for your test.

jrs@laptop:~/C_BASIC$ gcc
gcc: fatal error: no input files
compilation terminated.
jrs@laptop:~/C_BASIC$
« Last Edit: October 30, 2013, 11:21:00 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #14 on: October 25, 2013, 02:10:47 PM »
Thanks John.

Nimrod icludes gcc version 4.7, and calm is restored.

I am shocked that a bum windows binary of gcc could be distributed (4.8.1). and no direct way of contacting the developers.
« Last Edit: October 30, 2013, 11:21:11 AM by John »