Skip to content

Commit

Permalink
Deprecate range type (was unused)
Browse files Browse the repository at this point in the history
It's syntax was questionable because the colon (':') token is
overused already and causes conflicts and confusion.
Also it wasn't used anywhere and removing this makes the language simpler.
The range type idea may be revised in the future as an extension,
it was inspired by some other languages that does have them to quickly
access array and multidimensional sub ranges.
  • Loading branch information
edubart committed Jul 20, 2020
1 parent e29d747 commit 4fbd00e
Show file tree
Hide file tree
Showing 11 changed files with 3 additions and 121 deletions.
13 changes: 1 addition & 12 deletions docs/pages/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Global symbols are visible in other source files, they can only be declared in t

```nelua
global global_a = 1
global function f()
global function global_f()
end
```

Expand Down Expand Up @@ -554,17 +554,6 @@ local i: pointer(integer) -- pointer to an integer
local i: integer*
```

### Range

Ranges are used to specifying ranges for spans.

```nelua
local ra = 1:10
print(ra.low, ra.high) -- outputs: 1 10
local rb: range(integer) = 0:100
print(rb.low, rb.high) -- outputs: 0 100
```

### Span

Span are pointers to a block of contiguous elements which size is known at runtime.
Expand Down
7 changes: 0 additions & 7 deletions examples/overview.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,6 @@ do -- Record
print(d.name, d.age)
end

do -- Range
local ra = 1:10
print(ra.low, ra.high) -- outputs: 1 10
local rb: range(integer) = 0:100
print(rb.low, rb.high) -- outputs: 0 100
end

do -- Span
local arr = (@integer[4]) {1,2,3,4}
local s: span(integer) = &arr
Expand Down
13 changes: 0 additions & 13 deletions nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -604,19 +604,6 @@ function visitors.EnumType(context, node)
attr.value = types.EnumType(node, subtype, fields)
end

function visitors.RangeType(context, node)
local attr = node.attr
if attr.type then return end
local subtypenode = node[1]
context:traverse_node(subtypenode)
local subtype = subtypenode.attr.value
if not subtype.is_integral then
subtypenode:raisef("range subtype '%s' is not an integral type", subtype)
end
attr.type = primtypes.type
attr.value = types.RangeType(node, subtype)
end

function visitors.ArrayType(context, node)
local attr = node.attr
if attr.type then return end
Expand Down
3 changes: 0 additions & 3 deletions nelua/astdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ astbuilder:register('ArrayType', {
astbuilder:register('PointerType', {
ntypes.Node:is_optional(), -- subtype typexpr
})
astbuilder:register('RangeType', {
ntypes.Node, -- subtype typexpr
})
astbuilder:register('GenericType', {
stypes.string + ntypes.PreprocessName, -- generic name
stypes.array_of(ntypes.Node), -- list of typexpr or param expr
Expand Down
10 changes: 0 additions & 10 deletions nelua/cbuiltins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -688,16 +688,6 @@ function operators.len(_, emitter, argnode)
end --luacov:enable
end

function operators.range(node, emitter, lnode, rnode)
local subtype = node.attr.type.subtype
emitter:add_ctypecast(node.attr.type)
emitter:add('{')
emitter:add_val2type(subtype, lnode)
emitter:add(',')
emitter:add_val2type(subtype, rnode)
emitter:add('}')
end

local inlines = {}
cbuiltins.inlines = inlines

Expand Down
1 change: 0 additions & 1 deletion nelua/cdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ cdefs.binary_ops = {
['idiv'] = true,
['pow'] = true,
['mod'] = true,
['range'] = true,
--TODO: concat
}

Expand Down
10 changes: 2 additions & 8 deletions nelua/syntaxdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ local function get_parser()
expr6 <- ({} ''->'BinaryOp' {| expr7 (op_band expr7 )* |}) -> to_chain_binary_op
expr7 <- ({} ''->'BinaryOp' {| expr8 (op_bshift expr8 )* |}) -> to_chain_binary_op
expr8 <- ({} ''->'BinaryOp' expr9 (op_concat expr8 )? ) -> to_binary_op
expr9 <- ({} ''->'BinaryOp' expr10 (op_range expr9 )? ) -> to_binary_op
expr10 <- ({} ''->'BinaryOp' {| expr11 (op_add expr11)* |}) -> to_chain_binary_op
expr9 <- expr10 -- (free op slot, range operation was removed from here)
expr10 <- ({} ''->'BinaryOp' {| expr11 (op_add expr11)* |}) -> to_chain_binary_op
expr11 <- ({} ''->'BinaryOp' {| expr12 (op_mul expr12)* |}) -> to_chain_binary_op
expr12 <- ({} ''->'UnaryOp' {| op_unary* |} expr13) -> to_chain_unary_op
expr13 <- ({} ''->'BinaryOp' simple_expr (op_pow expr12)? ) -> to_binary_op
Expand Down Expand Up @@ -466,7 +466,6 @@ local function get_parser()
record_type /
enum_type /
array_type /
range_type /
pointer_type /
generic_type /
primtype /
Expand Down Expand Up @@ -501,10 +500,6 @@ local function get_parser()
enumfield <- ({} '' -> 'EnumFieldType'
name (%ASSIGN eexpr)?
) -> to_astnode
range_type <- (
{} 'range' -> 'RangeType'
%LPAREN etypexpr eRPAREN
) -> to_astnode
array_type <- (
{} 'array' -> 'ArrayType'
%LPAREN etypexpr eCOMMA etype_param_expr eRPAREN
Expand Down Expand Up @@ -563,7 +558,6 @@ local function get_parser()
op_deref
op_deref <- %DEREF -> 'deref'
op_pow <- %POW -> 'pow'
op_range <- %COLON -> 'range' ![({"']
]])

-- syntax expected captures with errors
Expand Down
34 changes: 0 additions & 34 deletions nelua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -678,11 +678,6 @@ local function integral_shift_operation(ltype, rtype)
return ltype
end

local function integral_range_operation(ltype, rtype, lattr, rattr)
local subtype = integral_arithmetic_operation(ltype, rtype, lattr, rattr)
return types.RangeType(nil, subtype)
end

local function make_integral_binary_opfunc(optypefunc, opvalfunc)
return function(ltype, rtype, lattr, rattr)
local retype, err = optypefunc(ltype, rtype, lattr, rattr)
Expand Down Expand Up @@ -780,8 +775,6 @@ IntegralType.binary_operators.shr = make_integral_binary_opfunc(integral_shift_o
return t:normalize_value(a >> b)
end)

IntegralType.binary_operators.range = integral_range_operation

--------------------------------------------------------------------------------
local FloatType = typeclass(ArithmeticType)
types.FloatType = FloatType
Expand Down Expand Up @@ -1439,33 +1432,6 @@ StringViewType.binary_operators.concat = function(ltype, rtype, lattr, rattr)
end
end

--------------------------------------------------------------------------------
local RangeType = typeclass(RecordType)
types.RangeType = RangeType
RangeType.is_range = true

function RangeType:_init(node, subtype)
local fields = {
{name = 'low', type = subtype},
{name = 'high', type = subtype}
}
RecordType._init(self, node, fields)
self.name = 'range'
self:set_codename(subtype.codename .. '_range')
self.metatype = MetaType()
self.subtype = subtype
end

function RangeType:is_equal(type)
return type.name == self.name and
getmetatable(type) == getmetatable(self) and
type.subtype == self.subtype
end

function RangeType:typedesc()
return sstream(self.name, '(', self.subtype, ')'):tostring()
end

--------------------------------------------------------------------------------
local ConceptType = typeclass()
types.ConceptType = ConceptType
Expand Down
7 changes: 0 additions & 7 deletions spec/02-syntaxdefs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1414,13 +1414,6 @@ describe("type expression", function()
{ n.IdDecl{'p', n.PointerType{n.PointerType{n.Type{'integer'}}}}}
}}})
end)
it("range type", function()
assert.parse_ast(nelua_parser, "local r: range(integer)",
n.Block{{
n.VarDecl{'local',
{ n.IdDecl{'r', n.RangeType{n.Type{'integer'}}}}
}}})
end)
it("generic type", function()
assert.parse_ast(nelua_parser, "local r: somegeneric(integer, 4)",
n.Block{{
Expand Down
15 changes: 0 additions & 15 deletions spec/03-typechecker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -765,21 +765,6 @@ it("spans", function()
assert.analyze_error([[require 'span' local a: span(void) ]], 'spans cannot be of')
end)

it("ranges", function()
assert.analyze_ast([[
local a: range(integer)
local low, high = a.low, a.high
local b = 1:2
low, high = b.low, b.high
]])
assert.ast_type_equals([[
local a = 1_u8:2_u16
]],[[
local a: range(uint16) = 1_u8:2_u16
]])
assert.analyze_error([[local a: range(stringview) ]], 'is not an integral type')
end)

it("arrays", function()
--assert.analyze_ast([[local a: array(integer, (2 << 1)) ]])
assert.analyze_ast([[local N <comptime> = 10; local a: array(integer, N) ]])
Expand Down
11 changes: 0 additions & 11 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1207,17 +1207,6 @@ it("cstring and string", function()
]], "hello\nhello")
end)

it("ranges", function()
assert.run_c([[
local a: range(integer)
assert(a.low == 0 and a.high == 0)
a = (2:3)
assert(a.low == 2 and a.high == 3)
a = (-1:0)
assert(a.low == -1 and a.high == 0)
]])
end)

it("arrays", function()
assert.generate_c(
"local a: array(boolean, 10)",
Expand Down

0 comments on commit 4fbd00e

Please sign in to comment.