Creating very small scripting language: conditional statements?
I'm extending my application with plugins and a small scripting language. It will only have 1 or 2 objects available.
I can probably do variables but I'm kind of at a loss for how to do conditional statements. I've never tried anything like this before and I'm actually kind of confused on where to start also.
But the main concern is conditional statements. It will only contain If/Then statements. Can someone give me some pseudo-code or logic or an example or something? :confused:
I found a few examples on Planet Source Code, but a lot were way too complex/sloppy-coded to be of much use and pick out the important parts.
Re: Creating very small scripting language: conditional statements?
You could try the Microsoft Script Control reference, see also:
http://support.microsoft.com/kb/184740
Re: Creating very small scripting language: conditional statements?
Thanks, but doesn't really help much since I'll be writing my own scripting language which won't really be much like Visual Basic or run Visual Basic code.
Thanks, though. :)
Re: Creating very small scripting language: conditional statements?
Have you defined your syntax already? Will it be procedure based or object orientated? And if I may know, what is the purpose?
It sounds like a very intense and interesting project!
Re: Creating very small scripting language: conditional statements?
It will use more-or-less the BASIC syntax. Its purpose is just to extend my application.
Users can write scripts to modify the GUI of the program, do things with data, etc. It will be a very basic/stripped-down language with only a couple objects, variables, conditional statements, and maybe if I can, for loops.
Edit: I've used the Microsoft Script Control that you linked to before, to run VB code, but unfortunately it won't work for this situation, and I'd really rather write it from scratch. :)
Re: Creating very small scripting language: conditional statements?
you could possibly write a couple of functions like this
Visual Basic Code:
Public function GreaterThan(byval Arg1 as string, byval Arg2 as string, optional EqualTo as boolean) as boolean
If EqualTo then
'GreaterThan or equal to (>=)
if arg1 >= arg 2 then GreaterThan = true
else
'Only GreaterThan (>)
if arg1 > arg 2 then GreaterThan = true
end If
end function
Public function LessThan(byval Arg1 as string, byval Arg2 as string, optional EqualTo as boolean) as boolean
If EqualTo then
'LessThan or equal to (<=)
if arg1 <= arg 2 then LessThan = true
else
'Only GreaterThan (<)
if arg1 < arg 2 then LessThan = true
end If
End Function
Of course you will need to change the Arg input types based on what you are comparing, but you can get the general idea.
Next, you will need to parse the text in the script, and then call the appropriate function eg
Script: If "abc" =< "bcd" Then ...
Your app will need to pull out the strings "abc" and "bcd" and then call LessThan like so: If LessThan("abc", "bcd", True)
Re: Creating very small scripting language: conditional statements?
I would recommend something similar to what Macka007 suggested, but a bit simplified.
Assuming that you have separated the items into variables arg1, arg2, and strOperator, you could use a Select Case like this:
Code:
Dim booResult as Boolean
Select Case strOperator
Case "="
booResult = (arg1=arg2)
Case ">"
booResult = (arg1>arg2)
Case ">="
booResult = (arg1>=arg2)
...
Case Else
MsgBox "Unknown operator!", vbExclamation
Exit Sub
End Select
..or re-arranged slightly:
Code:
Dim booResult as Boolean
Select Case strOperator
Case "=": booResult = (arg1=arg2)
Case ">": booResult = (arg1>arg2)
Case ">=": booResult = (arg1>=arg2)
...
Case Else
MsgBox "Unknown operator!", vbExclamation
Exit Sub
End Select
The booResult variable would then tell you the outcome of their If statement.
Note that you need to be careful with the data type of arg1 and arg2, or you may get unexpected results (if both were strings, 2 would be greater than 11).