Console based scripting language - by Montenegro

Making a console based

scripting language

Making a console scripting language with Liberty basic is very easy. All you have to know is some batch file commands. In the code below there will be a text window with a "Run" menu. When you press the Run menu a console window will open. I will not use any kinds of API calls or DLL's just old fashion batch files :) . The only command will be the print command. You can develop other commands on your own. First of all batch files are text files that execute DOS or Console commands. The commands that we will be using are:

Let's get started. The first thing you have to do is create a "run.bat" file with LB. Here is the code:

open "run.bat" for output as #rrun

I named the handle #rrun because it gives out an error when i use #run. With this command a batch file will be created in you current directory. now we have to print "@echo off" to the batch file to know that is shouldn't display the commands.

print #rrun "@echo off"

The next thing we have to do is find out the number of lines in a text editor with a handle #main.textedit1. This is how that is done

#main.textedit1, "!lines linenum"

The number of lines is now stored in a integer variable named linenum. Next we will make a variable that will hold the current line when the code is compiled and give it a value of 0 (zero). We will call it linee (because line is forbidden)

linee = 0

Now we need to start a loop that will process a line at a time. We will use a while loop.

while linee < linenum

I put in the statement linee < linenum because i wanted it to stop looping when linne is equal to linenum or in other words when all lines have been processed. Now we must increase the number of the current line for 1 and put that line in a string variable that we will call com$.

linee = linee + 1

#main.textedit1, "!line ";linee;" com$"

Now we must detect if the user typed a command to print something and if he did print the text to the console. It is done like this.

if (lower$(left$(com$, 7)) = "print "+chr$(34)) and (right$(com$, 1) = chr$(34)) then
let right$ = right$(com$, len(com$) - 7)
let printcode$ = left$(right$, len(right$) - 1)
#rrun, "echo " + printcode$
end if

At first sight it is not understandable so here is the explanation. first we find out if the user typed "print" and quotation marks on both sides (chr$(34)). In the other two lines we separate the text wrom the quotation marks. And finally we need to add a command that tells the console to print that text. I mentioned earlier that the "echo" command print text to the console. And finally we end the "if" statement.  The next thing we have to do is end the loop ,run the console ,close the #rrun handle and add the pause command that prompts the user to enter a key.

wend
#rrun, "pause"
close #rrun
run "run.bat"

We are now done. Wasn't this easy :D. And for the end, here is the complete code:

'Form created with the help of Freeform 3 v03-27-03
'Generated on Mar 05, 2007 at 16:53:31


[setup.main.Window]

'-----Begin code for #main

nomainwin
WindowWidth = 550
WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)


'-----Begin GUI objects code

TexteditorColor$ = "white"
texteditor #main.textedit1, 0, 0, 540, 360

'-----End GUI objects code

'-----Begin menu code

menu #main, "Run",_
"Run", [run]

menu #main, "Edit" ' <-- Texteditor menu.


'-----End menu code

open "CONSOLE example" for window as #main
print #main, "font ms_sans_serif 10"
print #main, "trapclose [quit.main]"


[main.inputLoop] 'wait here for input event
wait



[run]
open "run.bat" for output as #rrun
#rrun, "@echo off"
#main.textedit1,"!lines linenum"
linee = 0

while linee < linenum
linee = linee + 1
#main.textedit1, "!line ";linee;" com$"
if (lower$(left$(com$, 7)) = "print "+chr$(34)) and (right$(com$, 1) = chr$(34)) then
let right$ = right$(com$, len(com$) - 7)
let printcode$ = left$(right$, len(right$) - 1)
#rrun, "echo " + printcode$
end if
wend
#rrun, "pause"
close #rrun
run "run.bat"

wait

[quit.main] 'End the program
kill "run.bat"
close #main
end

Tutorial By MONTENEGRO