Monday, November 17, 2014

QB2C

QB2C has been around for along time. QB2C is a Quickbasic to C translator. So you can take the Old BASIC source and translate it to C for recompiling. For low end machines such as embedded system this can be a boon for not having to reinvent the wheel. Just put it on my Cisco NSLU2. Of course you have to compile the source code on the unit. That took a while but the working programs seem fast enough. There are some quirks as it is not perfect, so you may have to massage some C code for it to work.

 $ cat /proc/cpuinfo
Processor    : XScale-IXP42x Family rev 1 (v5l)
BogoMIPS    : 266.24
Features    : swp half thumb fastmult edsp
CPU implementer    : 0x69
CPU architecture: 5TE
CPU variant    : 0x0
CPU part    : 0x41f
CPU revision    : 1

Hardware    : Linksys NSLU2

But there can be some other advantages. One such advantage is that you can learn C code by comparing the original BASIC code to the resulting C code. Take for example your traditional helloworld program and compare it to the resulting C code.

PRINT "Hello, World!"

$ ./qb2c test.bas test.c
qb2c: translating  test.bas  -->  test.c
Translation done in 0 sec.
$  cat test.c
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <ctype.h>

/* This file was generated by QuickBasic to C translator */
/* qb2c  ver.3.2k 29.Mar.1999 Free version.                            */

#define LMAX 1024

/* Function declarations */

/* Shared variables and arrays declarations */

/* Open files pointers */
main()
{
 printf("%s\n","Hello, World!");
} /* End of MAIN */

Despite the extra code:

PRINT "Hello, World!"

Translates to:

printf("%s\n","Hello, World!");

Some ways a lot a like. 

If we wanted to do a loop then it might turn out like this: (only showing the relevant parts).


From:

FOR X = 1 TO 5
PRINT "Helllo, world!"
NEXT X

to:

  static float X;

 for(X = 1; X <= 5; X++)
 {
  printf("%s\n","Hello, world!");
 }

Think you get the idea!

Have fun.

No comments:

Post a Comment