Author Topic: C BASIC  (Read 65433 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #15 on: October 25, 2013, 02:17:17 PM »
Open source developers understand and acknowledge only one method of hearing your problems. Bug Tracker 

This may be related to your 4.8.1 issue.

http://sourceforge.net/p/mingw/bugs/2108/

« Last Edit: October 30, 2013, 11:15:34 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #16 on: October 25, 2013, 09:41:28 PM »
Yes it looks like the same bug.
« Last Edit: October 30, 2013, 11:15:48 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #17 on: October 25, 2013, 10:05:33 PM »
Looks like 4.8.1.4 solves the issue. The Nimrod 4.7 seems to work fine for me. I'm still bleeding from other cutting edges.  :D
« Last Edit: October 30, 2013, 11:16:00 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #18 on: October 26, 2013, 02:47:31 AM »
It has happened before:

http://forums.codeblocks.org/index.php?topic=10936.0

Next time I will make sure I always have a backup of key software.
« Last Edit: October 30, 2013, 11:16:12 AM by John »

kryton9

  • Guest
C BASIC
« Reply #19 on: October 26, 2013, 02:47:10 PM »
Sorry I couldn't get back earlier, you will see why in another post. Glad you got issues resolved.
Hope to get back soon enough.
« Last Edit: October 30, 2013, 11:16:25 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #20 on: October 26, 2013, 10:36:29 PM »
Charles,

Do you think it would be a lot of work doing a C BASIC version of Wetspot II? We already have a working QuickBASIC and gcc version to work with. My goal with this is to help QB programmers move there favorite games to C using your BASIC like C wrapper. I think there is a potential for a rebirth of retro games if we can get this simple enough to use.

John
« Last Edit: October 30, 2013, 11:16:37 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #21 on: October 27, 2013, 10:44:29 PM »
Not much work, I would take the GCC version and do symbol substitution. It's cheating slightly but the testing will be smooth. I see you have already been working on it :)

Here is the Basic.c include developed so far: The functions support strings and other dynamic objects.

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 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 create(A,B,C) A B=NewSpace((C)*sizeof(*B))
  #define copy(A,B,C) CopyInts((int*)A,(int*)B,C)
  #define HiWord(n) ((n>>16)and 0xffff)
  #define LoWord(n) (n and 0xffff)

  //indexbase 1

  function void* NewSpace(int count)
  begin
    char *v =  malloc(count);
    int i;
    for (i=0; limit i<count; step incr i) v[i]=0; // set chars to null
    return (void*) v;
  end

  sub CopyBytes(char*d,char*s,int count)
  begin
    int i;
    for (i=0; limit i<count; step incr i)
    begin
      *d=*s;
      next d;
      next s;
    end;
  end;

  sub CopyWords(char*d,char*s,int count)
  begin
    int i;
    short*v=(short*)d;
    short*w=(short*)s;
    for (i=0; limit i<count; step incr i)
    begin
      *v=*w;
      next v;
      next w;
    end;
  end;

  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;

  sub CopyWidth(char*t, int tw, char*s, int sw,int count)
  begin
    int i;
    if ((tw==sw)and(sw==2))
    begin
      CopyWords(s,t,count);
      return;
    end
    for (i=0; limit i<count; step i+=tw)
    begin
      *t=*s;
      t+=tw;
      s+=sw;
    end;
  end;

  function int ByteLen(char*s)
  begin
    int i=0;
    while (s[i]!=0) i++;
    return i;
  end

  function int WordLen(char*s)
  begin
    short*w=(short*)s;
    int i=0;
    while (w[i]!=0) i++;
    return i;
  end

  function int WordFill(char*s, int le, short c)
  begin
    short*w=(short*) s;
    int i=0;
    while (le--) w[i++]=c;
  end
« Last Edit: October 30, 2013, 11:16:48 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #22 on: October 27, 2013, 10:57:58 PM »
It seems to be going smoothly. I agree an automation tool would be helpful. I feel the IF/THEN/ELSE works fine and I'm on to CASE. I can do the easy stuff if you want concentrate on the no syntax match scenarios. Thanks for the update of your C BASIC library.
« Last Edit: October 30, 2013, 11:17:01 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #23 on: October 28, 2013, 01:58:41 AM »
switch is almose the same as select case, only you have to insert break; at the end of each case
« Last Edit: October 30, 2013, 11:17:14 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #24 on: October 28, 2013, 02:47:42 AM »
I just e-mail this to you before seeing this post.

Quote
Charles,

This is what I'm thinking for the C FOR/NEXT translation. I also change
the CASE to use the '_to_' for '..." in ranges. The 'to_' is use with
the FOR/NEXT.

Code: [Select]
  select_case (Game.status)
  begin_select
    case -500 _to_ -3:
      // The enemies are blocked by the clock
      incr Game.status++;
      if(Game.status == -2) then
        // Resumes the enemies
        Game.status = 0;
        if(Game.time < 16) then
          //ChangePal 0
        else_
          //ChangePal -1
        end_if
      end_if
      end_case
    case -2:
      // Do the lightnings
      Game.status = 0;
      for(int i = 0 to_ i < 80 step i++)
      begin_for
        DrawScreen();
      next
« Last Edit: October 30, 2013, 11:17:27 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #25 on: October 28, 2013, 11:37:10 PM »
I noticed that the C for() doesn't require {} if only a single statement follows. This breaks the current C BASIC rules and doesn't allow for a NEXT. My suggestion is that all C for() executing statement(s) be encased in a BEGIN_FOR/NEXT construct. ({})

C
Code: [Select]
int GetFreeObject()
{
for(int i = 0; i < MAXOBJS; i++)
if(Object[i].typ == 0) return i;

return (MAXOBJS-1);
}

C BASIC
Code: [Select]
FUNCTION int GetFreeObject()
BEGIN_FUNCTION
  FOR(int i = 0 TO i < MAXOBJS STEP i++)
  BEGIN_FOR
    IF(Object[i].typ == 0) THEN_DO RETURN i;
  NEXT
  RETURN (MAXOBJS-1);
END_FUNCTION

Another option is to add another keyword (END_FOR) and make NEXT optional. In the above C example it would look something like this.

Code: [Select]
FUNCTION int GetFreeObject()
BEGIN_FUNCTION
  FOR(int i = 0 TO i < MAXOBJS STEP i++)
    IF(Object[i].typ == 0) THEN_DO RETURN i;
  NEXT
  RETURN (MAXOBJS-1);
END_FUNCTION

Even though the above NEXT is optional, it would break the BASIC way FOR is understood if not use for readability.

If the FOR() executed multiple statements it takes the following formats.

Code: [Select]
FUNCTION int GetFreeObject()
BEGIN_FUNCTION
  FOR(int i = 0 TO i < MAXOBJS STEP i++)
  BEGIN_FOR
    IF(Object[i].typ == 0) THEN_DO RETURN i;
    PRINT i,"\n";
  END_FOR
  NEXT
  RETURN (MAXOBJS-1);
END_FUNCTION

Or without the NEXT.

Code: [Select]
FUNCTION int GetFreeObject()
BEGIN_FUNCTION
  FOR(int i = 0 TO i < MAXOBJS STEP i++)
  BEGIN_FOR
    IF(Object[i].typ == 0) THEN_DO RETURN i;
    PRINT i,"\n";
  END_FOR
  RETURN (MAXOBJS-1);
END_FUNCTION


« Last Edit: October 30, 2013, 11:17:40 AM by John »

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
C BASIC
« Reply #26 on: October 29, 2013, 02:27:43 AM »
That is true for all C block constructs. If there is only one statement within the block then curly braces are optional. So including curly braces in all instances, is okay, and may be preferable for translators.

I've adopted the more generic begin..end construct, rather than integrating braces with if .. endif etc. I think the code is quite easy to follow for BASIC programmers, certainly easier to write. With the string library and class generics, I've got nearly 700 lines of sample code so far.

Here is a sample: (these functions also support wide characters)
Code: [Select]
  method StringObject StringRtrim(StringObject this)
  begin
    int i;
    int w=this->width;
    int n=this->count;
    char*c = w * n - w + CharBase(this,0);
    if (w<2)
    begin
      for (i==n-w; to i<=0; step decr i)
      begin
        if (*c>32) then break;
        decr c;
      end
      return StringGetMid(this, 1, i, -1);
    end
    else
    begin
      for (i==n-w; to i<=0; i-=w)
      begin
        if ((c[0]>32)or(c[1] != 0)) then break;
        c+=w;
      end
      return StringGetMid(this, 1, i>>1, -1);
    end
  end


  method int StringInstr(int i, StringObject this,  StringObject k)
  //MATCHING MIXED WIDTHS BUT ONLY ON THE LOWER BYTE
  begin
    char*tb = CharBase(this,0);
    char*tc=tb;
    char*kc=CharBase(k,0);
    char*te=tc+this->count*this->width;
    char*ke=tc+k->count*k->width;
    if (i<0) then i+=this->count+1; // offset from right
    decr i; //indexbase 0
    tc+=i*this->width; // offset
    if (tc>=te) then return 0;
    if (kc>=ke) then return 0;
    char*td;
    char*kd;
    int tw=this->width;
    int kw=k->width;
    while (tc<te)
    begin
      if (*tc==*kc)
      begin
        td=tc;
        kd=kc;     
        while (*td==*kd)
        begin
          td+=tw;
          kd+=kw;
          if (kc==ke) then return ((td-tb)>>(tw-1))+1;
          if (td==te) then return 0;
          if (*td==*tc) then tc=td-1; // possible next match
        end
      end
      tc+=tw;
    end
  end
 

  function StringMethods StringClassTableBuild()
  begin
    static StringClassTable t;
    StringMethods vm references t;
    vm->Redim    references method StringRedim;
    vm->Get      references method StringGet;
    vm->Set      references method StringSet;
    vm->GetChars references method StringGetChars;
    vm->SetChars references method StringSetChars;
    vm->Show     references method StringShow;
    vm->GetMid   references method StringGetMid;
    vm->SetMid   references method StringSetMid;
    vm->Join     references method StringJoin;
    vm->Asc      references method StringAsc;
    vm->Ucase    references method StringUcase;
    vm->Lcase    references method StringLcase;
    vm->Ltrim    references method StringLtrim;
    vm->Rtrim    references method StringRtrim;
    vm->Instr    references method StringInstr;
    return vm;
  end
« Last Edit: October 30, 2013, 11:17:53 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #27 on: October 29, 2013, 09:00:04 AM »
Quote
I've adopted the more generic begin..end construct, rather than integrating braces with if .. endif etc. I think the code is quite easy to follow for BASIC programmers, certainly easier to write. With the string library and class generics, I've got nearly 700 lines of sample code so far.

It's even easier and less typing to use {}.

Maybe we should call it the BEGIN/END language and tell people it works sort of like Brainfuck.  :o

I guess I got carried away. My attempt may have been a bit verbose but the plan was once they enter their BASIC like program, the user would have the option to run a SB utility that would turn it back into standard C. (keywords revert back to their assigned values) I'll wait until I see where you're going with this before I put any more effort into Wetspot II.

Please post your current #define keyword list so I have a better idea what your thoughts are.
« Last Edit: October 30, 2013, 11:18:07 AM by John »

bigbass

  • Guest
C BASIC
« Reply #28 on: October 29, 2013, 11:30:47 AM »
Hey John


The expertise is from Charles Pegge and you !
I like how quickly the C BASIC looking syntax is being developed

*there are similar example demos that I have done for BaCon
with gtk and embedding C and ALIAS'es that could "bridge"
or connect together.

I do see C BASIC as a great prestep to porting code
and a simplified all in one BASIC syntax exchanged with C transparently

the game syntax ported
33 pages ya-ouch but a great test if it compiles and runs in C first


A suggestion have a include file that defines and does whatever macros
that will be needed it would be a light weight porting tool
to convert code to any BASIC

I have been working on this to have some transparent include file for BaCon
gtk2 and gtk3 have to have their own include files (there are still many demo widgets to convert)


Joe



some deps needed to compile the game
I may have other needed deps already installed



libsdl1.2-dev
libsdl-image1.2-dev
libsdl-mixer1.2-dev
« Last Edit: October 30, 2013, 11:18:32 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
C BASIC
« Reply #29 on: October 29, 2013, 11:59:37 AM »
Welcome Joe and thanks for joining in on the C BASIC project. Charles is a joy to work with and somehow keeps it on our level.

Armando got SDL2 working on C9 for the project and I'll send you an invite so you can join AIR, Kent, Daniel and me.  I will try to install the needed SDL2 additions you posted that doesn't come with the standard distro that Wetspot II requires. If all possible, I want to standardize on SDL2 going forward. If we are going to attract QB/PB programmers, they will expect a transparent DOS BASIC graphics library to port the thousands of games feared loss to obsolescence.

The fun doesn't end here. Once the game/app is in C/SDL, C4droid will compile it to an Android APK with touch and other Android UI features.

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