Jump to content


Photo

Parse a string script!


  • Please log in to reply
12 replies to this topic

#1 HelpMyFellowPeople

HelpMyFellowPeople

    GMC Member

  • GMC Member
  • 239 posts

Posted 15 July 2012 - 10:27 AM

So as always, ive been thinking of ways to make a scripting language or mini programming language and have stumbled upon this little beauty...

Place in a key press event or create event to see it work.
par = get_string('','');

//Just a test command to check if custom messages would work. Success
if(string_pos('msg ',par)=1){
    text1 = string_replace(par,'msg ','')
    if(!string_pos('"',text1)=0){
        text2 =string_replace_all(text1,'"','')
        text3 = text2;
        show_message(text3);
    }
    else{
        show_message('Not a valid string command!')
    }
}
//Show help
if(string_pos('help ',par)=1){
    text1 = string_replace(par,'help ','')
        text3 = text1;
        if(text3 = 'msg'){
            show_message("msg: Show a message box with the given text. Must be in string form.");
        }
    else{
        show_message('Not a valid function!')
    }
}


When the string box appears, type

msg "Hello World" //Will show a message box with the text "hello world"

help msg //Will show a message box with a description of what the function "msg" does

Enjoy!

Edited by HelpMyFellowPeople, 16 July 2012 - 07:58 AM.

  • 0

#2 famous

famous

    GMC Member

  • GMC Member
  • 169 posts
  • Version:Unknown

Posted 15 July 2012 - 10:38 AM

and now?

Edited by famous, 15 July 2012 - 10:42 AM.

  • 0

#3 YellowAfterlife

YellowAfterlife

    GMC Member

  • Global Moderators
  • 3490 posts
  • Version:GM:Studio

Posted 15 July 2012 - 10:45 AM

What if user writes something like
msgx "One" "Two"
? It would display text "x One Two", ignoring error in syntax.

Personally I prefer doing string_pos to find next delimiter in string, and split string into two, being part until delimiter and rest of string. This seems to be the simplest approach that still allows error handling to be done.
  • 0

#4 faty joe man

faty joe man

    GMC Member

  • GMC Member
  • 76 posts
  • Version:Unknown

Posted 15 July 2012 - 10:47 AM

That's pretty cool. Could come in handy.
  • 0

#5 HelpMyFellowPeople

HelpMyFellowPeople

    GMC Member

  • GMC Member
  • 239 posts

Posted 16 July 2012 - 04:38 AM

Hmmm. Thats a good pont there YellowAfterlife... Im guna look into that.
  • 0

#6 dannyjenn

dannyjenn

    GMC Member

  • GMC Member
  • 2041 posts
  • Version:Mac

Posted 16 July 2012 - 06:18 AM

Yeah, it should definitely use string_pos() (and then you could directly copy the string "Hello World" into the text3 without needing to string_replace_all() anything).

Also, the way you have it there is a problem... it will display " Hello World", not "Hello World". You gotta remove that first space somehow... one easy way would be to change "msg" to "msg " (and that would also fix the problem about the code working with "msgx" as well... although that problem would already have been taken care of if you change the code to use string_pos() )
  • 0

#7 HelpMyFellowPeople

HelpMyFellowPeople

    GMC Member

  • GMC Member
  • 239 posts

Posted 16 July 2012 - 08:00 AM

Fixed that problem(for now :P) and added a new command "help". Type "help msg" to view a description of the "msg" function.
  • 0

#8 Hyomoto

Hyomoto

    GMC Member

  • New Member
  • 170 posts
  • Version:GM:Studio

Posted 17 July 2012 - 07:32 AM

This is just the method I prefer, it uses a series of scripts and essentially 'routes' the idea of the command. You could easily merge all these scripts into one, I just have a preference for coding in this format and I threw it together fairly quickly, it works but isn't really practical. It's missing some error checking and obviously, doesn't actually do anything beyond display a few messages.
Spoiler

Edited by Hyomoto, 17 July 2012 - 08:10 AM.

  • 0

#9 HelpMyFellowPeople

HelpMyFellowPeople

    GMC Member

  • GMC Member
  • 239 posts

Posted 17 July 2012 - 08:21 AM

Ok now im gona go learn about switch functions lol
  • 0

#10 Hyomoto

Hyomoto

    GMC Member

  • New Member
  • 170 posts
  • Version:GM:Studio

Posted 17 July 2012 - 12:33 PM

Depending on the language, switch functions behave a little differently (for instance, a switch statement in Ruby). At their most basic level, they can be freely exchanged with if/else statements, but there are certain things one can do that the other won't. Switch statements will execute ALL other cases once one has been matched, until it hits a break, or the statement ends. That's why above you see mine end with either return or break, which ends processing in that branch. The ways this could be useful is if you wanted several different cases to do the same thing, as in:
switch(argument0){
  case "fruit":
  case "melon":
  case "watermelon":  {stuff to do}
}
Which makes it functionally equivalent to:
if argument0 == "fruit" || argument0 == "melon" || argument0 == "watermelon" then {stuff to do}
The place where it differs, however, is you could add processing to one branch if you wanted. Say, have it also do some general stuff when 'fruit' is the case, but not melon or watermelon. If then is usually advantageous if you have only one outcome:
if argument0 == "fruit" {do stuff}
There are other situations, like more complicated comparisons, where it might just be easier to use an if statement. I usually use whatever looks neat and orderly in the code, so its really up to the coder to decide which one they prefer.

Edited by Hyomoto, 17 July 2012 - 12:36 PM.

  • 0

#11 HelpMyFellowPeople

HelpMyFellowPeople

    GMC Member

  • GMC Member
  • 239 posts

Posted 18 July 2012 - 09:31 AM

Ah well thanks for that. Hey, you dont say, you would like to help me build a programming language? You seem to know a thing or two and I could really learn from you. Im just wanting to build it for fun, and it would be a great leap forward into a future language in say, c++... I know a bit about c++ but would not know where to start with making a language, but this would be alot of help. You know, just if your interested..
  • 0

#12 Hyomoto

Hyomoto

    GMC Member

  • New Member
  • 170 posts
  • Version:GM:Studio

Posted 20 July 2012 - 12:17 PM

You may be giving me a little too much credit. I can help with something like a scripting language because I've done MUD work (Accursed Lands anyone?), and I'm fairly accustomed to IF, so I understand a bit about parsing. When it comes to a interpretive language, I wouldn't even know where to begin. I apologize but I wouldn't be much help. In that spirit, I did decide to make use of the example I wrote while working an API toolset to help myself with completing projects and I decided to make use of the scripts I'd written. I'll put it here for other people interested in parsing, and you too if you are pursuing Gamemaker projects. The original example lacked error checking, and something I hadn't even realized, it wouldn't accept one word commands. Anyways, this is a functional parsing system. It's fairly efficient, but could easily be made better I presume.
Spoiler

That's pretty much it, once you've called parse_init(), you can simply use, for instance, parse_begin("offsetx 15 offsety 15 halign CENTER valign TOP",parse_handler) in a script and it will perform processing on the entire string. It works as such, first it goes through all 'basic' commands, and if it matches, it performs the appropriate function or sets 'parse'. On the next loop, if parse has been set it will either perform a new command if it the input matches, or if not, perform a function based upon what parse is. It only accepts commands of one or two words, but I'm only using it to initialize objects without having to call a bunch of functions in the setup script. This is just the general processing, the script_execute line will run a script like the one below which allows you to add situation-based parsing to the system. It avoids a lot of redundancy, but uses very rudimentary error checking.

Spoiler

  • 0

#13 NeoTalon27

NeoTalon27

    GMC Member

  • GMC Member
  • 219 posts
  • Version:GM:HTML5

Posted 07 August 2012 - 03:01 PM

This looks exciting, maybe this is the start of something (Game Maker game-maker) :thumbsup:
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users