Click to See Complete Forum and Search --> : [2.0] recognition
francisstokes
Jun 17th, 2006, 08:22 AM
im working on a scripting language project (well, i havent started yet, because i have this important question :)).
I'm not sure how I'd take commands that had arguments and process the arguments.
For example:
the command for printing some text to the console is:
Say "This is the text to be printed"
How would i recognise that the user has used the "say" command, and where the text they wish to print starts and finishes.
Thank you for any replies
ComputerJy
Jun 17th, 2006, 09:03 AM
store your commands in a table and check every line for StartsWith(..)
SLH
Jun 17th, 2006, 09:41 AM
Your implimentation will depend on how complex you want it.
I.e. do you want it to handle flow statements, e.g. switch, if, else etc.?
Do you want looping?
Do you want UDT's, Classes or Structs?
If you only want 'Command Arguments' as you suggest with ur 'say' command then the method suggested by ComputerJy is fine, but if you want it to be more complex you should research Tokenizing to start with, as well as writing a parser.
You could also look at the Reflection namespace in .NET, although it may not be that usefull (it allows you to create new data types e.g. new classes at runtime).
francisstokes
Jun 17th, 2006, 12:19 PM
I really just want to start the project quite small, see whats involved with making a language. I would eventually like to add looping and if-else but i don't think i'd make it OO.
penagate
Jun 17th, 2006, 12:42 PM
Regular expressions are brilliant for tokenising and when you use the compiled option they're quick too :)
francisstokes
Jun 17th, 2006, 02:08 PM
I was testing some of the stuf you guys said, and i came up with this:
string cmd = textBox1.Text;
if (cmd.StartsWith("Say \""))
{
cmd = cmd.TrimStart('S','a','y',' ','"');
cmd = cmd.TrimEnd('"');
MessageBox.Show(cmd);
}
That works, but if the first letter after the " is "S","a","y" or "', it cuts that letter off.
Any suggestions ?
wossname
Jun 17th, 2006, 03:33 PM
Don't use trim. Just ignore the first 5 characters using the Substring method. If you use trim in that ugly manner and the user types a string that starts with S,a,y or " ", then its going to break it.
whatever.Substring(5)
francisstokes
Jun 17th, 2006, 03:43 PM
Cheers wossname.
SLH
Jun 17th, 2006, 05:01 PM
Once you want more complex stuff (for example ignoring white space, commands with more parameters etc.) you'll want to look at tokanizing.
Btw, a similar excersize, that could be useful to try once you get into it would be making something that could do calculations like such:
1+ 5 * ((3)/(2-4))
Then u could expand it to add variables defined on other lines, then combine it with your command code.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.