PDA

View Full Version : New to interpreter type questions.



Steve
23-02-2008, 17:55
I have written programs with PureBasic and realBASIC but thinBasic is the first time I have worked with an interpreter - I have a couple of questions:

Is the interpreter able to run numerous programs simultaneously, or should you have an installation for each application you create?

Now for the question that suddenly got me excited about using an interpreter (oh, how I hope the answer is 'yes'...):
Is it possible to write a program that dynamically writes another program at runtime and then calls the interpreter to run that too?

Thanks

Michael Hartlef
23-02-2008, 20:23
Hi Steve,

welcome on board. You can run numerous scripts at the same time but they can't communicate with each other.
thinBasic Scripts can be bundled to an exe. If you bundled, they don't need thinBAsic installed on a computer.
If run them as a script (which can be obfuscated), then only one thinBASIC has to be installed.

And yes, you can run scripts from other scripts, but like I said, they can't communicate with each other. I wish they could.

If you have other questions, then please feel free to ask them.

Michael

ErosOlmi
23-02-2008, 20:36
Ciao Steve and welcome at thinBasic community.

First question: yes.
You can execute as many scripts as you need. Every execution will be handled by a different process. Just try by yourself executing many scripts by double clicking on them in Windows shell or by any other way.

Second question: yes.
The interpreted nature of thinBasic will let you write script on the fly, save them on temp files and invoking thinBasic ingine to execute them. I will prepare a quick example later.

Ciao
Eros

Petr Schreiber
23-02-2008, 20:38
EDIT: Too late :)

Hi Steve,

welcome to thinBASIC!

I will try to answer your questions, although better to wait for official reply from Eros ( Olmi ) or Roberto ( Bianchi ).


Is the interpreter able to run numerous programs simultaneously, or should you have an installation for each application you create?


If this means "can I run multiple scripts at the same time with single thinBASIC installation" then answer is "Yes" :)

One very nice feature of thinBASIC is its thinBundle - you can create EXE package of your script, and distribute this file further as you want ( and run in as many instances as you want ). So even not required to have thinBASIC installed on target platform.



Is it possible to write a program that dynamically writes another program at runtime and then calls the interpreter to run that too?


There are many ways of doing such a tricks. One is more "limited", and serves to evaluate string expressions, have a look at EVAL module in help file. So you can get formula and evaluate it on the fly.


USES "EVAL"

msgbox 0, EVAL_MATH("(5*3+2/5)^2")


Second way is to create script on the fly, and shell it:


' -- We need FILE module to write file, OS module to shell it
uses "FILE"
USES "OS"

' -- Script contents
dim Script as string = "MSGBOX 0, ""Just very basic script"""+$CRLF+ _
"ALERT(""That is all for now"", ""Thanks for trying"")"

' -- Write it to file
FILE_SAVE(APP_SCRIPTPATH+"\MyCustomScript.tBasic", Script)

msgbox (0, "Ok, file saved"+$CRLF+"All what you will see from now is from the other script file", %MB_ICONINFORMATION)

' -- Following line does two things
' -- APP_PATH+"thinBASIC.exe" returns correct thinBASIC install path and adds filename
' -- ... and it runs the script, created on the fly
OS_SHELL(APP_PATH+"thinBASIC.exe"+$SPC+APP_SCRIPTPATH+"\MyCustomScript.tBasic", %OS_WNDSTYLE_NORMAL, %OS_SHELL_SYNC)

msgbox (0, "Now we are back in main script file, hope you liked it, it will end now", %MB_ICONINFORMATION)


The third way would be to create EXE on the fly from script, and shell it.

Other advantage of thinBASIC being interpreted is that you can call functions by name specified on the run-time, try following script:


' -- We will use console
uses "Console"

' -- Text on screen
Console_WriteLine("Welcome!, enter one of the following words to get greeted in selected language:")
Console_WriteLine("ENGLISH")
Console_WriteLine("GERMAN")
Console_WriteLine("ITALIAN")
Console_Write("Your choice ? ( for example ITALIAN ): ")

' -- Retrieving reply
dim Reply as string
reply = console_Readline()

' -- Now if entered was "GERMAN", it will call GreetMe_German, if "ITALIAN" it will call GreetMe_Italian ...
CALL "GreetMe_"+Reply()

console_WaitKey

sub GreetMe_English()
Console_WriteLine("--> Hi")
end sub

sub GreetMe_German()
Console_WriteLine("--> Hallo")
end sub

sub GreetMe_Italian()
Console_WriteLine("--> Ciao")
end sub


Hope I made it clear, but as I said, better to wait for examples and samples from the masters :)


Bye,
Petr

ErosOlmi
23-02-2008, 20:43
Hey Petr, your reply is so complete ... that my reply embarrassed me :-[

Thanks a lot.
Eros

Steve
24-02-2008, 01:00
Hello everyone and thank you for being so welcoming.

Great examples Petr, thank you.

Is it possible to create a procedure that defines a window and it's event-loop, and then call that procedure more than once so you have multiple instances of the same window open, and for each window to handle it's own event-loop correctly?

There doesn't seem to be any examples that demonstrate the use of multiple windows communicating with each other, would somebody be kind enough to point me in the right direction?

Thanks again.
Steve

Michael Clease
24-02-2008, 01:17
Heres a quick and dirty way.




USES "UI"

DIM hDlg1, hDlg2 AS DWORD
DIM ExitFlag AS LONG VALUE = 1
Dim Msg, wparam,lparam As DWORD

DIALOG New 0, "APPTITLE1",50,50, 200, 200, _
%WS_POPUP Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION Or _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg1
DIALOG New 0, "APPTITLE2",150,130, 200, 200, _
%WS_POPUP Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION Or _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg2

CONTROL ADD BUTTON, hDlg1, 1001, "Click to kill", 10, 50, 150, 10
CONTROL ADD BUTTON, hDlg2, 2000, "Click to kill", 10, 50, 150, 10

DIALOG SHOW MODELESS hDlg1
DIALOG SHOW MODELESS hDlg2

While ExitFlag ' not equal to 0

Msg = GETMESSAGE(hDlg1, wParam, lParam)

Select Case Msg

Case %WM_Command

If wParam = 1001 Then ExitFlag = 0 : Exit While

Case %WM_SYSCOMMAND

If wParam = %SC_Close Then ExitFlag = 0 : Exit While

End Select

Msg = GETMESSAGE(hDlg2, wParam, lParam)

Select Case Msg

Case %WM_Command

If wParam = 2000 Then ExitFlag = 0 : Exit While

Case %WM_SYSCOMMAND

If wParam = %SC_Close Then ExitFlag = 0 : Exit While

End Select

Wend
DIALOG END hdlg1
DIALOG END hDlg2

ErosOlmi
24-02-2008, 01:22
@Abraxas, thanks a lot!

@Steve
I see you are starting from the simpler one ;)
To be honest I've never done a script handling more than 1 main window plus some open/close modal dialogs.
Anyhow, here another way of doing with 3 windows each having its own message pump. One window is considered main window and if it is closed, all will be released. But there can be many other ways of doing things depending on what you need.

Ciao
Eros



uses "UI"

dim hDlg1 as long
dim hDlg2 as long
dim hDlg3 as long

DIM Msg AS LONG
DIM wParam AS LONG
DIM lParam AS LONG

dim RetCode_MainWindow as long

DIALOG NEW 0, "thinBasic Main Window",-1,-1, 500, 200, _
%WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_OVERLAPPEDWINDOW, 0 to hDlg1

CONTROL ADD BUTTON, hDlg1, 10, "Click Me to close all" , 10, 10, 120, 15
CONTROL ADD BUTTON, hDlg1, 20, "Send message to window 3" , 10, 30, 120, 15

DIALOG NEW 0, "thinBasic Window 1",-1,-1, 400, 200, _
%WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_OVERLAPPEDWINDOW, 0 to hDlg2
CONTROL ADD BUTTON, hDlg2, 10, "Click Me", 10, 10, 60, 15

DIALOG NEW 0, "thinBasic Window 2",-1,-1, 300, 200, _
%WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_OVERLAPPEDWINDOW, 0 to hDlg3
CONTROL ADD BUTTON, hDlg3, 10, "Click Me", 10, 10, 60, 15

DIALOG SHOW modeless hDlg1
DIALOG SHOW modeless hDlg2
DIALOG SHOW modeless hDlg3

while iswindow(hDlg1)

RetCode_MainWindow = MessagePump_MainWindow(hdlg1)

MessagePump_HDLG2(hDlg2)
MessagePump_HDLG3(hDlg3)

'---If main window returns %TRUE it means user wants to close dialog
if RetCode_MainWindow = %TRUE then
DIALOG END hDlg2
DIALOG END hDlg3
exit while
end if
WEND

'---Close the dialog
DIALOG END hDlg1



'----------------------------------------------------------------------------
function MessagePump_MainWindow(hDlg as long)
'----------------------------------------------------------------------------

'---Get the message and fill wParam and lParam
Msg = getMessage(hDlg, wParam, lParam)

'---Now test the message
SELECT CASE Msg

'---Message fired at the very beginning when dialog is initialized
case %WM_INITDIALOG


CASE %WM_COMMAND

'---Test which control has been clicked
SELECT CASE wParam
case 10
Msgbox 0, "Button in main window"
function = %TRUE
case 20
DIALOG SEND hDlg3, %WM_USER + 100, hDlg, 12345678

end select


CASE %WM_SYSCOMMAND

SELECT CASE wParam
CASE %SC_CLOSE
function = %TRUE
END SELECT

CASE ELSE

END SELECT


end function

'----------------------------------------------------------------------------
function MessagePump_HDLG2(hDlg as long)
'----------------------------------------------------------------------------
DIM Msg AS LONG
DIM wParam AS LONG
DIM lParam AS LONG

Msg = getMessage(hDlg, wParam, lParam)

select case Msg
CASE %WM_COMMAND
SELECT CASE wParam
case 10
Msgbox 0, "Button in window 1"

end select

end select

end function

'----------------------------------------------------------------------------
function MessagePump_HDLG3(hDlg as long)
'----------------------------------------------------------------------------
DIM Msg AS LONG
DIM wParam AS LONG
DIM lParam AS LONG

Msg = getMessage(hDlg, wParam, lParam)

select case Msg
CASE %WM_COMMAND
SELECT CASE wParam
case 10
Msgbox 0, "Button in window 2"

end select

case %WM_USER + 100
Msgbox 0, "I'm window 3. I got a message from window " & wParam & " telling me: " & lParam

end select

end function



Script updated:

added example on how to send a message from one window to the other