Skip to content

Commit

Permalink
Refactor is functions from type class
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Feb 16, 2020
1 parent 21f9b66 commit 6715118
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 372 deletions.
6 changes: 3 additions & 3 deletions docs/pages/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -1025,15 +1025,15 @@ the preprocessor:

```nelua
local function pow(x: auto, n: integer)
## staticassert(x.type:is_arithmetic(), 'cannot pow variable of type "%s"', x.type)
## if x.type:is_integral() then
## staticassert(x.type.is_arithmetic, 'cannot pow variable of type "%s"', x.type)
## if x.type.is_integral then
-- x is an integral type (any unsigned/signed integer)
local r: #[x.type]# = 1
for i=1,n do
r = r * x
end
return r
## elseif x.type:is_float() then
## elseif x.type.is_float then
-- x is a floating point type
return x ^ n
## end
Expand Down
6 changes: 3 additions & 3 deletions examples/overview.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -497,15 +497,15 @@ end

do -- Preprocessing lazy functions
local function pow(x: auto, n: integer)
## staticassert(x.type:is_arithmetic(), 'cannot pow variable of type "%s"', x.type)
## if x.type:is_integral() then
## staticassert(x.type.is_arithmetic, 'cannot pow variable of type "%s"', x.type)
## if x.type.is_integral then
-- x is an integral type (any unsigned/signed integer)
local r: #[x.type]# = 1
for i=1,n do
r = r * x
end
return r
## elseif x.type:is_float() then
## elseif x.type.is_float then
-- x is a floating point type
return x ^ n
## end
Expand Down
2 changes: 1 addition & 1 deletion examples/record_inheretance.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ end

##[[
local function check_record_type(sym)
staticassert(sym and sym.type and sym.type:is_type() and sym.value:is_record(),
staticassert(sym and sym.type and sym.type.is_type and sym.value.is_record,
"symbol '%s' must be a type holding a record type", sym.name)
return sym.value
end
Expand Down
8 changes: 4 additions & 4 deletions lib/allocators/interface.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
end

function allocator.new(T: type, size: auto)
## if not size.type:is_nil() then
## staticassert(size.type:is_integral(), 'allocator.new: size must be an integral type')
## if not size.type:is_unsigned() then
## if not size.type.is_nil then
## staticassert(size.type.is_integral, 'allocator.new: size must be an integral type')
## if not size.type.is_unsigned then
check(size > 0, 'allocator.new: size must be greater than 0')
## end
return allocator.spanalloc0(T, (@usize)(size))
Expand All @@ -67,7 +67,7 @@
end

function allocator.delete(s: auto)
## staticassert(s.type:is_pointer() or s.type.is_span, "allocator.delete: invalid type '%s'", s.type)
## staticassert(s.type.is_pointer or s.type.is_span, "allocator.delete: invalid type '%s'", s.type)
## if s.type.is_span then
allocator.spandealloc(s)
## else -- pointer
Expand Down
42 changes: 21 additions & 21 deletions lib/math.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ global math.mininteger: integer <comptime> = -9223372036854775808

-- concepts
local is_arithmetic = #[concept(function(x)
return x.type:is_arithmetic()
return x.type.is_arithmetic
end)]#
local is_optional_arithmetic = #[concept(function(x)
return x.type:is_arithmetic() or x.type:is_nil()
return x.type.is_arithmetic or x.type.is_nil
end)]#

-- compile time utilities
##[[
local function choose_float_type(x)
if x.type:is_float32() then
if x.type.is_float32 then
return primtypes.float32
else
return primtypes.float64
Expand Down Expand Up @@ -74,9 +74,9 @@ for _,name in ipairs(mathfuncs1) do
local namef64, namef32 = name..'_f64', name..'_f32'
]]
function math.#(name)#(x: is_arithmetic) <inline,nosideeffect>
## if x.type:is_float32() then
## if x.type.is_float32 then
return #(namef32)#(x)
## elseif x.type:is_float() then
## elseif x.type.is_float then
return #(namef64)#(x)
## else
## if name == 'abs' then
Expand All @@ -97,9 +97,9 @@ for _,name in ipairs(mathfuncs2) do
local namef64, namef32 = name..'_f64', name..'_f32'
]]
function math.#(name)#(x: is_arithmetic, y: is_arithmetic) <inline,nosideeffect>
## if x.type:is_float32() then
## if x.type.is_float32 then
return #(namef32)#(x, y)
## elseif x.type:is_float() then
## elseif x.type.is_float then
return #(namef64)#(x, y)
## else
## if name == 'min' then
Expand All @@ -114,14 +114,14 @@ end
## end

function math.atan(y: is_arithmetic, x: is_optional_arithmetic) <inline,nosideeffect>
## if not x.type:is_nil() then
## if y.type:is_float32() and x.type:is_float32() then
## if not x.type.is_nil then
## if y.type.is_float32 and x.type.is_float32 then
return atan2_f32(y, x)
## else
return atan2_f64(y, x)
## end
## else
## if y.type:is_float32() then
## if y.type.is_float32 then
return atan_f32(y)
## else
return atan_f64(y)
Expand All @@ -130,9 +130,9 @@ function math.atan(y: is_arithmetic, x: is_optional_arithmetic) <inline,nosideef
end

function math.log(x: is_arithmetic, base: is_optional_arithmetic) <inline,nosideeffect>
## local suffix = x.type:is_float32() and '_f32' or '_f64'
## local suffix = x.type.is_float32 and '_f32' or '_f64'
## local log2, log10, log = 'log2'..suffix, 'log10'..suffix, 'log'..suffix
## if not base.type:is_nil() then
## if not base.type.is_nil then
if base == 2 then
return #(log2)#(x)
elseif base == 10 then
Expand Down Expand Up @@ -161,7 +161,7 @@ function math.modf(x: is_arithmetic) <inline,nosideeffect>
local R: type = #[choose_float_type(x)]#
local i: R
local f: R <noinit>
## if x.type:is_float32() then
## if x.type.is_float32 then
f = modf_f32(x, &i)
## else
f = modf_f64(x, &i)
Expand All @@ -170,9 +170,9 @@ function math.modf(x: is_arithmetic) <inline,nosideeffect>
end

function math.tointeger(x: is_arithmetic) <inline,nosideeffect>
## if x.type:is_integral() then
## if x.type.is_integral then
return (@integer)(x)
## elseif x.type:is_float() then
## elseif x.type.is_float then
local r = (@integer)(x)
if likely(x == r) then
return r
Expand All @@ -185,19 +185,19 @@ function math.tointeger(x: is_arithmetic) <inline,nosideeffect>
end

function math.type(x: is_arithmetic) <inline,nosideeffect>
## if x.type:is_float() then
## if x.type.is_float then
return 'float'
## elseif x.type:is_integral() then
## elseif x.type.is_integral then
return 'integer'
## else
return nil
## end
end

function math.ult(m: is_arithmetic, n: is_arithmetic): boolean <inline>
## if m.type:is_integral() and n.type:is_integral() then
## if m.type.is_integral and n.type.is_integral then
return (@uinteger)(m) < (@uinteger)(n)
## elseif x.type:is_float() then
## elseif x.type.is_float then
local mi, ni = (@integer)(m), (@integer)(n)
if likely(mi == m and ni == n) then
return (@uinteger)(mi) < (@uinteger)(ni)
Expand Down Expand Up @@ -257,7 +257,7 @@ end

function math.random(m: is_optional_arithmetic, n: is_optional_arithmetic) <inline>
local r: number = default_random:random()
## if not m.type:is_nil() and not n.type:is_nil() then
## if not m.type.is_nil and not n.type.is_nil then
local low: integer, up: integer = (@integer)(m), (@integer)(n)
if not likely(low < up) then
panic("random: interval is empty")
Expand All @@ -266,7 +266,7 @@ function math.random(m: is_optional_arithmetic, n: is_optional_arithmetic) <inli
end
r = r * ((@number)(up - low) + 1.0)
return (@integer)(r) + low
## elseif not m.type:is_nil() then
## elseif not m.type.is_nil then
local low: integer = (@integer)(m)
r = r * (@number)(low)
return (@integer)(r)
Expand Down
2 changes: 1 addition & 1 deletion lib/sequence.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
impl: SequenceImplT*
}

## SequenceT.value.contiguous = true
## SequenceT.value.is_contiguous = true
## SequenceT.value.is_sequence = true
## SequenceT.value.subtype = T

Expand Down
4 changes: 2 additions & 2 deletions lib/span.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
size: usize
}

## SpanT.value.contiguous = true
## SpanT.value.is_contiguous = true
## SpanT.value.is_span = true
## SpanT.value.subtype = T

Expand All @@ -34,7 +34,7 @@

-- Initializes span from pointers to other arrays.
function SpanT.__convert(values: #[concept(function(x)
if x.type:is_pointer() and x.type.subtype and x.type.subtype:is_contiguous_of(T) then
if x.type.is_pointer and x.type.subtype and x.type.subtype:is_contiguous_of(T) then
return true
end
end)]#): SpanT <inline>
Expand Down
2 changes: 1 addition & 1 deletion lib/vector.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
data: span(T)
}

## VectorT.value.contiguous = true
## VectorT.value.is_contiguous = true
## VectorT.value.is_vector = true
## VectorT.value.subtype = T

Expand Down
Loading

0 comments on commit 6715118

Please sign in to comment.