How to run a string?

General FreeBASIC programming questions.
Bob Paw
Posts: 8
Joined: Nov 23, 2014 14:22

How to run a string?

Postby Bob Paw » Nov 23, 2014 14:45

So, I need to run text from a file, saved as a string within my program. I've tried a separate file that saves "print "hi" & 1" as a variable, then I placed just my variable name on a new line, but it says that I need an equals sign (=). How can I make it execute the string?
fxm
Posts: 9813
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to run a string?

Postby fxm » Nov 23, 2014 15:42

Bob Paw wrote:So, I need to run text from a file, saved as a string within my program. I've tried a separate file that saves "print "hi" & 1" as a variable, then I placed just my variable name on a new line, but it says that I need an equals sign (=). How can I make it execute the string?
???

#include "file"
Bob Paw
Posts: 8
Joined: Nov 23, 2014 14:22

Re: How to run a string?

Postby Bob Paw » Nov 23, 2014 15:45

fxm wrote:
Bob Paw wrote:So, I need to run text from a file, saved as a string within my program. I've tried a separate file that saves "print "hi" & 1" as a variable, then I placed just my variable name on a new line, but it says that I need an equals sign (=). How can I make it execute the string?
???

#include "file"

I would, but it's a .txt file. :| I'm not a complete novice. And anyway, the file needs to be edited later, so if it's compiled, I can't edit it.
fxm
Posts: 9813
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to run a string?

Postby fxm » Nov 23, 2014 15:56

Excuse me, but I still do not exactly understand the question!
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: How to run a string?

Postby petan » Nov 23, 2014 16:49

How can I make it execute the string?

You can do with string :
create/save/read/edit
To execute - only language command.
Do you mean this ?

To solve your problem, would you show us the code and what is saved/what you want to execute ??
Bob Paw
Posts: 8
Joined: Nov 23, 2014 14:22

Re: How to run a string?

Postby Bob Paw » Nov 23, 2014 17:50

Code: Select all

dim as string code
code = "print ""Hello World"" & 7" 'what the code should do
code 'execution
dkl
Site Admin
Posts: 3212
Joined: Jul 28, 2005 14:45
Location: Germany

Re: How to run a string?

Postby dkl » Nov 23, 2014 19:16

FB can't execute code contained in a string variable because it's a compiler, not an interpreter. So the way to execute code is to write it into a .bas file, then invoke fbc.exe on it to compile it into an .exe file, and then to run the resulting .exe file. It's possible to have a program doing this though, if needed, by creating the .bas file with the desired code and then invoking fbc and the exe file using shell() or exec().
RockTheSchock
Posts: 231
Joined: Mar 12, 2006 16:25

Re: How to run a string?

Postby RockTheSchock » Nov 24, 2014 6:58

angros47
Posts: 1670
Joined: Jun 21, 2005 19:04

Re: How to run a string?

Postby angros47 » Nov 24, 2014 7:30

Or you can try to embed OxygenBasic
marcov
Posts: 3002
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: How to run a string?

Postby marcov » Nov 24, 2014 8:12

I think the wisest is to try to eliminate the need for running a string. It might be as solvable with e.g. arrays of function pointer, or an expression parser.

IOW before resorting to an external interpreter, see if you can fix it the FB way first. If you are stuck, describe your problem in a new thread as abstract as possible with a little bit of code.
I Daniel
Posts: 43
Joined: May 13, 2010 10:34
Location: Centurion, South Africa

Re: How to run a string?

Postby I Daniel » Nov 25, 2014 9:22

The purpose of you requirement is not very clear, however you could try the following suggestions -
Fxm's approach as I see it is the easiest and no hastle approach and will give what you require. FBIde is what I use so I don't have command line hastles.
**************************************************************************
There used to be an old BASIC program the "Creator" to write programmes. You chose what you wanted and it wrote the program according to your choices. Even setting up a whole input page for you. (With a number of fields)
It would have programming lines such as
print "hello world"
OR
a=2
B=4
print a + B

These lines were written to a file with the normal .BAS ext. I see no reason why it cannot have any other ext when used as a #Include file.
If you want to use this text file save it as say Hello.bas, or whatever and use #include "Filename" and walla (Folk etymology for Viola) it will print or execute that which is in your #Include file. (Oops not really - only if it runs on an interpreter. So the main program will have to be recompiled.)

So you say the file may need editing. Use Wordpad, Notepad or FBIde. Leave the file name as is and edit as you please.
You can even open the file and then overwrite it, add to it or print (dont use "write") to it from your program. When you print to it or edit it just remember that normal programming conventions must be observed. If you dont define a variable it will as you have observed throw out an error. (variable not defined) This means your text file is in fact a basic program.
One drawback if the file is changed it will have to again be #Included and your main program will have to be recompiled. Alternatively You could use the Shell or EXEC Commands. In this case only the text file, if it was edited, will have to be recompiled. Drawback - you can use "SHELL or EXEC" at the spot in your program where you want it executed, which then is another program and will probably open its own default screen etc.
***************************************************************
Oh well sorry it is "bad" luck when using a compiled program. The #Include will work without problems in an interpreted language such as QB64 (?).

But just one question - Can one Shell or run a script from within FreeBasic to compile the Text file?

Rambling old man I am - this is more and expansion of what has already been said
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: How to run a string?

Postby petan » Nov 25, 2014 13:35

Simply use macros, if we don't talk about interpreted execution.
dodicat
Posts: 6634
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to run a string?

Postby dodicat » Nov 25, 2014 14:14

The only literal string that can run straight from a file is a graphics string via DRAW, as far as I know.
Draw is it's own parser.
Play would be another(as in Quickbasic) but is not implemented in fb directly.

Here is an example:
File 1
Datafile.txt

Code: Select all

BM400,300BM+0,-100Bm+100,0C4M+-2,10M+-7,10M+-10,9M+-14,8M+-17,6M+-19,5M+-21,2M+-20,0M+-21,-2M+-19,-5M+-17,-6M+-14,-8M+-10,-9M+-7,-10M+-2,-10M+2,-10M+7,-10M+10,-9M+14,-8M+17,-6M+19,-5M+21,-2M+20,0M+21,2M+19,5M+17,6M+14,8M+10,9M+7,10M+2,10BM+-100,0P4,4BM+0,200Bm+100,0C4M+-2,10M+-7,10M+-10,9M+-14,8M+-17,6M+-19,5M+-21,2M+-20,0M+-21,-2M+-19,-5M+-17,-6M+-14,-8M+-10,-9M+-7,-10M+-2,-10M+2,-10M+7,-10M+10,-9M+14,-8M+17,-6M+19,-5M+21,-2M+20,0M+21,2M+19,5M+17,6M+14,8M+10,9M+7,10M+2,10BM+-100,0P4,4


File 2
Test.bas

Code: Select all

Function LoadFile(ByRef filename As String) As String
    Dim h As Integer
    Dim txt As String
    h = FreeFile
    If Open( filename For Binary Access Read As #h ) <> 0 Then Return "File not found"
    If LOF(h) > 0 Then
        txt = String(LOF(h), 0)
        If Get( #h, ,txt ) <> 0 Then txt = ""
    End If
    Close #h
    Return txt
End Function

dim as string s=loadfile("Datafile.txt")

print s
if s="File not found" then beep:sleep:end
screen 19,,2
screenset 1,0
dim as single a
do
    cls
    a=a+.5
draw "Ta" & int(a)  & s
flip
sleep 1,1
loop until len(inkey)
sleep

sleep
 


If you pop both files into a temporary folder you can test.
Then you may delete the lot.
RockTheSchock
Posts: 231
Joined: Mar 12, 2006 16:25

Re: How to run a string?

Postby RockTheSchock » Nov 25, 2014 16:17

A slightly modified version of lua tutorial code from:
http://www.freebasic-portal.de/tutorial ... 02-s3.html

formula.lua

Code: Select all

y=x^2+x+2


Code: Select all

#INCLUDE ONCE "Lua/lua.bi"
#INCLUDE ONCE "Lua/lauxlib.bi"
#INCLUDE ONCE "Lua/lualib.bi"

' Lua bereitstellen
DIM Lua AS lua_State PTR                  ' Schnittstelle zu Lua
Lua = luaL_newstate
'luaL_openlibs(Lua)                        ' Standardbibliotheken oeffnen
luaL_requiref (lua,LUA_MATHLIBNAME,@luaopen_math,1)                'sandboxing only math allowed

For x As Integer=-11 To 11
   Print x,
   lua_pushinteger(Lua, x)                 ' Zahl auf den Stack legen
   lua_setglobal(Lua, "x")

   IF luaL_dofile(Lua, "formula.lua") THEN    ' Lua-Script aus einer Datei ausfuehren
        PRINT "Skriptfehler: " & *lua_tostring(Lua, -1)
   END If   
   lua_getglobal(Lua, "y")                ' Wert von "y" auf den Stack legen
   PRINT lua_tonumber(Lua, 1)             ' Wert ausgeben
   lua_pop(Lua, 1)                        ' remove from stack
Next

PRINT "Programm-Ende"
lua_close Lua                             ' Lua-State freigeben
Sleep


instead of loading a lua file you can also use: luaL_dostring
dodicat
Posts: 6634
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to run a string?

Postby dodicat » Nov 25, 2014 19:15

I can no longer locate the library files. (I was about to try the lua code).
Normally they are here?:
http://sourceforge.net/projects/fbc/files/Binaries%20-%20Windows/Libraries/

Return to “General”

Who is online

Users browsing this forum: Dinosaur and 8 guests