write some documentation

This commit is contained in:
theFox6 2020-02-11 18:03:59 +01:00
parent 75b7207ed9
commit e2131ff87f
Signed by: theFox6
GPG Key ID: C884FE8D3BCE128A
5 changed files with 89 additions and 9 deletions

21
LICENSE Normal file
View File

@ -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.

54
README.md Normal file
View File

@ -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.

View File

@ -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,14 +100,18 @@ 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
if env then
return loadstring(fc,"@"..file,"t",env)
else
return loadstring(fc,"@"..file)
end
end
function parser.dovenus(file)
local ff, err = parser.loadvenus(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

View File

@ -1,3 +1,3 @@
local p = dofile("init.lua")
local vp = dofile("init.lua")
p.dovenus("test.venus")
vp.dovenus("test.venus")

View File

@ -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