Skip to content

Commit

Permalink
add match operator (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Hundt committed Dec 16, 2014
1 parent 502409e commit 4d136e5
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ precedence. Operators marked with a trailing `_` are unary operators.
| `and` | 2 | left | logical and |
| `==` | 3 | left | equality |
| `!=` | 3 | left | inequality |
| `~~` | 3 | left | matches |
| `!~` | 3 | left | does not match |
| `is` | 4 | left | type equality |
| `as` | 4 | left | type coercion |
| `>=` | 5 | left | greater than or equal to |
Expand Down
Binary file modified boot/src/shine/lang/parser.raw
Binary file not shown.
Binary file modified boot/src/shine/lang/translator.raw
Binary file not shown.
Binary file modified boot/src/shine/lang/tree.raw
Binary file not shown.
3 changes: 3 additions & 0 deletions editor/vim/syntax/shine.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ syn keyword shineFunction assert print yield getmetatable setmetatable tonumber
syn keyword shineFunction rawget rawset rawlen newproxy type typeof tostring
syn keyword shineFunction _G __magic__ error pairs ipairs require

syn match shineFunction "[a-zA-Z_][a-zA-Z_0-9]*\((\)\@="
syn match shineFunction "[a-zA-Z_][a-zA-Z_0-9]*\s*\(<-\)\@="

" Error Type Keywords
syn keyword shineKeyword Error

Expand Down
18 changes: 14 additions & 4 deletions lib/async/io.shn
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,18 @@ class ByteStream
wait.ready()
end
end

get eof()
return self.is_eof
end

get writable()
if self.size == 0 then
return -1
end
return 1
end

get readable()
if self.size == 0 then
return -1
Expand Down Expand Up @@ -274,8 +277,7 @@ class ByteStream
end
end

class FileStream
include AsyncStream
class FileStream include AsyncStream

self(stream)
self.stream = stream
Expand Down Expand Up @@ -325,16 +327,18 @@ class FileStream
end
end

module AsyncSocket
include AsyncStream
module AsyncSocket include AsyncStream
super = ...

self(...)
super(...)
end

close()
self.poller.stop()
super.close()
end

end

class TCPSocket extends _TCPSocket
Expand All @@ -343,19 +347,23 @@ end

class TCPServer extends _TCPServer
include AsyncSocket super

accept()
if self.readable > 0 then
return super.accept() as TCPClient
else
return nil
end
end

end

class TCPClient extends _TCPClient
include AsyncSocket super

local rderr = 0
local wrerr = 0

read(len = 4096)
if self.readable > 0 then
r = super.read(len)
Expand All @@ -368,6 +376,7 @@ class TCPClient extends _TCPClient
return nil
end
end

write(buf)
len = #buf
out = 0
Expand All @@ -388,5 +397,6 @@ class TCPClient extends _TCPClient
end
return out
end

end

8 changes: 6 additions & 2 deletions lib/util/guards.shn
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,23 @@ end
le = guard 'l3', function(a, b)
return a <= b
end
eq = guard 'eq', function(a, b)
return a == b
end
ne = guard 'ne', function(a, b)
return a != b
end

export gt, ge, lt, le, ne
export gt, ge, lt, le, eq, ne

?gt = maybe gt
?ge = maybe ge
?lt = maybe lt
?le = maybe le
?eq = maybe eq
?ne = maybe ne

export ?gt, ?ge, ?lt, ?le, ?ne
export ?gt, ?ge, ?lt, ?le, ?eq, ?ne

function enum(opts)
rmap = { }
Expand Down
2 changes: 1 addition & 1 deletion sample/sys.shn
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

sys::closedir(dir)

stat = sys::stat('./a.shn')
stat = sys::stat('./README.md')
for k, v in stat do
print "stat:", k, v
end
Expand Down
55 changes: 53 additions & 2 deletions src/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,57 @@ local Array = class("Array", function(self)
end
return b
end
function self:__match(that)
if type(that) == "table" then
if not __is__(that, Array) then
return false
end
if that.length ~= self.length then
return false
end
for i=0, self.length - 1 do
if not __match__(self[i], that[i]) then
return false
end
end
return true
end
end
end)

local Any = class("Any", function(self)
function self.__match(self, that)
for i=1, #self do
if __match__(self[i], that) then
return true
end
end
return false
end
end)

local function any(...)
return setmetatable({ ... }, Any)
end

local All = class("All", function(self)
function self.__match(self, that)
for i=1, #self do
if not __match__(self[i], that) then
return false
end
end
return true
end
end)

local function all(...)
return setmetatable({ ... }, All)
end

local function try(try, catch, finally)
local ok, rv = pcall(try)
if not ok then
if not ok and catch ~= nil then
ok, rv = pcall(catch, rv)
if not ok then
error("error in error handling", 2)
Expand Down Expand Up @@ -609,7 +655,6 @@ function __match__(that, this)
local type_this = type(this)
local type_that = type(that)

local meta_this = getmetatable(this)
local meta_that = getmetatable(that)
if meta_that then
if meta_that.__match then
Expand Down Expand Up @@ -813,6 +858,10 @@ end
Pattern.__tostring = function(self)
return string.format('Pattern<%p>', self)
end
Pattern.__match = function(self, subj)
if type(subj) ~= 'string' then return false end
return self:match(subj)
end
Pattern.__index.__match = function(self, subj, ...)
if type(subj) ~= 'string' then return false end
return self:match(subj, ...)
Expand Down Expand Up @@ -1162,6 +1211,8 @@ __magic__ = {
include = include;
typeof = typeof;
eval = eval;
any = any;
all = all;

-- utility
environ = environ;
Expand Down
10 changes: 5 additions & 5 deletions src/lang/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ local patt = [=[
keyword <- (
<reserved> / "class" / "module" / "continue" / "throw" / "super"
/ "import" / "export" / "try" / "catch" / "finally" / "is" / "as"
/ "include" / "grammar" / "given" / "case" / "macro"
/ "include" / "grammar" / "given" / "case" / "macro"/ "with"
) <idsafe>
sep <- <bcomment>? (<nl> / ";" / <lcomment>) / <ws> <sep>?
Expand Down Expand Up @@ -473,9 +473,9 @@ local patt = [=[
) -> exprStmt
binop <- {
"+" / "-" / "~" / "/" / "**" / "*" / "%" / "^" / "|" / "&"
/ ">>>" / ">>" / ">=" / ">" / "<<" / "<=" / "<" / ".."
/ "!=" / "==" / ":" [+-~/*%^|&><!?=]
"+" / "-" / "~~" / "~" / "/" / "**" / "*" / "%" / "^"
/ "|" / "&" / ">>>" / ">>" / ">=" / ">" / "<<" / "<="
/ "<" / ".." / "!=" / "==" / "!~" / ":" [+-~/*%^|&><!?=]
/ ("or" / "and" / "is" / "as") <idsafe>
}
Expand All @@ -484,7 +484,7 @@ local patt = [=[
) -> infixExpr
prefix_expr <- (
({ "#" / "-" !'-' / "~" / "!" / "not" <idsafe> } s)? <term>
({ "#" / "-" !'-' / "~" / "!" !"~" / "not" <idsafe> } s)? <term>
) -> prefixExpr
assop <- {
Expand Down
10 changes: 8 additions & 2 deletions src/lang/translator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ local globals = {
'Nil','Number','Boolean', 'String', 'Function', 'Coroutine', 'Range',
'UserData', 'Table', 'Array', 'Error', 'Module', 'Class', 'Object',
'Grammar', 'Pattern', 'ArrayPattern', 'TablePattern', 'ApplyPattern',
'__magic__', 'yield', 'take', 'typeof', 'null', 'warn', 'eval',
'__FILE__', '__LINE__', '_M', '_PACKAGE', '_NAME', 'Meta'
'__magic__', 'yield', 'take', 'typeof', 'null', 'warn', 'eval', 'any', 'all',
'__FILE__', '__LINE__', '_M', '_PACKAGE', '_NAME', 'Meta', 'Any', 'All'
}
for k,v in pairs(_G) do
globals[#globals + 1] = k
Expand Down Expand Up @@ -989,6 +989,12 @@ function match:BinaryExpression(node)
if o == '..' then
return Op{'!call', '__range__', self:get(node.left), self:get(node.right)}
end
if o == "~~" then
return Op{'!call', '__match__', self:get(node.right), self:get(node.left)}
end
if o == "!~" then
return Op{'!not', Op{'!call', '__match__', self:get(node.right), self:get(node.left)} }
end
if string.sub(o, 1, 1) == ':' then
return Op{'!call', '__usrop__', Op(o), self:get(node.left), self:get(node.right) }
end
Expand Down
2 changes: 2 additions & 0 deletions src/lang/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ local op_info = {

["=="] = { 3, 'L' },
["!="] = { 3, 'L' },
["~~"] = { 3, 'L' },
["!~"] = { 3, 'L' },

["is"] = { 4, 'L' },
["as"] = { 4, 'L' },
Expand Down

0 comments on commit 4d136e5

Please sign in to comment.