Saturday, March 21, 2009

Working with QB45 in Crimson Editor

In the summer of 2004 I was tinkering with different scripting and programming languages. It was like a hobby. Over a weekend I'd figure out how to write, compile, and run programs from the command prompt or within Crimson Editor. After I understood how compilers, linkers, and interpreters work I became confident I could use Crimson Editor to write and run programs in any programming language. C and C++, no sweat! Java, simple. Python? Yep.

Then I got the bright idea to write and compile Microsoft QuickBasic from Crimson. I figured out how to link and compile .bas files on the command line and then went to the task of doing it from Crimson. This was more difficult than anything I'd ever done with Crimson. Night after night I would write and re-write batch files and Crimson toolsets. Weeks rolled on and still I was unable to compile QB programs in Crimson.

This changed suddenly one day in September or October when I read an article in QB Express, an online QB community magazine. In Issue #2 of QB Express there is an article titled Liberate QB from DOS by Martin Rampersad. In the article Mr. Rampersad shows how you can use GNU Make to compile .bas files by right clicking on a folder and choosing an option. I never did get the registry file to work as it is demonstrated in the article. I did, however, do something cooler.

By the end of this article you will be able to write, compile, and run QB45 programs from the comfort of Crimson Editor!

I assume you have QB45 and Crimson Editor installed on your computer and I assume you know what paths and directories are. Once you have QB45, Crimson, and know what paths and directories are, please proceed with the article.

One more quick note, all of the tools, programs, and scripts in this article work on my laptop running WindowsXP SP2, but I do not guarantee they will work perfectly on your computer. You take full responsiblility for anything you do on your computer.

Before you can get started you need to download a small utility known as make. I got my version of make from THIS sourceforge page. You could go there, download the archive, extract the archive, find make.exe, copy make.exe to your QB45 install folder, navigate back to the archive and extracted files and delete everything you didn't use... OR you could click HERE and download a .zip file with make.exe and all the batch files described in this article. Just download the .zip and extract all the files to your QB45 folder.

OK, now we're ready! Open Crimson and press Ctrl+n. Type the following text into this file:


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


Save this file as "CommandLineExecutor.bat" in your QB45 install directory. Make sure to use quotes so the file gets saved with the proper file extension.

CommandLineExecutor.bat will be used to run the programs you compile in Crimson.

Press Ctrl+n and enter the following text:


:: Display "about"

ECHO makeMaker.bat
ECHO generates makefiles for use
ECHO with QuickBasic
ECHO Written by Jason Bales
ECHO A.K.A. Gamer++

ECHO Writing paths...
REM Change these three paths if
REM your QB45 install directory
REM isn't C:\QBasic
> makefile ECHO BC=C:\QBasic\BC.EXE
>>makefile ECHO LINK=C:\QBasic\LINK.EXE
>>makefile ECHO BCOM=C:\QBasic\BCOM45.LIB

>>makefile ECHO.
>>makefile ECHO all: %1.exe
>>makefile ECHO.
>>makefile ECHO %%.exe: %%.obj
>>makefile ECHO $(LINK) $^<, $@, , $^(BCOM^); >>makefile ECHO.
>>makefile ECHO %%.obj: %%.bas
>>makefile ECHO $(BC) $^< /o; >>makefile ECHO.

ECHO Your makefile is ready now

Save this file as "makeMaker.bat" in your QB45 install directory. This batch file will make the files that are used by QB45 and GNU make to create executables. If you installed QB anywhere other than C:\QBasic, you should change the batch file to match your QB install directory.

Press Ctrl+n and enter the following text:

@echo off

:: Display "about"
ECHO GNU Make Shell Extension
ECHO uses GNU Make to compile
ECHO with QuickBasic

ECHO Written by Jason Bales
ECHO A.K.A. Gamer++
ECHO based on the the file given
ECHO by Martin Rampersad in
ECHO QB Express Issue #2 ~ September 15, 2004
ECHO %1 is the project directory
ECHO %2 is the .bas file

:: Make the makefile
REM Change this path to point to makeMaker.bat
CALL "C:\QBasic\makeMaker.bat" %~n2

:: Call GNU make
REM Change this path to point to make.exe on your computer
CALL "C:\QBasic\make.exe"

REM Pause on Error
if errorlevel = 1 pause

Save this file as "shellmake.bat" in you QB45 folder. This file makes the magic happen. You pass it the project directory and the name of the .bas file you are working on and it makes a nice, neat executable. As with the last batch file, you may need to change the paths to point to makeMaker.bat and make.exe wherever they are at on your system.

Press Ctrl+n one last time and enter this text:


CALL C:\QBasic\shellmake.bat %1 %2
CALL C:\QBasic\CommandLineExecutor.bat %2


Save this file as "CompileAndRun.bat" in your QB45 directory. This batch file will compile and run whatever .bas file you are working on. Once again, you may need to change the paths to match your system.

These batch files will just sit there in your QB45 directory unless you tell Crimson to do something with them. That means we need to make a toolset. In Crimson click Tools>Conf. User Tools...
A new window should pop open. Click on the second slot for user tools and fill in the information until your first tool looks like this picture. Make sure the Command section has the full path to makeMaker.bat on your computer. Also make sure to check "save before execute" and "capture output". This tool works with Crimson to generate a makefile in the directory you save your .bas file to. Click on the third tool slot and make it look like this picture, again making sure you fill in the proper path to make.exe for the command portion.
Click the fifth slot and make it look like this:
For the final tool click on the ninth tool slot and enter the following information:
Make sure the argument is "$(FileDir)" $(FileTitle) with a space between each argument.

Click the Save Tools button and save the toolset. Click Apply and then OK.

Now you are ready to test your tools. Press Ctrl+n and type the following code:

INPUT "Enter in your name please: ", name$
PRINT "Why, hello "; name$; "!"


Save this as "Hello.bas" into a new directory anywhere on your computer.

Now press Ctrl+2. You should see a new window pop open in Crimson and you should see text appear in the window. If all went well the last line of text in the new window should read "Terminated with exit code 0".

Press Ctrl+3. You will see new text in Crimson's output window. You are looking for zero errors, zero warnings and exit code zero. Zero is good.

Press Ctrl+5. You should see the program you just compiled pop open in a new command prompt.

If you press Ctrl+9 you will produce a new makefile, compile, and execute in one step.

This article was meant as an introduction to using QB with Crimson Editor. With ingenuety and time you can adapt and expand the toolset to suit your needs.

No comments: