Skip to content

Commit

Permalink
VenusParser was renamed to LuaVenusCompiler, added accumulators
Browse files Browse the repository at this point in the history
  • Loading branch information
theFox6 committed Mar 22, 2020
1 parent 941fcf0 commit 4337b41
Show file tree
Hide file tree
Showing 19 changed files with 281 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exclude_files = {"VenusParser/","development/"}
exclude_files = {"LuaVenusCompiler/","development/"}
read_globals = {"minetest"}
globals = {"modutil"}
2 changes: 2 additions & 0 deletions LuaVenusCompiler/.luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globals = {"LuaVenusCompiler"}
exclude_files = {"testout"}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local local_path = VenusParser.path or ""
local vp_util = dofile(local_path.."vp_util.lua")
local local_path = LuaVenusCompiler.path or ""
local vc_util = dofile(local_path.."vc_util.lua")

local parser = {}
local compiler = {}

local elements = {
names = "^(%a%w*)$",
Expand All @@ -14,8 +14,8 @@ local elements = {

local non_space_elements = {elements.special_combined,elements.special,elements.strings}

function parser.warn(msg)
print("VenusParser warning: " .. msg)
function compiler.warn(msg)
print("LuaVenusCompiler warning: " .. msg)
end

--TODO: check if spaces before a curly brace were already added
Expand Down Expand Up @@ -61,7 +61,7 @@ local function parse_element(el,pc)

if el == "=>" then
if not pc.lambargs then
parser.warn(("invalid lambda in line %i"):format(pc.line))
compiler.warn(("invalid lambda in line %i"):format(pc.line))
return el
end
local larg = pc.lambargs
Expand All @@ -72,7 +72,7 @@ local function parse_element(el,pc)
return "function" .. larg .. " "
elseif pc.lambend then
if prefix then
parser.warn(("end statement and lambda match end may be mixed in line %i"):format(pc.line))
compiler.warn(("end statement and lambda match end may be mixed in line %i"):format(pc.line))
prefix = pc.lambargs .. prefix
else
prefix = pc.lambargs
Expand Down Expand Up @@ -154,7 +154,7 @@ local function parse_element(el,pc)
pc.ifend = "end"
return "",prefix
else
parser.warn(("closing curly bracket in line %i could not be matched to an opening one"):format(pc.line))
compiler.warn(("closing curly bracket in line %i could not be matched to an opening one"):format(pc.line))
return el,prefix
end
elseif el == "foreach" then
Expand Down Expand Up @@ -220,7 +220,7 @@ local function parse_element(el,pc)
pc.optassign = false
return " = " .. nam .. " + 1"
else
parser.warn(("empty increment in line %i"):format(pc.line))
compiler.warn(("empty increment in line %i"):format(pc.line))
return el, prefix
end
elseif el == "+=" then
Expand All @@ -229,47 +229,47 @@ local function parse_element(el,pc)
pc.optassign = false
return "= " .. nam .. "+"
else
parser.warn(("empty increment assignment in line %i"):format(pc.line))
compiler.warn(("empty increment assignment in line %i"):format(pc.line))
end
elseif el == "-=" then
if pc.optassign then
local nam = pc.optassign
pc.optassign = false
return "= " .. nam .. "-"
else
parser.warn(("empty decrement assignment in line %i"):format(pc.line))
compiler.warn(("empty decrement assignment in line %i"):format(pc.line))
end
elseif el == "*=" then
if pc.optassign then
local nam = pc.optassign
pc.optassign = false
return "= " .. nam .. "*"
else
parser.warn(("empty multiply assignment in line %i"):format(pc.line))
compiler.warn(("empty multiply assignment in line %i"):format(pc.line))
end
elseif el == "/=" then
if pc.optassign then
local nam = pc.optassign
pc.optassign = false
return "= " .. nam .. "/"
else
parser.warn(("empty divide assignment in line %i"):format(pc.line))
compiler.warn(("empty divide assignment in line %i"):format(pc.line))
end
elseif el == "^=" then
if pc.optassign then
local nam = pc.optassign
pc.optassign = false
return "= " .. nam .. "^"
else
parser.warn(("empty power assignment in line %i"):format(pc.line))
compiler.warn(("empty power assignment in line %i"):format(pc.line))
end
elseif el == ".=" then
if pc.optassign then
local nam = pc.optassign
pc.optassign = false
return "= " .. nam .. ".."
else
parser.warn(("empty concatenation assignment in line %i"):format(pc.line))
compiler.warn(("empty concatenation assignment in line %i"):format(pc.line))
end
end
--print(el,pc.instring and "in string" or "")
Expand Down Expand Up @@ -324,7 +324,7 @@ local function handle_prefix(el,p)
print("prel:" .. el)
end
--]]
return vp_util.concat_optnil(pre,el," "),lpre
return vc_util.concat_optnil(pre,el," "),lpre
end
return el
end
Expand Down Expand Up @@ -354,7 +354,9 @@ end
local function store_optassign(el,pc)
if pc.optassign and el ~= "" then
if pc.linestart and el:match(elements.names) then
if (pc.linestart and el:match(elements.names)) or
(pc.optassign and el == ".") or
(pc.optassign and pc.optassign ~= true and pc.optassign:sub(#pc.optassign) == "." and el:match(elements.names)) then
if pc.optassign == true then
pc.optassign = el
else
Expand All @@ -368,13 +370,13 @@ end
local function parse_line(l,pc)
local pl = ""
for sp,s in vp_util.optmatch(l,elements.spaces) do
for sp,s in vc_util.optmatch(l,elements.spaces) do
if s then
if store_space(sp,pc) then
pl = pl .. sp
end
else
for st in vp_util.optmatch(sp,non_space_elements) do
for st in vc_util.optmatch(sp,non_space_elements) do
if pc.slcomm then
pl = pl .. st
else
Expand Down Expand Up @@ -441,10 +443,10 @@ local function handle_lineend_lambargs(pc)
return ""
end
function parser.tl_venus_string(str)
function compiler.tl_venus_string(str)
local fc = ""
local pc = {instring = false, opencurly = {}, line = 0}
for l,e in vp_util.optmatch(str,"\n") do
for l,e in vc_util.optmatch(str,"\n") do
if e then
if pc.slcomm then
pc.slcomm = false
Expand All @@ -460,40 +462,40 @@ function parser.tl_venus_string(str)
end
end
if (#pc.opencurly > 0) then
parser.warn("not all curly brackets were closed")
compiler.warn("not all curly brackets were closed")
end
return fc
end
function parser.tl_venus_file(file)
function compiler.tl_venus_file(file)
local f = io.open(file)
local ret = parser.tl_venus_string(f:read("*a"))
local ret = compiler.tl_venus_string(f:read("*a"))
f:close()
return ret
end
function parser.loadvenus(file,env)
local fc = parser.tl_venus_file(file)
function compiler.loadvenus(file,env)
local fc = compiler.tl_venus_file(file)
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)
function compiler.dovenus(file)
local ff, err = compiler.loadvenus(file)
if ff == nil then
error(err,2)
end
return ff()
end
function parser.convert_venus_file(venus_file_in,lua_file_out)
local s = parser.tl_venus_file(venus_file_in)
function compiler.convert_venus_file(venus_file_in,lua_file_out)
local s = compiler.tl_venus_file(venus_file_in)
local f = io.open(lua_file_out,"w")
f:write(s)
f:close()
end
return parser
return compiler
38 changes: 21 additions & 17 deletions VenusParser/README.md → LuaVenusCompiler/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# venus lua parser
A parser that loads venus files into lua. Written in lua.
The parser reads a venus file and replaces venus syntax by lua syntax.
# LuaVenusCompiler
[![luacheck][luacheck badge]][luacheck workflow]
A compiler that translates venus files into lua. Written in lua.
The compiler reads a venus file and replaces venus syntax by lua syntax.
It can also load and run the result.

## features
Expand Down Expand Up @@ -131,15 +132,15 @@ Assignment operators `+=`, `-=`, `*=`, `/=`, `^=` and `.=` can be used.
local a = 0
-- increment
a += 2
-- decrement
// decrement
a -= 1
-- multiply
## multiply
a *= 8
-- divide
// divide
a /= 2
-- to the power of
a ^= 3
-- concatenate string
// concatenate string
a .= " str"
```
will generate
Expand All @@ -159,21 +160,21 @@ a = a ^ 3
a = a .. " str"
```

## working with the parser
## working with the compiler
### loading
The init.lua returns a function for loading the parser.
The init.lua returns a function for loading the compiler.
You have to call it with the path to the script itself as argument.
In case you have the VenusParser directory within your project's
In case you have the LuaVenusCompiler directory within your project's
ways of loding it may be:
```lua
--in case your project is run within it's own folder
local vc = dofile("VenusParser/init.lua")("VenusParser/")
local vc = dofile("LuaVenusCompiler/init.lua")("LuaVenusCompiler/")
--in case you have a variable called project_loc containing the path to your projects folder
local vc = dofile(project_loc.."/VenusParser/init.lua")(project_loc.."/VenusParser/")
local vc = dofile(project_loc.."/LuaVenusCompiler/init.lua")(project_loc.."/LuaVenusCompiler/")
--using require
local vc = require("VenusParser")("VenusParser/")
local vc = require("LuaVenusCompiler")("LuaVenusCompiler/")
```
When it is loaded it can also be accessed with the global called "VenusParser".
When it is loaded it can also be accessed with the global called "LuaVenusCompiler".

### running venus files
`vc.dovenus(file)` works like `dofile(file)`
Expand All @@ -185,14 +186,17 @@ It's argument can be a relative or absolute path to the file that should be load
It returns a function that runs the generated lua.

### generating lua code
`vp.tl_venus_file(file)` returns the lua generated from the files contents
`vc.tl_venus_file(file)` returns the lua generated from the files contents
It's argument can be a relative or absolute path to the file that should be translated.
It returns the generated lua as string.

`vp.tl_venus_string(str)` returns the lua generated from the given string
`vc.tl_venus_string(str)` returns the lua generated from the given string
It returns the generated lua as string.

### generating lua files
`vp.convert_venus_file(venus_file_in,lua_file_out)` generates a lua file
`vc.convert_venus_file(venus_file_in,lua_file_out)` generates a lua file
It's arguments can be relative or absolute paths.
The venus_file_in will be converted to lua and written to lua_file_out.

[luacheck badge]: https://github.com/theFox6/LuaVenusCompiler/workflows/luacheck/badge.svg
[luacheck workflow]: https://github.com/theFox6/LuaVenusCompiler/actions?query=workflow%3Aluacheck
16 changes: 16 additions & 0 deletions LuaVenusCompiler/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
if rawget(_G,"LuaVenusCompiler") then
print("LuaVenusCompiler warning: already initialized")
else
LuaVenusCompiler = {}
end

function LuaVenusCompiler.loadFromPath(path)
LuaVenusCompiler.path = path
local ret = dofile(path.."LuaVenusCompiler.lua")
for i,v in pairs(ret) do
LuaVenusCompiler[i] = v
end
return ret
end

return LuaVenusCompiler.loadFromPath
3 changes: 3 additions & 0 deletions LuaVenusCompiler/runTest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local vc = dofile("init.lua")("")

vc.dovenus("test.venus")
6 changes: 5 additions & 1 deletion VenusParser/test.venus → LuaVenusCompiler/test.venus
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
print("running venus test script")

local vp_util = dofile("vp_util.lua")
local vp_util = dofile("vc_util.lua")

local function for_range_test()
local a = 0
Expand Down Expand Up @@ -205,4 +205,8 @@ assert(vp_util.dftc(concatsub(tt),{"hello there", "venus test"}))

assert(not (() => {if(true){return}return true})())

local ctt = {test = "hello"}
ctt.test .= " world"
assert(ctt.test == "hello world")

print("venus test end")
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
print("running venus test script")

local vp_util = dofile("vp_util.lua")
local vp_util = dofile("vc_util.lua")

local function for_range_test()
local a = 0
Expand Down Expand Up @@ -205,4 +205,8 @@ assert(vp_util.dftc(concatsub(tt) ,{"hello there", "venus test"}))

assert(not (function() if(true) then return end return true end)() )

local ctt = {test = "hello"}
ctt.test = ctt.test .. " world"
assert(ctt.test == "hello world")

print("venus test end")
6 changes: 6 additions & 0 deletions LuaVenusCompiler/translateTest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local vc = dofile("init.lua")("")
local o = "testout/test.lua"

vc.convert_venus_file("test.venus",o)

dofile(o)

0 comments on commit 4337b41

Please sign in to comment.