Added vulgar_fraction function, and increased resilience.
This commit is contained in:
parent
0ab7942023
commit
7ccb9e8123
15
number.lua
15
number.lua
|
@ -1,6 +1,10 @@
|
|||
|
||||
local utf8 = require "utf8" -- FIXME: I don't really like this. Either have a local copy of the files, or remove the functionallity requiring it.
|
||||
|
||||
-- Make sure we imported the correct library.
|
||||
local CAN_USE_SHORTHAND = not not utf8.width
|
||||
if not CAN_USE_SHORTHAND then utf8.width = function(a) return #a end end
|
||||
|
||||
-- Constants
|
||||
|
||||
local MAXIMUM_INT = 2^53 -- The maximum double for where all integers can be represented exactly.
|
||||
|
@ -9,6 +13,11 @@ local MAXIMUM_ZERO = 10^-7 -- Used when attempting to determine fraction. Anyt
|
|||
--------------------------------------------------------------------------------
|
||||
-- Util
|
||||
|
||||
local function vulgar_fraction (a, b)
|
||||
-- Returns a string representing the fraction a/b, with unicode characters.
|
||||
return utf8.superscript(a) .. '⁄' .. utf8.subscript(b)
|
||||
end
|
||||
|
||||
local function is_integer (n)
|
||||
-- Predicate. Neither +inf, -inf nor nan are integer.
|
||||
return n == math.floor(n) and n ~= 1/0 and n ~= -1/0
|
||||
|
@ -68,8 +77,8 @@ local SPECIAL_NUMBER = {
|
|||
-- x = a/b
|
||||
{ est = calculate_fraction,
|
||||
real = function (a, b) return b ~= 1 and (a/b) end,
|
||||
repr = function (a, b) return format_num(a)..'/'..format_num(b) end,
|
||||
short = function (a, b) return format_num(a)..'/'..format_num(b) end,
|
||||
repr = function (a, b) return a..'/'..b end,
|
||||
short = function (a, b) return a..'/'..b end,
|
||||
},
|
||||
-- Factorial
|
||||
{ est = function (n) return inverse_factorial(n) end,
|
||||
|
@ -155,5 +164,5 @@ end
|
|||
|
||||
return function (value, options, depth, l)
|
||||
-- TODO: Add support for more relaxed representations.
|
||||
l[#l+1] = format_num(value, options.math_shorthand)
|
||||
l[#l+1] = format_num(value, CAN_USE_SHORTHAND and options.math_shorthand)
|
||||
end
|
||||
|
|
37
pretty.lua
37
pretty.lua
|
@ -5,18 +5,25 @@
|
|||
local format_number, format_function, analyze_structure, get_table_info, TABLE_TYPE
|
||||
do
|
||||
local thispath = ... and select('1', ...):match('.+%.') or ''
|
||||
local function import (name, ignore_failure)
|
||||
local was_loaded, lib_or_error = pcall(require, thispath..name)
|
||||
if not was_loaded then
|
||||
if ignore_failure then return nil end
|
||||
error('[pretty]: Could not load vital library: '..name..'.lua:\n\t'..lib_or_error)
|
||||
end
|
||||
return lib_or_error
|
||||
end
|
||||
|
||||
-- Load number and function formatting
|
||||
format_number = select(2, pcall(require, thispath..'number'))
|
||||
format_function = select(2, pcall(require, thispath..'function'))
|
||||
-- Will use a very simple number formatting, if number.lua is not available.
|
||||
-- Will use a very simple function formatting, if function.lua is not available.
|
||||
format_number = import('number', true) or function (value, _, _, l) l[#l+1] = tostring(value) end
|
||||
format_function = import('function', true) or function (value, _, _, l) l[#l+1] = 'function (...) --[['..tostring(value):sub(11)..']] end' end
|
||||
|
||||
-- Load other stuff
|
||||
local was_loaded
|
||||
was_loaded, analyze_structure = pcall(require, thispath..'analyze_structure')
|
||||
analyze_structure = import 'analyze_structure'
|
||||
analyze_structure, get_table_info = analyze_structure[1], analyze_structure[2]
|
||||
assert(was_loaded, '[pretty]: Could not load vital library: analyze_structure')
|
||||
was_loaded, TABLE_TYPE = pcall(require, thispath..'table_type')
|
||||
assert(was_loaded, '[pretty]: Could not load vital library: table_type')
|
||||
TABLE_TYPE = import 'table_type'
|
||||
end
|
||||
--
|
||||
|
||||
|
@ -251,7 +258,7 @@ local function fix_seperator_info (l, indent_char, max_depth)
|
|||
local depth, inline_depth = 0, nil
|
||||
for i = 1, #l do
|
||||
--print(i, l[i], depth, inline_depth)
|
||||
|
||||
|
||||
if type(l[i]) ~= 'table' then
|
||||
-- Do nothing
|
||||
elseif l[i][1] == 'seperator' then
|
||||
|
@ -431,13 +438,6 @@ local function format_string (str, options, depth, l)
|
|||
l[#l+1] = right
|
||||
end
|
||||
|
||||
if not format_number then
|
||||
-- Very simple number formatting, if number.lua is not available.
|
||||
format_number = function (value, _, _, l)
|
||||
l[#l+1] = tostring(value)
|
||||
end
|
||||
end
|
||||
|
||||
local function format_coroutine (value, options, depth, l)
|
||||
l[#l+1] = coroutine.status(value)
|
||||
l[#l+1] = ' coroutine: '
|
||||
|
@ -448,13 +448,6 @@ local function format_primitive (value, options, depth, l)
|
|||
l[#l+1] = tostring(value)
|
||||
end
|
||||
|
||||
if not format_function then
|
||||
-- Very simple function formatting, if function.lua is not available.
|
||||
format_function = function (value, _, _, l)
|
||||
l[#l+1] = 'function (...) --[['..tostring(value):sub(11)..']] end'
|
||||
end
|
||||
end
|
||||
|
||||
local TYPE_TO_FORMAT_FUNC = {
|
||||
['nil'] = format_primitive,
|
||||
['boolean'] = format_primitive,
|
||||
|
|
|
@ -42,6 +42,14 @@ SUITE:addTest('Dont allow bad types in options', function ()
|
|||
end)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Survive not loading libs
|
||||
|
||||
|
||||
-- TODO: Find a way to test this.
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Strange standard libs
|
||||
|
||||
SUITE:addTest('no_std_lib', function ()
|
||||
-- This tests whether one could load the library with an empty env, without
|
||||
|
|
Loading…
Reference in New Issue
Block a user