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
jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

Re: Raio buttons

#121 Post by jpeps »

jpeps wrote:
vovchik wrote:Dear jpeps,

In my tests, radio buttons in HUG work exactly as advertised. Try this code:

Code: Select all

INCLUDE "hug.bac"

win_width = 200
win_height = 100
mainwin = WINDOW("Radio test", win_width, win_height)
my_radio1 = RADIO("On", 50, 30, my_radio2)
my_radio2 = RADIO("Off", 50, 30, my_radio1)
ATTACH(mainwin, my_radio1, 20, 20)
ATTACH(mainwin, my_radio2, 20, 50)
DISPLAY
With kind regards,
vovchik
Thanks vovchik,

And for a group? Specifically, I was looking at:

FUNCTION RADIO (STRING text$, int xsize, int ysize, NUMBER group)

(I did something similar using checkbuttons with a reset function)
This seems to work:

Code: Select all

INCLUDE "hug.bac"

win_width = 200
win_height = 100
mainwin = WINDOW("Radio test", win_width, win_height)
my_radio1 = RADIO("One", 50, 30, my_radio3)
my_radio2 = RADIO("Two", 50, 30, my_radio1)
my_radio3 = RADIO("Three", 50, 30, my_radio1)
ATTACH(mainwin, my_radio1, 20, 20)
ATTACH(mainwin, my_radio2, 20, 50)
ATTACH(mainwin, my_radio3, 20, 80)    

DISPLAY 

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

radio buttons

#122 Post by vovchik »

Dear jpeps,

I was hoping you would do just that. After posting and seeing your reply, I wrote the EXACT same code.

With kind regards,
vovchik

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

Re: radio buttons

#123 Post by jpeps »

vovchik wrote:Dear jpeps,

I was hoping you would do just that. After posting and seeing your reply, I wrote the EXACT same code.

With kind regards,
vovchik
:D

Here's revised code for MyFavorites loader:
(note: points to my own programs, so edit)

http://jpeters.net/apps/MyPrograms2.txt
Last edited by jpeps on Tue 16 Nov 2010, 20:50, edited 1 time in total.

2lss
Posts: 225
Joined: Sun 20 Sep 2009, 23:54

#124 Post by 2lss »

I gave the source code editor on basic-converter.org a shot and I liked it. So I decided to try adding a few features to make it more like an ide (similar to tinyide)

I added the ability to compile the code (Menu > Build > Compile) and the ability to execute the code ( Menu > Build > Execute).

There are a few issues with each though. When "compile" is clicked, it prompts the user to save the file. If the user clicks yes and the file has not been saved before it will bring up the 'save_as' dialog however, it will compile the code before the 'save' or 'cancel' button is clicked giving an error .

Clicking "execute" will launch xterm (if TERM=xterm) and execute the program. I'm not sure how to make this work with rxvt since it does not have a '-hold' option.

I originally wanted to pipe stdout into a text box widget instead of calling xterm but I was unable to get the pipe to work. Maybe someone else could find the problem. (lines 825 - 846)
Attachments
source_mod.tar.gz
(6.62 KiB) Downloaded 415 times

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

#125 Post by BarryK »

Maybe you guys who are BaCon experts can see how this can be done elegantly....

In many shell scripts we have:

. /etc/rc.d/PUPSTATE

Where PUPSTATE is a file, which has something like this in it:

Code: Select all

PUPMODE=12
PDEV1='sr0'
DEV1FS='iso9660'
PUPSFS='sda7,ext3,/w0981011.143/wary_098.sfs'
PUPSAVE='sda7,ext3,/warysave-098.2fs'
PMEDIA='usbcd'
#kernel with libata pata has both sata and pata drives in ATADRIVES...
ATADRIVES='sda '
#these directories are unionfs/aufs layers in /initrd...
SAVE_LAYER='/pup_rw'
PUP_LAYER='/pup_ro2'
#The partition that has the warysave file is mounted here...
PUP_HOME='/mnt/dev_save'
#(in /initrd) ...note, /mnt/home is a link to it.
#this file has extra kernel drivers and firmware...
ZDRV='sda7,ext3,/w0981011.143/zw098114.sfs'
#complete set of modules in the initrd (moved to main f.s.)...
ZDRVINIT='no'
#Partition no. override on boot drive to which session is (or will be) saved...
PSAVEMARK=''
So the problem is how to read in each line in a BaCon program, ignoring comments, and assign those variables so they can be used in the program.

Unlike the INCLUDE operation in BaCon, this has to be a run-time evaluation. I can't quite see how to do it. I suppose an associative array? ...but I would really like to assign each variable independently.

Perhaps there is some obvious way to do it, and I just can't see the obvious.
[url]https://bkhome.org/news/[/url]

User avatar
PjotAwake
Posts: 34
Joined: Wed 03 Nov 2010, 20:58
Location: The Hague, The Netherlands
Contact:

#126 Post by PjotAwake »

Hi 2lss,

Thanks for your contributions. In fact, I have plans for this editor - I would like to add the BaCon converter itself, as a whole, to the program, so it's all embedded in one. We would have a fourth incarnation of BaCon but completely graphical based on GTK.

For the conversion progress, this is something I would rewrite using a Progress Bar.

Furthermore I would add a configuration panel where it is possible to fill in which type of output terminal is being used: xterm, rxvt, Terminal, gnome-terminal. Also in which size it has to appear.

The 'hold' of the terminal, you could embed the execution of the resulting program in a script which simply finishes with a 'read' - this will wait for input from the keyboard.

Tabbing should be added as well.

Syntax completion would be nice but I could not get that to work.

Your additions are a valuable step to these ideas, thanks! So, lots to do, wish there was more time! :-)

Regards
Peter
Last edited by PjotAwake on Thu 18 Nov 2010, 13:06, edited 1 time in total.

User avatar
PjotAwake
Posts: 34
Joined: Wed 03 Nov 2010, 20:58
Location: The Hague, The Netherlands
Contact:

#127 Post by PjotAwake »

So the problem is how to read in each line in a BaCon program, ignoring comments, and assign those variables so they can be used in the program.

Unlike the INCLUDE operation in BaCon, this has to be a run-time evaluation.
Well that is a difficult thing to do as the program will be compiled - C cannot evaluate strings into actual variable names. An interpreter is needed for that. So you probably end up with a program like the below, which probably can be optimized.

Regards
Peter

Code: Select all

OPEN "/etc/rc.d/PUPSTATE" FOR READING AS handle

WHILE NOT(ENDFILE(handle))

    READLN line$ FROM handle

    SPLIT line$ BY "=" TO item$ SIZE dim
    
    IF dim > 0 THEN
        SELECT item$[0]
            CASE "PUPMODE"
                PUPMODE = VAL(item$[1])
            CASE "PDEV1"
                PDEV1$ = item$[1]
            CASE "DEV1FS"
                DEV1FS$ = item$[1]
            CASE "PUPSFS"
                PUPSFS$ = item$[1]
            CASE "PUPSAVE"
                PUPSAVE$ = item$[1]
            CASE "PMEDIA"
                PMEDIA$ = item$[1]
            CASE "ATADRIVES"
                ATADRIVES$ = item$[1]
            CASE "SAVE_LAYER"
                SAVE_LAYER$ = item$[1]
            CASE "PUP_LAYER"
                PUP_LAYER$ = item$[1]
            CASE "PUP_HOME"
                PUO_HOME$ = item$[1]
            CASE "ZDRV"
                ZDRV$ = item$[1]
            CASE "ZDRVINIT"
                ZDRVINIT$ = item$[1]
            CASE "PSAVEMARK"
                PSAVEMARK$ = item$[1]
        END SELECT
    END IF

WEND

CLOSE FILE handle

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

#128 Post by vovchik »

Dear Barry,

This isn't elegant or pretty, but it works. If the varnames are known in advance (and they are), this is one easy way of getting the assignments done.

Code: Select all

OPTION BASE 1
myfile$ = "PUPSTATE"
mygrep$=CONCAT$(" | grep -v ", CHR$(34),"#",CHR$(34))
mycmd$ = CONCAT$("cat ", myfile$, mygrep$)
mypupstate$ = CHOP$(EXEC$(mycmd$))
SPLIT mypupstate$ BY NL$ TO mypuplines$ SIZE pupline_entries
FOR i = 1 TO pupline_entries
	valvar$ = ""
	varname$ = ""
	mypuplines$[i] = REPLACE$(mypuplines$[i],"'","") 
	varname$ = LEFT$(mypuplines$[i], INSTR(mypuplines$[i], "=") - 1)
	valvar$ = MID$(mypuplines$[i], INSTR(mypuplines$[i], "=") + 1)
	SELECT varname$
		CASE "PUPMODE"
			PUPMODE$ = valvar$
		CASE "PDEV1"
			PDEV1$ = valvar$
		CASE "DEV1FS"
			DEV1FS$ = valvar$
		CASE "PUPSFS"
			PUPSFS$ = valvar$
		CASE "PUPSAVE"
			PUPSAVE$ = valvar$
		CASE "PMEDIA"
			PMEDIA$ = valvar$
		CASE "ATADRIVES"
			ATADRIVES$ = valvar$
		CASE "SAVE_LAYER"
			SAVE_LAYER$ = valvar$
		CASE "PUP_LAYER"
			PUP_LAYER$ = valvar$
		CASE "PUP_HOME"
			PUP_HOME$ = valvar$
		CASE "ZDRV"
			ZDRV$ = valvar$
		CASE "ZDRVINIT"
			ZDRVINIT$ = valvar$
		CASE "PSAVEMARK"
			PSAVEMARK$ = valvar$
	END SELECT
NEXT i
With kind regards,
vovchik

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

#129 Post by BarryK »

I actually did do this in another compiled language ...I don't recall which one, it might have been Genie. On that occasion I think that I used an associative array.

I just tried it in BaCon:

Code: Select all

GLOBAL pupstate$ ASSOC STRING

REM read /etc/rc.d/PUPSTATE and evaluate vars...
OPEN "/etc/rc.d/PUPSTATE" FOR READING AS handle1
WHILE NOT(ENDFILE(handle1)) DO
 READLN line$ FROM handle1
 IF NOT(ENDFILE(handle1)) THEN
  first$=LEFT$(line$,1)
  IF NOT(EQUAL(first$,"#")) THEN
   SPLIT line$ BY "=" TO array$ SIZE dimension
   val$=CHOP$(array$[1],"' ")
   pupstate$(array$[0])=val$
  ENDIF
 ENDIF
WEND
CLOSE FILE handle1

REM test result:
REM retrieves all elements of assoc array:
LOOKUP pupstate$ TO c$ SIZE d
FOR x=0 TO d-1
 PRINT c$[x]
NEXT
REM print one variable:
PRINT pupstate$("PUPSFS")
...yep, that works. It just means that the variable PUPSFS has to be referrred to in the program as pupstate$("PUPSFS") ...I can live with that.
[url]https://bkhome.org/news/[/url]

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

Much better

#130 Post by vovchik »

Dear Barry,

Nicer solution, to be sure....

With kind regards,
vovchik

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

#131 Post by BarryK »

Thanks to Peter who has implemented CONTINUE, I have tidied up the program. It now checks for blank lines and indented '#' comment lines (doesn't test for comments on the end of lines, but that could be done):

Code: Select all

GLOBAL pupstate$ ASSOC STRING

REM read /etc/rc.d/PUPSTATE and evaluate vars...
OPEN "/etc/rc.d/PUPSTATE" FOR READING AS handle1
WHILE NOT(ENDFILE(handle1)) DO
 READLN line$ FROM handle1
 chopped$=CHOP$(line$)
 IF EQUAL(chopped$,"") THEN CONTINUE
 first$=LEFT$(chopped$,1)
 IF EQUAL(first$,"#") THEN CONTINUE
 SPLIT chopped$ BY "=" TO array$ SIZE dimension
 val$=CHOP$(array$[1],"' ")
 pupstate$(array$[0])=val$
WEND
CLOSE FILE handle1
[url]https://bkhome.org/news/[/url]

User avatar
Ted Dog
Posts: 3965
Joined: Wed 14 Sep 2005, 02:35
Location: Heart of Texas

Can't we just let BASIC die already.......

#132 Post by Ted Dog »

:twisted: Can't we just let BASIC die already.......

Slightly kidding..

I know it works with tcc according to the website, would be nice to chain them together. so we would not need dev.sfs

mechanic
Posts: 29
Joined: Sun 12 Apr 2009, 20:20

Another version of assoc arrays

#133 Post by mechanic »

BarryK wrote:I actually did do this in another compiled language ...I don't recall which one, it might have been Genie. On that occasion I think that I used an associative array.

I just tried it in BaCon:

Code: Select all

GLOBAL pupstate$ ASSOC STRING

REM read /etc/rc.d/PUPSTATE and evaluate vars...
OPEN "/etc/rc.d/PUPSTATE" FOR READING AS handle1
WHILE NOT(ENDFILE(handle1)) DO
 READLN line$ FROM handle1
 IF NOT(ENDFILE(handle1)) THEN
  first$=LEFT$(line$,1)
  IF NOT(EQUAL(first$,"#")) THEN
   SPLIT line$ BY "=" TO array$ SIZE dimension
   val$=CHOP$(array$[1],"' ")
   pupstate$(array$[0])=val$
  ENDIF
 ENDIF
WEND
CLOSE FILE handle1

REM test result:
REM retrieves all elements of assoc array:
LOOKUP pupstate$ TO c$ SIZE d
FOR x=0 TO d-1
 PRINT c$[x]
NEXT
REM print one variable:
PRINT pupstate$("PUPSFS")
...yep, that works. It just means that the variable PUPSFS has to be referrred to in the program as pupstate$("PUPSFS") ...I can live with that.
Hi Barry, here is an alternate version that I played with when testing the assoc arrays. Yours may be faster, I haven't checked.

Code: Select all


DECLARE prop$ ASSOC STRING

infile$ = "/etc/rc.d/PUPSTATE"
OPEN infile$ FOR READING AS fh
WHILE NOT(ENDFILE(fh)) DO
    READLN txt$ FROM fh
    txt$ = CHOP$(txt$)
	IF EQUAL(LEFT$(txt$, 1), "#") THEN CONTINUE
	l$ = LEFT$(txt$,INSTR(txt$,"=")-1)
	r$ = RIGHT$(txt$,LEN(txt$)-INSTR(txt$,"="))
	prop$(l$) = r$ 
WEND

PRINT prop$("DEV1FS")
PRINT prop$("ZDRV")
PRINT prop$("PUPSAVE")

PS. Thanks for Puppy Linux!
regards,
mechanic

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#134 Post by seaside »

BarryK wrote:Thanks to Peter who has implemented CONTINUE, I have tidied up the program. It now checks for blank lines and indented '#' comment lines (doesn't test for comments on the end of lines, but that could be done):
Barry,

This user added contribution function on the website-

Code: Select all

ExtractStr(STRING Main$,STRING Delim$)
might work to return everything to the left of #

Code: Select all

val$=ExtractStr(array$[1],"#")
Regards,
s

(I'm afraid there's probably nothing quite as simple and eloquent as

Code: Select all

 .  VARFILE
) :D

2lss
Posts: 225
Joined: Sun 20 Sep 2009, 23:54

#135 Post by 2lss »

PjotAwake wrote:Hi 2lss,

Thanks for your contributions. In fact, I have plans for this editor - I would like to add the BaCon converter itself, as a whole, to the program, so it's all embedded in one. We would have a fourth incarnation of BaCon but completely graphical based on GTK.

For the conversion progress, this is something I would rewrite using a Progress Bar.

Furthermore I would add a configuration panel where it is possible to fill in which type of output terminal is being used: xterm, rxvt, Terminal, gnome-terminal. Also in which size it has to appear.

The 'hold' of the terminal, you could embed the execution of the resulting program in a script which simply finishes with a 'read' - this will wait for input from the keyboard.

Tabbing should be added as well.

Syntax completion would be nice but I could not get that to work.

Your additions are a valuable step to these ideas, thanks! So, lots to do, wish there was more time! :-)

Regards
Peter
Glad I could offer some help. I'll keep plugging away at it when I get time.

As far as the terminal "hold" I had a similar thought, but figured the extra script would confuse things. Unless the editor creates/downloads the script when run (similar to the syntax file).....

What do you think about a FIND/HIGHLIGHT option? I found some example C code that might work.

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

#136 Post by BarryK »

2lss wrote:
PjotAwake wrote:Hi 2lss,

Thanks for your contributions. In fact, I have plans for this editor - I would like to add the BaCon converter itself, as a whole, to the program, so it's all embedded in one. We would have a fourth incarnation of BaCon but completely graphical based on GTK.

For the conversion progress, this is something I would rewrite using a Progress Bar.

Furthermore I would add a configuration panel where it is possible to fill in which type of output terminal is being used: xterm, rxvt, Terminal, gnome-terminal. Also in which size it has to appear.

The 'hold' of the terminal, you could embed the execution of the resulting program in a script which simply finishes with a 'read' - this will wait for input from the keyboard.

Tabbing should be added as well.

Syntax completion would be nice but I could not get that to work.

Your additions are a valuable step to these ideas, thanks! So, lots to do, wish there was more time! :-)

Regards
Peter
Glad I could offer some help. I'll keep plugging away at it when I get time.

As far as the terminal "hold" I had a similar thought, but figured the extra script would confuse things. Unless the editor creates/downloads the script when run (similar to the syntax file).....

What do you think about a FIND/HIGHLIGHT option? I found some example C code that might work.
Puppy already has a script for xterm. All Woof-built puppies have /usr/bin/xterm which is a script, unless any particular developer has replaced it.

Wary, Quirky, and many older versions of Puppy have it, in fact if my memory serves me right, even Puppy 4.3.1 has it.

Here is the script:

Code: Select all

#!/bin/sh
#rxvt does not understand the '-hold' option.

if [ "`echo -n ${*} | grep '\-hold '`" != "" ];then
 EXECCOMMAND="`echo -n ${*} | grep -o ' \-e .*' | sed -e 's/ \-e //'`"
 if [ "$EXECCOMMAND" != "" ];then
  echo '#!/bin/sh' > /tmp/xterm_simulate_hold.sh
  echo "$EXECCOMMAND"  >> /tmp/xterm_simulate_hold.sh
  echo 'echo' >> /tmp/xterm_simulate_hold.sh
  echo 'echo -n "FINISHED. PRESS ENTER KEY TO CLOSE THIS WINDOW: "' >> /tmp/xterm_simulate_hold.sh
  echo 'read simuldone' >> /tmp/xterm_simulate_hold.sh
  chmod +x /tmp/xterm_simulate_hold.sh
  exec rxvt -e /tmp/xterm_simulate_hold.sh
 fi
fi

exec rxvt "${@}"
...it tests for '-hold' on the commandline.
[/code]
[url]https://bkhome.org/news/[/url]

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

#137 Post by vovchik »

Dear Barry and Doyle,

Slightly more terse....

Code: Select all

DECLARE prop$ ASSOC STRING

infile$ = "PUPSTATE"
OPEN infile$ FOR READING AS fh
WHILE NOT(ENDFILE(fh)) DO
	READLN txt$ FROM fh
	txt$ = CHOP$(txt$)
	IF INSTR(txt$, "#") EQ 1 THEN CONTINUE
	l$ = LEFT$(txt$,INSTR(txt$, "=") - 1)
	prop$(l$) = CHOP$(MID$(txt$, INSTR(txt$, "=") + 1), "'")
WEND
PRINT prop$("DEV1FS")
PRINT prop$("ZDRV")
PRINT prop$("PUPSAVE")
With kind regards,
vovchik

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#138 Post by jpeps »

Version of previously posted apps loader using arrays:

http://jpeters.net/apps/MyPrograms.bac.txt

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

picscale v. 0.1a - image converter/rescaler in BaCon

#139 Post by vovchik »

Dear puppians,

Here is revised picture rescaler/converter written in BaCon that makes use of gdk_pixbuf to work its magic. It is pretty small (38k compiled). Since it is a command-line prog, you can use it in bash scripts for the batch processing of image directories etc.

The help screen looks like this:

Code: Select all

picscale image resizer/converter - v.0.1a by vovchik, Puppy Linux Forum, Nov 2010

Input formats supported: pnm, pbm, pgm, ppm, tga, xpm, tiff, pcx, gif,
xbm, wmf, icns, bmp, png, jpg and ico.

Output formats supported: png, jpg, bmp, tiff and ico.

Input parameters: oldfilename newfilename height width quality/compression/depth

Example: picscale old.png new.png 128 128 9

Quality/compression/depth settings:

        bmp (N/A):               0 - 100
        jpeg (quality):          0 - 100
        png (compression):       0 - 9
        tiff (compression type): 1 - 8
        ico (depth):             16, 24 or 32

Check out BaCon at www.basic-converter.org
If you have any suggestions or code mods, please tell me or post them here. The source is in the archive and a working binary in the pet file (installs in /usr/local/bin).

With kind regards,
vovchik
Attachments
picscale.bac.tar.gz
(2.49 KiB) Downloaded 659 times
picscale-0.1a.pet
(13.37 KiB) Downloaded 869 times

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

#140 Post by big_bass »

Hey vovchik

fast and small
what more can you want and
compiled it in bacon 8)


I can see many good apps to follow from you

thanks for posting the app and the source

Joe

Post Reply