Freebasic interpreter trick

Windows specific questions.
Post Reply
geminis4941
Posts: 64
Joined: Jul 15, 2009 12:41

Freebasic interpreter trick

Post by geminis4941 »

I was searching a freebasic interpreter and realised that you can do a pipe connection and call a freebasic program from other freebasic program.

If could be interesting for anyone.

Code: Select all

Dim result As WString*50

open "c:\myprogram.bas" for output as #1

print #1, "dim a as integer"
print #1, "dim b as integer"
print #1, "a= 10"
print #1, "b= 5"
Print #1, "print sqr(a+b)"

close #1

Shell "fbc -x  c:\myprogram.exe c:\myprogram.bas"



open pipe "c:\myprogram.exe" for input as #1
input #1,result
Close #1
print "Result: ",result


'
Sleep

marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Freebasic interpreter trick

Post by marcov »

More elegantly, you could generate a DLL that way, and load it on-the-fly.

Sometimes apps do this with plugins or report generators sometimes generate code to create reports that way.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Freebasic interpreter trick

Post by caseih »

On Linux I wrote up a little bash script that compiles to a temporary executable and then runs that. The nice thing is I can use a #! on the first line and run it like a normal script:

Code: Select all

#!/usr/local/bin/fbrun
print "Hello, World!"
My script removes the #! line since it's not valid FB code. If I mark the .bas file as executable, then it runs like an normal script. I could have implemented caching of the binary, but I haven't bothered yet.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Freebasic interpreter trick

Post by fxm »

I do not see the interest of such a method compared to the command "Quick run" of all IDEs.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Freebasic interpreter trick

Post by Tourist Trap »

fxm wrote:I do not see the interest of such a method compared to the command "Quick run" of all IDEs.
For instance, I think you could make the compiler evaluate ("on the fly") only math expressions. It would maybe be less smooth than a true interpreter, but fast enough if you just compile the math instruction. It's then more general than if you stick with the IDE because you can write a custom frontend.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Freebasic interpreter trick

Post by caseih »

I use my script all the time to run FB files. For instance if someone posts a little snippet on the forum and I want to try it, I just copy it to the clipboard (now that we can't save directly from the forum), cat it to a file, and run it with my quick run script. Way faster than loading up some IDE for me. Especially for one-off runs. Besides that, there are some benefits to running a FB .bas file like a script.
geminis4941
Posts: 64
Joined: Jul 15, 2009 12:41

Re: Freebasic interpreter trick

Post by geminis4941 »

The interest for me is that you can pass parameter (in the edition) and obtain a return value. Is for calling small programs stored in database or in files with end users allowed to modify it. The question is, can I do a program that the user can modify partially?
Perhaps I wrote a so simple sample that it seems that only evaluates an expresión, but you can put intructions like IF , WHILE and others that evaluates dont solve.
Last edited by geminis4941 on Jul 29, 2016 21:08, edited 2 times in total.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Freebasic interpreter trick

Post by marcov »

Tourist Trap wrote:
fxm wrote:I do not see the interest of such a method compared to the command "Quick run" of all IDEs.
For instance, I think you could make the compiler evaluate ("on the fly") only math expressions.
The faster expression evaluators usually generate fairly straightforward (and linear) code directly to a memory block for which executable rights are obtained (on *nix with mprotect), and use neither of these "real compile" methods.

Simply because they don't really need a complete compiler/assembler, usually it is only a short sequence of asm instructions per construct (value or operator), without complicated extra passes for labels. (and still already a major boost over an interpreter)
It would maybe be less smooth than a true interpreter, but fast enough if you just compile the math instruction. It's then more general than if you stick with the IDE because you can write a custom frontend.
Well, maybe if you make it directly generate a series. To plot a graph, an execution per value might be a bit much.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Freebasic interpreter trick

Post by D.J.Peters »

Looks not very clever for me.
The target platform needs a complete FreeBASIC install with all dependencies.
If you need any kind of runtime executing you can use Squirrel for FreeBASIC.

You can get it from here: http://www.freebasic.net/forum/viewtopi ... 14&t=17573

Joshy
theAdmin10
Posts: 1
Joined: Sep 26, 2018 1:34

Re: Freebasic interpreter trick

Post by theAdmin10 »

caseih wrote:On Linux I wrote up a little bash script that compiles to a temporary executable and then runs that. The nice thing is I can use a #! on the first line and run it like a normal script:

Code: Select all

#!/usr/local/bin/fbrun
print "Hello, World!"
My script removes the #! line since it's not valid FB code. If I mark the .bas file as executable, then it runs like an normal script. I could have implemented caching of the binary, but I haven't bothered yet.
caseih, love this!!! Great job!
Would you mind sharing your fbrun script???
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Freebasic interpreter trick

Post by caseih »

Sure. Here it is. Place it somewhere and chmod a+x it. Be sure to set the FBC variable to point to where you have the compiler installed.

Code: Select all

#!/bin/bash
#set -x

# Set to somewhere writable and executable if /tmp is not executable
TEMP_EXE_DIR=

FBC=/opt/FreeBASIC/bin/fbc
FBC_FLAGS=


if [ ! -z "$TEMP_EXE_DIR" ]; then
        tempexe=$(mktemp -p "$TEMP_EXE_DIR")
else
        tempexe=$(mktemp)
fi

if [ -z "$1" ]; then
        # assume standard in
        tempbas=$(mktemp)
        cat > "$tempbas.bas"
        "$FBC" $FBC_FLAGS -x "$tempexe" "$tempbas.bas" || exit $?
else
        fullpath=$(readlink -f "$1")

        # strip shebang from source and compile it
        if [ -z "$(egrep '^#!' "$fullpath")" ]; then
                # compile directly; no shebang
                "$FBC" $FBC_FLAGS -x "$tempexe" "$fullpath" || exit $?
        else
                tempbas=$(mktemp)
                cat "$fullpath" | egrep -v '^#!' > "$tempbas.bas"
                "$FBC" $FBC_FLAGS -x "$tempexe" "$tempbas.bas" || exit $?
        fi
fi

# clean up command line invocation so we can pass args
if [ "$0" == "fbrun" ]; then
        # drop fbrun from command line
        shift
fi

#drop the name of the script from command line
shift

#execute executable, passing remaining args to it
"$tempexe" "$@"

#cleanup
rm -f "$tempbas.bas" "$tempbas" "$tempexe"
Post Reply