Does FBC compile some small code in command line?

New to FreeBASIC? Post your questions here.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Does FBC compile some small code in command line?

Post by Tourist Trap »

Hello,

I wonder if there is a way to tell FBC to take a code simply quoted (and not placed within a file) as argument - in command line, like:

Code: Select all

fbc " 'freebasic code : beep : end"
?
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Does FBC compile some small code in command line?

Post by marcov »

Yeah, that gets me thinking out of the box.

Or pipes ? Or maybe DBUS. Drag and drop to the compiler binary perhaps? Does DDE still exist ?

Seriously, for quick and dirty stuff a shebang usage of FB sounds best.
Last edited by marcov on Mar 02, 2019 14:14, edited 1 time in total.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Does FBC compile some small code in command line?

Post by counting_pine »

For very basic stuff, something like this might do on Windows:

Code: Select all

::fbrun.bat
@echo off
echo Running command: %*
echo %* > %TEMP%\fbrun.bas
fbc %TEMP%\fbrun.bas && %TEMP%\fbrun.exe

Code: Select all

fbrun print 10*4+2
Running: print 10*4+2
 42
Obviously adapt if necessary to make sure it can find fbc..

There will probably be issues with command line parameter escaping etc. In particular, characters like quotes, greater/less or caret signs might cause problems.

In general it's probably a lot easier in Linux bash, where escaping is more well-defined, so it's possible to specify arbitrary strings as command-line parameters.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Does FBC compile some small code in command line?

Post by Tourist Trap »

marcov wrote:Drag and drop to the compiler binary perhaps?
At least this, on windows is easy:

Code: Select all

'drop some file on the icon of this stuff

if command(1)<>"" then
    if exec("fbc.exe", command(1))<>0 then ? "fbc not found"
end if

sleep
counting_pine wrote:For very basic stuff, something like this might do on Windows:

Code: Select all

::fbrun.bat
@echo off
echo Running command: %*
echo %* > %TEMP%\fbrun.bas
fbc %TEMP%\fbrun.bas && %TEMP%\fbrun.exe

Code: Select all

fbrun print 10*4+2
Running: print 10*4+2
 42
Nice, thanks a lot.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Does FBC compile some small code in command line?

Post by UEZ »

For such purposes an interpreter language such as Autoit is the better choice. Afaik, you can add a text file to the interpreter directly to parse and execute it.

I assume FB must compile the code before it can be executed.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Does FBC compile some small code in command line?

Post by Tourist Trap »

UEZ wrote: I assume FB must compile the code before it can be executed.
Yes but fbc is quite fast. Leaving asid autoIt that gives nighmares to my antivirus :), if you try PowerShell, you will probably agree that PowerShell is very slow, and probably slower than FB would be even if PowerShell is interpreted... and 100% native...
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Does FBC compile some small code in command line?

Post by jj2007 »

UEZ wrote:I assume FB must compile the code before it can be executed.
As Tourist Trap noted, fbc is pretty fast. I use my own editor (RichMasm) to build FB code, and it takes usually less than one second between hitting F5 and seeing the result. That is almost like using an interpreter. It requires an IDE or editor, though, that compiles the code "as is", i.e. without the need to hit Ctrl S first to save the content to disk.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Does FBC compile some small code in command line?

Post by UEZ »

Tourist Trap wrote:
UEZ wrote: I assume FB must compile the code before it can be executed.
Yes but fbc is quite fast. Leaving asid autoIt that gives nighmares to my antivirus :), if you try PowerShell, you will probably agree that PowerShell is very slow, and probably slower than FB would be even if PowerShell is interpreted... and 100% native...
Well, a lot of malicious code is written in AutoIt and thus it is very hard to compile a clean exe for a AV app. For me PS is more for administrating the environment whereas AutoIt can more or less do the same but has more advantages in creating compiled apps which is usually faster than PS. I've written a complete screen capturing app with AutoIt and it's "pretty" fast for its conditions.

The main disadvantage for AutoIt is speed and that's the reason why I'm here. ^^ But it very easy to learn and has a lot of user defined functions which facilitating the work with winapi extremely.
jj2007 wrote:
UEZ wrote:I assume FB must compile the code before it can be executed.
As Tourist Trap noted, fbc is pretty fast. I use my own editor (RichMasm) to build FB code, and it takes usually less than one second between hitting F5 and seeing the result. That is almost like using an interpreter. It requires an IDE or editor, though, that compiles the code "as is", i.e. without the need to hit Ctrl S first to save the content to disk.
I didn't want to say that the compilation with FB takes too long, I just wanted to mention that the purpose for this an interpreter language such as Autoit might be the better choice.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Does FBC compile some small code in command line?

Post by jj2007 »

UEZ wrote:I didn't want to say that the compilation with FB takes too long, I just wanted to mention that the purpose for this an interpreter language such as Autoit might be the better choice.
Once upon a time, people tested their code in a BASIC interpreter, and when they judged it OK, they compiled it to get the fast standalone version. I've done that for ages with GfaBasic. The interpreter was already fast enough for most purposes, but there was this extra kick when you compiled it - roughly a factor 3 faster.

But that was in the 1980s and '90s. Compiling a few thousand lines of Fortran IV on a PDP-11 was an occasion to have a coffee break with colleagues. Indeed, a BASIC interpreter was a much better choice for code that required testing and debugging (=100% of all code).

Today, the 20,000+ lines of my editor's source assemble in about one second. There is absolutely no need for an interpreter. Compiling an FB source with GCC is admittedly a bit slower, but the sources published in this forum compile typically in one second, too. If that is too much, use GAS. A full-fledged Windows GUI application takes half a second to compile with GAS.

Now one could argue that an interpreter offers something for debugging variables directly etc etc, but this is another topic (hint: you can use debug macros that can be switched off completely with conditional compilation).
Post Reply