|
Post by bigbass on Jun 3, 2013 0:34:29 GMT 1
Talk about a new twist What if you are porting C code and you want to use a little of it just for fun or you would like to get used to converting code examples to BaCon or maybe you get some code that you cant figure out how to port yet no problem imagine the fun you could have look at this
PRAGMA LDFLAGS `pkg-config --cflags --libs gtk+-2.0`
'--- how to embed some C commands in PROTO '--- using a write to file example
PROTO fopen PROTO fprintf PROTO fclose
DECLARE *fp TYPE FILE
'---embed C code fp = fopen ("/tmp/testme.txt","w") fprintf (fp, "This is line \n") fclose (fp)
now make reading easy
PRAGMA LDFLAGS `pkg-config --cflags --libs gtk+-2.0`
'--- how to embed some C commands in PROTO '--- using a write to file example
PROTO fopen ALIAS OPEN_FILE PROTO fprintf ALIAS WRITE_FILE PROTO fclose ALIAS CLOSE_FILE
DECLARE *fp TYPE FILE
'--- now make it look easy using ALIAS fp = OPEN_FILE ("/tmp/testme2.txt","w") WRITE_FILE (fp, "This is line \n") CLOSE_FILE (fp)
|
|
|
Post by bigbass on Jun 4, 2013 15:09:13 GMT 1
I am going to take this a step at a time because converting even a single line of code could cause a lot of trial and error in this example I will introduce some new commands if and printf to test if a file exist and if not print an error warning all of this is very common to see in C code but porting it over to BaCon using the official C commands is not common practice for BaCon users to embed with so showing some easy commands first the idea is seen clearly dont use an important file name here use some" test file" you will overwrite with one line of txt as is this will produce an error because that file doesn't exist yet "/tmp/testme4.txtfileopen-proto.bac PRAGMA LDFLAGS `pkg-config --cflags --libs gtk+-2.0`
'--- how to embed some C commands in PROTO '--- using a write to file example
PROTO fopen PROTO fprintf PROTO fclose PROTO if PROTO printf
DECLARE *fp TYPE FILE
'---embed C code add an error message
fp = fopen ("/tmp/testme4.txt","w") fprintf (fp, "This is line \n") if (fp == NULL ) printf ( "I could not open your output file.\n" ) ; printf ( "Make sure the file name and path is correct.\n" ) ;
fclose (fp)
if you look carefully there is no need for an end ifso this shows that if is working as expected since no file was found by the given name I hope it starts to make more sense why using PROTO has some new advantages when embedding code and all of this allows for us to get more code examples for BaCon ported quickly then we can go back and" clean up" the code and make it BaCon friendly with the syntax we are used to seeing and use P.S if I knew more about C coding the documentation part would be faster so I have a lot of homework to do Joe
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jun 4, 2013 19:35:19 GMT 1
Joe,
Remember BaCon is a BASIC to C translator. It's output is not something that would be use to develop further before the final compile.
To me turning efficient C code into BaCon just so it makes sense to BASIC programmers and then turned back in to C with a lot of BaCon baggage seems silly.
I would suggest downloading cint or one of the other C interpreters to learn and experiment with C.
John
|
|
|
Post by vovchik on Jun 4, 2013 22:29:17 GMT 1
Dear all,
Joe is doing something very valuable - demonstrating that you can enhance BaCon by embedding functions, thereby making it more powerful. Also, by using ALIASes, he makes it easy to use. I can only encourage him to keep at it....
With kind regards, vovchik
PS. The words of Cafavy come blissfully to mind (badly paraphrased):
If there were no Ithaca, there would be no journey. If there were no journey, there would no Ithaca.
|
|
|
Post by alexfish on Jun 4, 2013 23:21:32 GMT 1
Hi Joe Nice demonstration how flexible BaCon can be... Looks much like what Cint can do , without the baggage of another download Good work , Keep it up BR Alex
|
|
|
Post by bigbass on Jun 5, 2013 15:00:11 GMT 1
Hey John
in the above examples I am just documenting some un documented features of BaCon
C code can be embedded using USEC or USEH but using PROTO with C functions hasn't been shown or documented before
This word "efficient" can be seen several ways
C is the mother language of many other languages even GTK was written in it (which is a tool kit not a language ) so the ability to convert it to another language isn't silly it is just a fundamental development first step that is required which is usually hidden from us in the process in the examples posted they just show that this mother language could still be easily used if needed nothing less or nothing more most people here agree that BASIC syntax is more friendly to work with
another thought about C coding why I personally dislike it (even though sometimes it is the needed tool to use) It is a first person communication between the compiler so you are forced to think like a compiler not how a human would say the same thing
and BASIC is a first person communication with another person about the third "thing" the computer (its not a never will be a third person) getting BASIC so it is easily readable /speakable /useable is still a big task but the closer we get the better
Joe
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jun 7, 2013 3:15:07 GMT 1
Joe,
If C is not something you enjoy working with then why are you using a BASIC to C converter?
If you ask Peter why he wrote BaCon instead of continuing on using SB, he would say that 64 bit and faster compiled code was his motivation. Peter is already an excellent C, bash, BASIC, ... programmer and uses Bacon as a time saver and not something uses to write commercial/production code. HUG was a great idea to wrap Gtk. Once again, time saving steps.
At the end of the day, all your efforts end up as a very difficult to read BaCon generated C files that magically run as your BASIC code intended.
John
|
|
|
Post by alexfish on Jun 7, 2013 3:53:38 GMT 1
Reading ::
did not find this one difficult to READ From the OP
HTH
Alex
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jun 7, 2013 4:46:42 GMT 1
PRINT "Hello World"
One would expect something like this being generated from a BASIC to C translator.
#include <stdio.h>
main() { printf("Hello World"); }
Please post the BaCon C source and header files generated that does the same.
Note: You may hit the max allow character limit per post doing so.
|
|
|
Post by bigbass on Jun 7, 2013 6:24:42 GMT 1
Hey John Strange to say I have never wanted to see the unformatted C code generated I just want the basic code to compileand however BaCon does that part is fine with me because it works even when we import undocumented stuff it still works It is really quite amazing considering all the stuff we throw at it Joe
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jun 7, 2013 7:53:26 GMT 1
If you're having fun and learning something along the way, that's all that counts.
I wonder if this is how Microsoft innovates.
|
|
|
Post by alexfish on Jun 8, 2013 1:45:28 GMT 1
If you're having fun and learning something along the way, that's all that counts. I wonder if this is how Microsoft innovates. I as a Person have never ...::: Never see such Statements. I as a Person have never ...:::Never seen on a Forum , a person , who purports to be the "&" quote "The Project Manager" of a some software............ I as a person with a free mind , have reasonable doubts about You as in JRS > I think you have Just attracted Attention .. Think MS and IUP will be interest in your resent activities , and Statements , at the personal level , I can only feel Sadness , Awaiting Results of these responses.. Alex
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jun 8, 2013 2:49:54 GMT 1
Do you live in a cave? If my responses shock you, turning on the TV may be fatal. Get a life Alex and take a break from your PC.
|
|
|
Post by alexfish on Jun 8, 2013 9:07:06 GMT 1
Yes Sir ...
No Sir ..
3 Bags Full Sir
The Modern world Left those days long ago.. Sir
|
|
|
Post by bigbass on Jul 22, 2013 17:26:10 GMT 1
This will show how to import the atoi C function to convert a string to an integer
will call it TOINT = meaning to integer
using the ALIAS
----------------- "1234" is a string then it gets converted to an integer = i we use the FORMAT %d option to PRINT we can prove that i is an integer by adding something to it we can do some things other BASIC's can't do import any C function
PROTO atoi ALIAS TOINT
i = TOINT("1234")
PRINT TOINT("1234") FORMAT "%d\n"
PRINT i +2000 FORMAT "%d\n"
Here is the same idea with atol convert a string to long
PROTO atol ALIAS TOLONG
i = TOLONG("100000000")
PRINT TOLONG("100000000") FORMAT "%d\n"
PRINT i +2000 FORMAT "%d\n"
|
|