Tuesday, May 19, 2009

Compiling and Running QB Programs from Inside Programmer's Notepad

I previously wrote how I had been inspired to use the methods found in Issue #2 of QB Express to liberate QB from DOS. Since that time I have stopped thinking of liberating QB and started thinking of ways to enhance the QB experience. In this article I describe how I write, compile, and run QB programs within a WindowsXP environment.

The first thing you need to decide on is what text editor you want to use as a programming environment. For the last few years I have used Crimson Editor almost exclusively, but a few days ago I switched to Programmer's Notepad. Both text editors can be configured to do everything I describe in this article. Each editor has features not found in the other. It is up to you to choose your programming environment, but for the sake of this article I will be using Programmer's Notepad (PN).

In PN, create a text file and type the following line of code:
PRINT "Hello, Windows!"
Save this as "hello.bas" anywhere on your computer. Now we have created QuickBASIC source code in our editor, but what can we do with it?

Press Ctrl+N to create a new document. Type (or copy and paste) the following text into the new file:
@ECHO OFF
:ABOUT
:: O---------------------------------------------O
:: |compileMaker.bat v1.0 generates compile%1.bat|
:: |which can then be used to compile your |
:: |QuickBasic project. |
:: | |
:: |This batch file was created by Jason |
:: |Bales (gamerplusplus@gmail.com) |
:: |http://gamerpp.blogspot.com/ |
:: O---------------------------------------------O

SETLOCAL
PROMPT
TITLE COMPILE MAKER IS NOW RUNNING
COLOR 1F

:VARIABLES
REM %1 IS YOUR BASIC FILE NAME
REM %optiones% SHOULD BE YOUR ADDED COMMAND-LINE COMPILE OPTIONS LIKE /X/E
REM Make sure to change all paths so they point to
REM the right place on your computer
SET BC=C:\QBasic\BC.EXE
SET LINK=C:\QBasic\LINK.EXE
SET BCOM=C:\QBasic\BCOM45.LIB

:: get command-line options
CLS
SET /p optiones="Enter any special command-line options: "

:makeIt
> COMPILE%1.BAT ECHO @ECHO OFF
>>COMPILE%1.BAT ECHO REM Run this batch file to compile your .bas file
>>COMPILE%1.BAT ECHO.
>>COMPILE%1.BAT ECHO :: Compile and Link
>>COMPILE%1.BAT ECHO CALL %BC% %1.bas %optiones%/o;
>>COMPILE%1.BAT ECHO CALL %LINK% %1.OBJ, %1.EXE, , %BCOM%
>>COMPILE%1.BAT ECHO.
>>COMPILE%1.BAT ECHO :: Do a little cleanup
>>COMPILE%1.BAT ECHO DEL %1.OBJ
>>COMPILE%1.BAT ECHO DEL %1.MAP

ENDLOCAL
Save this file as "compileMaker.bat". I recommend saving it to your QB install folder. Make sure you change the paths in this batch file so they are correct on your system.

This batch file creates another batch file in your .BAS file's directory. This new batch file will be used to compile your code.

Before we can use this batch file we will need to create a tool to pass our file name and path to compileMaker.bat. In PN, click Tools>Options. A new window should pop open. In this, the Options window, select Tools and then click the Add button you see on the right.
Yet another window will open.
Make the window look like this:
Make sure that the Command section of this tool contains the full path, file name, and extension to compileMaker.bat. You can click the button to the right of the field to browse to the file. You can choose whatever shortcut keys you like.

In this window click the Console I/O tab and make sure the Capture output checkbox is un-checked.
Now click on OK in the tool properties window and then the OK in the Options window.

If you click on Tools you should now see your new tool. Every time you create a tool for .BAS files, it will appear here.
With our hello.bas file selected in PN, run the Create compile%1.BAT tool. A console window should pop open:
If you needed to send command-line options to BC, this is where you would type them. Since hello.bas only has a print statement, just press return.

Running this tool creates a batch file in the same directory as our code. If we run that new batch file we can compile our code into an executable. One way of running batch files is to navigate in Windows explorer to the file and double click it. Another way of running the file is to set up a user tool.

In PN, click Tools>Options. Click OK once you've made the properties window look something like this:
As with the previous tool, select the Console I/O tab and make sure the capture output option is un-selected.

Those of you actually following along will know what's coming next...

In PN, press Ctrl+N and type the following text:
@ECHO OFF

REM Pass the file title into this batch to compile.

:: If there's no COMPILE%1.BAT...
:: Make the COMPILE%1.BAT
IF NOT EXIST COMPILE%1.BAT CALL C:\QBasic\compileMaker.bat %1

:: Compile our .BAS file
CALL COMPILE%1.bat

PAUSE

Save This file in your QB install folder as "batchCompile.bat". Make sure the path to compileMaker.bat is correct for your computer.

Now run your new tool (find it in the Tools menu or use your hotkeys). If all went well you should see a prompt like this:
What this means is you just compiled a QuickBASIC program!

To run the program, we will make yet another tool. In PN, click Tools>Options and make the options window look like this:
Make sure the command portion of the tool points to your QB install folder. Again, on the Console I/O tab, de-select the capture output option. Click OK then OK again.

Back in PN, press Ctrl+N and enter the following text:
@ECHO OFF
CLS
PROMPT
COLOR 1F
TITLE "%~N1" IS NOW RUNNING...

REM Use this simple batch to run command-line programs
REM from explorer and programming editors.
REM Pass into it the full path with file name and extension.

CALL %1

ECHO.
ECHO.
PAUSE
Save this as CommandLineExecutor.bat in your QB install folder.

In PN, select hello.bas again and use your newest tool to run your compiled program:

No comments: