|
Post by bigbass on Jan 21, 2020 22:57:54 GMT 1
Hello I didn't plan to post this today I just was testing the latest bacon 4.0 and wanted to see how it was coming along with the many new changes one of the the most used languages is c and if we can port working code to bacon it gives bacon basic some added power to the mix the "trickery" is getting the syntax right even what appears to be something simple will not be simple without a working demo code example and can take many tries before it will compile and run A direct c code port from the link below with the goal of showing the proper bacon syntax used to compile and run and linking to the source code the author`s work code and explanation www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE
DECLARE str[80] = "This is - www.tutorialspoint.com - website" TYPE char
'--- you can easily change the delimter here by changing the "-" symbol DECLARE s[2] = "-" TYPE const char DECLARE token TYPE char* '---get the first token token = strtok(str, s) '--- walk through other tokens WHILE (token != NULL) PRINT token token= strtok(NULL, s) WEND
|
|
|
Post by bigbass on Feb 22, 2020 7:00:34 GMT 1
using the same example as above but using functions and subs so that we can write to a file then read the file a mix of C and bacon so that there are examples using both PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith 'OPTION PARSE FALSE
PROTO fopen , fprintf ,strtok ,fclose
'creating file pointer to work with files DECLARE fptr TYPE FILE* DECLARE sentence TYPE STRING ' opening file in writing mode fptr = fopen("program.txt", "w") ' ========================== SUB TOK_SAVE(STRING TOK$) ' ========================== PRINT "a token ", TOK$ sentence = TOK$
' dont print a null but print to the file IF fptr != NULL THEN fprintf(fptr, "%s \n", sentence) ENDIF END SUB '=================== FUNCTION FILE_GET$() '=================== DECLARE FILEDATA$ TYPE STRING IF FILEEXISTS("program.txt") THEN FILEDATA$ = LOAD$("program.txt") END IF RETURN FILEDATA$ END FUNCTION
DECLARE str[80] = "This is - www.tutorialspoint.com - website" TYPE char
'--- you can easily change the delimter here by changing the "-" symbol DECLARE s[2] = "-" TYPE const char DECLARE token TYPE char* ' without the declare no output to the callback DECLARE TOK$ TYPE STRING '---get the first token '--- dont print a null IF fptr != NULL THEN token = strtok(str, s) ENDIF '--- walk through other tokens WHILE token != NULL TOK$=(char*)token IF fptr != NULL THEN TOK_SAVE(TOK$) ELSE END ENDIF token = strtok(NULL, s) WEND fclose(fptr) FILE_GET$() PRINT FILEDATA$
TERMINAL OUT a token This is a token www.tutorialspoint.com a token website This is www.tutorialspoint.com website
|
|