Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add number.one?, .zero?, .negative?; condition val in true?, false?, null?, & when blocks #26

Merged
merged 1 commit into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 78 additions & 41 deletions core/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,28 @@ local escape_ops = { ["!"] = "_bang",
["="] = "_equal",
["%"] = "_percent",
["_"] = "_under",
["$"] = "_dollar"
["$"] = "_dollar"
}

local unescape_ops = { ["bang"] = "!",
["star"] = "*",
["minus"] = "-",
["plus"] = "+",
["or"] = "|" ,
["and"] = "&",
["at"] = "@",
["tilde"] = "~",
["up"] = "^",
["forward"] = "/",
["back"] = "\\",
["question"] = "?",
["less"] = "<",
["greater"] = ">",
["notequal"] = "!=",
["equal"] = "=",
["percent"] = "%",
["under"] = "_",
["dollar"] = "$"
local unescape_ops = { ["bang"] = "!",
["star"] = "*",
["minus"] = "-",
["plus"] = "+",
["or"] = "|" ,
["and"] = "&",
["at"] = "@",
["tilde"] = "~",
["up"] = "^",
["forward"] = "/",
["back"] = "\\",
["question"] = "?",
["less"] = "<",
["greater"] = ">",
["notequal"] = "!=",
["equal"] = "=",
["percent"] = "%",
["under"] = "_",
["dollar"] = "$"
}

local esc_symbols = orex.new("!=|>=|<=|\\||[!?\\-*+^@~\\/\\\\><$_%|&=]")
Expand Down Expand Up @@ -292,7 +292,7 @@ end

function object:new (...)
local nb

nb = new_brat(self)


Expand Down Expand Up @@ -676,6 +676,9 @@ end
-- true or the then_value if one is given. If the condition is false, returns
-- false or the else_value if one is given.
--
-- If they are functions, they will be called with the original condition value
-- (before it was converted to a boolean) as a first argument, but the argument can be ignored.
--
-- Typically, then_value and else_value should be functions, to allow for
-- delayed evaluation.
--
Expand Down Expand Up @@ -730,7 +733,7 @@ function object:_2_true_question (condition, true_branch)

if is_true(condition) then
if is_callable(true_branch) then
return true_branch(self)
return true_branch(self, condition)
else
return true_branch
end
Expand All @@ -746,13 +749,13 @@ function object:_3_true_question (condition, true_branch, false_branch)

if is_true(condition) then
if is_callable(true_branch) then
return true_branch(self)
return true_branch(self, condition)
else
return true_branch
end
else
if is_callable(false_branch) then
return false_branch(self)
return false_branch(self, condition)
else
return false_branch
end
Expand All @@ -772,6 +775,9 @@ end
-- Typically, then_value and else_value should be functions, to allow for
-- delayed evaluation.
--
-- If they are functions, they will be called with the original condition value
-- (before it was converted to a boolean) as a first argument, but the argument can be ignored.
--
-- Example:
--
-- false? 2 + 2 == 5
Expand Down Expand Up @@ -823,7 +829,7 @@ function object:_2_false_question (condition, true_branch)

if not is_true(condition) then
if is_callable(true_branch) then
return true_branch(self)
return true_branch(self, condition)
else
return true_branch
end
Expand All @@ -839,13 +845,13 @@ function object:_3_false_question (condition, true_branch, false_branch)

if not is_true(condition) then
if is_callable(true_branch) then
return true_branch(self)
return true_branch(self, condition)
else
return true_branch
end
else
if is_callable(false_branch) then
return false_branch(self)
return false_branch(self, condition)
else
return false_branch
end
Expand All @@ -865,6 +871,9 @@ end
-- Typically, then_value and else_value should be functions, to allow for
-- delayed evaluation.
--
-- If they are functions, they will be called with the original condition value
-- (before it was converted to a boolean) as a first argument, but the argument can be ignored.
--
-- Example:
--
-- null? x
Expand Down Expand Up @@ -916,7 +925,7 @@ function object:_2_null_question (condition, true_branch)

if condition == object.__null then
if is_callable(true_branch) then
return true_branch(self)
return true_branch(self, condition)
else
return true_branch
end
Expand All @@ -932,13 +941,13 @@ function object:_3_null_question (condition, true_branch, false_branch)

if condition == object.__null then
if is_callable(true_branch) then
return true_branch(self)
return true_branch(self, condition)
else
return true_branch
end
else
if is_callable(false_branch) then
return false_branch(self)
return false_branch(self, condition)
else
return false_branch
end
Expand Down Expand Up @@ -1548,7 +1557,8 @@ end
-- Call: when condition, result
--
-- Takes any number of condition-result pairs. Checks each condition and when
-- one returns true, returns the result associated with that condition.
-- one returns true, returns the result associated with that condition, calling
-- it with the condition if it is callable.
--
-- Example:
--
Expand Down Expand Up @@ -1576,7 +1586,7 @@ function object:when (...)
if is_true(cond) then
result = args[index + 1]
if is_callable(result) then
result = result(self)
result = result(self, cond)
end
break
end
Expand Down Expand Up @@ -1621,7 +1631,7 @@ function object:when_underequal (...)
result = args[index + 1]

if is_callable(result) then
result = result(self)
result = result(self, cond)
break
end
end
Expand Down Expand Up @@ -2000,7 +2010,7 @@ end
--
-- Truncates number.
function number_instance:to_underi ()
return math.floor(self._lua_number)
return math.floor(self._lua_number)
end

-- Object: number instance
Expand All @@ -2025,6 +2035,33 @@ function number_instance:to_underchar ()
return base_string:new(string.char(self._lua_number))
end

-- Object: number instance
-- Call: number.zero?
-- Returns: boolean
--
-- Test whether number is zero.
function number_instance:zero_question ()
return self._lua_number == 0
end

-- Object: number instance
-- Call: number.one?
-- Returns: boolean
--
-- Test whether number is one.
function number_instance:one_question ()
return self._lua_number == 1
end

-- Object: number instance
-- Call: number.negative?
-- Returns: boolean
--
-- Test whether number is negative.
function number_instance:negative_question ()
return self._lua_number < 0
end

-- This is to use native operations when possible.
local number_native_operations = { _plus = number_instance._plus;
_minus = number_instance._minus;
Expand Down Expand Up @@ -2209,7 +2246,7 @@ function enumerable:select (block)
if type(block) == "table" and block._lua_string then
f = function (_self, item)
local meth

if type(item) == "number" then
meth = number:new(item):get_undermethod(block)
else
Expand Down Expand Up @@ -2744,7 +2781,7 @@ end
-- If start_index is specified, start searching from the given index.
function array_instance:rindex_underof (item, start)
local len = self._length

local k
if start == nil then
k = len
Expand Down Expand Up @@ -3504,7 +3541,7 @@ function array_instance:join (separator, final)

while i <= len do
obj = a[i]

if obj == nil then
obj = object.__null
end
Expand Down Expand Up @@ -3634,7 +3671,7 @@ function array_instance:_equal_equal (rhs)
local tr = type(vr)
local tl = type(vl)

if tr == "table" and
if tr == "table" and
tl == "table" and
vl._is_an_object and
vr._is_an_object and
Expand Down Expand Up @@ -4659,7 +4696,7 @@ end
-- This method can be used to find substrings inside a string matching the
-- given regular expression. An optional start index can be provided.
--
-- If a match is found, an match object is
-- If a match is found, an match object is
function string_instance:match (regx, start_index)
if type(regx) ~= "table" or regx._lua_regex == nil then
error(exception:argument_error("string.match", "regex", tostring(regx)))
Expand Down Expand Up @@ -4832,7 +4869,7 @@ function string_instance:get (start_index, end_index)
start_index = len + start_index
end

local val = string.sub(self._lua_string, start_index + 1, start_index + 1)
local val = string.sub(self._lua_string, start_index + 1, start_index + 1)
if val == nil then
return object.__null
else
Expand Down Expand Up @@ -5230,7 +5267,7 @@ function exception_instance:to_unders ()
end
end

function exception:argument_undererror (method, expected, given)
function exception:argument_undererror (method, expected, given)
return self:new("Argument error: " .. tostring(method) .. " expected " .. tostring(expected) .. " argument(s) but was given " .. tostring(given) .. ".", "argument error")
end

Expand Down
47 changes: 37 additions & 10 deletions test/test-misc.brat
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ add_results setup name: "miscellaneous tests" {
assert_null { when { false } { 'hello' } }
assert_equal "goodbye" { when { false } { 'hello' } { true } { 'goodbye' } }
assert_equal "goodbye" { when false, 'hello', true, 'goodbye' }

assert_equal :hello { when :hello { h | h } }
assert_false null { when false, { h | h } }
assert_equal :goodbye { when false, { h | h }, :goodbye, { h | h } }
}

test "when equal" {
assert_equal 2 { when_equal 2, 2, 2 }
assert_equal 2 { when_equal 2, 2, { 2 } }
assert_equal 2 { when_equal 2, { 2 }, { 2 } }
assert_equal 2 { when_equal { 2 }, { 2 }, { 2 } }
assert_equal 2 { when_equal { 2 }, 2, { 2 } }

assert_equal 2 { when_equal 2, 2, { v | v } }
assert_equal 2 { when_equal 2, { 2 }, { v | v } }
assert_equal 2 { when_equal { 2 }, { 2 }, { v | v } }
assert_equal 2 { when_equal { 2 }, 2, { v | v } }
}

test "random" {
Expand Down Expand Up @@ -75,6 +92,11 @@ add_results setup name: "miscellaneous tests" {
assert_equal 0 { true? true.true?, 0, 1 }
assert_equal 1 { true? false.true?, 0, 1 }
assert_equal 1 { true? null.true?, 0, 1 }

assert_equal 4 { true? 4, { n | n } }
assert_equal 4 { true? 4, { n | n }, { n | n } }
assert_false 4 { true? false { 0 }, { n | n } }
assert_equal :null, { true? null, { 0 } { n | n.to_s } }
}

test "false" {
Expand All @@ -89,6 +111,9 @@ add_results setup name: "miscellaneous tests" {
assert_equal 0 { false? true.false?, 0, 1 }
assert_equal 0 { true? false.false?, 0, 1 }
assert_equal 0 { true? null.false?, 0, 1 }

assert_equal :true { false? true, { 0 } { v | p 'v: ', v; v.to_s } }
assert_equal :false { false? false, { v | v.to_s } { 1 } }
}

test "null" {
Expand All @@ -101,6 +126,9 @@ add_results setup name: "miscellaneous tests" {
assert_false { null? {true? 1}, {0} }
assert_equal 0 { false? null?, 0, 1 }
assert_equal 0 { true? null.null?, 0, 1 }

assert_equal :null { null? null, { n | n.to_s } }
assert_equal 4 { null? 4 { 0 }, { n | n } }
}

test "variable_scope" {
Expand All @@ -123,25 +151,25 @@ add_results setup name: "miscellaneous tests" {
}

test "comments" {
assert_equal 1 { x = 1; #x = 6
assert_equal 1 { x = 1; #x = 6
x }
assert_equal 1 { x = 1; #* x = 6 *#
assert_equal 1 { x = 1; #* x = 6 *#
x }
assert_equal 1 { x = 1; #*
x = 6
*#
assert_equal 1 { x = 1; #*
x = 6
*#
x }
}

test "nested_comments" {
assert_equal 1 { x = 1; #* x = 3 *# }
assert_equal 1 { x = 1; #*
x = 3
x = 3
*# }
assert_equal 1 { x = 1; #*
#*
x = 3
*#
#*
x = 3
*#
*# }
}

Expand Down Expand Up @@ -207,4 +235,3 @@ add_results setup name: "miscellaneous tests" {
assert_false { 1.hash? }
}
}