diff --git a/docs/pages/overview.md b/docs/pages/overview.md index 699332a1..1436de78 100644 --- a/docs/pages/overview.md +++ b/docs/pages/overview.md @@ -536,7 +536,7 @@ Array is a list with where its size is fixed and known at compile-time: local a: array(integer, 4) = {1,2,3,4} print(a[0], a[1], a[2], a[3]) -- outputs: 1 2 3 4 -local b: integer[4] -- "integer[4]" is syntax sugar for "array(integer, 4)" +local b: [4]integer -- "[4]integer" is syntax sugar for "array(integer, 4)" print(b[0], b[1], b[2], b[3]) -- outputs: 0 0 0 0 local len = #b -- get the length of the array, should be 4 print(len) -- outputs: 4 @@ -635,7 +635,7 @@ local p: pointer --a generic pointer to anything, initialized to nilptr local i: pointer(integer) -- pointer to an integer -- syntax sugar -local i: integer* +local i: *integer ``` Pointers are straight translated to C raw pointers. @@ -697,7 +697,7 @@ are pointers to a block of contiguous elements which size is known at runtime: ```nelua require 'span' -local arr = (@integer[4]) {1,2,3,4} +local arr = (@[4]integer) {1,2,3,4} local s: span(integer) = &arr print(s[0], s[1]) -- outputs: 1 2 print(#s) -- outputs 4 @@ -1013,13 +1013,13 @@ For defining or calling a method the colon token `:` must be used, just like in local Rect = @record{x: number, y: number, w: number, h: number} function Rect:translate(x: number, y: number) - -- 'self' here is of the type 'Rect*' + -- 'self' here is of the type '*Rect' self.x = self.x + x self.y = self.y + y end function Rect:area() - -- 'self' here is of the type 'Rect*' + -- 'self' here is of the type '*Rect' return self.w * self.h end @@ -1175,7 +1175,7 @@ require 'memory' require 'allocators.gc' local Person = @record{name: string, age: integer} -local p: Person* = gc_allocator:new(@Person) +local p: *Person = gc_allocator:new(@Person) p.name = "John" p.age = 20 print(p.name, p.age) @@ -1200,7 +1200,7 @@ require 'memory' require 'allocators.general' local Person = @record{name: string, age: integer} -local p: Person* = general_allocator:new(@Person) -- allocate the appropriate size for Person +local p: *Person = general_allocator:new(@Person) -- allocate the appropriate size for Person p.name = tostring("John") -- another allocation here p.age = 20 print(p.name, p.age) @@ -1230,7 +1230,7 @@ The compiler can perform automatic referencing or dereferencing for records or a ```nelua local Person = @record{name: stringview, age: integer} local p: Person = {"John", 20} -local p_ref: Person* = p -- the referencing with `&` is implicit here +local p_ref: *Person = p -- the referencing with `&` is implicit here local p_copy: Person = p_ref -- the dereferencing with `$` is implicit here ``` @@ -1239,7 +1239,7 @@ For instance, the above code is equivalent to: ```nelua local Person = @record{name: stringview, age: integer} local p: Person = {"John", 20} -local p_ref: Person* = &p +local p_ref: *Person = &p local p_copy: Person = $p_ref ``` @@ -1250,7 +1250,7 @@ but permits auto referencing when doing method calls: local Person = @record{name: stringview, age: integer} -- note that this function only accept pointers -function Person.print_info(self: Person*) +function Person.print_info(self: *Person) print(self.name, self.age) end @@ -1463,7 +1463,7 @@ local a = #[create_sequence(integer, 10)]# The above code compile exactly as: ```nelua -local a = (@integer[10]){1,2,3,4,5,6,7,8,9,10} +local a = (@[10]integer){1,2,3,4,5,6,7,8,9,10} ``` ### Code blocks as arguments to preprocessor functions @@ -1729,7 +1729,7 @@ It's hard to explain in words, lets view a full example: -- Define a record using T and MaxSize compile-time parameters. local FixedStackArrayT = @record { - data: T[MaxSize], + data: [MaxSize]T, size: isize } @@ -1982,7 +1982,7 @@ local function sum_container(container: indexable_concept) end -- We create our customized array type. -local MyArray = @record {data: integer[10]} +local MyArray = @record {data: [10]integer} function MyArray:__index(i: integer) return self.data[i] end @@ -1990,7 +1990,7 @@ function MyArray:__len() return #self.data end -local a: integer[10] = {1,2,3,4,5,6,7,8,9,10} +local a: [10]integer = {1,2,3,4,5,6,7,8,9,10} local b: MyArray = {data = a} -- sum_container can be called with 'a' because it matches the concept diff --git a/examples/brainfuck.nelua b/examples/brainfuck.nelua index 5e8662f3..e9e05a3d 100644 --- a/examples/brainfuck.nelua +++ b/examples/brainfuck.nelua @@ -1,6 +1,6 @@ require 'C.stdio' -local data: int32[1024] +local data: [1024]int32 local ptr: int32 = 0 ##[=[ diff --git a/examples/condots.nelua b/examples/condots.nelua index 632f09f8..6d6c3088 100644 --- a/examples/condots.nelua +++ b/examples/condots.nelua @@ -33,7 +33,7 @@ local SDL_Surface = @record{ pitch: cint, pixels: pointer } -local SDL_Event = @record{type: uint32, padding: byte[56]} +local SDL_Event = @record{type: uint32, padding: [56]byte} local SDL_Rect = @record{x: cint, y: cint, w: cint, h: cint} local SDL_Window = @record{} local SDL_Renderer = @record{} @@ -64,30 +64,30 @@ local SDL_TEXTUREACCESS_STREAMING: cint -- import SDL functions local function SDL_Init(flags: uint32): int32 end -local function SDL_CreateWindow(title: cstring, x: cint, y: cint, w: cint, h: cint, flags: uint32): SDL_Window* end +local function SDL_CreateWindow(title: cstring, x: cint, y: cint, w: cint, h: cint, flags: uint32): *SDL_Window end local function SDL_Quit() end -local function SDL_DestroyWindow(window: SDL_Window*) end -local function SDL_PollEvent(event: SDL_Event*): int32 end +local function SDL_DestroyWindow(window: *SDL_Window) end +local function SDL_PollEvent(event: *SDL_Event): int32 end local function SDL_GetTicks(): uint32 end local function SDL_Delay(ms: uint32) end local function SDL_CreateRGBSurfaceWithFormatFrom(pixels: pointer, width: cint,height: cint, depth: cint, pitch: cint,format: uint32): SDL_Surface end local function SDL_FreeSurface(surface: SDL_Surface) end local function SDL_LockSurface(surface: SDL_Surface) end -local function SDL_GetWindowSurface(window: SDL_Window*): SDL_Surface end -local function SDL_BlitSurface(src: SDL_Surface, srcrect: SDL_Rect*, dst: SDL_Surface, dstrect: SDL_Rect*): cint end -local function SDL_UpdateWindowSurface(window: SDL_Window*) end -local function SDL_CreateRenderer(window: SDL_Window*, index: cint, flags: uint32): SDL_Renderer* end -local function SDL_DestroyRenderer(renderer: SDL_Renderer*) end -local function SDL_RenderPresent(renderer: SDL_Renderer*) end -local function SDL_RenderClear(renderer: SDL_Renderer*) end -local function SDL_CreateTexture(renderer: SDL_Renderer*, format: uint32, access: cint, w: cint, h: cint): SDL_Texture* end -local function SDL_DestroyTexture(texture: SDL_Texture*) end -local function SDL_RenderCopy(renderer: SDL_Renderer*, texture: SDL_Texture*, srcrect: SDL_Rect*, dstrect: SDL_Rect*): cint end -local function SDL_LockTexture(texture: SDL_Texture*, rect: SDL_Rect*, pixels: pointer*, pitch: cint*): cint end -local function SDL_UnlockTexture(texture: SDL_Texture*) end -local function SDL_SetRenderDrawBlendMode(renderer: SDL_Renderer*, blendMode: int32): cint end -local function SDL_SetTextureBlendMode(texture: SDL_Texture*, blendMode: int32): cint end -local function SDL_UpdateTexture(texture: SDL_Texture*, rect: SDL_Rect*, pixels: pointer, pitch: cint): cint end +local function SDL_GetWindowSurface(window: *SDL_Window): SDL_Surface end +local function SDL_BlitSurface(src: SDL_Surface, srcrect: *SDL_Rect, dst: SDL_Surface, dstrect: *SDL_Rect): cint end +local function SDL_UpdateWindowSurface(window: *SDL_Window) end +local function SDL_CreateRenderer(window: *SDL_Window, index: cint, flags: uint32): *SDL_Renderer end +local function SDL_DestroyRenderer(renderer: *SDL_Renderer) end +local function SDL_RenderPresent(renderer: *SDL_Renderer) end +local function SDL_RenderClear(renderer: *SDL_Renderer) end +local function SDL_CreateTexture(renderer: *SDL_Renderer, format: uint32, access: cint, w: cint, h: cint): *SDL_Texture end +local function SDL_DestroyTexture(texture: *SDL_Texture) end +local function SDL_RenderCopy(renderer: *SDL_Renderer, texture: *SDL_Texture, srcrect: *SDL_Rect, dstrect: *SDL_Rect): cint end +local function SDL_LockTexture(texture: *SDL_Texture, rect: *SDL_Rect, pixels: *pointer, pitch: *cint): cint end +local function SDL_UnlockTexture(texture: *SDL_Texture) end +local function SDL_SetRenderDrawBlendMode(renderer: *SDL_Renderer, blendMode: int32): cint end +local function SDL_SetTextureBlendMode(texture: *SDL_Texture, blendMode: int32): cint end +local function SDL_UpdateTexture(texture: *SDL_Texture, rect: *SDL_Rect, pixels: pointer, pitch: cint): cint end local function SDL_GetError(): cstring end -------------------------------------------------------------------------------- @@ -98,7 +98,7 @@ local window, renderer, texture local SCREEN_WIDTH = 1280 local SCREEN_HEIGHT = 720 -local pixels: vec4b[SCREEN_WIDTH][SCREEN_HEIGHT] +local pixels: [SCREEN_HEIGHT][SCREEN_WIDTH]vec4b local NUM_PARTICLES = 200 local RADIUS = 3 @@ -106,7 +106,7 @@ local MIN_DISTANCE = 40 local MAX_DISTANCE = 80 local MIN_DISTANCE2 = MIN_DISTANCE * MIN_DISTANCE local MAX_DISTANCE2 = MAX_DISTANCE * MAX_DISTANCE -local particles: Particle[NUM_PARTICLES] +local particles: [NUM_PARTICLES]Particle do for i=0, = @record { - prev: ListNodeT*, - next: ListNodeT*, + prev: *ListNodeT, + next: *ListNodeT, value: T } - local function new_node(prev: ListNodeT*, next: ListNodeT*, value: T): ListNodeT* + local function new_node(prev: *ListNodeT, next: *ListNodeT, value: T): *ListNodeT local node = allocator:new(@ListNodeT) node.prev = prev node.next = next @@ -19,8 +19,8 @@ local allocator: auto = general_allocator end local ListT = @record{ - head: ListNodeT*, - tail: ListNodeT* + head: *ListNodeT, + tail: *ListNodeT } function ListT:prepend(value: T) @@ -47,7 +47,7 @@ local allocator: auto = general_allocator self.tail = node end - function ListT:find(value: T): ListNodeT* + function ListT:find(value: T): *ListNodeT local it = self.head while it do if it.value == value then @@ -62,7 +62,7 @@ local allocator: auto = general_allocator return self:find(value) ~= nilptr end - function ListT:erase(node: ListNodeT*): ListNodeT* + function ListT:erase(node: *ListNodeT): *ListNodeT if node == self.head then self.head = node.next end diff --git a/examples/mersenne.nelua b/examples/mersenne.nelua index 52196b07..49a8b149 100644 --- a/examples/mersenne.nelua +++ b/examples/mersenne.nelua @@ -21,7 +21,7 @@ end -- generates a random number on [0, 0xffffffff] function mt19937:random_uint32() - local MAG01: uint32[2] = {0x0, 0x9908b0df} + local MAG01: [2]uint32 = {0x0, 0x9908b0df} local MASK = @enum(uint32) { LOWER = 0x7fffffff, UPPER = 0x80000000 } local N , M = MT19937_N, MT19937_M local y diff --git a/examples/overview.nelua b/examples/overview.nelua index 76027819..5b4787ea 100644 --- a/examples/overview.nelua +++ b/examples/overview.nelua @@ -263,7 +263,7 @@ do -- Array local a: array(integer, 4) = {1,2,3,4} print(a[0], a[1], a[2], a[3]) -- outputs: 1 2 3 4 - local b: integer[4] -- "integer[4]" is syntax sugar for "array(integer, 4)" + local b: [4]integer -- "[4]integer" is syntax sugar for "array(integer, 4)" print(b[0], b[1], b[2], b[3]) -- outputs: 0 0 0 0 end @@ -316,7 +316,7 @@ do -- Record end do -- Span - local arr = (@integer[4]) {1,2,3,4} + local arr = (@[4]integer) {1,2,3,4} local s: span(integer) = &arr print(s[0], s[1]) -- outputs: 1 2 print(#s) -- outputs 4 @@ -463,7 +463,7 @@ end do -- Allocating memory local Person = @record{name: string, age: integer} - local p: Person* = general_allocator:new(@Person) + local p: *Person = general_allocator:new(@Person) p.name = "John" p.age = 20 print(p.name, p.age) @@ -642,7 +642,7 @@ do -- Mixing C code local function memset(s: pointer, c: int32, n: usize): pointer ',nodecl> end local function free(ptr: pointer) ',nodecl> end - local a = (@int64[10]*)(malloc(10 * 8)) + local a = (@*[10]int64)(malloc(10 * 8)) memset(a, 0, 10*8) assert(a[0] == 0) a[0] = 1 diff --git a/examples/record_inheretance.nelua b/examples/record_inheretance.nelua index 4e8e794f..930bc8c0 100644 --- a/examples/record_inheretance.nelua +++ b/examples/record_inheretance.nelua @@ -43,9 +43,9 @@ end function Shape:area(): number switch self.kind case ShapeKind.Rectangle then - return Rectangle.area((@Rectangle*)(self)) + return Rectangle.area((@*Rectangle)(self)) case ShapeKind.Circle then - return Circle.area((@Circle*)(self)) + return Circle.area((@*Circle)(self)) else return 0 end @@ -59,10 +59,10 @@ do -- test it print(' rectangle area is', rectangle:area()) print(' circle area is', circle:area()) - local shape: Shape* - shape = (@Shape*)(&rectangle) + local shape: *Shape + shape = (@*Shape)(&rectangle) print(' circle shape area is', shape:area()) - shape = (@Shape*)(&circle) + shape = (@*Shape)(&circle) print('rectangle shape area is', shape:area()) print '' end @@ -117,7 +117,7 @@ local function override() method(function()]] -- no problem to use ifs instead of switches because C compilers usually optimizes as a switch if self.__kind == #[rectype.kindid]# then - return (@#[rectype]#*)(self):#|name|#() + return (@*#[rectype]#)(self):#|name|#() end ##[[ end) end @@ -168,10 +168,10 @@ do -- test it print(' rectangle area is', rectangle:area()) print(' circle area is', circle:area()) - local shape: Shape* - shape = (@Shape*)(&rectangle) + local shape: *Shape + shape = (@*Shape)(&rectangle) print(' circle shape area is', shape:area()) - shape = (@Shape*)(&circle) + shape = (@*Shape)(&circle) print('rectangle shape area is', shape:area()) print '' end diff --git a/examples/snakesdl.nelua b/examples/snakesdl.nelua index 1204eb44..9d1e9f15 100644 --- a/examples/snakesdl.nelua +++ b/examples/snakesdl.nelua @@ -6,7 +6,7 @@ require 'math' -- import SDL structures local SDL_Event = @record { type: uint32, - padding: byte[56] + padding: [56]byte } local SDL_Keysym = @record { scancode: cint, @@ -45,16 +45,16 @@ local SDL_RENDERER_PRESENTVSYNC = 0x4 -- import SDL functions local function SDL_Init(flags: uint32): int32 end -local function SDL_CreateWindow(title: cstring, x: cint, y: cint, w: cint, h: cint, flags: uint32): SDL_Window* end +local function SDL_CreateWindow(title: cstring, x: cint, y: cint, w: cint, h: cint, flags: uint32): *SDL_Window end local function SDL_Quit() end -local function SDL_DestroyWindow(window: SDL_Window*) end -local function SDL_PollEvent(event: SDL_Event*): int32 end -local function SDL_CreateRenderer(window: SDL_Window*, index: cint, flags: uint32): SDL_Renderer* end -local function SDL_DestroyRenderer(renderer: SDL_Renderer*) end -local function SDL_RenderPresent(renderer: SDL_Renderer*) end -local function SDL_RenderClear(renderer: SDL_Renderer*) end -local function SDL_SetRenderDrawColor(renderer: SDL_Renderer*, r: uint8, g: uint8, b: uint8, a: uint8): cint end -local function SDL_RenderFillRect(renderer: SDL_Renderer*, rect: SDL_Rect*): cint end +local function SDL_DestroyWindow(window: *SDL_Window) end +local function SDL_PollEvent(event: *SDL_Event): int32 end +local function SDL_CreateRenderer(window: *SDL_Window, index: cint, flags: uint32): *SDL_Renderer end +local function SDL_DestroyRenderer(renderer: *SDL_Renderer) end +local function SDL_RenderPresent(renderer: *SDL_Renderer) end +local function SDL_RenderClear(renderer: *SDL_Renderer) end +local function SDL_SetRenderDrawColor(renderer: *SDL_Renderer, r: uint8, g: uint8, b: uint8, a: uint8): cint end +local function SDL_RenderFillRect(renderer: *SDL_Renderer, rect: *SDL_Rect): cint end local function SDL_GetTicks(): uint32 end -- game types @@ -78,7 +78,7 @@ local quit = false local nextmove local score local headpos, tailpos, applepos -local tiles: Direction[GRID_SIZE][GRID_SIZE] +local tiles: [GRID_SIZE][GRID_SIZE]Direction local function move_point(pos: Point2D, dir: Direction) switch dir @@ -145,7 +145,7 @@ local function poll_events() case SDL_QUIT then quit = true case SDL_KEYDOWN then - local kevent = (@SDL_KeyboardEvent*)(&event) + local kevent = (@*SDL_KeyboardEvent)(&event) local headdir = get_tile(headpos) switch kevent.keysym.sym case SDLK_UP then diff --git a/lib/C/locale.nelua b/lib/C/locale.nelua index 6846d730..5fa6083f 100644 --- a/lib/C/locale.nelua +++ b/lib/C/locale.nelua @@ -28,7 +28,7 @@ local lconv = @record { } function C.setlocale(category: cint, locale: cstring): cstring end -function C.localeconv(): lconv* end +function C.localeconv(): *lconv end -- Constants global C.LC_ALL: cint diff --git a/lib/C/math.nelua b/lib/C/math.nelua index dea38f67..79cf17c7 100644 --- a/lib/C/math.nelua +++ b/lib/C/math.nelua @@ -13,9 +13,9 @@ function C.remainder(x: float64, y: float64): float64 end function C.remainderl(x: clongdouble, y: clongdouble): clongdouble end -function C.remquo(x: float64, y: float64, quo: cint*): float64 end -function C.remquof(x: float32, y: float32, quo: cint*): float32 end -function C.remquol(x: clongdouble, y: clongdouble, quo: cint*): clongdouble end +function C.remquo(x: float64, y: float64, quo: *cint): float64 end +function C.remquof(x: float32, y: float32, quo: *cint): float32 end +function C.remquol(x: clongdouble, y: clongdouble, quo: *cint): clongdouble end function C.fma(x: float64, y: float64, z: float64): float64 end function C.fmaf(x: float32, y: float32, z: float32): float32 end @@ -33,9 +33,9 @@ function C.fdim(x: float64, y: float64): float64 end function C.fdimf(x: float32, y: float32): float32 end function C.fdiml(x: clongdouble, y: clongdouble): clongdouble end -function C.nan(tagb: cchar*): float64 end -function C.nanf(tagb: cchar*): float32 end -function C.nanl(tagb: cchar*): clongdouble end +function C.nan(tagb: cstring): float64 end +function C.nanf(tagb: cstring): float32 end +function C.nanl(tagb: cstring): clongdouble end -- Exponential functions function C.exp(x: float64): float64 end @@ -200,17 +200,17 @@ function C.nearbyintf(x: float32): float32 end function C.nearbyintl(x: clongdouble): clongdouble end -- Floating-point manipulation functions -function C.frexp(x: float64, exponent: cint*): float64 end -function C.frexpf(x: float32, exponent: cint*): float32 end -function C.frexpl(x: clongdouble, exponent: cint*): clongdouble end +function C.frexp(x: float64, exponent: *cint): float64 end +function C.frexpf(x: float32, exponent: *cint): float32 end +function C.frexpl(x: clongdouble, exponent: *cint): clongdouble end function C.ldexp(x: float64, exponent: cint): float64 end function C.ldexpf(x: float32, exponent: cint): float32 end function C.ldexpl(x: clongdouble, exponent: cint): clongdouble end -function C.modf(x: float64, iptr: float64*): float64 end -function C.modff(x: float32, iptr: float32*): float32 end -function C.modfl(x: clongdouble, iptr: clongdouble*): clongdouble end +function C.modf(x: float64, iptr: *float64): float64 end +function C.modff(x: float32, iptr: *float32): float32 end +function C.modfl(x: clongdouble, iptr: *clongdouble): clongdouble end function C.scalbln(x: float64, n: clong): float64 end function C.scalblnf(x: float32, n: clong): float32 end diff --git a/lib/C/stdio.nelua b/lib/C/stdio.nelua index 68866bb3..a8127c3d 100644 --- a/lib/C/stdio.nelua +++ b/lib/C/stdio.nelua @@ -6,78 +6,78 @@ local fpos_t = @record{} -- Operations on files function C.remove(filename: cstring): cint end function C.rename(old: cstring, new: cstring): cint end -function C.tmpfile(): FILE* end +function C.tmpfile(): *FILE end function C.tmpnam(s: cstring): cstring end -- File access -function C.fclose(stream: FILE*): cint end -function C.fflush(stream: FILE*): cint end -function C.fopen(filename: cstring, modes: cstring): FILE* end -function C.freopen(filename: cstring, modes: cstring, stream: FILE*): FILE* end +function C.fclose(stream: *FILE): cint end +function C.fflush(stream: *FILE): cint end +function C.fopen(filename: cstring, modes: cstring): *FILE end +function C.freopen(filename: cstring, modes: cstring, stream: *FILE): *FILE end -function C.setbuf(stream: FILE*, buf: cstring): void end -function C.setvbuf(stream: FILE*, buf: cstring, modes: cint, n: csize): cint end +function C.setbuf(stream: *FILE, buf: cstring): void end +function C.setvbuf(stream: *FILE, buf: cstring, modes: cint, n: csize): cint end -- Formatted input/output function C.scanf(format: cstring, ...): cint end -function C.fscanf(stream: FILE*, format: cstring, ...): cint end +function C.fscanf(stream: *FILE, format: cstring, ...): cint end function C.sscanf(s: cstring, format: cstring, ...): cint end function C.vscanf(format: cstring, ...): cint end -function C.vfscanf(stream: FILE*, format: cstring, ...): cint end +function C.vfscanf(stream: *FILE, format: cstring, ...): cint end function C.vsscanf(s: cstring, format: cstring, ...): cint end function C.printf(format: cstring, ...): cint end -function C.fprintf(stream: FILE*, format: cstring, ...): cint end +function C.fprintf(stream: *FILE, format: cstring, ...): cint end function C.sprintf(s: cstring, format: cstring, ...): cint end function C.snprintf(s: cstring, maxlen: csize, format: cstring, ...): cint end function C.vprintf(format: cstring, ...): cint end -function C.vfprintf(stream: FILE*, format: cstring, ...): cint end +function C.vfprintf(stream: *FILE, format: cstring, ...): cint end function C.vsprintf(s: cstring, format: cstring, ...): cint end function C.vsnprintf(s: cstring, maxlen: csize, format: cstring, ...): cint end -- Character input/output -function C.getc(stream: FILE*): cint end -function C.putc(c: cint, stream: FILE*): cint end +function C.getc(stream: *FILE): cint end +function C.putc(c: cint, stream: *FILE): cint end function C.getchar(): cint end function C.putchar(c: cint): cint end -function C.fgetc(stream: FILE*): cint end -function C.fputc(c: cint, stream: FILE*): cint end +function C.fgetc(stream: *FILE): cint end +function C.fputc(c: cint, stream: *FILE): cint end -function C.fgets(s: cstring, n: cint, stream: FILE*): cstring end -function C.fputs(s: cstring, stream: FILE*): cint end +function C.fgets(s: cstring, n: cint, stream: *FILE): cstring end +function C.fputs(s: cstring, stream: *FILE): cint end function C.gets(s: cstring): cstring end function C.puts(s: cstring): cint end -function C.ungetc(c: cint, stream: FILE*): cint end +function C.ungetc(c: cint, stream: *FILE): cint end -- Direct input/output -function C.fread(ptr: pointer, size: csize, n: csize, stream: FILE*): csize end -function C.fwrite(ptr: void*, size: csize, n: csize, sream: FILE*): csize end +function C.fread(ptr: pointer, size: csize, n: csize, stream: *FILE): csize end +function C.fwrite(ptr: pointer, size: csize, n: csize, sream: *FILE): csize end -- File positioning -function C.fgetpos(stream: FILE*, pos: fpos_t*): cint end -function C.fsetpos(stream: FILE*, pos: fpos_t*): cint end +function C.fgetpos(stream: *FILE, pos: *fpos_t): cint end +function C.fsetpos(stream: *FILE, pos: *fpos_t): cint end -function C.fseek(stream: FILE*, off: clong, whence: cint): cint end -function C.ftell(stream: FILE*): clong end +function C.fseek(stream: *FILE, off: clong, whence: cint): cint end +function C.ftell(stream: *FILE): clong end -function C.rewind(stream: FILE*): void end +function C.rewind(stream: *FILE): void end -- Error handling -function C.clearerr(stream: FILE*): void end -function C.feof(stream: FILE*): cint end -function C.ferror(stream: FILE*): cint end +function C.clearerr(stream: *FILE): void end +function C.feof(stream: *FILE): cint end +function C.ferror(stream: *FILE): cint end function C.perror(s: cstring): void end -- Global variables -global C.stdin: FILE* -global C.stdout: FILE* -global C.stderr: FILE* +global C.stdin: *FILE +global C.stdout: *FILE +global C.stderr: *FILE -- Constants global C.EOF: cint diff --git a/lib/C/stdlib.nelua b/lib/C/stdlib.nelua index caed2867..33be7b79 100644 --- a/lib/C/stdlib.nelua +++ b/lib/C/stdlib.nelua @@ -33,14 +33,14 @@ function C.atoi(nptr: cstring): cint end function C.atol(nptr: cstring): clong end function C.atoll(nptr: cstring): clonglong end -function C.strtof(nptr: cstring, endptr: cstring*): float32 end -function C.strtod(nptr: cstring, endptr: cstring*): float64 end -function C.strtold(nptr: cstring, endptr: cstring*): clongdouble end - -function C.strtol(nptr: cstring, endptr: cstring*, base: cint): clong end -function C.strtoll(nptr: cstring, endptr: cstring*, base: cint): clonglong end -function C.strtoul(nptr: cstring, endptr: cstring*, base: cint): culong end -function C.strtoull(nptr: cstring, endptr: cstring*, base: cint): culonglong end +function C.strtof(nptr: cstring, endptr: *cstring): float32 end +function C.strtod(nptr: cstring, endptr: *cstring): float64 end +function C.strtold(nptr: cstring, endptr: *cstring): clongdouble end + +function C.strtol(nptr: cstring, endptr: *cstring, base: cint): clong end +function C.strtoll(nptr: cstring, endptr: *cstring, base: cint): clonglong end +function C.strtoul(nptr: cstring, endptr: *cstring, base: cint): culong end +function C.strtoull(nptr: cstring, endptr: *cstring, base: cint): culonglong end -- Integer arithmetics function C.abs(x: cint): cint end diff --git a/lib/C/time.nelua b/lib/C/time.nelua index fca36286..4cb400ec 100644 --- a/lib/C/time.nelua +++ b/lib/C/time.nelua @@ -29,16 +29,16 @@ local timespec = @record { -- Time manipulation function C.clock(): clock_t end function C.difftime(time1: time_t, time0: time_t): float64 end -function C.mktime(tp: tm*): time_t end -function C.strftime(s: cstring, maxsize: csize, format: cstring, tp: tm*): csize end -function C.time(timer: time_t*): time_t end +function C.mktime(tp: *tm): time_t end +function C.strftime(s: cstring, maxsize: csize, format: cstring, tp: *tm): csize end +function C.time(timer: *time_t): time_t end -- Conversion -function C.asctime(tp: tm*): cstring end -function C.ctime(timer: time_t*): cstring end -function C.gmtime(timer: time_t*): tm* end -function C.localtime(timer: time_t*): tm* end -function C.timespec_get(ts: timespec*, base: cint): cint end +function C.asctime(tp: *tm): cstring end +function C.ctime(timer: *time_t): cstring end +function C.gmtime(timer: *time_t): *tm end +function C.localtime(timer: *time_t): *tm end +function C.timespec_get(ts: *timespec, base: cint): cint end -- Constants global C.CLOCKS_PER_SEC: clock_t diff --git a/lib/allocators/arena.nelua b/lib/allocators/arena.nelua index 559f6bce..f25a0076 100644 --- a/lib/allocators/arena.nelua +++ b/lib/allocators/arena.nelua @@ -47,7 +47,7 @@ local function memcpy(dest: pointer, src: pointer, n: csize): pointer = 24 -local GC_primes: usize[GC_PRIMES_COUNT] = { +local GC_primes: [GC_PRIMES_COUNT]usize = { 0, 1, 5, 11, 23, 53, 101, 197, 389, 683, 1259, 2417, @@ -246,7 +246,7 @@ function GC:_mark_ptr(ptr: pointer) return end for k:usize=0,<(self.items[i].size // #@pointer) do - self:_mark_ptr(((@pointer[0]*)(self.items[i].ptr))[k]) + self:_mark_ptr(((@*[0]pointer)(self.items[i].ptr))[k]) end return end @@ -263,7 +263,7 @@ function GC:_mark_stack() bot, top = top, bot end for pi:usize=top,<=bot,#@pointer do - self:_mark_ptr($(@pointer*)(pi)) + self:_mark_ptr($(@*pointer)(pi)) end end @@ -285,7 +285,7 @@ function GC:_mark() continue end for k:usize=0, return qtr end - local p: GCItem* = self:_get_ptr(ptr) + local p: *GCItem = self:_get_ptr(ptr) if p then if qtr == ptr then p.size = size @@ -494,7 +494,7 @@ function GC:realloc0(ptr: pointer, size: usize, oldsize: usize): pointer end function GC:dealloc(ptr: pointer) - local p: GCItem* = self:_get_ptr(ptr) + local p: *GCItem = self:_get_ptr(ptr) if p then if p.finalizer then p.finalizer(ptr) @@ -531,9 +531,9 @@ function GC:_mark_statics() ]] end -local function nelua_main(argc: cint, argv: cstring*): cint end +local function nelua_main(argc: cint, argv: *cstring): cint end -local function main(argc: cint, argv: cstring*): cint +local function main(argc: cint, argv: *cstring): cint gc:start(&argc) gc:_mark_statics() local ret: cint = nelua_main(argc, argv) diff --git a/lib/allocators/heap.nelua b/lib/allocators/heap.nelua index c5a3e103..ebc9d1a1 100644 --- a/lib/allocators/heap.nelua +++ b/lib/allocators/heap.nelua @@ -44,30 +44,30 @@ local NODE_COOKIE: usize = 0xA7512BCF_usize -- or 16 bytes on 32bit systems, perfect for the 16 byte alignment requirement local Node = @record{ size: usize, - prev_adj: Node*, - next: Node*, - prev: Node* + prev_adj: *Node, + next: *Node, + prev: *Node } -- the double linked list for free nodes local Bin = @record{ - head: Node* + head: *Node } -- the heap stores bins for different chunk sizes local Heap = @record{ - bins: Bin[BIN_COUNT] + bins: [BIN_COUNT]Bin } function Node:set_used() -- the lower bits of the pointer should be always 0 because we allocate with alignment -- so it's impossible to have conflicts in next/prev with these values due to alignment - self.next = (@Node*)(1_usize) - self.prev = (@Node*)(NODE_COOKIE) + self.next = (@*Node)(1_usize) + self.prev = (@*Node)(NODE_COOKIE) end function Node:is_used() - return self.next == (@Node*)(1_usize) and self.prev == (@Node*)(NODE_COOKIE) + return self.next == (@*Node)(1_usize) and self.prev == (@*Node)(NODE_COOKIE) end -- import efficient clz (compute leading zeros) from C @@ -113,8 +113,8 @@ local function align_forward(addr: usize, align: usize): usize end -- insert a node inside a bin linked list -function Heap:add_node(node: Node*) - local bin: Bin* = &self.bins[get_bin_index(node.size)] +function Heap:add_node(node: *Node) + local bin: *Bin = &self.bins[get_bin_index(node.size)] node.prev = nilptr if likely(bin.head ~= nilptr) then -- bin is not empty, forward the head @@ -127,8 +127,8 @@ function Heap:add_node(node: Node*) end -- remove a node from a bin linked list -function Heap:remove_bin_node(bin_index: uint32, node: Node*) - local bin: Bin* = &self.bins[bin_index] +function Heap:remove_bin_node(bin_index: uint32, node: *Node) + local bin: *Bin = &self.bins[bin_index] -- update the head in case we are removing it if node == bin.head then bin.head = node.next @@ -144,23 +144,23 @@ function Heap:remove_bin_node(bin_index: uint32, node: Node*) end -- remove a node from a bin linked list -function Heap:remove_node(node: Node*) +function Heap:remove_node(node: *Node) self:remove_bin_node(get_bin_index(node.size), node) end -- return next adjacent node -local function get_next_adj_node(node: Node*): Node* - return (@Node*)((@usize)(node) + #@Node + node.size) +local function get_next_adj_node(node: *Node): *Node + return (@*Node)((@usize)(node) + #@Node + node.size) end -- get a node given a pointer -local function get_ptr_node(p: pointer): Node* +local function get_ptr_node(p: pointer): *Node -- check for misaligned invalid pointers if unlikely((@usize)(p) & (ALLOC_ALIGN-1) ~= 0) then return nilptr end -- the actual head of the node is not p, it is p minus the size of the node - local node: Node* = (@Node*)((@usize)(p) - #@Node) + local node: *Node = (@*Node)((@usize)(p) - #@Node) -- check if is a valid allocator node if unlikely(not node:is_used()) then return nilptr @@ -183,14 +183,14 @@ function Heap:add_memory_region(region: pointer, region_size: usize) -- first we create the initial region -- the heap starts as just one big chunk of allocable memory - local start_node: Node* = (@Node*)(heap_start) + local start_node: *Node = (@*Node)(heap_start) start_node.size = heap_size - #@Node -- this node itself use some metadata space start_node.prev_adj = nilptr start_node.next = nilptr start_node.prev = nilptr -- set the ending node - local end_node: Node* = (@Node*)(heap_start + heap_size) + local end_node: *Node = (@*Node)(heap_start + heap_size) end_node.size = 0 end_node.prev_adj = start_node end_node:set_used() -- the end node is never free @@ -211,7 +211,7 @@ function Heap:alloc(size: usize): pointer -- we always want to allocate aligned sizes size = align_forward(size + #@Node, ALLOC_ALIGN) - #@Node - local found: Node* + local found: *Node -- advance through the bins until we find a chunk that fits the size local bin_index: uint32 = get_bin_index(size) @@ -256,7 +256,7 @@ function Heap:alloc(size: usize): pointer -- do the math to get where to split at, then set its metadata local split_size: usize = found.size - size - #@Node found.size = size - local split: Node* = get_next_adj_node(found) + local split: *Node = get_next_adj_node(found) split.size = split_size -- update adjacent links @@ -283,13 +283,13 @@ function Heap:dealloc(p: pointer) if unlikely(p == nilptr) then return end -- the actual head of the node is not p, it is p minus the size of the node - local head: Node* = get_ptr_node(p) + local head: *Node = get_ptr_node(p) if unlikely(head == nilptr) then panic('invalid pointer passed in heap dealloc') end - local prev: Node* = head.prev_adj - local next: Node* + local prev: *Node = head.prev_adj + local next: *Node -- if the previous node is free we can coalesce! if likely(prev ~= nilptr) and not prev:is_used() then @@ -344,7 +344,7 @@ function Heap:realloc(p: pointer, size: usize) size = align_forward(size + #@Node, ALLOC_ALIGN) - #@Node -- the actual head of the node is not p, it is p minus the size of the node - local head: Node* = get_ptr_node(p) + local head: *Node = get_ptr_node(p) if unlikely(head == nilptr) then panic('invalid pointer passed in heap realloc') end @@ -353,7 +353,7 @@ function Heap:realloc(p: pointer, size: usize) if likely(size > head.size) then -- we can only grow if the next adjacent node -- is not the end, is free and has enough space - local next: Node* = get_next_adj_node(head) + local next: *Node = get_next_adj_node(head) if not next:is_used() and head.size + next.size + #@Node >= size then -- remove it from its bin self:remove_node(next) @@ -388,7 +388,7 @@ function Heap:realloc(p: pointer, size: usize) -- do the math to get where to split at, then set its metadata local split_size: usize = head.size - size - #@Node head.size = size - local split: Node* = get_next_adj_node(head) + local split: *Node = get_next_adj_node(head) split.size = split_size -- update adjacent links @@ -412,7 +412,7 @@ end local HeapAllocatorT = @record{ initialized: boolean, heap: Heap, - buffer: byte[HEAP_SIZE] + buffer: [HEAP_SIZE]byte } -- Initialize the heap allocator. diff --git a/lib/allocators/interface.nelua b/lib/allocators/interface.nelua index 70101166..d60821c1 100644 --- a/lib/allocators/interface.nelua +++ b/lib/allocators/interface.nelua @@ -61,7 +61,7 @@ require 'span' p = self:realloc(p, newsize, oldsize) if likely(newsize > oldsize and p ~= nilptr) then -- zero the grown part - memset(&(@byte[0]*)(p)[oldsize], 0, newsize - oldsize) + memset(&(@*[0]byte)(p)[oldsize], 0, newsize - oldsize) end return p end @@ -70,7 +70,7 @@ require 'span' function Allocator:spanalloc(T: type, size: usize) local s: span(T) if likely(size > 0) then - s.data = (@T[0]*)(self:alloc(size * #T)) + s.data = (@*[0]T)(self:alloc(size * #T)) s.size = size end return s @@ -79,7 +79,7 @@ require 'span' function Allocator:spanalloc0(T: type, size: usize) local s: span(T) if likely(size > 0) then - s.data = (@T[0]*)(self:alloc0(size * #T)) + s.data = (@*[0]T)(self:alloc0(size * #T)) s.size = size end return s @@ -87,7 +87,7 @@ require 'span' function Allocator:spanrealloc(s: is_span, size: usize) local T: type = #[s.type.subtype]# - local p: T[0]* = (@T[0]*)(self:realloc(s.data, size * #T, s.size)) + local p: *[0]T = (@*[0]T)(self:realloc(s.data, size * #T, s.size)) if unlikely(size > 0 and p == nilptr) then -- reallocation failed, return the original span return s @@ -99,7 +99,7 @@ require 'span' function Allocator:spanrealloc0(s: is_span, size: usize) local T: type = #[s.type.subtype]# - local p: T[0]* = (@T[0]*)(self:realloc0(s.data, size * #T, s.size * #T)) + local p: *[0]T = (@*[0]T)(self:realloc0(s.data, size * #T, s.size * #T)) if unlikely(size > 0 and p == nilptr) then -- reallocation failed, return the original span return s @@ -133,11 +133,11 @@ require 'span' function Allocator:new(what: auto) ## if what.type.is_type then local T = what - local ptr: T* = (@T*)(self:alloc0(#T)) + local ptr: *T = (@*T)(self:alloc0(#T)) check(ptr ~= nilptr, 'Allocator.new: out of memory') ## else local T = #[what.type]# - local ptr: T* = (@T*)(self:alloc(#T)) + local ptr: *T = (@*T)(self:alloc(#T)) check(ptr ~= nilptr, 'Allocator.new: out of memory') memcpy(ptr, &what, #T) ## end diff --git a/lib/allocators/pool.nelua b/lib/allocators/pool.nelua index c089a15d..b248c177 100644 --- a/lib/allocators/pool.nelua +++ b/lib/allocators/pool.nelua @@ -25,12 +25,12 @@ require 'allocators.interface' static_assert(CHUNK_SIZE >= primtypes.pointer.size, 'PoolAllocator: chunk size must be at least a pointer in size') ]] - local PoolFreeNode = @record{next: PoolFreeNode*} - local PoolChunk = @record {data: byte[#[CHUNK_SIZE]#]} + local PoolFreeNode = @record{next: *PoolFreeNode} + local PoolChunk = @record {data: [#[CHUNK_SIZE]#]byte} local PoolAllocatorT = @record{ initialized: boolean, - head: PoolFreeNode*, - buffer: PoolChunk[#[SIZE]#] + head: *PoolFreeNode, + buffer: [#[SIZE]#]PoolChunk } -- Free all allocations. @@ -38,7 +38,7 @@ require 'allocators.interface' self.head = nilptr -- link all free nodes in reverse order for i:isize=#self.buffer-1,0,-1 do - local node: PoolFreeNode* = (@PoolFreeNode*)(&self.buffer[i]) + local node: *PoolFreeNode = (@*PoolFreeNode)(&self.buffer[i]) node.next = self.head self.head = node end @@ -59,7 +59,7 @@ require 'allocators.interface' return nilptr end -- get the latest free node - local node: PoolFreeNode* = self.head + local node: *PoolFreeNode = self.head -- the node will be nilptr if not initialized or out of memory if unlikely(node == nilptr) then if not self.initialized then @@ -86,7 +86,7 @@ require 'allocators.interface' local offset: usize = (@usize)(p) - (@usize)(&self.buffer[0]) check(offset // #PoolChunk < #self.buffer and offset % #PoolChunk == 0, 'PoolAllocator.dealloc: pointer not in buffer bounds') -- push free node - local node: PoolFreeNode* = (@PoolFreeNode*)(p) + local node: *PoolFreeNode = (@*PoolFreeNode)(p) node.next = self.head self.head = node end diff --git a/lib/allocators/stack.nelua b/lib/allocators/stack.nelua index 36ac4fa3..bc240f07 100644 --- a/lib/allocators/stack.nelua +++ b/lib/allocators/stack.nelua @@ -47,7 +47,7 @@ local StackAllocHeader = @record { local StackAllocatorT = @record{ prev_offset: usize, curr_offset: usize, - buffer: byte[SIZE] + buffer: [SIZE]byte } -- Free all allocations. @@ -68,7 +68,7 @@ local StackAllocHeader = @record { end local p: pointer = &self.buffer[offset] -- push the current state in the stack header - local header: StackAllocHeader* = (@StackAllocHeader*)(&self.buffer[offset - #@StackAllocHeader]) + local header: *StackAllocHeader = (@*StackAllocHeader)(&self.buffer[offset - #@StackAllocHeader]) header.prev_offset = (@uint32)(self.prev_offset) header.curr_offset = (@uint32)(self.curr_offset) self.prev_offset = offset @@ -80,7 +80,7 @@ local StackAllocHeader = @record { if unlikely(p == nilptr) then return end local offset: usize = (@usize)(p) - (@usize)(&self.buffer[0]) if likely(offset == self.prev_offset) then -- is the very last allocation? - local header: StackAllocHeader* = (@StackAllocHeader*)(&self.buffer[offset - #@StackAllocHeader]) + local header: *StackAllocHeader = (@*StackAllocHeader)(&self.buffer[offset - #@StackAllocHeader]) -- pop the current state from the stack header self.prev_offset = header.prev_offset self.curr_offset = header.curr_offset diff --git a/lib/arg.nelua b/lib/arg.nelua index f8e99903..0796ff5a 100644 --- a/lib/arg.nelua +++ b/lib/arg.nelua @@ -6,7 +6,7 @@ require 'allocators.general' -- Import argc and argv from C 'nelua_main' local nelua_argc: cint -local nelua_argv: cstring[0]* +local nelua_argv: *[0]cstring -- List of command line arguments. -- The index 0 usually is filled with the program executable. diff --git a/lib/basic.nelua b/lib/basic.nelua index 04142105..d7d377da 100644 --- a/lib/basic.nelua +++ b/lib/basic.nelua @@ -10,10 +10,10 @@ #endif ]=])]] local FILE '> = @record{} -local function fwrite(ptr: void*, size: csize, n: csize, s: FILE*): csize '> end -local function fputc(c: cint, stream: FILE*): cint '> end +local function fwrite(ptr: *void, size: csize, n: csize, s: *FILE): csize '> end +local function fputc(c: cint, stream: *FILE): cint '> end local function abort(): void '> end -local stderr: FILE* '> +local stderr: *FILE '> global function likely(x: boolean): boolean end diff --git a/lib/filestream.nelua b/lib/filestream.nelua index 6db254ae..6bd5f597 100644 --- a/lib/filestream.nelua +++ b/lib/filestream.nelua @@ -58,7 +58,7 @@ function filestream._from_fp(fp: FILEptr): filestream end function filestream:_get_fp(): FILEptr - local p: FILEptr* = pool:get(self.id) + local p: *FILEptr = pool:get(self.id) if p then return $p end return nilptr end @@ -136,13 +136,13 @@ end local READ_CHUNK_SIZE = 1024 -local function readline(sb: stringbuilder*, fp: FILEptr, chop: boolean): boolean +local function readline(sb: *stringbuilder, fp: FILEptr, chop: boolean): boolean -- TODO: lock file on POSIX? local NL: byte = '\n'_byte local c: cint repeat local nr: uint32 = 0 - local buff: byte[0]* = sb:prepare(READ_CHUNK_SIZE) -- preallocate buffer space + local buff: *[0]byte = sb:prepare(READ_CHUNK_SIZE) -- preallocate buffer space while nr < READ_CHUNK_SIZE do -- read up to buffer limit c = getc(fp) if c == EOF or c == NL then -- end of line @@ -160,14 +160,14 @@ local function readline(sb: stringbuilder*, fp: FILEptr, chop: boolean): boolean return (c == NL or sb.size > 0) end -local function readchars(sb: stringbuilder*, fp: FILEptr, n: usize): boolean +local function readchars(sb: *stringbuilder, fp: FILEptr, n: usize): boolean local p: pointer = sb:prepare(n) local nr: csize = fread(p, 1, n, fp) sb:commit(nr) return nr > 0 end -local function readall(sb: stringbuilder*, fp: FILEptr): boolean +local function readall(sb: *stringbuilder, fp: FILEptr): boolean repeat -- read in chunks local p: pointer = sb:prepare(READ_CHUNK_SIZE) local nr: csize = fread(p, 1, READ_CHUNK_SIZE, fp) diff --git a/lib/math.nelua b/lib/math.nelua index 8d01e72d..9dc6940b 100644 --- a/lib/math.nelua +++ b/lib/math.nelua @@ -66,10 +66,10 @@ for _,name in ipairs(cmathfuncs2) do ## end -- manually import special math functions with 2 arguments -local function modf_f64(x: float64, y: float64*): float64 end -local function modf_f32(x: float32, y: float32*): float32 end -local function frexp_f64(x: float64, exp: int32*): float64 end -local function frexp_f32(x: float32, exp: int32*): float32 end +local function modf_f64(x: float64, y: *float64): float64 end +local function modf_f32(x: float32, y: *float32): float32 end +local function frexp_f64(x: float64, exp: *int32): float64 end +local function frexp_f32(x: float32, exp: *int32): float32 end local function ldexp_f64(x: float64, exp: int32): float64 end local function ldexp_f32(x: float32, exp: int32): float32 end @@ -248,14 +248,14 @@ function math.ult(m: is_arithmetic, n: is_arithmetic): boolean end -- Pseudo Random Number Generator based on xoshiro256** -local xoshiro256 = @record{state: uint64[4]} +local xoshiro256 = @record{state: [4]uint64} local function rotl(x: uint64, n: int32): uint64 return (x << n) | (x >> (64 - n)) end function xoshiro256:nextrand(): uint64 - local state: uint64[4] = { + local state: [4]uint64 = { self.state[0], self.state[1], self.state[2] ~ self.state[0], diff --git a/lib/memory.nelua b/lib/memory.nelua index b6dde2c3..f906d9b0 100644 --- a/lib/memory.nelua +++ b/lib/memory.nelua @@ -55,7 +55,7 @@ function memory.find(heystack: pointer, heystacksize: usize, else check(heystack and needle, 'memory.find: invalid pointer') if needlesize == 1 then - return memchr(heystack, $(@byte*)(needle), heystacksize) + return memchr(heystack, $(@*byte)(needle), heystacksize) end local heystackbegin: usize = (@usize)(heystack) for i:usize=heystackbegin,heystackbegin+(heystacksize-needlesize) do @@ -196,7 +196,7 @@ end function memory.swapval(a: is_value_pointer, b: is_value_pointer) ## static_assert(a.type == b.type, 'cannot swap different types') local N: usize = #[a.type.subtype.size]# - local tmp: byte[N] + local tmp: [N]byte memcpy(&tmp[0], a, N) memcpy(a, b, N) memcpy(b, &tmp[0], N) diff --git a/lib/os.nelua b/lib/os.nelua index d974a443..3dab7b16 100644 --- a/lib/os.nelua +++ b/lib/os.nelua @@ -40,9 +40,9 @@ local function rename(old: cstring, new: cstring): cint ',nodecl> end local function setlocale(category: cint, locale: cstring): cstring ',nodecl> end local function time(tloc: pointer): time_t ',nodecl> end -local function mktime(tp: tm*): time_t ',nodecl> end -local function localtime(timer: time_t*): tm* ',nodecl> end -local function strftime(s: cstring, maxsize: csize, format: cstring, tp: tm*): csize ',nodecl> end +local function mktime(tp: *tm): time_t ',nodecl> end +local function localtime(timer: *time_t): *tm ',nodecl> end +local function strftime(s: cstring, maxsize: csize, format: cstring, tp: *tm): csize ',nodecl> end -------------------------------------------------------------------------------- -- os module @@ -57,7 +57,7 @@ function os.date(): string --TODO: all the other options from Lua local t: time_t = time(nilptr); local ts: tm = localtime(&t) - local buf: cchar[250] + local buf: [250]cchar local size: csize = strftime(&buf[0], 250, "%c", &ts) return tostring(&buf[0]) end @@ -195,7 +195,7 @@ function os.tmpname(): string end ## end - local buf: cchar[TMPBUFSIZE] + local buf: [TMPBUFSIZE]cchar tmpnam(&buf[0]) return tostring(&buf[0]) end diff --git a/lib/resourcepool.nelua b/lib/resourcepool.nelua index 90945154..70be1e2e 100644 --- a/lib/resourcepool.nelua +++ b/lib/resourcepool.nelua @@ -171,7 +171,7 @@ local INIT_SIZE = 64 -- Return a pointer to a resource associated with an id. -- In case the resource id is invalid then throws a runtime error. - function resourcepoolT:at(id: uint64): T* + function resourcepoolT:at(id: uint64): *T local slot_index: uint32 = self.slot_pool:get_slot_index(id) check(slot_index ~= 0, 'resourcepool.at: invalid id') return &self.items[slot_index] @@ -179,7 +179,7 @@ local INIT_SIZE = 64 -- Return a pointer to a resource associated with an id. -- In case the resource id is invalid then returns nilptr. - function resourcepoolT:get(id: uint64): T* + function resourcepoolT:get(id: uint64): *T local slot_index: uint32 = self.slot_pool:get_slot_index(id) if unlikely(slot_index == 0) then return nilptr end return &self.items[slot_index] @@ -191,7 +191,7 @@ local INIT_SIZE = 64 end -- Allocate a new resource in the pool returning its id fallowed by its pointer. - function resourcepoolT:alloc(): (uint64, T*) + function resourcepoolT:alloc(): (uint64, *T) local slot_id: uint64, slot_index: uint32 = self.slot_pool:alloc_slot() if unlikely(self.items.size < self.slot_pool.gen_ctrs.size) then -- slot pool has grown, allocate more space @@ -203,7 +203,7 @@ local INIT_SIZE = 64 -- Allocate a new resource in the pool returning its id, initializes the contents from `v`. function resourcepoolT:allocinit(v: T): uint64 - local id: uint64, res: T* = self:alloc() + local id: uint64, res: *T = self:alloc() $res = memory.moveval(&v) return id end diff --git a/lib/sequence.nelua b/lib/sequence.nelua index 62a90bec..f59309b9 100644 --- a/lib/sequence.nelua +++ b/lib/sequence.nelua @@ -25,7 +25,7 @@ require 'memory' } local sequenceT = @record{ - impl: sequenceimplT*, + impl: *sequenceimplT, allocator: Allocator } @@ -48,7 +48,7 @@ require 'memory' -- This is already implicitly called by other sequence functions when needed. function sequenceT:init() if likely(self.impl) then return end - self.impl = (@sequenceimplT*)(self.allocator:alloc0(#sequenceimplT)) + self.impl = (@*sequenceimplT)(self.allocator:alloc0(#sequenceimplT)) check(self.impl ~= nilptr, 'sequence.init: out of memory') end @@ -181,7 +181,7 @@ require 'memory' -- Returns reference to element at index `i`. -- If `i` is the sequence size plus 1, then a zeroed element is added and return its reference. -- If `i` is larger then the sequence size plus 1, then throws a runtime error. - function sequenceT:__atindex(i: usize): T* + function sequenceT:__atindex(i: usize): *T self:init() if unlikely(i > self.impl.size) then check(i == self.impl.size + 1, 'sequence.__atindex: position out of bounds') diff --git a/lib/span.nelua b/lib/span.nelua index 32b7d93e..af6d3c72 100644 --- a/lib/span.nelua +++ b/lib/span.nelua @@ -14,7 +14,7 @@ ]] local T = @#[T]# local spanT = @record { - data: T[0]*, + data: *[0]T, size: usize } @@ -26,7 +26,7 @@ -- Returns reference to element at index `i`. -- If `i` is greater of equal to span size, then throws a runtime error. - function spanT:__atindex(i: usize): T* + function spanT:__atindex(i: usize): *T check(i < self.size, 'span.__atindex: index out of range') return &self.data[i] end diff --git a/lib/string.nelua b/lib/string.nelua index 9ddd99fd..a063c329 100644 --- a/lib/string.nelua +++ b/lib/string.nelua @@ -29,7 +29,7 @@ local function toupper(c: cint): cint ',nodecl> end -- Define the string type. global string = @record{ - data: byte[0]*, + data: *[0]byte, size: usize } @@ -47,7 +47,7 @@ function string._create(size: usize): string local self: string check(size > 0, 'string._create: attempt to create an empty string') self.size = size - self.data = (@byte[0]*)(string_allocator:alloc(size+1)) + self.data = (@*[0]byte)(string_allocator:alloc(size+1)) check(self.data ~= nilptr, 'string._create: out of memory') self.data[size] = 0 return self diff --git a/lib/stringbuilder.nelua b/lib/stringbuilder.nelua index 25bdabc1..79e332e1 100644 --- a/lib/stringbuilder.nelua +++ b/lib/stringbuilder.nelua @@ -22,7 +22,7 @@ local function snprintf_1cs(s: cstring, maxlen: csize, format: cstring, x: cstri -- format utilities local MAX_FORMAT = 32 -local function scanformat(strfmt: byte[0]*, form: byte[MAX_FORMAT]*): usize +local function scanformat(strfmt: *[0]byte, form: *[MAX_FORMAT]byte): usize local L_FMTFLAGS = "-+ #0" local p: usize = 0 while strfmt[p] ~= 0 and strchr(L_FMTFLAGS, strfmt[p]) ~= nilptr do @@ -47,7 +47,7 @@ local function scanformat(strfmt: byte[0]*, form: byte[MAX_FORMAT]*): usize return p end -local function addlenmod(form: byte[MAX_FORMAT]*, lenmod: cstring, lenmodsize: csize) +local function addlenmod(form: *[MAX_FORMAT]byte, lenmod: cstring, lenmodsize: csize) local l: csize = strlen(&form[0]) local spec: byte = form[l - 1] strcpy(&form[l - 1], lenmod) @@ -82,7 +82,7 @@ end self.size = 0 end - function stringbuilderA:prepare(size: usize): byte[0]* + function stringbuilderA:prepare(size: usize): *[0]byte -- allocate 1 additional byte to allow casting to cstring local needed: usize = self.size + size + 1 local cap: usize = self.data.size @@ -100,7 +100,7 @@ end end end end - return (@byte[0]*)(&self.data[self.size]) + return (@*[0]byte)(&self.data[self.size]) end function stringbuilderA:commit(size: usize) @@ -110,7 +110,7 @@ end end function stringbuilderA:write_byte(c: byte): boolean - local p: byte[0]* = self:prepare(1) + local p: *[0]byte = self:prepare(1) if unlikely(p == nilptr) then return false end p[0] = c self.size = self.size + 1 @@ -118,7 +118,7 @@ end end function stringbuilderA:write_string(s: stringview): boolean - local p: byte[0]* = self:prepare(s.size) + local p: *[0]byte = self:prepare(s.size) if unlikely(p == nilptr) then return false end memory.copy(p, s.data, s.size) self.size = self.size + s.size @@ -142,7 +142,7 @@ end if not self:write_byte(L_ESC) then return false end pos = pos + 1 else -- format item - local form: byte[MAX_FORMAT] -- to store the format ('%...') + local form: [MAX_FORMAT]byte -- to store the format ('%...') local maxitem: usize = MAX_ITEM local buff: cstring = (@cstring)(self:prepare(MAX_ITEM)) -- to put formatted item if not buff then diff --git a/lib/stringview.nelua b/lib/stringview.nelua index d0820d2b..58ca3a83 100644 --- a/lib/stringview.nelua +++ b/lib/stringview.nelua @@ -13,8 +13,8 @@ require 'memory' -- C imports local errno: cint ',nodecl> -local function strtoll(str: cstring, endptr: cstring*, base: cint): clonglong ',nodecl> end -local function strtod(str: cstring, endptr: cstring*): float64 ',nodecl> end +local function strtoll(str: cstring, endptr: *cstring, base: cint): clonglong ',nodecl> end +local function strtod(str: cstring, endptr: *cstring): float64 ',nodecl> end local function strspn(s: cstring, accept: cstring): csize ',nodecl> end local function isdigit(x: cint): cint ',nodecl> end local function isalnum(x: cint): cint ',nodecl> end diff --git a/lib/vector.nelua b/lib/vector.nelua index a027f190..925c19ec 100644 --- a/lib/vector.nelua +++ b/lib/vector.nelua @@ -162,7 +162,7 @@ require 'memory' -- Returns reference to element at index `pos`. -- If `pos` is greater of equal to vector size, then throws a runtime error. - function vectorT:__atindex(i: usize): T* + function vectorT:__atindex(i: usize): *T check(i < self.size, 'vector.at: position out of bounds') return &self.data[i] end diff --git a/nelua/pegparser.lua b/nelua/pegparser.lua index 81b5a9eb..593f94c0 100644 --- a/nelua/pegparser.lua +++ b/nelua/pegparser.lua @@ -61,11 +61,24 @@ function PEGParser:set_astbuilder(astbuilder) end end - defs.to_chain_late_unary_op = function(expr, opnodes) - for i=1,#opnodes do - local op = opnodes[i] - op[3] = expr - expr = to_astnode(table.unpack(op)) + defs.to_chain_late_unary_op = function(opnodes, expr) + if opnodes then + for i=#opnodes,1,-1 do + local op = opnodes[i] + op[3] = expr + expr = to_astnode(table.unpack(op)) + end + end + return expr + end + + defs.to_chain_late_unary_op_suffix = function(expr, opnodes) + if opnodes then + for i=1,#opnodes do + local op = opnodes[i] + op[3] = expr + expr = to_astnode(table.unpack(op)) + end end return expr end diff --git a/nelua/syntaxdefs.lua b/nelua/syntaxdefs.lua index 521cedca..4caae3df 100644 --- a/nelua/syntaxdefs.lua +++ b/nelua/syntaxdefs.lua @@ -464,7 +464,8 @@ local function get_parser() typexpr <- typexpr0 typexpr0 <- ({} '' -> 'UnionType' {| typexpr1 (%BOR typexpr1)* |}) -> to_list_astnode - typexpr1 <- (simple_typexpr {| unary_typexpr_op* |}) -> to_chain_late_unary_op + typexpr1 <- ({| unary_typexpr_op* |} typexpr2) -> to_chain_late_unary_op + typexpr2 <- (simple_typexpr {| unary_typexpr_op+ |}?) -> to_chain_late_unary_op_suffix simple_typexpr <- func_type / diff --git a/spec/02-syntaxdefs_spec.lua b/spec/02-syntaxdefs_spec.lua index 2a660ff0..f41ce043 100644 --- a/spec/02-syntaxdefs_spec.lua +++ b/spec/02-syntaxdefs_spec.lua @@ -1358,17 +1358,17 @@ 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: integer[10]", + assert.parse_ast(nelua_parser, "local a: [10]integer", n.Block{{ n.VarDecl{'local', { n.IdDecl{'a', n.ArrayType{n.Type{'integer'}, n.Number{'dec', '10'}}}} }}}) - assert.parse_ast(nelua_parser, "local a: integer[10][10]", + assert.parse_ast(nelua_parser, "local a: [10][20]integer", n.Block{{ n.VarDecl{'local', { n.IdDecl{'a', n.ArrayType{ - n.ArrayType{n.Type{'integer'}, n.Number{'dec', '10'}}, + n.ArrayType{n.Type{'integer'}, n.Number{'dec', '20'}}, n.Number{'dec', '10'}}}} }}}) end) @@ -1438,12 +1438,12 @@ describe("type expression", function() }}}) end) it("optional type", function() - assert.parse_ast(nelua_parser, "local u: integer?", + assert.parse_ast(nelua_parser, "local u: ?integer", n.Block{{ n.VarDecl{'local', { n.IdDecl{'u', n.OptionalType{n.Type{'integer'}}}} }}}) - assert.parse_ast(nelua_parser, "local u: integer*?", + assert.parse_ast(nelua_parser, "local u: ?*integer", n.Block{{ n.VarDecl{'local', { n.IdDecl{'u', n.OptionalType{n.PointerType{n.Type{'integer'}}}}} @@ -1475,12 +1475,12 @@ describe("type expression", function() n.VarDecl{'local', { n.IdDecl{'p', n.PointerType{n.Type{'integer'}}}} }}}) - assert.parse_ast(nelua_parser, "local p: integer*", + assert.parse_ast(nelua_parser, "local p: *integer", n.Block{{ n.VarDecl{'local', { n.IdDecl{'p', n.PointerType{n.Type{'integer'}}}} }}}) - assert.parse_ast(nelua_parser, "local p: integer**", + assert.parse_ast(nelua_parser, "local p: **integer", n.Block{{ n.VarDecl{'local', { n.IdDecl{'p', n.PointerType{n.PointerType{n.Type{'integer'}}}}} @@ -1493,7 +1493,7 @@ describe("type expression", function() n.IdDecl{'r', n.GenericType{"somegeneric", { n.Type{'integer'}, n.Number{"dec", "4"}}}}} }}}) - assert.parse_ast(nelua_parser, "local r: somegeneric(array(integer, 4), integer*)", + assert.parse_ast(nelua_parser, "local r: somegeneric(array(integer, 4), *integer)", n.Block{{ n.VarDecl{'local', { n.IdDecl{'r', n.GenericType{"somegeneric", { @@ -1502,7 +1502,7 @@ describe("type expression", function() }}}}}}}) end) it("complex types", function() - assert.parse_ast(nelua_parser, "local p: integer*[10]*[10]", + assert.parse_ast(nelua_parser, "local p: [10]*[10]*integer", n.Block{{ n.VarDecl{'local', { n.IdDecl{'p', diff --git a/spec/03-typechecker_spec.lua b/spec/03-typechecker_spec.lua index b17e2abe..d76afa73 100644 --- a/spec/03-typechecker_spec.lua +++ b/spec/03-typechecker_spec.lua @@ -520,7 +520,7 @@ it("late deduction", function() local a: auto = nilptr or &i ]],[[ local i: integer - local a: integer* = nilptr or &i + local a: *integer = nilptr or &i ]]) end) @@ -699,7 +699,7 @@ it("function multiple return", function() ]]) assert.analyze_ast([[ local R = @record{x: integer} - function R.foo(self: R*): (boolean, integer) return true, self.x end + function R.foo(self: *R): (boolean, integer) return true, self.x end function R:boo(): (boolean, integer) return true, self.x end local r = R{} local function foo(): (boolean, integer) return R.foo(r) end @@ -788,12 +788,12 @@ it("callbacks", function() local callback: callback_type = nilptr ]]) assert.analyze_ast([[ - local callback_type = @function(integer*, integer[4], record{x:integer}): (integer[4], boolean) + local callback_type = @function(*integer, [4]integer, record{x:integer}): ([4]integer, boolean) local callback: callback_type = nilptr ]]) assert.analyze_ast([[ - local Callback = @function(void*) - local g: function(integer*) + local Callback = @function(*void) + local g: function(*integer) local f: Callback = (@Callback)(g) local f: Callback = (@Callback)(nilptr) local p: pointer @@ -986,7 +986,7 @@ end) it("records metamethods", function() assert.analyze_ast([[ local R = @record{} - function R:__atindex(x: integer): integer* return nilptr end + function R:__atindex(x: integer): *integer return nilptr end function R:__len(): integer return 0 end local r: R local x = r[0] @@ -1064,7 +1064,7 @@ it("record methods", function() local v: vec2 = vec2.create(1,2) local l: integer = v:length() - local vec2pointer = @vec2* + local vec2pointer = @*vec2 function vec2pointer:len() return self.x + self.y end l = v:len() ]]) @@ -1254,9 +1254,9 @@ it("pointers", function() local b: pointer a = b ]], "no viable type conversion") - assert.analyze_error([[local a: integer*, b: number*; b = a]], "no viable type conversion") - assert.analyze_error("local a: auto*", "is not addressable thus cannot have a pointer") - assert.analyze_error("local a: type*", "is not addressable thus cannot have a pointer") + assert.analyze_error([[local a: *integer, b: *number; b = a]], "no viable type conversion") + assert.analyze_error("local a: *auto", "is not addressable thus cannot have a pointer") + assert.analyze_error("local a: *type", "is not addressable thus cannot have a pointer") end) it("dereferencing and referencing", function() @@ -1278,13 +1278,13 @@ it("automatic referencing", function() local R = @record{x: integer} local p: pointer(R) local a: R - local function f(): R* return a end + local function f(): *R return a end p = f() ]]) assert.analyze_ast([[ local A = @record{x: integer} local B = @record{a: A} - function B:f() local i: A* = self.a end + function B:f() local i: *A = self.a end ]]) assert.analyze_error([[ local R = @record{x: integer} @@ -1318,7 +1318,7 @@ it("automatic dereferencing", function() local a: R = p ]]) assert.analyze_ast([[ - local A = @integer[4] + local A = @[4]integer local p: pointer(A) local a: A = p ]]) @@ -1326,7 +1326,7 @@ it("automatic dereferencing", function() local R = @record{x: integer} local function f(x: R): R return x end local p: pointer(R) - local r: record{x: R*} + local r: record{x: *R} f(r.x) ]]) assert.analyze_error([[ @@ -1440,8 +1440,8 @@ it("concepts", function() f(2_uinteger) f(3) f(4) - g((@integer[2]){1,2}) - g((@integer[3]){1,2,3}) + g((@[2]integer){1,2}) + g((@[3]integer){1,2,3}) ]]) assert.analyze_error([[ local an_integral = #[concept(function(x) @@ -1553,13 +1553,13 @@ end) it("custom braces initialization", function() assert.analyze_ast([==[ - local vector = @record{data: integer[4]} + local vector = @record{data: [4]integer} ##[[ vector.value.choose_braces_type = function(nodes) return types.ArrayType(primtypes.integer, 4) end ]] - function vector.__convert(data: integer[4]): vector + function vector.__convert(data: [4]integer): vector local v: vector v.data = data return v @@ -1568,13 +1568,13 @@ it("custom braces initialization", function() assert(v.data[0] == 1 and v.data[1] == 2 and v.data[2] == 3 and v.data[3] == 4) ]==]) assert.analyze_error([==[ - local vector = @record{data: integer[4]} + local vector = @record{data: [4]integer} ##[[ vector.value.choose_braces_type = function(nodes) return nil end ]] - function vector.__convert(data: integer[4]): vector + function vector.__convert(data: [4]integer): vector local v: vector v.data = data return v diff --git a/spec/05-cgenerator_spec.lua b/spec/05-cgenerator_spec.lua index a7034f19..a86808e7 100644 --- a/spec/05-cgenerator_spec.lua +++ b/spec/05-cgenerator_spec.lua @@ -71,7 +71,7 @@ it("type cast", function() assert.generate_c([[ local R = @record{x: integer} - local a = (@integer[4])() + local a = (@[4]integer)() local i = (@integer)() local u = (@uinteger)() local b = (@boolean)() @@ -557,7 +557,7 @@ it("function multiple returns", function() assert(a == 2 and b == false and c == 1) local R = @record{x: integer} - function R.foo(self: R*): (boolean, integer) return true, self.x end + function R.foo(self: *R): (boolean, integer) return true, self.x end function R:boo(): (boolean, integer) return true, self.x end local r = R{1} local function foo(): (boolean, integer) return R.foo(r) end @@ -658,9 +658,9 @@ it("unary operator `unm`", function() end) it("unary operator `deref`", function() - assert.generate_c("local a: integer*; local x = $a", "x = (*nelua_assert_deref_nlint64_ptr(a));") + assert.generate_c("local a: *integer; local x = $a", "x = (*nelua_assert_deref_nlint64_ptr(a));") config.pragma.nochecks = true - assert.generate_c("local a: integer*; local x = $a", "x = (*a);") + assert.generate_c("local a: *integer; local x = $a", "x = (*a);") config.pragma.nochecks = nil end) @@ -675,7 +675,7 @@ end) it("unary operator `len`", function() assert.generate_c("local x = #@integer", "x = 8;") assert.generate_c("local x = #'asd'", "x = 3;") - assert.generate_c("local x = #@integer[4]", "x = 32;") + assert.generate_c("local x = #@[4]integer", "x = 32;") --assert.generate_c("a = 'asd'; local x = #a", "x = 3;") end) @@ -727,7 +727,7 @@ it("binary operator `eq`", function() assert.generate_c("local x = '1' == 1", "x = false;") assert.generate_c("local x = '1' == '1'", "x = true;") assert.generate_c("local a,b = 1,2; local x = a == b", "x = (a == b);") - assert.generate_c("local a: pointer, b: boolean*; local x = a == b", "x = (a == (void*)b);") + assert.generate_c("local a: pointer, b: *boolean; local x = a == b", "x = (a == (void*)b);") assert.generate_c("local x = 0e12 == 0", "x = true;") end) @@ -953,12 +953,12 @@ end) it("array comparisons", function() assert.run_c([[ - local A = @integer[4] + local A = @[4]integer local a: A = {1,2,3,4} local b: A = {1,2,3,4} local c: A = {1,2,3,5} assert(a == a) - assert(a == (@integer[4]){1,2,3,4}) + assert(a == (@[4]integer){1,2,3,4}) assert(a == b) assert(not (a ~= b)) assert(not (a == c)) @@ -1008,7 +1008,7 @@ it("record comparisons", function() assert(a ~= c) - local Q = @record{x: integer[2]} + local Q = @record{x: [2]integer} local a: Q = {{1,2}} local b: Q = {{1,2}} local c: Q = {{1,3}} @@ -1053,7 +1053,7 @@ it("binary conditional operators", function() "b2 = ((i == 0) || p);" }) assert.generate_c([[ - local p: integer* + local p: *integer local a: pointer, b: pointer if p and a == b then end while p and a == b do end @@ -1304,7 +1304,7 @@ it("arrays", function() end do - local words: cstring[2] = { + local words: [2]cstring = { "hello", "world", } @@ -1318,12 +1318,12 @@ end) it("array bounds checking", function() assert.run_error_c([[ - local a: integer[4] + local a: [4]integer local i = 4 print(a[i]) ]], "array index: position out of bounds") assert.run_error_c([[ - local a: integer[4] + local a: [4]integer local i = -1 print(a[i]) ]], "array index: position out of bounds") @@ -1331,7 +1331,7 @@ end) it("arrays inside records", function() assert.run_c([[ - local R = @record{v: integer[4]} + local R = @record{v: [4]integer} local a: R a.v[0]=1 a.v[1]=2 a.v[2]=3 a.v[3]=4 assert(a.v[0]==1 and a.v[1]==2 and a.v[2]==3 and a.v[3]==4) @@ -1341,8 +1341,8 @@ it("arrays inside records", function() local b: R = {v = {1,2,3,4}} assert(b.v[0]==1 and b.v[1]==2 and b.v[2]==3 and b.v[3]==4) - local function f(): integer[2][2] - local a: integer[2][2] + local function f(): [2][2]integer + local a: [2][2]integer a[0][0] = 1 a[0][1] = 2 a[1][0] = 3 @@ -1354,16 +1354,16 @@ it("arrays inside records", function() assert(f()[1][0] == 3) assert(f()[1][1] == 4) - local function g(): integer[2][2] - return (@integer[2][2]){{1,2},{3,4}} + local function g(): [2][2]integer + return (@[2][2]integer){{1,2},{3,4}} end assert(g()[0][0] == 1) assert(g()[0][1] == 2) assert(g()[1][0] == 3) assert(g()[1][1] == 4) - local R = @record{v: integer[4]} - local v = (@integer[4]){1,2,3,4} + local R = @record{v: [4]integer} + local v = (@[4]integer){1,2,3,4} local a: R = {v=v} assert(a.v[0] == 1 and a.v[1] == 2 and a.v[2] == 3 and a.v[3] == 4) ]]) @@ -1371,8 +1371,8 @@ end) it("multi dimensional arrays", function() assert.run_c([[ - local function f(): integer[2][2] - local a: integer[2][2] + local function f(): [2][2]integer + local a: [2][2]integer a[0][0] = 1 a[0][1] = 2 a[1][0] = 3 @@ -1384,8 +1384,8 @@ it("multi dimensional arrays", function() assert(f()[1][0] == 3) assert(f()[1][1] == 4) - local function g(): integer[2][2] - return (@integer[2][2]){{1,2},{3,4}} + local function g(): [2][2]integer + return (@[2][2]integer){{1,2},{3,4}} end print(g()[0][0]) print(g()[0][1]) @@ -1394,10 +1394,10 @@ it("multi dimensional arrays", function() do local Object = @record { - values: integer[0]*[0]* + values: *[0]*[0]integer } - local b: integer[4] = {1,2,3,4} - local a: integer[0]*[4] = {&b, &b, &b, &b} + local b: [4]integer = {1,2,3,4} + local a: [4]*[0]integer = {&b, &b, &b, &b} local o: Object o.values = &a assert(o.values[0][0] == 1) @@ -1491,7 +1491,7 @@ it("records size", function() end do - local R = @record { a: float64[64][64], c: cchar } + local R = @record { a: [64][64]float64, c: cchar } local r: R, s: integer ##[[cemit('s = sizeof(r);')]] assert(#R == s) end @@ -1533,14 +1533,14 @@ it("record methods", function() function vec2:length3() return self:length() end assert(v:length3() == 3) - function vec2.length4(self: vec2*) return self:length() end + function vec2.length4(self: *vec2) return self:length() end assert(v:length4() == 3) assert(vec2.length4(v) == 3) function vec2:lenmul(a: integer, b: integer) return (self.x + self.y)*a*b end assert(v:lenmul(2,3) == 18) - local vec2pointer = @vec2* + local vec2pointer = @*vec2 function vec2pointer:len() return self.x + self.y end assert(v:len() == 3) @@ -1553,9 +1553,9 @@ end) it("record metametods", function() assert.run_c([[ local intarray = @record { - data: integer[100] + data: [100]integer } - function intarray:__atindex(i: usize): integer* + function intarray:__atindex(i: usize): *integer return &self.data[i] end function intarray:__len(): isize @@ -1605,7 +1605,7 @@ it("record metametods", function() assert(g(4) == 4) local R = @record { - x: integer[2] + x: [2]integer } ## R.value.choose_braces_type = function() return types.ArrayType(primtypes.integer, 2) end function R.__convert(x: auto): R @@ -1627,7 +1627,7 @@ it("record string conversions", function() local s: stringview = r assert(s == 'R') local cs: cstring = r - assert((@stringview){size=1,data=(@byte[0]*)(cs)} == 'R') + assert((@stringview){size=1,data=(@*[0]byte)(cs)} == 'R') ]]) end) @@ -1715,7 +1715,7 @@ it("record globals", function() assert(Math.PI == 3) local R = @record{x: integer} - global R.values: integer[4] = {1,2,3,4} + global R.values: [4]integer = {1,2,3,4} assert(R.values[0] == 1) assert(R.values[1] == 2) assert(R.values[2] == 3) @@ -1725,14 +1725,14 @@ end) it("records referencing itself", function() assert.run_c([[ - local NodeA = @record{next: NodeA*} - local ap: NodeA* + local NodeA = @record{next: *NodeA} + local ap: *NodeA local a: NodeA assert(ap == nilptr and a.next == nilptr) - local NodeB = @record{next: NodeB*} + local NodeB = @record{next: *NodeB} local b: NodeB - local bp: NodeB* + local bp: *NodeB assert(bp == nilptr and b.next == nilptr) ]]) end) @@ -1799,15 +1799,15 @@ end) it("automatic reference", function() assert.run_c([[ local R = @record{x: integer} - local p: R*, q: R* + local p: *R, q: *R local a: R = R{1} p = a q = &a assert(p.x == q.x) assert(($p).x == 1) - local function f(p: R*) return $p end + local function f(p: *R) return $p end assert(f(a).x == 1) - local function g(): (integer, R*) return 1, a end + local function g(): (integer, *R) return 1, a end local function h(): (integer, R) return 1, p end local _, r: R = g() assert(r.x == 1) @@ -1816,9 +1816,9 @@ end) it("automatic dereference", function() assert.run_c([[ - local A = @integer[1] + local A = @[1]integer local a: A = {1} - local p: A* = &a + local p: *A = &a local b: A b = p assert(b[0] == 1) @@ -1930,9 +1930,9 @@ end) it("implicit casting for unbounded arrays", function() assert.run_c([[ local i: integer = 1 - local p: integer* = &i - local a4: integer[4] - local a: integer[0]* + local p: *integer = &i + local a4: [4]integer + local a: *[0]integer a = p p = a assert(i == 1) @@ -2061,8 +2061,8 @@ end) it("hook main", function() assert.run_c([[ - local function nelua_main(argc: cint, nelua_argv: cstring*): cint end - local function main(argc: cint, argv: cstring*): cint + local function nelua_main(argc: cint, nelua_argv: *cstring): cint end + local function main(argc: cint, argv: *cstring): cint print 'before' local ret = nelua_main(argc, argv) print 'after' @@ -2088,7 +2088,7 @@ it("sizeof builtin", function() assert(#@int16 == 2) assert(#@int32 == 4) assert(#@int64 == 8) - assert(#@int32[4] == 16) + assert(#@[4]int32 == 16) local A = @record{ s: int16, -- 2 @@ -2098,7 +2098,7 @@ it("sizeof builtin", function() -- 3 pad } assert(#A == 12) - assert(#@A[8] == 96) + assert(#@[8]A == 96) local B = @record{ i: int32, -- 4 @@ -2359,11 +2359,11 @@ it("GC requirements", function() assert.generate_c([=[ global gp: pointer global gr: record{x: pointer} - global ga: integer*[4] + global ga: [4]*integer global g local p: pointer local r: record{x: pointer} - local a: integer*[4] + local a: [4]*integer local l local function markp(what: pointer) @@ -2420,8 +2420,8 @@ it("concepts", function() ## print(a.type) assert(a[0] == x) end - local a: integer[4] = {1,2,3,4} - local b: number[3] = {5,6,7} + local a: [4]integer = {1,2,3,4} + local b: [3]number = {5,6,7} f(a, a[0], #a) f(b, b[0], #b) @@ -2506,7 +2506,7 @@ it("concepts", function() return v end - local MyArray = @record {data: integer[10]} + local MyArray = @record {data: [10]integer} function MyArray:__index(i: integer) return self.data[i] @@ -2516,7 +2516,7 @@ it("concepts", function() return #self.data end - local a: integer[10] = {1,2,3,4,5,6,7,8,9,10} + local a: [10]integer = {1,2,3,4,5,6,7,8,9,10} local b: MyArray = {data = a} assert(sum_container(&a) == 55) diff --git a/spec/06-preprocessor_spec.lua b/spec/06-preprocessor_spec.lua index e8670dd4..acf8e674 100644 --- a/spec/06-preprocessor_spec.lua +++ b/spec/06-preprocessor_spec.lua @@ -19,10 +19,10 @@ it("evaluate expressions", function() local e = 1 ]]) assert.ast_type_equals([=[ - local a: integer[10] + local a: [10]integer a[#[0]#] = 1 ]=], [[ - local a: integer[10] + local a: [10]integer a[0] = 1 ]]) assert.analyze_error("local a = #[function() end]#", "unable to convert preprocess value of lua type") @@ -207,7 +207,7 @@ it("print types", function() local n: float64 local s: stringview local b: boolean - local a: int64[2] + local a: [2]int64 local function f(a: int64, b: int64): (int64, int64) return 0,0 end local R: type = @record{a: integer, b: integer} function R:foo() return 1 end @@ -224,7 +224,7 @@ it("print types", function() local n: float64 local s: stringview local b: boolean - local a: int64[2] + local a: [2]int64 local function f(a: int64, b: int64): (int64, int64) return 0,0 end local R: type = @record{a: integer, b: integer} function R:foo() return 1 end diff --git a/tests/allocators_test.nelua b/tests/allocators_test.nelua index 8ebbc4f6..789ab5c2 100644 --- a/tests/allocators_test.nelua +++ b/tests/allocators_test.nelua @@ -6,27 +6,27 @@ require 'vector' do -- Arena local allocator: ArenaAllocator(1024,8) - local i: integer* = (@integer*)(allocator:alloc0(#integer)) + local i: *integer = (@*integer)(allocator:alloc0(#integer)) assert($i == 0) $i = 0xff assert($i == 0xff) assert(allocator.prev_offset == 0 and allocator.curr_offset == 8) allocator:dealloc(i) assert(allocator.prev_offset == 0 and allocator.curr_offset == 0) - local ni: integer* = (@integer*)(allocator:alloc0(#integer)) + local ni: *integer = (@*integer)(allocator:alloc0(#integer)) assert(ni == i and $ni == 0) assert(allocator.prev_offset == 0 and allocator.curr_offset == 8) - local a: integer[0]* = (@integer[0]*)(allocator:realloc0(i, 32 * #@integer, #@integer)) - assert((@integer*)(a) == i) + local a: *[0]integer = (@*[0]integer)(allocator:realloc0(i, 32 * #@integer, #@integer)) + assert((@*integer)(a) == i) assert(allocator.prev_offset == 0 and allocator.curr_offset == 8*32) for i=0,<32 do a[i] = 0xff end - a = (@integer[0]*)(allocator:realloc0(i, 8 * #@integer, 32 * #@integer)) + a = (@*[0]integer)(allocator:realloc0(i, 8 * #@integer, 32 * #@integer)) assert(allocator.prev_offset == 0 and allocator.curr_offset == 8*8) for i=0,<8 do assert(a[i] == 0xff) end assert(allocator:realloc0(i, 0, 8 * #@integer) == nilptr) for i=0,<32 do assert(a[i] == 0xff) end assert(allocator.prev_offset == 0 and allocator.curr_offset == 0) - a = (@integer[0]*)(allocator:realloc0(nilptr, 1024, 0)) + a = (@*[0]integer)(allocator:realloc0(nilptr, 1024, 0)) assert(a ~= nilptr) assert(allocator.prev_offset == 0 and allocator.curr_offset == 1024) assert(allocator:alloc0(1) == nilptr) @@ -68,14 +68,14 @@ do -- Pool y: integer } local allocator: PoolAllocator(Object, 2) - local o: Object* = (@Object*)(allocator:alloc0(#Object)) + local o: *Object = (@*Object)(allocator:alloc0(#Object)) assert(allocator.initialized == true) assert(o.x == 0) o.x = 0xff assert(o.x == 0xff) allocator:dealloc(o) -- must reuse the same free address - local no: Object* = (@Object*)(allocator:alloc0(#Object)) + local no: *Object = (@*Object)(allocator:alloc0(#Object)) assert(no == o and no.x == 0) -- last avail memory allocator:alloc0(#Object) @@ -84,18 +84,18 @@ do -- Pool -- free all allocator:dealloc_all() -- realloc - local i: integer* = (@integer*)(allocator:realloc0(nilptr, #integer, 0)) - local ri: integer* = (@integer*)(allocator:realloc0(i, 2*#integer, #integer)) + local i: *integer = (@*integer)(allocator:realloc0(nilptr, #integer, 0)) + local ri: *integer = (@*integer)(allocator:realloc0(i, 2*#integer, #integer)) -- should use the same address assert(i == ri) -- should deallocate - ri = (@integer*)(allocator:realloc(ri, 0, 2*#integer)) + ri = (@*integer)(allocator:realloc(ri, 0, 2*#integer)) assert(ri == nilptr) end do -- Heap local Allocator = @HeapAllocator(65536, true) - local IntVector = @vector(integer, Allocator*) + local IntVector = @vector(integer, *Allocator) local allocator: Allocator local va = IntVector.make(&allocator) local vb = IntVector.make(&allocator) diff --git a/tests/memory_test.nelua b/tests/memory_test.nelua index 9b2f26e5..e8434d84 100644 --- a/tests/memory_test.nelua +++ b/tests/memory_test.nelua @@ -20,7 +20,7 @@ end do -- alloc0 assert(allocator:alloc0(0) == nilptr) - local p = (@vec2*)(allocator:alloc0(#@vec2)) + local p = (@*vec2)(allocator:alloc0(#@vec2)) assert(p) assert(p.x == 0 and p.y == 0) allocator:dealloc(p) @@ -40,27 +40,27 @@ end do -- realloc0 assert(allocator:realloc0(nilptr, 0, 0) == nilptr) - local p = (@vec2[0]*)(allocator:alloc0(#@vec2)) + local p = (@*[0]vec2)(allocator:alloc0(#@vec2)) assert(p) assert(p[0].x == 0 and p[0].y == 0) p[0] = vec2{x=1, y=2} - p = (@vec2[0]*)(allocator:realloc0(p, 2*#@vec2, #@vec2)) + p = (@*[0]vec2)(allocator:realloc0(p, 2*#@vec2, #@vec2)) assert(p) assert(p[0].x == 1 and p[0].y == 2) assert(p[1].x == 0 and p[1].y == 0) - p = (@vec2[0]*)(allocator:realloc0(p, #@vec2, #@vec2)) + p = (@*[0]vec2)(allocator:realloc0(p, #@vec2, #@vec2)) assert(p) assert(p[0].x == 1 and p[0].y == 2) - p = (@vec2[0]*)(allocator:realloc0(p, #@vec2, 0)) + p = (@*[0]vec2)(allocator:realloc0(p, #@vec2, 0)) assert(p) assert(p[0].x == 0 and p[0].y == 0) - p = (@vec2[0]*)(allocator:realloc0(p, 0, #@vec2)) + p = (@*[0]vec2)(allocator:realloc0(p, 0, #@vec2)) assert(p == nilptr) end do -- copy - local pa = (@vec2*)(allocator:alloc0(#@vec2)) - local pb = (@vec2*)(allocator:alloc0(#@vec2)) + local pa = (@*vec2)(allocator:alloc0(#@vec2)) + local pb = (@*vec2)(allocator:alloc0(#@vec2)) $pa = {x=1,y=2} memory.copy(pb, pa, #@vec2) assert(pb.x == 1 and pb.y == 2) @@ -69,16 +69,16 @@ do -- copy end do -- move - local p = (@vec2[0]*)(allocator:alloc0(2*#@vec2)) + local p = (@*[0]vec2)(allocator:alloc0(2*#@vec2)) local pa, pb = &p[0], &p[1] $pa, $pb = {x=1,y=2}, {x=3,y=4} - memory.move(&((@integer[0]*)(p))[1], p, 3 * #@integer) + memory.move(&((@*[0]integer)(p))[1], p, 3 * #@integer) assert(pa.x == 1 and pa.y == 1 and pb.x == 2 and pb.y == 3) allocator:dealloc(p) end do -- set and zero - local p = (@vec2*)(allocator:alloc0(#@vec2)) + local p = (@*vec2)(allocator:alloc0(#@vec2)) memory.set(p, 0xff, #@vec2) assert(p.x == 0xffffffffffffffff and p.y == 0xffffffffffffffff) memory.zero(p, #@vec2) @@ -87,8 +87,8 @@ do -- set and zero end do -- compare - local pa = (@vec2*)(allocator:alloc0(#@vec2)) - local pb = (@vec2*)(allocator:alloc0(#@vec2)) + local pa = (@*vec2)(allocator:alloc0(#@vec2)) + local pb = (@*vec2)(allocator:alloc0(#@vec2)) assert(memory.compare(pa, pb, #@vec2) == 0) pa.x = 1 pb.x = 2 assert(memory.compare(pa, pb, #@vec2) == -1) @@ -101,8 +101,8 @@ do -- compare end do -- equals - local pa = (@vec2*)(allocator:alloc0(#@vec2)) - local pb = (@vec2*)(allocator:alloc0(#@vec2)) + local pa = (@*vec2)(allocator:alloc0(#@vec2)) + local pb = (@*vec2)(allocator:alloc0(#@vec2)) assert(memory.equals(pa, pb, #@vec2)) pa.x = 1 assert(not memory.equals(pa, pb, #@vec2)) @@ -111,7 +111,7 @@ do -- equals end do -- scan - local p = (@vec2*)(allocator:alloc0(#@vec2)) + local p = (@*vec2)(allocator:alloc0(#@vec2)) p.x = 1 p.y = 2 assert(memory.scan(p, 1, #@vec2) == &p.x) assert(memory.scan(p, 2, #@vec2) == &p.y) @@ -120,7 +120,7 @@ do -- scan end do -- find - local p = (@vec2*)(allocator:alloc0(#@vec2)) + local p = (@*vec2)(allocator:alloc0(#@vec2)) p.x = 1 p.y = 2 local x: integer, y: integer = 1, 2 assert(memory.find(p, #@vec2, &x, #@integer) == &p.x) @@ -199,8 +199,8 @@ do -- spanmove local p = allocator:spanalloc0(@vec2, 2) local pa, pb = &p[0], &p[1] $pa, $pb = {x=1,y=2}, {x=3,y=4} - local sb = (@span(integer))({ data=(@integer[0]*)(p.data), size=3 }) - local db = (@span(integer))({ data=(@integer[0]*)(&((@integer[0]*)(p.data))[1]), size=3 }) + local sb = (@span(integer))({ data=(@*[0]integer)(p.data), size=3 }) + local db = (@span(integer))({ data=(@*[0]integer)(&((@*[0]integer)(p.data))[1]), size=3 }) memory.spanmove(db, sb) --assert(pa.x == 1 and pa.y == 1 and pb.x == 2 and pb.y == 3) allocator:spandealloc(p) @@ -302,7 +302,7 @@ end -- moving and swapping values local NonCopyable = @record{x: integer} -function NonCopyable:__copy(other: NonCopyable*) +function NonCopyable:__copy(other: *NonCopyable) error "copy not expected" end diff --git a/tests/raii_test2.nelua b/tests/raii_test2.nelua index b9dc71eb..a1412a4b 100644 --- a/tests/raii_test2.nelua +++ b/tests/raii_test2.nelua @@ -17,7 +17,7 @@ function Obj:__destroy() self.id = 0 end -function Obj:__copy(o: Obj*) +function Obj:__copy(o: *Obj) --print('copy', self.id) self.id = o.id assert(self.id ~= 0) diff --git a/tests/resourcepool_test.nelua b/tests/resourcepool_test.nelua index 127afd70..6ab7ac8a 100644 --- a/tests/resourcepool_test.nelua +++ b/tests/resourcepool_test.nelua @@ -3,7 +3,7 @@ require 'resourcepool' local SLOT_SHIFT = 32 local SLOT_MASK = (1 << SLOT_SHIFT) - 1 -local id: uint64, p: integer* +local id: uint64, p: *integer local pool: resourcepool(integer) assert(pool:get(0) == nilptr) diff --git a/tests/string_test.nelua b/tests/string_test.nelua index 5ccd54c7..f5c43fad 100644 --- a/tests/string_test.nelua +++ b/tests/string_test.nelua @@ -4,7 +4,7 @@ local maxi: integer = #[primtypes.isize.max]# local mini: integer = #[primtypes.isize.min]# do -- initialization, len, eq and reset - local arr: byte[6] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00} + local arr: [6]byte = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00} local a: string = (@cstring)(&arr[0]) local b: string = (@cstring)('hello') assert('a' == 'a') @@ -110,7 +110,7 @@ do -- char end do -- byte - local arr: byte[4] = {1,2,3,0} + local arr: [4]byte = {1,2,3,0} local a: string = (@cstring)(&arr[0]) assert(a:byte( 1) == 1 and a:byte( 2) == 2 and a:byte( 3) == 3) assert(a:byte(-3) == 1 and a:byte(-2) == 2 and a:byte(-1) == 3)