https://github.com/rorkh/riwwppp
Classes description language for Lua
https://github.com/rorkh/riwwppp
Last synced: 2 months ago
JSON representation
Classes description language for Lua
- Host: GitHub
- URL: https://github.com/rorkh/riwwppp
- Owner: Rorkh
- Created: 2022-05-14T19:42:32.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-17T12:43:28.000Z (about 3 years ago)
- Last Synced: 2025-02-12T21:36:46.738Z (4 months ago)
- Language: Lua
- Size: 68.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# riwwppp
Classes description language for Lua
## Alarm!
[lpeg-dev](https://github.com/Rorkh/riwwppp/tree/lpeg-dev/) branch what internally uses lpeg in active develpment with few more features realized and features in plans.
See [PR](https://github.com/Rorkh/riwwppp/pull/1 "PR#1") to stay tuned.
## Content
- [Exports](#exports)
- [Instructions](#instructions)
- [@constructor](#constructor-instruction)
- [@field](#field-instruction)
- [@data](#data-instruction)
- [@data modifiers](#data-modifiers)
- [Pragmas](#pragmas)
- [Usage](#usage)
- [Examples](#examples)## Exports
| name | arguments | description |
| ------------ | ------------ | ------------ |
| riwwppp.load | string str | Builds and executes class from description language code |
| riwwppp.loadFile | string filename | Builds and executes class from file |
## Instructions
Instructions in riwwppp are always followed by `@` symbol
Example:
```lua
@class Person
```| name | example | description |
| ------------ | ------------ | ------------ |
| pragma | `@pragma safe` | Specifies pragma for builder |
| class | `@class Person` | Defines class name |
| constructor | `@constructor Create` | Defines constructor name |
| field | `@field age` | Defines new class field (with no accessors) |
| data | `@data name` | Defines new class field with setters and getters |### constructor instruction
Example:
```lua
@class Person
@constuctor Create
```
->
```lua
Animal = {}function Animal:Create()
local o = {}
setmetatable(o, self)
self.__index = self
return o
end-- Uses in internal purposes for inheritance and generates for every class
Animal._constructor = Animal.Create
```
If not specified, default constructor is ```new```
### field instruction
Example:
```lua
@class Person@field wealth
@field age = 18
```
->
```lua
Person = {}function Person:new()
local o = {}
setmetatable(o, self)
self.__index = self
self.wealth = nil
self.age = 18
return o
endPerson._constructor = Person.new
```
```lua
local Peter = Person:new()
print(Peter.age) -- 18
```
### data instruction
```lua
@class Person@data name
@data [number] age = 18
@data [number safe] money = 1000@data [const] truth = 42
```
->
```lua
Person = {}function Person:new()
local o = {}
setmetatable(o, self)
self.__index = self
self.age = 18
self.money = 1000
return o
endfunction Person:getName()
return self.name
endfunction Person:setName(value)
self.name = value
endfunction Person:getAge()
return tonumber(self.age)
endfunction Person:setAge(value)
self.age = value
endfunction Person:getMoney()
return tonumber(self.money)
endfunction Person:setMoney(value)
if type(value) ~= "number" then error("Trying to set forbidden type for field money") end
self.money = value
endfunction Person:getTruth()
return 42
endPerson._constructor = Person.new
```
## data modifiers
| name | description |
| ------------ | ------------ |
| string | Sets internal data type and adds conversion in getter |
| number | Sets internal data type and adds conversion in getter |
| table | Sets internal data type |
| safe | Adds typecheck in setter if type is setted |
| const | Adds getter with inlined value |## Pragmas
| name | description |
| ------------ | ------------ |
| capitalizeMethods | Capitalizes `set` and `get` in setters and getters |
| safe | All ```@data``` instructions have safe modifier by default |## Usage
```lua
local riwwppp = require("riwwppp")
riwwppp.debug = false -- Temporary default is trueriwwppp.load(str)
riwwppp.loadFile(filename)
```### Examples
See [*here*](https://github.com/Rorkh/riwwppp/tree/main/tests "*here*")