OPENBOX
DISPLAYTEXT Hi! This text is written in a box
WAITKEY
CLOSEBOX
END
That are some simple instructions you can make a program respond on.
Like, with OPENBOX it opens a box, then displays the text, waits
for a key to be pressed, closes the box and ends. How do you make
this stuff with QBasic? Well, that's why I'm writing this, to explain that
to you!
A simple scripting language
CLEARSCREEN
DISPLAYTEXT Wow! This is my first scripting language!!
DISPLAYTEXT Press any key to end...
WAITFORKEY
DISPLAYTEXT You pressed a key and now the program ends!
ENDSCRIPT
DISPLAYTEXT This action will NOT be executed because there
DISPLAYTEXT is an ENDSCRIPT command before it!
Save it as script.dat. So now we have some commands in an external file.
But QBasic can't do anything with that so we need to make a program
that reads the script-file and ports it so QBasic can understand it.
A good way is to read the script-file line by line, then check if the line contains
commands and what they are, if there are any expressions after the command
store them in a temporarily variable and then execute the command. For example
to let the program execute the commands we just used you could use this code:
OPEN "script.dat" FOR INPUT AS #1
DO
INPUT #1, ReadLine$
ReadLine$ = LTRIM$(RTRIM$(ReadLine$)
IF LEFT$(ReadLine$, 11) = "DISPLAYTEXT" THEN
Temp1$ = RIGHT$(ReadLine$, LEN(ReadLine$) - 12)
PRINT Temp1$
END IF
IF LEFT$(ReadLine$, 7) = "WAITKEY" THEN
WHILE INKEY$ = "": WEND
END IF
IF LEFT$(ReadLine$, 9) = "ENDSCRIPT" THEN
EXIT DO
END IF
LOOP UNTIL EOF(1)
CLOSE #1
I'll discuss the code line by line now:
OPEN "script.dat" FOR INPUT AS #1
Opens our scripting file 'script.dat' for reading as file #1
DO
Start the Do...Loop event that runs until the file ends
INPUT #1, ReadLine$
Reads a single line from the file.
ReadLine$ = LTRIM$(RTRIM$(ReadLine$)
Removes all the spaces from the left and right of the line.
IF LEFT$(ReadLine$, 11) = "DISPLAYTEXT" THEN
If the first 11 characters from the line we just read form the word
DISPLAYTEXT the DISPLAYTEXT command is run.
Temp1$ = RIGHT$(ReadLine$, LEN(ReadLine$) - 12)
The DISPLAYTEXT command contains one expression so we just read the
right part of the line we took minus twelve character (the DISPLAYTEXT
command + the space).
PRINT Temp1$
We PRINT the expression we just took.
END IF
End the IF...THEN command
IF LEFT$(ReadLine$, 7) = "WAITKEY" THEN
If the first 7 characters from the line we read are WAITKEY we run it.
WHILE INKEY$ = "": WEND
We wait until a key is pressed
END IF
End the IF...THEN command
IF LEFT$(ReadLine$, 9) = "ENDSCRIPT" THEN
If the first 9 characters from the line we read are ENDSCRIPT we run the command.
EXIT DO
We exit the DO...LOOP event.
END IF
End the IF...THEN command
LOOP UNTIL EOF(1)
Loop back to the DO
CLOSE #1
We close the file.
Now you have your first (small) scripting language! Now onto the harder things!
Well, this isn't exactly hard but I still wanted to include it. I'll now explain on how to add QBasic like comments (the little ' thing). Add a line with a command somewhere in the 'script.dat' file before the ENDSCRIPT command.
Ok, now add this 2 lines somewhere in the do loop:
IF LEFT$(ReadLine$,1) = "'" THEN
END IF
This checks if the first command is a ' and if it is, it just does nothing
because a comment does nothing. Good, now you can have comments in your scripts!
Next time I'll talk about variables and IF...THEN commands. Maybe also about DO...LOOPs and FOR...NEXTs. Until then, enjoy the rest of the issue!