Page 1 of 1

[WIP][v1] Convert Formatted Text to Code Library

Posted: Wed Apr 05, 2017 2:59 pm
by ArchAngel075
https://github.com/ArchAngel075/CodeSheetParser
For a WIP project I need a method of parsing some formatted text to code.
Aims :
  • The text must read well in english
  • Allow users to specify variables
  • Must support infinitely deep recurring functions
Using this library one can convert from my current format i have specified to lua code.

Designations
Designations are keywords that the library will recognize and convert to lua code.
The designations will also specify the amount of variables present and the CodeSheet (text) format of the code
example of my if then end designation :

Code: Select all

local IF = { 
  variables = {
    true,
    {EQUAL_TO = "==" , NOT_EQUAL_TO = "~=" , LESS_THAN = "<" , LESS_THAN_OR_EQUAL_TO = "<=" , GREATER_THAN = ">" , GREATER_THAN_OR_EQUAL_TO = ">="},
    true,
    true,
  },
  words = {
    "IF",
    "variable1",
    "variable2",
    "variable3",
    "variable4",
  },
  lua = "if variable1 variable2 variable3 then variable4 end"
}
The variables table allows one to specify the number of variables expected - it also can specify what replacements variables get if any.
IF a variable is a table then the Library will replace an instance of a KEY to the VALUE of that KEY
example:
* IF (variable1) (NOT_EQUAL_TO) (variable3) then (variable4)
The designation states variable2 replaces NOT_EQUAL_TO with "~="
thus we get
* IF (variable1) (~=) (variable3) then (variable4)

the lua key of a designation specifies the resulting lua code,
for example the IF designation is
* "if variable1 variable2 variable3 then variable4 end"
the library replaces "variableN" to whatever the variable evaluates to EVEN if another keyword designation.

Using The library :
simply require the CodeSheetParser.lua and use .ParseSheetText() on any CodeSheet :

Code: Select all

CSP.ParseSheetText('IF (GETVAR ("Target")) (NOT_EQUAL_TO) (false) THEN (SETVAR ("Target") (POSITION_OF (CELL)))')
NOTE : a small requirement with code sheets is that the internals of a variable MUST start after the opening bracket with no spacing.

The above example evaluates to

Code: Select all

if self.Variables:GetVar["Target"] ~= false then self.Variables:SetVar["Target",self.Cell:GetPosition[]]
I am still needing to convert the [] brackets to proper () brackets, the need for [] brackets was due to how the library parses the code sheet.
but it would be very simple to convert (next version will have this done for the user)

To add or change designations edit the required "code designations.lua"

If you have any critique on the method i used or advice etc please feel free to post, im sharing this mainly out of desire for critique and help with methods of accomplishing my goals set above

Re: [WIP][v1] Convert Some Fromat Text to Code Library

Posted: Wed Apr 05, 2017 4:07 pm
by Forgemistress
It seems interesting, though I can not help but wonder what purpose it serves. What is it you are hoping to accomplish with this library that couldn't otherwise be done using regular Lua?

Also, the __call metamethod is your friend in the case of the square brackets. ;)

I'll update this post with my critique of the code when I get around to looking at it.

Re: [WIP][v1] Convert Some Fromat Text to Code Library

Posted: Wed Apr 05, 2017 6:44 pm
by ArchAngel075
The purpose is in the game users will 'program' behavior of their units.
The reason for not having them program in pure lua is because not everyone playing the game will know lua and i wanted it to be usable by a larger audience. My inspiration was the warcraft 3 map editor which uses a UI approach for basic scripting. My intention is for a similar system to be possible in my game.

square brackets are not an issue in my above code -
Infact the only thing i need to change is before returning the result is replace all instances of [] with respective ()

In my game i want users to select a command (say GETVAR) and then selecting from options what the variable is (a string? another result from a command? etc)
Ofcourse there will be a raw code option where users can code in pure lua.