From e2131ff87fc00632b38b54c3aa95d15cf038334d Mon Sep 17 00:00:00 2001 From: theFox6 Date: Tue, 11 Feb 2020 18:03:59 +0100 Subject: [PATCH] write some documentation --- LICENSE | 21 +++++++++++++++++++++ README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 11 ++++++++--- runTest.lua | 4 ++-- test.venus | 8 ++++---- 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ce9d748 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 theFox6 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..64084d3 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# venus lua parser +A parser that loads venus files into lua. Written in lua. +The parser reads the lua file replaces venus syntax by lua syntax and loads the result. + +## features +### foreach +The `foreach` statement will geneate a `pairs` statement. + +```lua +local table = {2,1,3,"test"} + +foreach el in table do + print(el) +end +``` +will generate +```lua +local table = {2,1,3,"test"} + +for _, el in table do + print(el) +end +``` + +### comments +for comments --, // and ## can be used +if something follows a -- it will always be treated as comment + +##todo +- curly braces +- increment, decrement, etc. +- fn and lambdas +- eventually be able to produce lua files + +## working with the parser +### loading +The init.lua returns a table containing the parser. +In case you have the VenusParser directory within your project's +ways of loding it may be: +```lua +-- using require (cached) +local vc = require("VenusParser") +-- using dofile +local vc = dofile("VenusParser/init.lua") +``` + +### running venus files +`vc.dovenus(file)` works like `dofile(file)` +It's argument can be a relative or absolute path to the file that should be run. + +### loading venus files +`vc.loadvenus(file)` works like `loadfile(file)` +It's argument can be a relative or absolute path to the file that should be loaded. +It returns a function that runs the generated lua. diff --git a/init.lua b/init.lua index 97ee298..00245f4 100644 --- a/init.lua +++ b/init.lua @@ -47,7 +47,7 @@ local function parse_element(el,pc) end if el == "foreach" then pc.foreach = 1 - return "for" + return "for _," elseif el == "for" then pc.foreach = 0 elseif el == "in" then @@ -100,13 +100,17 @@ local function parse_line(l,pc) return pl end -function parser.loadvenus(file) +function parser.loadvenus(file,env) local fc = "" local pc = {instring == false, opencurly = {}} for l in io.lines(file) do fc = fc .. parse_line(l,pc) .. "\n" end - return loadstring(fc,"@"..file) + if env then + return loadstring(fc,"@"..file,"t",env) + else + return loadstring(fc,"@"..file) + end end function parser.dovenus(file) @@ -117,6 +121,7 @@ function parser.dovenus(file) return ff() end +-- in case anybody wants to use it too parser.optmatch = optmatch return parser diff --git a/runTest.lua b/runTest.lua index db139e9..00a83d2 100644 --- a/runTest.lua +++ b/runTest.lua @@ -1,3 +1,3 @@ -local p = dofile("init.lua") +local vp = dofile("init.lua") -p.dovenus("test.venus") +vp.dovenus("test.venus") diff --git a/test.venus b/test.venus index ee70bf7..816f86d 100644 --- a/test.venus +++ b/test.venus @@ -10,12 +10,12 @@ for i = 0,5 do print(i) end -for _,el in pairs(testt) do - print(el) +for i,el in pairs(testt) do + print(i.." = "..el) end -foreach i,el in testt do - print(i.." = "..el) +foreach el in testt do + print(el) end // comment