BaCon - Bash-based Basic-to-C converter/compiler

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

BaCon - Bash-based Basic-to-C converter/compiler

#1 Post by vovchik »

Dear Puppians,

I know this should probably be in "Cutting Edge", but it is too late. Peter van Eerten, author of gtk-server, has written an amazing lexical parser/converter/compiler from basic to C in bash! He describes the program thus:
BaCon is a free BASIC to C converter for Unix-based systems. The following design goals were followed:
Must run on each Unix/Linux/BSD platform, including MacOSX
Converted sourcecode must be compilable with GCC
Must resemble genuine BASIC with implicit variable delarations
Spoken language constructs are preferred
To use Bacon your system must have Korn Shell or Bourne Again Shell (BASH) available. If both Korn Shell nor Bash are available on your platform, download and install the free Public Domain Korn Shell which can execute BaCon also. Furthermore BaCon works with the traditional Korn Shell KSH88, but also with a newer Kornshell implementation like the MirBSD Korn Shell.
It supports gtk gui creation via glade and via calls to gtk libs. I have packaged the "compiled" compiler (yes, BaCon compiles itself!), the bash version and examples in the attached archive. The GTK apps I tried were about 20-30k in size after stripping.

BaCon presupposes a compiling environment, so if you don't have gcc, gtk headers etc. installed, you won't be able to do anything except to examine the source. I have also cleaned up a few examples. Documentation is included in the /doc dir.

Now, if we could only write a little lib (in Bacon - it supports includes) to understand gtkdialog syntax, we would have a nice little tool and very and small speedy apps - and the problems that Zigbert retularly experiences with widgets misbehaving or not recognizing one another's states would be solved.

With kind regards,
vovchik

BTW. BaCon - via functions and subroutines - is a "structured" variety of the language, which means no spaghetti. You can use it like Pascal and also make direct C calls. The sources are available at: http://www.basic-converter.org.
Attachments
bacon.jpg
(28.28 KiB) Downloaded 10269 times
bacon.tar.gz
(166.06 KiB) Downloaded 2478 times

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#2 Post by Lobster »

Vovchik,

Extraordinary.
I look forward to trying this.
Hey BASIC Puppy programmers - must be a few . . .
Wot ya gonna work on?

Very exciting :)
The GTK apps I tried were about 20-30k in size after stripping.
How big were they before stripping?
How long did the stripping take?
Did you require a high degree of C knowledge to do this?

Hope to see this in future devx
:)
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
BarryK
Puppy Master
Posts: 9392
Joined: Mon 09 May 2005, 09:23
Location: Perth, Western Australia
Contact:

#3 Post by BarryK »

I continue to be amazed at the new projects that come out.

We should ask Peter to write a Bash-to-C converter!

Ahem, before anyone responds that there is one, no there isn't. There is a public domain tool that embeds Bash source inside a binary executable. That offers no advantages except hides the Bash source.

There is actually a shell to C converter, a commercial product, but it is not for Bash, one of the other shells.

Anyway, back onto BaCon -- yes, fascinating!
[url]https://bkhome.org/news/[/url]

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#4 Post by MU »

bash scripts usually make heavy use of pipes.
A pipe is like a "send the result to"

Code: Select all

found=`ls | grep "test"`
echo "$found"
show the content of the current folder - send the result to the search-tool "grep", and search for "test"

In Basic, or C, you usually use inbuilt functions for this, so no subshell is opened.

Code: Select all

result = dir()
found = search (result , "test")
print found

function search (thearray , thesearchword)
  REM here would follow some lines of search code
  return theresult
end function
So the design of Basic/C and Bash programs use very different concepts.
Basic/C: inbuilt code
Bash: external utilities, the typical GNU idea to put utilities in a chain.

I think the modular approach of Bash, is also the potential drawback, that might make it much slower than the other languages.
Each utility is run in a subshell, and even worse: invoking another interpreter, if the utility is a script.


------------------------
Bacon looks interesting. Small executables, and by importing functions from shared libraries, the function calls are very close to the C syntax.
Like this existing C documentation can be used as programming reference.

Like http://www.gtk.org/documentation.html.

Mark
Last edited by MU on Sat 14 Nov 2009, 01:13, edited 4 times in total.
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#5 Post by vovchik »

Dear Lobster,

I think we gain about 25% reduction by stripping (see the make-all script). It is instantaneous with such small binaries. The C stuff I changed in, for instance, gtkabout1.bac was pretty trivial - and I annotated my changes just to show how to create a button. Of course, we could write BaCon functions to wrap the gtk lib calls and make everything pretty and transparent - and more Basic-like.

I just got a nice email from Peter, the author, and he promises a working setenv, in addition to getenv, and a better system call (returning stdout and stderr) soon. I would also like to see CASE implemented, and he is considering it. We should send him bug reports or post them here, so that he gets feedback and continues to improve what I think is a very small, useful and CLEVER development tool.

If we have interesting libs or progs, we should also let him know so that he can post them on his site, too.

With kind regards,
vovchik

PS. I will look into the monkeymenu business. I think I first copied the Mr. Tech dir from FF into the appropriate Seamonkey dir, added a bit to the rdf (copied from a legitimate Seamonkey2 extension) and, after restarting Seamonkey2 and having fixed the rdf for monkeymenu in the same way, used Mr. Tech's local install feature to bring it in. It really works, although I haven't been able to adjust the size of the search box but did not try very hard. Since I was experimenting and not really counting on success, I didn't do a good job of recording my steps.

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#6 Post by Lobster »

I just got a nice email from Peter
. . . Sizzle . . .
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#7 Post by vovchik »

Dear Puppians,

No great BaCon progs yet, but here is a little BaCon template generator I bashed out this morning to ensure that henceforth we all program "cleanly". I AM making a joke. :)

With kind regards,
vovchik

PS. I should have written it in BaCon, I think.
Attachments
mkbac.tar.gz
(859 Bytes) Downloaded 2252 times

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#8 Post by vovchik »

Dear Puppians,

Although no great shakes here, I managed, as a test, to translate my bash mkbac into BaCon. My coding is exceptionally primitive, but I simply wanted to see how much time it would take to understand the syntax for basic procedures such as string handling, simple file i/o and parameter passing. The size of the BaCon binary is, obviously, much larger than the bash file, but that was to be expected, since the bash version doesn't really do that much and makes its own external calls (e.g date). I have attached the source and a binary.

I imagine MU and a few others on the forum could write a number of useful function libraries to simplify file, string handling, gtk calls and file i/o. What is amazing is that it works!

With kind regards,
vovchik
Attachments
bac-mkbak.tar.gz
(10.69 KiB) Downloaded 2201 times

gerry
Posts: 986
Joined: Thu 26 Jul 2007, 21:49
Location: England

#9 Post by gerry »

@Vovchik- I may be thoroughly confused here. Are you using Bacon language? If yes, then visit 99-bottles-of-beer.org, and be the first to do the exercise in Bacon.

Gerry

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#10 Post by vovchik »

Dear Puppians,

Peter is fabulous. He has done a new beta (attached) that includes everything I suggested: a select/case statement, a setenv and, most importantly, exec, which allows us to do things like this:

Code: Select all

result$ = EXEC$("ls -l")
Hats off! Now to test all of this....

With kind regards,
vovchik

PS. Gerry, I will have a look at 99 bottles. Yes, the previous post contains my BaCon port of a bash script.
Attachments
bacon-vb2.tar.gz
(120.24 KiB) Downloaded 2161 times

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#11 Post by vovchik »

Dear Gerry,

Thanks for the suggestion. I posted this at 99-bottles-of-beer.net. The team will have to examine the code first, but here it is, in all its inelegant glory. It does, however, demonstrate the new SELECT/CASE statement, which I wanted to try out. There are 1000 ways of doing this, at the very minimum. My approach was guided by the esteemed and widely accepted principle of "minimum thought".

Code: Select all

' *****************************************************
' PROGRAM:	beer.bac
' PURPOSE:	http://99-bottles-of-beer.net	
' AUTHOR:		vovchik (Puppy Linux forum)	
' DEPENDS:	gcc, bacon, bash
' PLATFORM:	Puppy Linux (actually, any *nix)
' DATE:		15-11-2009
' COMMENTS:	Not elegant - but works
' *****************************************************

' *********************
' INIT VARS
' *********************
beer_bottles = 100
stock$ = "bottles"
what$ = "of beer"
where$ = "on the wall"
action$ = "Take one down and pass it around, "
' *********************
' END INIT VARS
' *********************

' *********************
' MAIN
' *********************
REPEAT
	beer_bottles = beer_bottles - 1
	SELECT beer_bottles
		CASE 0
			stock$ = "No more bottles"
			action$ = "Go to the store and buy some more, "
			stock_depleted$ = CONCAT$(STR$(beer_bottles + 99)," ","bottles")
		CASE 1 
			stock$ = CONCAT$(STR$(beer_bottles)," ","bottle")
			stock_depleted$ = "no more bottles"
		CASE 2 
			stock$ = CONCAT$(STR$(beer_bottles)," ","bottle")
			stock_depleted$ = CONCAT$(STR$(beer_bottles-1)," ","bottle") 
			' stock_depleted$ = "no more bottles"
		DEFAULT
			stock$ = CONCAT$(STR$(beer_bottles)," ","bottles")
			stock_depleted$ = CONCAT$(STR$(beer_bottles-1)," ","bottles") 
	END SELECT
	PRINT
	PRINT stock$," ", what$," ", where$, ", ", LCASE$(stock$), " ", what$, "."
	PRINT action$, stock_depleted$, " ", what$," ", where$,"."
	PRINT
UNTIL beer_bottles EQ 0
END
' *********************
' END MAIN
' *********************
With kind regards,
vovchik

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#12 Post by vovchik »

Dear Puppians,

When I tried the following:

Code: Select all

 result$ = EXEC$("ls -l")
PRINT result$
I got this:

Code: Select all

root$ [/mnt/hdb6/install/bacon-vb2/tests]-> bacon exectest.bac
Starting conversion... done.
Applying indentation... done.
Starting compilation... done.
Program 'exectest' ready.
root$ [/mnt/hdb6/install/bacon-vb2/tests]-> ./exectest
total 72
-rwxr-xr-x 1 root root 22160 2009-11-15 14:59 beer
-rw-r--r-- 1 root root  1518 2009-11-15 15:04 beer.bac
-rw-r--r-- 1 root root 12172 2009-11-15 12:28 beer.txt
-rwxr-xr-x 1 root root 27051 2009-11-15 15:30 exectest
-rw-r--r-- 1 root root  1229 2009-11-15 15:30 exectest.bac

root$ [/mnt/hdb6/install/bacon-vb2/tests]-> ERROR: signal for
SEGMENTATION FAULT received - memory invalid or array out of bounds?
Try to compile the program with TRAP LOCAL to find the cause.
The exec routine executes correctly, as the screen output indicates, but something happens on returning from the function.

I have already mentioned this to Peter, so expect a bugfix soon.

Please post your but reports here, if you have any, so that he can attend to them.

With kind regards,
vovchik

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#13 Post by vovchik »

Dear Puppians,

Mea culpa! Turns out I was an idiot. Peter showed me the correct syntax for GETENVIRON and SETENVIRON:

Code: Select all

x$="MYGUI"
y$="<This is a test.>"
SETENVIRON x$, y$
z$ = GETENVIRON$(x$)
PRINT z$
The commands work as advertised.

Importantly, Peter has fixed the EXEC$ function. The new BaCon files are attached.

With kind regards,
vovchik
Attachments
bacon-vb2a.tar.gz
(116.72 KiB) Downloaded 2113 times

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#14 Post by big_bass »

Hey vovchik


A nice find thank you


I have to blow the dust off some old floppies
I have few programs I want to test


I didn't see the OUT command
that would allow port control for robotics/cnc :D

is that a possible ?

*when I was a student all the formulas to help solve math for electronics I wrote in
basic


Joe

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#15 Post by vovchik »

Dear Joe (Big Bass),

We could always ask Peter whether he might consider implementing an OUT command directly. He is very forthcoming. But, you could also write your own function that would call a C routine and adjust your source accordingly. That is the nice thing about BaCon - it is "best friends" with C, so that if a routine is available in a C lib, you can generally call it - first having figured out the calling syntax and required var types. I am certain you could integrate your existing formulas pretty easily in an include lib. That lib might also interest some others (like the gmp include lib) in the first post.

With kind regards,
vovchik

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#16 Post by vovchik »

Dear Puppians,

Peter has released his beta v. 2. Check out his site (bottom of my first post).

I have played around with Peter's hug library, which enables GTK GUIs to be created pretty painlessly. Attached is a little program that displays pngs and gifs, shows how to create buttons and to launch child windows, and how to run external progams and get the stdout results back for further processing. A bit of string handling is also in there.

The archive also includes a functioning binary and the hug lib. With a little work, this prog could also become a general pic viewer - although we already have two of Mark's excellent contributions (although they don't run on old puppy versions).

With kind regards,
vovchik
Attachments
bacon2.jpg
(51.07 KiB) Downloaded 3094 times
imgshow.tar.gz
(48.39 KiB) Downloaded 1361 times

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#17 Post by vovchik »

Dear Puppians,

Just finished coding an icon generator in Bacon. It's here - with source:
http://www.murga-linux.com/puppy/viewtopic.php?t=49129

With kind regards,
vovchik

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#18 Post by vovchik »

Dear Puppians,

Peter has now released his beta 3 of BaCon. You can get it here:

http://www.basic-converter.org/

With kind regards,
vovchik

edexter
Posts: 8
Joined: Wed 25 Nov 2009, 16:46

#19 Post by edexter »

I think you can con bcx basic into giving you gcc code and compiling for linux, you also have a mostly compatable interpeter availabe you would want the one from the yahoo group and you would need a dos emulator for it (unless there is a llinux version I am unaware of). It works with at least 7 c compilers including gcc, windows ce and the wii a. sorry if this is to off topic.I have some expierence with it as a package that may be transferable to the bash one also if someone is after more tools for it and has python with thier version of puppy linux(I haven't tried it yet, I think I have a bash for windows as well as linux so it sounds like it could be cross platform ). .

here is one of my tools for example, I can easily come up with source to html and maybe an ide with highlighting. between bcx basic and csound I have done alot of this type of thing.

http://dexrow.blogspot.com/2008/04/bcx- ... locks.html

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#20 Post by vovchik »

Dear Puppians,

Peter has now released his beta 4 of BaCon, in preparation for the release of version 1 (stable). You can get it here:

http://www.basic-converter.org/

With kind regards,
vovchik

PS. Pikona compiles fine with the new version.

Post Reply