Wasaya
Wasaya is a Programming Language designed by PSTF.
Inspired from
I get the inspiration from Python, C++ and Lua.
I let Wenxinyiyan to generate a language, then he gives the name "LuaT".
Syntax
Command Block and Exegesis
- Use
---
as Single line exegesis. - Use
#---[[ ]]---#
as Multi line exegesis.
Command block are defined as this:
do #---[[ your code ]]---# end
Data types
- Numbers, such as 3, 7, 3.14, -2, 114514, 1e20, 3.683E-34, 3.130582375184619046479671967486, etc.
- Strings, such as 'Hello' (or "Hello"). Strings can be enclosed in double quotes or single quotes.
- Booleans. It can only be True, or False.
- Tables. This is the most important data type in Wasaya, it can express a list or a dictionary, such as {1, 2, 3}, or {key = value}.
- Tuples. It is just a "unchangable table", such as (5, 2), (4, 8, 3, 8, 9), but (1, ) instead of (1).
- Functions. They're 1st-class citizens, which can be passed and returned as arguments.
- Empty types. They has only a value, nil.
- User-defined types. They're defined by "struct" command.
Variables
To define a variable, you may write this command:
local a = 10 --- Define a local variable b = 15 --- Define a global variable a = nil --- Delete the variable a c = 20 d = b + c --- d = 35
Control Structure
Conditional statement
This is an example:
x = 20 if x > 10: print(f"{x} > 10") elseif x == 10: print(f"{x} == 10") else: print(f"{x} < 10") endif
Loop statement
This is an example:
x = 0 while x <= 20 do x += 1 end repeat x -= 1 until x == 0
Iterative loop statement
Wasaya supports value iterative loop, or generic iterative loop.
for i = 1,10 do print(i) end local t = {1, 2, 3} for i, v in ipairs(t) do print(i, v) end for i = 99,0,-1 do print(i) end
Function
Basics
Function are defined as this command:
function greet(s) print(f"Hi, {s}! How are you today?") greet("PrySigneToFry")
Advanced
For example:
local functionFactory = function(x) return function(y) return x + y end end local add5 = functionFactory(5) print(add5(3)) --- Output 8
Operation on tables
Here is another example:
local m = {name = "Oliver", age = 14} print(f"Hi! my name is {m["name"]}.") #---[[ The table here stores two key-value pairs, so you can use the key as a subscript, or even use the table as a user-defined type that treats the key as its property. ]]---# m.age = 15 print(f"I'm {m.age} years old.")
Input and Output
You can use io library to do I/O(except print command):
while true do local s = io.read() print(s) end
Module and Package
There are two files, one is a module, and another is main program.
math_utils.wsy
local M = {} function M.square(x) return x * x end return M
main.wsy
local mathUtils = require("math_utils") x = int(io.read()) print(mathUtils.square(x))
Recursion&Regression
Wasaya supports Rec&Reg:
function fac(x) if x == 0: return 1 else: return x * fac(x - 1) endif end y = int(io.read()) print(fac(y))
Metatable and Metamethods
Metatables and Metamethods allow customization of the table's behavior, such as custom comparisons, arithmetic operations, and so on.
local t = setmetatable({}, {__add = function(a, b) return a.value + b.value end}) t.value = 10 local t2 = setmetatable({}, {__add = t.__add}) t2.value = 20 print(t + t2)
User-defined Data Structures and Classes
Struct
This is an example -- Using struct to let a virtual person to make a simple self-introduction.
struct student: local name local age local location local sx function intro() print(f"Hello! I'm {self.name}.\nI'm {self.age} years old.\nI'm from {self["location"]title()}.\nI'm a {self.sx?"girl":"boy"}.") end end student l = {"Mike", 13, "China", false} l.intro() l = {nil, nil, nil, nil}
Class
This is an example -- Defination of a Circle.
class Circle: local radius function __init__(r) self.radius = r end function cf(r) return self.radius * 2 * math.pi end function area(r) return math.pow(self.radius, 2) * math.pi end function __destroy__() self.radius = nil end end f = Circle.__init__(int(io.read())) print(f.area(f.radius)) f.__destroy__()
Error Handling
In order to keep the code robust, we need a series of code that can handle errors.
try --- Code that may throw an error catch(Exception) --- Error handling else --- Successfully done, optional finally --- Optional end
Examples
Hello, world!
print("Hello, world!")
Cat program
See Input and Output
A+B Problem
a = int(io.read()) b = int(io.read()) print(a + b)
XKCD Random Number
print ("4")
Alternated
print(4)
Alternated
print(chr(48 + 4))
Alternated
x = 0 while x != 4: x = random.randint(1, 6) print(x)
= Alternated =
x = 4 print(x)
== Alternated ==
print(1 + (6 >> 1) + 1 - 1 + 1 - 1)