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.
Message
Author
User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#466 Post by L18L »

Dear vovchik,

thanks for your SCREENSIZE hint.
vovchik wrote:...If you use it and have a list of keywords after the INCLUDE "hug.bac" business, don't forget to include SCREESIZE in that list. ...
...and now it is first time that I use hug.bac and a list :roll:
# head -1 m_INCLUDE.bac
INCLUDE "/usr/share/BaCon/hug.bac", INIT, GET, SCREENSIZE, WINDOW, COMBO, CALLBACK, DISPLAY, QUIT, ATTACH, TEXT
#
# time bacon m_INCLUDE.bac
Converting 'm_INCLUDE.bac'... done.
Compiling 'm_INCLUDE.bac'... done.
Program 'm_INCLUDE' ready.

real 0m56.783s
user 0m20.282s
sys 0m0.617s
#
# ls -l m_INCLUDE
-rwxr-xr-x 1 root root 166924 Feb 25 10:27 m_INCLUDE
#
before, always using hug_imports (without a list) only:
# head -1 m_import.bac
INCLUDE "/usr/share/BaCon/hug_imports.bac"
#
# time bacon m_import.bac
Converting 'm_import.bac'... done.
Compiling 'm_import.bac'... done.
Program 'm_import' ready.

real 0m4.561s
user 0m1.757s
sys 0m0.190s
# # ls -l m_import
-rwxr-xr-x 1 root root 61006 Feb 25 10:29 m_import
#
Thus I do not understand why anybody should use hug.bac list
I must be missing something :roll:

With kind regards

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#467 Post by L18L »

e_mattis wrote:... Now, is there a means to keep the window close (X) button from showing on the window?..
and all the others like _
try
x = SCREENSIZE(0) / 3
mainwin = WINDOW("combo example", x, 100)
SETPROPERTY(mainwin, "decorated", FALSE)

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#468 Post by e_mattis »

AH! Thanks L18L! I hadn't thought about properties. :oops:

Have not found all the settings to go with properties, but I am familiar with decorated - I should have realized :D

You guys are the best! Thanks so much!

E

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#469 Post by e_mattis »

Well, i'm back again. :shock:

I'm now trying to learn how to red/write record files using BaCon. I understand the simplest form:

Record <var>
LOCAL <member1> TYPE <type>
LOCAL <member2> TYPE <type>
END RECORD

I have been successful writing a file using this approach. :) I have tried to adapt it to be able to use for multiple records, but have not been able to figure it out. :( Below is the current state of this attempt:

Code: Select all

REM read/write data to file

REM GLOBAL Date$,Rnds$,TF$ TYPE str
GLOBAL Date$ ARRAY 3
GLOBAL Rnds$ ARRAY 3
GLOBAL TF$ ARRAY 3
GLOBAL Rec$ ARRAY 3
LOCAL TF

INCLUDE "/usr/share/BaCon/hug.bac"

FOR a = 1 TO 2


	Date$(a)= "Monday, Feb. 25.2013"

	Rnds$(a)= STR$((0+a)*100)

        TF=a

	TF$(a)= STR$(TF)
	

	RECORD Rec$(a)
		
		PUTLINE Date$(a) TO Rec$
		
		PUTLINE Rnds$(a) TO Rec$ 
		
		PUTLINE TF$(a) TO Rec$
	
	END RECORD

NEXT A
	
 
OPEN "test.dab" FOR WRITING AS handle
	
	FOR b = 1 TO 2
		
		WRITELN Rec$(b) TO handle
	
	NEXT b
 
CLOSE FILE handle



END
However, this returns the following errors at compile:

Code: Select all

# bacon recordtest.bac

Converting 'recordtest.bac'. . . done

Compiling 'recordtest.bac . . . Compiler emits messages!



Problem:
	
	file 'recordtest.bac line 16: END RECORD



Cause:
	
	paramter names (without types) in function decloration [enabled by default]
I'm kinda at a loss :oops: In the program I am working on, I will have 3 variables (date/rnds/tf) with several 'records'. In the other basic languages I am familiar with, this would be done in an csv array using the first element as the record number. So I am wondering if using the CONCAT$ would be a better alternative here, such as

Code: Select all

FOR a = 1 TO 2
...
   Rec$(a) = CONCAT$(Date$(a),":",Rnds$(a),":",TF$(a))
NEXT a

...

FOR b = 1 TO 2
   WRITELN Rec$(b) TO handle
NEXT b
I tried this method but also had an error at compile (not the same error I do not think). So I turn to the master(s) for assistance! Any help is appreciated!

Thanks

E

.

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#470 Post by Mobeus »

E

This got me too when I started learning BaCon. BaCon does not support arrays of records, but it does support records of arrays.

Code: Select all

TRAP LOCAL
CATCH GOTO CATCHERROR

' One record of dynamic arrays
RECORD Rec
    LOCAL Date$ ARRAY 3
    LOCAL Rnds$ ARRAY 3
    LOCAL TF$ ARRAY 3
END RECORD


FOR a = 1 TO 2
	TF=a
	Rec.Date$[a] = "Monday, Feb. 25.2013"
	Rec.Rnds$[a] = STR$((0+a)*100)
	Rec.TF$[a] = STR$(TF)
NEXT

OPEN "test.dab" FOR WRITING AS handle

FOR b = 1 TO 2
	WRITELN CONCAT$(Rec.Date$[b], ":", Rec.Rnds$[b], ":", Rec.TF$[b]) TO handle
NEXT b

CLOSE FILE handle

REDIM Rec.Date$ TO 6
REDIM Rec.Rnds$ TO 6
REDIM Rec.TF$ TO 6

FOR a = 3 TO 5
	TF=a
	Rec.Date$[a] = "Tuesday, Feb. 26.2013"
	Rec.Rnds$[a] = STR$((0+a)*100)
	Rec.TF$[a] = STR$(TF)
NEXT

OPEN "test.dab" FOR APPENDING AS handle

FOR b = 3 TO 5
	WRITELN CONCAT$(Rec.Date$[b], ":", Rec.Rnds$[b], ":", Rec.TF$[b]) TO handle
NEXT b

CLOSE FILE handle

END

LABEL CATCHERROR
PRINT ERR$(ERROR)
END
Regards,
Mobeus

PS. It really doesn't make any sense to me to use records like this. It's less typing to use the dynamic arrays by themselves without wrapping them in a record. :?
/root for the home team

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#471 Post by e_mattis »

Thanks Mobeus!

I believe I better understand it now. :D
I am now working with reading the file (should be able to figure that out on my own - I hope :roll: ) and get deeper into this thing.

Your assistance is very appreciated!

Thanks!

E

[update]

Well, that didn't take long. :(

Apparently the documentation at basic-converter.org is worthless. :evil:

Code: Select all

OPTION BASE 1
LOCAL dimension
SPLIT "one,two,,three" BY "," TO array$ SIZE dimension
FOR i = 1 to dimension
  PRINT array$[i]
NEXT i
I typed in the example given (above) for the 'split' command and it returns an error : "Line 5: SPLIT "one,two,,three" BY "," TO array$ SIZE dimension - expected declaration or statement at end of input" . I replaced the '[ ]' with '( )' (below) and got another error : Line 5... called object 'array$' is not a function.! :twisted:

Code: Select all

OPTION BASE 1
LOCAL dimension
SPLIT "one,two,,three" BY  "," TO array$ SIZE  dimension
FOR x = 1 to dimension
  PRINT array$(i)
NEXT x
Basically, all I am doing is having the user input some information (about 8 - 10 variables total ). I need to write those variables to a file that i can later read and show in a window.

In the 'old school' basic I'm familiar with, it would go something like this:

Code: Select all

INPUT "Name ";name$
INPUT "Date ";Date$
INPUT "Age ";age$

OPEN "test.dab" FOR WRITE AS #handle
  PUT name$;",";Date$;",";age$
Close #handle

OPEN "test.dab" FOR READ AS #handel
 FOR X - 1 to VAL(array$)
     INPUTTO ","; array$
  PRINT array$(x)
NEXT x
This code would take the input, write it to file, then read it, and finally print it to screen.

Maybe i'm just over-frustrated at this point, but Bacon should have a method as simple - I just cannot find it :cry:

I am still trudging along trying to figure this out, so any suggestions is appreciated.

Thanks!

E

.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#472 Post by L18L »

Keep cool
e_mattis wrote:...
Apparently the documentation at basic-converter.org is worthless. :evil:

Code: Select all

OPTION BASE 1
LOCAL dimension
SPLIT "one,two,,three" BY "," TO array$ SIZE dimension
FOR i = 1 to dimension
  PRINT array$[i]
NEXT i
I typed in the example given (above) for the 'split' command and it returns an error : "Line 5: SPLIT "one,two,,three" BY "," TO array$ SIZE dimension - expected declaration or statement at end of input" . I replaced the '[ ]' with '( )' (below) and got another error : Line 5... called object 'array$' is not a function.! :twisted:
...
.
I pasted your example into file in.bac

Code: Select all

INCLUDE "/usr/share/BaCon/hug_imports.bac"
INIT
OPTION BASE 1
LOCAL dimension
SPLIT "one,two,,three" BY "," TO array$ SIZE dimension
FOR i = 1 to dimension
  PRINT array$[i]
NEXT i 
and
#bacon in.bac
Converting 'in.bac'... 8
ERROR: missing TO in FOR statement at line 8 in file 'in.bac'!
#

corrected to to TO
and...
# bacon in.bac
Converting 'in.bac'... done.
Compiling 'in.bac'... done.
Program 'in' ready.
# ./in
one
two

three
#
Exactly as expected :D
documentation.html wrote:SPLIT

SPLIT <string> BY <sub> TO <array> SIZE <variable>

Type: statement
So please keep cool 8)
Don't type if you can copy & paste

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#473 Post by Mobeus »

Don't type if you can copy & paste
Watch out if you use Midori 0.2.2 (in Lupu) to open the documentation.html to copy and paste any examples!

This perfectly correct code, copied in Midori and pasted into a .bac file in Geany

Code: Select all

OPEN "data.txt" FOR READING AS myfile
WHILE NOT(ENDFILE(myfile)) DO
    READLN txt$ FROM myfile
    IF NOT(ENDFILE(myfile)) THEN
        PRINT txt$
    ENDIF
WEND
CLOSE FILE 
Gives this when built with BaCon

Code: Select all

# bacon ./pasted.bac
Converting ‘./pasted.bac’... 3
ERROR: could not parse line 3 in file ‘pasted.bac’!
#  
The reason? The white space characters in Midori do not play well with code!
/root for the home team

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#474 Post by L18L »

Mobeus wrote:The white space characters in Midori do not play well with code!
We all are learning! Thanks, good to know that midori is buggy.
seamonkey is OK.
...and copy & paste from console to forum post is working well.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#475 Post by sunburnt »

And also using hug_imports.bac is buggy.
Works most of the time, but fails occasionally and it`s a bear to detect.

There`s code at the forum for a menu bar import that hug doesn`t have.
Now if someone would post the import code for the GTK+ grid.

Importing GTK+ compiles much faster than hug.bac.
GTK`s widgets are such a jumble of methods and the code`s a mess.

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#476 Post by e_mattis »

@ L18L

Copied your code for in.bac to my system, ran '#bacon in.bac' in console. Returned error for the ''MISSING TO in FOR ..LINE 6" error. Corrected that, recompiled, got another error :( -

Code: Select all

PROBLEM:
file 'in.bac' line 7: PRINT array$[i]

Cause: 
expected declaration or statement at end of input
Again, code copied directly from your post.

Is it just my system? :?

I'm using precise 5.4.3, Geany, & urxvt terminal.


@Sunburnt

A link to that forum for the menu post?

Thanks guys!

E

.
Last edited by e_mattis on Thu 28 Feb 2013, 02:58, edited 1 time in total.

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#477 Post by e_mattis »

@ Mobeus

Code: Select all

OPEN "data.txt" FOR READING AS myfile
WHILE NOT(ENDFILE(myfile)) DO
    READLN txt$ FROM myfile
    IF NOT(ENDFILE(myfile)) THEN
        PRINT txt$
    ENDIF
WEND
CLOSE FILE 
Thanks! I actually got a file to write and was able to read from it! Now if I can get this "SPLIT" thing to work, I'll be able to to have some data!

Thanks!

E
.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#478 Post by L18L »

e_mattis wrote:@ L18L

Copied your code for in.bac to my system, ran '#bacon in.bac' in console. Returned error for the ''MISSING TO in FOR ..LINE 6" error. Corrected that, recompiled, got another error :( -

Code: Select all

PROBLEM:
file 'in.bac' line 7: PRINT array$[i]

Cause: 
expected declaration or statement at end of input
Again, code copied directly from your post.

Is it just my system? :?

I'm using precise 5.4.3, Geany, & urxvt terminal.
Maybe you did copy exactly i.e. without last new line.
I'm also using precise 5.4.3, Geany, & urxvt terminal
and could reproduce the same error after deleting last empty line.

Code: Select all

# bacon ./in.bac
Converting './in.bac'... done.
Compiling './in.bac'... Compiler emits messages!

Problem:
	 file './in.bac' line 7: PRINT array$[i]
Cause:
	 expected declaration or statement at end of input

# 
vovchik wrote:PPS. I also have the situation with a newline being required at the end of the code - but not on all machines!
Thus the "all systems proof" method would be:
Always have a new line at end of code

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

#479 Post by vovchik »

Dear guys,

There is a reason to use a list after INCLUDE "hug.bac", and there is a reason not to use "hug_imports.bac". If you use a list with "hug.bac", the compilation is faster. I compile a lot, use the latest bacon and hug versions and do not want an extra dependency in my code that is based on the libhug.so that might be present on my machine only - and being different on other machines. That dependency is created by using hug_imports.bac, which uses the shared lib libhug.so.

Also, I am interested in small and self-contained binaries (the smaller the better). For that reason, I use the following compile line:

Code: Select all

bacon -o -s -o -Os -o -fdata-sections -o -ffunction-sections -o -Wl,--gc-sections "$mybaconfile"
and then UPX the binary (the compile line already strips unneeded symbols). This typically gives me 16-25k binaries without any huglib.so dependency issues for GTK apps and 7-10k CLI binaries - much better than 70k+ or larger. I also do this when compiling bacon and bacongui. They weigh in at 61k and 78k respectively after this "treament".

With kind regards,
vovchik

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#480 Post by sunburnt »

e_mattis ; You can also use Bash to read and write files.

Code: Select all

Txt$ = EXEC$(CONCAT$("cat ", $PathFile))

SYSTEM CONCAT$("echo \"", $Txt, "\" > ", $PathFile)
The read code is good, and I think the write code should be too.


vovchik; Thanks again, the Bacon apps. I`m working on can use a shrinking.

# Also... Could you post code for UPXing the exec.?

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

#481 Post by vovchik »

Dear Terry,

Glad to see youa re so busy :) There is a link to UXP binaries on the forum:

http://208.109.22.214/puppy/viewtopic.p ... cb706abdd5

With kind regards,
vovchik

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#482 Post by e_mattis »

Hey guys,

@ L18L

yea, isn't it funny how such a small mistake can make you look like such a big dummy :oops: ! I went back and sure enough- no line after the end. I put one there and got a good run through. Thanks m8!
@ vovchik

I see what you mean. Tried the hug_imports just for 'grins & giggles' to see what would happen. what a mess! LOL Thanks for the fore-warning!


@ everyone

Now I have more questions.... :shock: :roll:

I need to incorporate a text-area on my window. Also, I will have a listbox to list the 'records' within a file (i.e. lines of data contained within). :D

I've looked through the docs I have, but do not see any indication of a text area command. I'm sure it is probably something i am overlooking - like multiline or something - so I ask, is there a means of producing multi-lined entry?

As far as the second issue, I have the listbox set up. Question is what would be the best method of inserting data obtained from the read-in of a file? Can I simply create an array and print it to the listbox?

:!: You guys are the best! I have gained a lot of insight from you and appreciate it more than I can express. :!:

Thanks!

E

.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#483 Post by sunburnt »

If you`re using OPEN to read the file then it`s already in an array, correct?
If your using Txt$ = EXEC$(CONCAT$("cat ", $PathFile)) as I suggested then:

Code: Select all

SPLIT Txt$ BY NL$ TO File$ Size fileSz
Now the file`s contents is in the array $File$[]

The MARK is Bacon`s "Label", it only shows text and can do multiple lines.

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#484 Post by e_mattis »

Thanks sunburnt,

Yea, the info is already in an array, just not sure how to print it in the list box. assuming the 'MARK' attribute will do that? :?

Also, the multi lined area is not the listbox. It will actually be a 'notes' area - for entering your personal notations. As an ENTRY box, i would assume there would be some sort of word-wrap or multi-line setting to use in order to accomplish this. Haven't found the code yet though.

Thanks!

E

[update] 022813 23:32 est

Looks like i figured out the print to listbox (finally :) ):

TEXT(listbox,variable)

Still working on how to make a multi-line ENTRY box, so any help will be appreciated :D

.

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

#485 Post by vovchik »

Dear e_mattis,

This may or may not be what you are after, but HUG's EDIT box allows for multiple lines:

Code: Select all

EDIT(xsize, ysize)
Type: directive
Defines a multiline text widget with a width of <xsize> and a height of <ysize>.
Returns the ID of the created text widget.
With kind regards,
vovchik

PS. For reading in text files, which I do often, I use my own hand-rolled CAT command:

Code: Select all

' --------------------
FUNCTION CAT(STRING FILENAME$)
' --------------------
	LOCAL fileline$, txt$ TYPE STRING
	IF FILEEXISTS(FILENAME$) THEN
		OPEN FILENAME$ FOR READING AS catfile
		WHILE NOT(ENDFILE(catfile)) DO
			READLN fileline$ FROM catfile
			txt$ = CONCAT$(txt$, fileline$, NL$)
		WEND
		CLOSE FILE catfile
	END IF
	RETURN CHOP$(txt$)
END FUNCTION

x$ = CAT("/etc/profile")
PRINT x$
And, as sunburnt has said, if you use NL$ as your SPLIT delimiter, every separate line will be an array element. If you don't want to use arrays, you can always use my little GETWORD$ function:

Code: Select all

' --------------
FUNCTION GETWORD$(STRING mystring$, NUMBER word_number, STRING delimiter$)
' --------------
	LOCAL  i, mypos, n TYPE NUMBER
	LOCAL string_temp$ TYPE STRING
	i = 0
	mypos  = 0
	n = 1
	string_temp$ = ""
	mystring$ = CONCAT$(mystring$, delimiter$)
	REPEAT
		mypos  = INSTR(mystring$, delimiter$, n)
		INCR i
		IF i = word_number THEN
			string_temp$ = MID$(mystring$, n, (mypos - n))
		END IF
		n = mypos + 1
	UNTIL mypos IS FALSE
	RETURN string_temp$
END FUNCTION

x$ = CAT("/etc/profile")
' grab first line
y$ = GETWORD$(x$, 1, NL$)
PRINT y$

Post Reply