Code Monkey home page Code Monkey logo

luavenuscompiler's Introduction

LuaVenusCompiler

luacheck
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:

"foreach" loop

The foreach statement will geneate a pairs statement.

local table = {2,1,3,"test"}

foreach el in table {
  print(el)
}

will generate:

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

Functions

fn can be used instead of function.

fn test() {
	print("hi")
}

will generate

function test()
	print("hi")
end

Curly braces based syntax

The do,then and end statements can be replaced by curly braces syntax.
They can be used in functions, loops, conditions, etcetera.
For example:

do {
	local table = {2,1,3,"test","test2",3}

	fn findTest(t) {
		repeat {
			local found = false
			local el = table.remove(t)
			if el == "test" {
				found = true
			} else {
				print(el)
			}
		} until found
	}
}

will generate:

do
	local table = {2,1,3,"test","test2",3}

	function findTest(t) 
		repeat
			local found = false
			local el = table.remove(t)
			if el == "test" then
				found = true
			else
				print(el)
			end
		until found
	end
end

Lambdas / Anonymous functions

Lambda syntax (args) => {...} can be used to create anonymous functions.

local result
fn store_it(f) {
	result = f(10,6)
}

store_it((a,b) => {
	return (a - b) * 2
})

will generate:

local result
function store_it(f)
	result = f(10,6)
end

store_it(function(a,b)
	return (a - b) * 2
end)

Increment and Decrement

++ and -- can be used to add/sub by 1

local i = 0
local j = 0

i++
j--

will generate:

local i = 0
local j = 0

i = i + 1
j = j - 1

assignments

Assignment operators +=, -=, *=, /=, ^= and .= can be used for math on variables.

local a = 0
-- Increased by
a += 2
## Decreased by
a -= 1
## Multiplied by
a *= 8
-- Divided by
a /= 2
-- Powered by
a ^= 3
## Concatenate string
a .= " str"

will generate

local a = 0
-- Increased by
a = a + 2
-- Decreased by
a = a - 1
-- Multiplied by
a = a * 8
-- Divided by
a = a / 2
-- Powered by
a = a ^ 3
-- Concatenate string
a = a .. " str"

Working with the compiler

Loading

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 LuaVenusCompiler directory within your project's
ways of loding it may be:

--in case your project is run within it's own folder
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.."/LuaVenusCompiler/init.lua")(project_loc.."/LuaVenusCompiler/")
--using require
local vc = require("LuaVenusCompiler")("LuaVenusCompiler/")

When it is loaded it can also be accessed with the global called "LuaVenusCompiler".

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.

Generating Lua code

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.

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

Generating Lua files

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.

luavenuscompiler's People

Contributors

darltrash avatar thefox6 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

viegasfh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.