Skip to content

Commit

Permalink
Add CHANGELOG.md
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaRunic committed Jul 16, 2021
1 parent b1779f0 commit d0b779d
Show file tree
Hide file tree
Showing 12 changed files with 828 additions and 82 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Update 1.1.0

- Add string split operator `/`
- Add function union operator `+`
- Fix Queue data structure
- Add lots of LDoc documentation
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ print(-"abcdefg") --> gfedcba
-- repeat
print("xyz" * 4) --> xyzxyzxyzxyz

-- split
luay.util.repr("foo.bar.baz.luay" / ".") --> {"foo", "bar", "baz", "luay"}

-- character indexing
local str = "abc"
print(str[2]) --> b

-- substrings
local str = "hello world"
print(str[{1;5}]) --> hello

-- iteration
for char in ~"hello world" do
io.write(char) --> hello world
Expand Down Expand Up @@ -92,7 +103,7 @@ end

## Object Oriented

Object oriented programming is made easy in Luay using a set of functions to create classes. Since Luay is only an embedded version of Lua, changing the grammar could cause more problems than just incompatibility with <a href="#main-method">Main Methods</a>. Thus, we have these functions in Luay: `class(name: string) -> Class`, `extend(class: Class, super: instanceof Class) -> void`, `constructor(class: Class, body?: function) -> ClassInstance`, and `namespace(name: string) -> ((body: table) -> {alias = (name: string) -> void})`. Here's each one of them in use, to show you the syntax:
Object oriented programming is made easy in Luay using a set of functions to create classes. Since Luay is only an embedded version of Lua, changing the grammar could cause more problems than just incompatibility with <a href="#main-method">Main Methods</a>. Thus, we have these functions in Luay: `class(name: string) -> Class`, `extend(class: Class, super: instanceof Class) -> void`, `constructor(class: Class, body?: function) -> ClassInstance`, `namespace(name: string) -> (body: table) -> {alias = (name: string) -> void}`, and `singleton(name: string)`. Here's each one of them in use, to show you the syntax:

1. Single Class
```lua
Expand Down Expand Up @@ -173,6 +184,7 @@ end
3. Classes with Static and Regular Methods
```lua
using(luay.std)
using(luay.util)

Array = class "Array" do
function Array.new()
Expand All @@ -187,7 +199,7 @@ Array = class "Array" do
arr:Add(v)
end
return arr
end
end

function Array:Add(value)
table.insert(self.cache, value)
Expand All @@ -211,6 +223,41 @@ arr:Remove(2)
repr(arr)--> {"foo", "baz", "luay"}
```

4. Namespace Binding
```lua
...

namespace "MyStuff" {
Animal = Animal;
Dog = Dog;
Array = Array;
}

local arr = MyStuff.Array()
arr:Add("foo")
repr(arr) --> {"foo"}
```

## Function Unions

Using the `luay.util` namespace, you can utilize the `+` operator to create a union of two or more functions. Obviously you can't use the operator on the normal `function` type, as that would require magic. However, you can easily create a new `Function` using `luay::util::Function`. Here's an example that's not so practical, but is certainly cool:
```lua
using(luay.util)

function main()
local half = Function(function(x)
return x / 2
end)

local double = Function(function(x)
return x * 2
end)

local halfAndDouble = half + double
print(halfAndDouble(10)) --> 5 20
end
```

## Lambdas

Yes, you read that right. Lambdas are a shorthand way of writing an anonymous function that represents data. For example, say I have a list of numbers. I want to double each value in that list. Normally, you could do it like this in standard Luay:
Expand Down
Binary file modified bin/luay.exe
Binary file not shown.
50 changes: 50 additions & 0 deletions examples/html_generator/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using(luay.util)

function main()
local line = HTML.Newline

HTML:BeginBlock()

HTML:BeginBlock()
local headContent =
line() +
HTML:StylesheetLink("styles.css") +
line()
HTML:EndBlock()

local head = line() + HTML:Head(headContent)

HTML:BeginBlock()

HTML:BeginBlock()
local formContent =
line() +
HTML:Input {'type="button"', 'value="Open my Other Document in New Window"', 'onclick="openWindow()"'} +
line()
HTML:EndBlock()

local bodyContent =
line() +
HTML:H1("My Document") +
line() +
HTML:A("https://document.com", "My Other Document") +
line() +
HTML:P("This document will go over some very important things.") +
line() +
HTML:Script([[
function openWindow() {
window.open("https://document.com");
}
]]) +
line() +
HTML:Form(formContent) +
line()
HTML:EndBlock()

local body = line() + HTML:Body(bodyContent)

HTML:EndBlock()
local html = HTML:Html(head + body + line())

print(html)
end
18 changes: 18 additions & 0 deletions examples/html_generator/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>My Document</h1>
<a href="https://document.com">My Other Document</a>
<p>This document will go over some very important things.</p>
<script>
function openWindow() {
window.open("https://document.com");
}
</script>
<form>
<input type="button" value="Open my Other Document in New Window" onclick="openWindow()" />
</form>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/queues/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using(luay.std)

function main()
local q = Queue()
q:Enqueue("lil uzi vert")
q:Enqueue("j cole")
q:Enqueue("smokepurpp")
repr(q)
q:Dequeue()
repr(q)
end
19 changes: 14 additions & 5 deletions lib/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ end
function import(module)
local data = require(module)
using(data)
return
end

---
---@param name string
---@return table
function singleton(name)
local body = _ENV[name]
local lib = body[1]
Expand Down Expand Up @@ -46,7 +46,6 @@ function namespace(name)
__newindex = function(self, k, v)
throw(luay.std.Error("cannot write to namespace"))
end;

---@return string
__tostring = function()
return ("<namespace '%s'>"):format(state.name);
Expand Down Expand Up @@ -120,6 +119,7 @@ end
function constructor(body, initializer)
local self = instance(body)
;(initializer or function() end)(self)
self.meta.__metatable = {}
return self
end

Expand Down Expand Up @@ -160,6 +160,14 @@ function throw(err, level)
error(colors("%{red}" + err.message), 2 + (level or 0))
end

---@param message string
---@vararg ...
---@return void
function warn(message, ...)
local args = luay.std.Vector("string", {message, ...})
print(args:Map(lambda "|m| -> colors('%{yellow}' + m)"):Join("\t"))
end

---@param name string
function enum(name)
return function(body)
Expand Down Expand Up @@ -202,10 +210,12 @@ function setfenv(fn, env)
end

---@param content string
---@return Function
function lambda(content)
assert(typeof(content) == "string", "lambda function converts a string to a function expression")
assert(content:Includes("|") and not content:IsBlank(), "malformed lambda")

local env = _ENV
local components = content:Trim():Split("|")
local body = components:At(1):Trim()
local params = luay.std.List()
Expand All @@ -216,8 +226,7 @@ function lambda(content)

local luaString = body:Replace("->", "return")
return luay.util.Function(function(...)
local env = _ENV
for i, v in varargs(...) do
for i, v in pairs {...} do
local name = params:At(i)
if name then
env[name] = v
Expand Down
37 changes: 36 additions & 1 deletion lib/data.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---@param condition any
---@param err string
---@param lvl integer
---@return void
_G.assert = function(condition, err, lvl)
if not condition then
error(err, 3 + (lvl or 0))
Expand All @@ -6,27 +10,51 @@ end

--string util
local mtstr = getmetatable("")
---@param a string
---@param b string
---@return string
function mtstr.__add(a, b)
return a .. b
end

---@param a string
---@param b string
---@return string
function mtstr.__shr(a, b)
return b .. a
end

---@param a string
---@return function
function mtstr.__bnot(a)
return a:Chars()
end

---@param a string
---@param b string
---@return string
function mtstr.__mul(a, b)
return a:rep(b)
end

---@param a string
---@param b string
---@return string
function mtstr.__unm(a, b)
return a:reverse()
end

---@param a string
---@param b string
---@return Vector
function mtstr.__div(a, b)
return a:Split(b)
end

local old_mtIndex = mtstr.__index
---@param str string
---@param i integer | table
---@return string
function mtstr.__index(str, i)
if type(i) == "number" then
local char = str:sub(i, i)
Expand All @@ -44,6 +72,8 @@ function mtstr.__index(str, i)
end

--table util
---@param t table
---@return table
function table.inverse(t)
assert(type(t) == "table", "cannot inverse table of type '" + typeof(t) + "'")
local r = {}
Expand All @@ -53,8 +83,11 @@ function table.inverse(t)
return r
end

---@param t table
---@return function
function values(t)
local i = 0
---@return any
return function()
i = i + 1
if i <= #t then
Expand All @@ -63,8 +96,10 @@ function values(t)
end
end

---@vararg ...
---@return function
function varargs(...)
return pairs {...}
return values {...}
end

colors = require "ansicolors"
2 changes: 2 additions & 0 deletions lib/main.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---@alias void nil

require "data"
require "core"
require "util"
Expand Down
Loading

0 comments on commit d0b779d

Please sign in to comment.