AllBASIC

BASIC Developer & Support Resources => Translators => BaCon => Topic started by: Pjot on September 08, 2010, 11:47:36 am

Title: BaCon
Post by: Pjot on September 08, 2010, 11:47:36 am
All,

There is also the BaCon project (http://www.basic-converter.org/), which implements a 2nd generation BASIC for Unix environments and converts this to C code, which then is compilable by GCC, TCC or the Compaq C compiler (and maybe even more compilers). The translation is based on the idea of passing expressions as-they-are to the resulting C code, allowing the design of high-performance calculations. This concept also leads to speedy translations. The resulting binaries are extremely small, even for large programs using GTK (around 50 to 60 kByte). Also several interfaces to other libraries exist, like SQlite, nCurses, GMP, Glade and more!

Greetings
Peter
Title: Re: BaCon
Post by: E.K.Virtanen on September 08, 2010, 11:49:17 am
Hi Pjot, and nice to see you onboard as well.

I am planning to build my next interpreter with your dialect. Would you like to check out my problem at http://www.allbasic.info/forum/index.php?topic=11.0 incase you could give me some ideas how to do it with BaCon directly  ::)
Title: Re: BaCon
Post by: JRS on September 08, 2010, 12:05:04 pm
Thanks Peter for joining the AllBasic forum and giving us an update where things stand with your Basic translator. I moved your thread to the translator board as it was an intro and not GP.

Title: Re: BaCon
Post by: Pjot on September 08, 2010, 12:17:48 pm
Hi John, I just saw your list of BASIC languages and BaCon was missing, so I thought I'ld mention it there... anyway nice to see it is the first post in this Translators forum  :)

Regards
Peter
Title: Re: BaCon
Post by: JRS on September 08, 2010, 12:28:29 pm
I guess The Basics' Page missed it. I'll add it to the list. Can you send me a graphic to use and I'll point it to your project site.

Welcome aboard!
Title: Re: BaCon
Post by: Pjot on September 08, 2010, 12:45:03 pm
E.K.,

It's the good old problem of line parsing...? Multiple statements on one line are separated by the colon symbol... Hmm... so first you always must make sure the colon symbols occuring in a line are not part of a string themselves. It is unavoidable to write a miniparser to replace non-embedded ':' symbols to some other value, for example NL (0x0A). This is what I do myself too in the BaCon version of BaCon.

Then I would use the SPLIT statement to dynamically separate the string into elements, each of which can be parsed individually.

Hope the below helps!

Peter

Code: [Select]
' The variable with the line of BASIC
line$ = "PRINT \"MYBASIC: parser\": FOR x = 1 TO 10: PRINT x: NEXT x: END"

' Some declarations
token$ = ""
escaped = 0
in_string = 0

' Step 1: make sure colon is translated to newline symbol
FOR x = 1 TO LEN(line$)
    SELECT MID$(line$, x, 1)
' Separator
CASE ":"
    IF ISFALSE(in_string) THEN
token$ = CONCAT$(token$, CHR$(10))
escaped = FALSE
    ELSE
token$ = CONCAT$(token$, ":")
    END IF
' Escape symbol
CASE CHR$(92)
    token$ = CONCAT$(token$, CHR$(92))
    IF ISFALSE(escaped) THEN
escaped = TRUE
    ELSE
escaped = FALSE
    END IF
' Quote symbol
CASE CHR$(34)
    token$ = CONCAT$(token$, CHR$(34))
    IF ISFALSE(escaped) THEN in_string = NOT(in_string)
    escaped = FALSE
DEFAULT
    token$ = CONCAT$(token$, MID$(line$, x, 1))
    escaped = FALSE
    END SELECT
NEXT

' Step 2: split into separate elements
SPLIT token$ BY CHR$(10) TO element$ SIZE dim

' Print results
FOR x = 0 TO dim-1
    PRINT CHOP$(element$[x])
NEXT

Title: Re: BaCon
Post by: Pjot on September 08, 2010, 12:55:54 pm
John, I have sent a picture by mail. Thanks for adding BaCon to the list!

Peter
Title: Re: BaCon
Post by: Pjot on September 09, 2010, 09:31:55 am
Hi Aurel,

Of course! As BaCon passes expressions as-they-are to the C compiler, arrays are basically C arrays. This means that the notation with square brackets is maintained. Of course you need to declare an array first (this never is done implicitly).

Example:

Code: [Select]
OPTION BASE 1

DECLARE array[10] TYPE NUMBER

FOR x = 1 TO 10
    array[x] = 5*x
NEXT

You can find more information on arrays in the  BaCon Manual (http://www.basic-converter.org/documentation.html#ARRAYS).

Hope this helps,
Peter
Title: Re: BaCon
Post by: Pjot on September 09, 2010, 11:11:03 am
Well, as mentioned, arrays in BaCon are not 'implemented', but passed as-they-are to the C compiler.  :) So in fact, the C compiler does all the work for me. That's why it is mentioned in the manual that BaCon is a lazy converter: the parsing of expressions and assignments is left to the C compiler.

Therefore you'ld better ask: how does a C compiler implement arrays? That's a whole different ballgame which leads to a long technical story. There's a lot to find on this on the net.

But what BaCon does implement is the associative array. An associative array is an array where the index is defined by a string, instead of a number. BaCon uses a C implementation where the information is stored in an array of structs. However we need an array then, and that is what we want to implement, right?  ;)

So, for BaCon, it is all done by the C compiler...

Regards
Peter
Title: Re: BaCon
Post by: codeguy on September 18, 2010, 10:05:21 pm
arrays in qb64 and its emitted c code ARE supported. i will be a strong proponent of qb64 as it is the best of 2 worlds: basic's simplicity and c's speed and power. every compilation results in a file of corresponding c code. if you navigate to the folder it's in, you've got the exact c code used by gcc to compile qb64 source. we are still in need of an interpreter, so if you want to help and know something about interpreter design, any help would be appreciated. this will be a standalone interpreter and may one day be integrated into qb64 itself.
Title: Re: BaCon
Post by: Pjot on September 21, 2010, 12:06:20 am
Hi Codeguy,

So I downloaded the v0.91 package from qb64.net but it seems to be all Windows? Is there a Unix version? (I do not have Win32/Win64 platform at all...)

Also there are a lot if .cpp files, so to compile qb64 it seems we need a C++ compiler...?

Regards,
Peter
Title: Re: BaCon
Post by: JRS on September 21, 2010, 02:55:19 am
I would like to see both BaCon and QB64 only generate the needed run-time for the application written.

BCX does this and it's MUCH easier to work with at the C(++) level.

Other than that, it's nice to see traditional Basic reincarnating itself with updated interfaces and OS support.
Title: Re: BaCon
Post by: Pjot on September 21, 2010, 11:50:32 am
Quote
I would like to see both BaCon and QB64 only generate the needed run-time for the application written.

John what do you mean...? For any BaCon binary there is only dependency to libc, libm and libdl. It generates only one binary. So no overhead to other stuff like GTK, GMP, GL or other stuff, not even in case BaCon progams actually use these libraries.

Or do you mean something else....?

Regards
Peter
Title: Re: BaCon
Post by: JRS on September 21, 2010, 11:57:49 am
Quote
John what do you mean...?

Hello World (http://www.allbasic.info/mboard/index.php?topic=881.0)

I'm talking about how BCX only generates the needed runtime functions based on the user coded application. Both BaCon and QB64 includes a full runtime with every compiled program.
Title: Re: BaCon
Post by: E.K.Virtanen on September 21, 2010, 01:42:35 pm
hmm...BCX is pretty old? I think it was something like 1999 or so when BCX project was launched. I might remember wrong though.
I dont see any reason to compare such a project for young ones, such as QB64 or BaCon.

Also it is good to remember what are the goals of the project. QB64 is updated QB and for sure, galleon does not currently ponder too much about optimizing yet. BaCon is .bash script which converts basic code to C and then uses gcc to compile it. I would not compare BCX for it at all.
Title: Re: BaCon
Post by: Pjot on September 21, 2010, 02:46:33 pm
Quote
Both BaCon and QB64 includes a full runtime with every compiled program.

Call me stupid but I simply don't understand what this means. Can you elaborate on the word "runtime"?

Thanks,
Peter

EDIT: I see your HELLO WORLD reference now. So I think I understand what you mean: the BaCon header file always contains all macros and definitions, while the C file contains the program. I guess you mean that this header file contains a lot of code which is not actually being used in the Hello World program. This header file you refer to with the term "runtime".

If this is what you mean, then you have to understand the way C macros work. For the header file of BaCon is used by the C Pre Processor to expand inline functions in the BaCon code. Meaning that only those macros are used which appear in the BaCon program. Meaning that the C compiler just uses the code actually referred to.

Surely, all macro code is generated during conversion, but beware, not all code is actually being used by the C compiler. That is the reason why the BaCon binarties are so small.

Also statements like "#include <stdio.h>" do not actually include a whole library. These statements just refer to header files which allow the C compiler to understand some libc function names.

So in terms of "runtime", BaCon binaries depend dynamically on libc, libm and libdl.

Nevertheless, the C code emitted by BCX is superior. It is very clean code which actually is human readable and it is easy to understand. And the code emitted by BaCon is not.

In a sense, BaCon uses C as intermediate code just to be able to create a binary. BaCon does not care if the C source is readable or not - it cares if it is compilable.

Regards
Peter
Title: Re: BaCon
Post by: JRS on September 21, 2010, 03:28:03 pm
Thanks Peter for taking the time to explain how BaCon compiles it's source.

I was wondering how you got your executables so small when QB64 is generating a 800KB runtime footprint for PRINT "Hello World".

Title: Re: BaCon
Post by: Pjot on September 22, 2010, 12:47:14 am
Well in BaCon, the "Hello world" program in binary form has a size of 25kB, after a 'strip'. This is still 5x bigger than a plain C implementation where the "Hello world" results into a 5kB program. Probably BCX can generate binaries as small as the plain C sources also, which is very nice.

On the other hand, a GTK GUI program in BaCon, for example the Source Code Editor, has barely 50kB after compilation (yes fifty), while it is a full blown GUI application.

Regards
Peter
Title: Re: BaCon
Post by: JRS on September 22, 2010, 12:59:35 am
I would like to add that your Gtk Server (http://www.gtk-server.org/) library is another example of the fine work you do. I remember when I pleaded for a DLL version of Gtk-Server back in 2005/6 and you created a ScriptBasic extension module. (one function wonder  :o )