Jump to content




[Pretty much solved] Patterns


  • You cannot reply to this topic
3 replies to this topic

#1 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 13 July 2015 - 05:38 PM

For my latest Redstone Engine (The first one might be in my signature),

I save the instructions to a file as code

An example file:
rs.setOutput("right",true)
sleep(1)
rs.setOutput("right",false)

Creating and saving is working great. My problem is reading the code again to be able to edit it.


I'd like to use string patterns to extract the arguments from the functions

Like this:
local func = [[rs.setOutput("right",true)]]

args = { string.match( func , "pattern") }  --# Is it gmatch? EDIT: No, its string.match

print( textutils.serialize( args ) )
Expected Output:
{
  "\"right\"",
  "true",
}


All the functions:
See spoiler below for ones I've completed
(All arguments should be returned as strings, side needs its quotes still)
rs.setOutput("side",state) --#> "side",state
rs.setBundledOutput("side",number) --#> "side",number
rs.setBundledOutput("side",colors.combine(rs.getBundledOutput("side"),number)) --#> "side",number
rs.setBundledOutput("side",colors.subtract(rs.getBundledOutput("side"),number)) --#> "side",number

Spoiler

NOTE I'd like 4 separate patterns, not one for all 4
( Is one for all 4 even possible? )

Edited by HPWebcamAble, 13 July 2015 - 08:12 PM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 July 2015 - 07:31 PM

You may find that you'll need to parse the function arguments character-by-character to catch all of the corner cases (embedded commas, escaped quotes, etc.).

Alternatively, since you're storing valid Lua, you could just loadstring() it.

#3 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 13 July 2015 - 07:55 PM

View PostLyqyd, on 13 July 2015 - 07:31 PM, said:

Alternatively, since you're storing valid Lua, you could just loadstring() it.

Wow I can't believe that I forgot about that. I actually had a strange idea along those lines...

Could I run it in an environment where the functions, instead of their normal behavior, would add their arguments to a table?



If not, I could still use patterns.

I am terrible at patterns, but I got one working:
code = [[rs.setBundledOutput("right",120)]]

print( string.match( code , [[rs.setBundledOutput(%(("a+"),(%d+)%)]] ) --#> "right" , 120 (two variables, both strings)

Edited by HPWebcamAble, 13 July 2015 - 07:57 PM.


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 July 2015 - 07:59 PM

Yes, you could create an environment that would do that. It would be easiest to construct if you don't need the APIs to contain anything but functions.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users