Skip to content

Commit

Permalink
Revert "Remove syntax sugar for arrays"
Browse files Browse the repository at this point in the history
This reverts commit 5ebf31b.
  • Loading branch information
edubart committed Oct 6, 2019
1 parent e0f209c commit be2107c
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 24 deletions.
8 changes: 6 additions & 2 deletions docs/pages/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ Array is a fixed size array known at compile time:
```nelua
local a1: array(integer, 4) = {1,2,3,4}
local a2: array(integer, 4)
-- syntax sugar
local a3: integer[4] = {1,2,3,4}
local a4 = @integer[4] {1,2,3,4}
```

### Enum
Expand Down Expand Up @@ -611,7 +615,7 @@ local r: range(integer) = 1:10
Span are pointers to a block of contiguous elements at runtime.

```nelua
local arr = @array(integer,4) {1,2,3,4}
local arr = @integer[4] {1,2,3,4}
local s = arr[1:2]
print(s[0], s[1]) -- outputs 2 3
Expand Down Expand Up @@ -1105,7 +1109,7 @@ Nelua can import C functions from C headers:
local function malloc(size: usize): pointer !cimport('malloc','<stdlib.h>') end
local function memset(s: pointer, c: int32, n: usize): pointer !cimport('memset','<stdlib.h>') end
local function free(ptr: pointer) !cimport('free','<stdlib.h>') end
local a = @array(int64,10)*(malloc(10 * 8))
local a = @int64[10]*(malloc(10 * 8))
memset(a, 0, 10*8)
assert(a[0] == 0)
a[0] = 1
Expand Down
2 changes: 1 addition & 1 deletion examples/brainfuck.nelua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'C.stdio'

local data: array(int32,1024)
local data: int32[1024]
local ptr: int32 = 0

[##[
Expand Down
6 changes: 3 additions & 3 deletions examples/condots.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ local SDL_Surface = @record{
pitch: cint,
pixels: pointer
}
local SDL_Event = @record{type: uint32, padding: array(byte,56)}
local SDL_Event = @record{type: uint32, padding: byte[56]}
local SDL_Rect = @record{x: cint, y: cint, w: cint, h: cint}
local SDL_WindowPtr = @record{}*
local SDL_RendererPtr = @record{}*
Expand Down Expand Up @@ -116,15 +116,15 @@ local window, renderer, texture

local SCREEN_WIDTH: int32 !compconst = 1280
local SCREEN_HEIGHT: int32 !compconst = 720
local pixels: array(array(vec4b,SCREEN_WIDTH),SCREEN_HEIGHT) !aligned(16)
local pixels: vec4b[SCREEN_WIDTH][SCREEN_HEIGHT] !aligned(16)

local NUM_PARTICLES !compconst = 200
local RADIUS !compconst = 3
local MIN_DISTANCE !compconst = 40
local MAX_DISTANCE !compconst = 80
local MIN_DISTANCE2 !compconst = 1600--MIN_DISTANCE * MIN_DISTANCE
local MAX_DISTANCE2 !compconst = 6400--MAX_DISTANCE * MAX_DISTANCE
local particles: array(Particle,NUM_PARTICLES)
local particles: Particle[NUM_PARTICLES]

do
for i=0,<NUM_PARTICLES do
Expand Down
2 changes: 1 addition & 1 deletion examples/mersenne.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ end

-- generates a random number on [0, 0xffffffff]
function mt19937:random_uint32()
local MAG01: array(uint32,2) = {0x0, 0x9908b0df}
local MAG01: uint32[2] = {0x0, 0x9908b0df}
local LOWER_MASK !compconst = 0x7fffffff_u32
local UPPER_MASK !compconst = 0x80000000_u32
local N !compconst, M !compconst = MT19937_N, 397_u32
Expand Down
4 changes: 2 additions & 2 deletions examples/snakesdl.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-- import SDL structures
local SDL_Event !cimport = @record {
type: uint32,
padding: array(byte,56)
padding: byte[56]
}
local SDL_Keysym !cimport = @record {
scancode: cint,
Expand Down Expand Up @@ -87,7 +87,7 @@ local quit = false
local nextmove
local score
local headpos, tailpos, applepos
local tiles: array(array(Direction,GRID_SIZE),GRID_SIZE)
local tiles: Direction[GRID_SIZE][GRID_SIZE]

local function move_point(pos: Point2D, dir: Direction)
switch dir
Expand Down
4 changes: 2 additions & 2 deletions lib/math.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ end

-- Pseudo Random Number Generator based on xoshiro256**

local xoshiro256 = @record{state: array(uint64,4)}
local xoshiro256 = @record{state: uint64[4]}

local FLOAT64_MANT_DIGS !compconst = 53

Expand All @@ -82,7 +82,7 @@ local function rotl(x: uint64, n: int32) !inline
end

function xoshiro256:nextrand(): uint64
local state: array(uint64,4) = {
local state: uint64[4] = {
self.state[0],
self.state[1],
self.state[2] ~ self.state[0],
Expand Down
2 changes: 1 addition & 1 deletion lib/os.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ end
function os.tmpname(): string
--TODO: windows implementation without POSIX
local TMPBUFSIZE !compconst = 32
local buf: array(cchar,TMPBUFSIZE)
local buf: cchar[TMPBUFSIZE]
strncpy(&buf[0], "/tmp/lua_XXXXXX", TMPBUFSIZE)
local fd = mkstemp(&buf[0])
if fd == -1 then
Expand Down
3 changes: 2 additions & 1 deletion nelua/syntaxdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ local function get_parser(std)
ppexpr
unary_typexpr_op <-
{| {} %MUL -> 'PointerType' |}
{| {} %MUL -> 'PointerType' |} /
{| {} %LBRACKET -> 'ArrayType' cnil typexpr_param_expr eRBRACKET |}
func_type <- (
{} '' -> 'FuncType'
Expand Down
6 changes: 3 additions & 3 deletions spec/02-syntaxdefs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1329,12 +1329,12 @@ describe("type expression", function()
{ n.IdDecl{'a', n.ArrayType{n.Type{'integer'},
n.BinaryOp{"shr", n.Number{"dec", "2"}, n.Number{"dec", "1"}}}}}
}}})
assert.parse_ast(nelua_parser, "local a: array(integer,10)",
assert.parse_ast(nelua_parser, "local a: integer[10]",
n.Block{{
n.VarDecl{'local',
{ n.IdDecl{'a', n.ArrayType{n.Type{'integer'}, n.Number{'dec', '10'}}}}
}}})
assert.parse_ast(nelua_parser, "local a: array(array(integer,10),10)",
assert.parse_ast(nelua_parser, "local a: integer[10][10]",
n.Block{{
n.VarDecl{'local',
{ n.IdDecl{'a',
Expand Down Expand Up @@ -1428,7 +1428,7 @@ describe("type expression", function()
}}})
end)
it("complex types", function()
assert.parse_ast(nelua_parser, "local p: array(array(integer*,10)*,10)",
assert.parse_ast(nelua_parser, "local p: integer*[10]*[10]",
n.Block{{
n.VarDecl{'local',
{ n.IdDecl{'p',
Expand Down
2 changes: 1 addition & 1 deletion spec/03-typechecker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ end)

it("range indexing", function()
assert.ast_type_equals([[
local a: array(integer,8)
local a: integer[8]
local s = a[0:3]
local s2 = s[0:1]
]],[[
Expand Down
6 changes: 3 additions & 3 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ it("ranges", function()
span2[9] = 3
assert(buff[0] == 2 and buff[9] == 3)
local arr = @array(integer,4) {1,2,3,4}
local arr = @integer[4] {1,2,3,4}
local s = arr[1:2]
assert(s[0] == 2 and s[1] == 3)
]])
Expand Down Expand Up @@ -980,7 +980,7 @@ it("sizeof builtin", function()
assert(#@int16 == 2)
assert(#@int32 == 4)
assert(#@int64 == 8)
assert(#@array(int32,4) == 16)
assert(#@int32[4] == 16)
local A = @record{
s: int16, -- 2
Expand All @@ -990,7 +990,7 @@ it("sizeof builtin", function()
-- 3 pad
}
assert(#A == 12)
assert(#@array(A,8) == 96)
assert(#@A[8] == 96)
local B = @record{
i: int32, -- 4
Expand Down
8 changes: 4 additions & 4 deletions spec/06-preprocessor_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ it("evaluate expressions", function()
local e = 1
]])
assert.ast_type_equals([=[
local a: array(integer,10)
local a: integer[10]
a[#[0]] = 1
]=], [[
local a: array(integer,10)
local a: integer[10]
a[0] = 1
]])
assert.analyze_error("local a = #[function() end]", "unable to convert preprocess value of type")
Expand Down Expand Up @@ -181,7 +181,7 @@ it("print types", function()
local n: float64
local s: string
local b: boolean
local a: array(int64,2)
local a: int64[2]
local function f(a: int64, b: int64): int64, int64 return 0,0 end
local function g(a: boolean | string) end
local R: type = @record{a: integer, b: integer}
Expand All @@ -201,7 +201,7 @@ it("print types", function()
local n: float64
local s: string
local b: boolean
local a: array(int64,2)
local a: int64[2]
local function f(a: int64, b: int64): int64, int64 return 0,0 end
local function g(a: boolean | string) end
local R: type = @record{a: integer, b: integer}
Expand Down

0 comments on commit be2107c

Please sign in to comment.