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
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"
}
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)))')
The above example evaluates to
Code: Select all
if self.Variables:GetVar["Target"] ~= false then self.Variables:SetVar["Target",self.Cell:GetPosition[]]
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