Skip to content

Commit

Permalink
New syntax, sugar type expressions must be placed in the left
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Sep 4, 2020
1 parent 0261c67 commit 556660d
Show file tree
Hide file tree
Showing 43 changed files with 373 additions and 359 deletions.
28 changes: 14 additions & 14 deletions docs/pages/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
```

Expand All @@ -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
```

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -1982,15 +1982,15 @@ 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
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
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: int32[1024]
local data: [1024]int32
local ptr: int32 = 0

##[=[
Expand Down
42 changes: 21 additions & 21 deletions examples/condots.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ local SDL_Surface <cimport,nodecl> = @record{
pitch: cint,
pixels: pointer
}
local SDL_Event <cimport,nodecl> = @record{type: uint32, padding: byte[56]}
local SDL_Event <cimport,nodecl> = @record{type: uint32, padding: [56]byte}
local SDL_Rect <cimport,nodecl> = @record{x: cint, y: cint, w: cint, h: cint}
local SDL_Window <cimport,nodecl> = @record{}
local SDL_Renderer <cimport,nodecl> = @record{}
Expand Down Expand Up @@ -64,30 +64,30 @@ local SDL_TEXTUREACCESS_STREAMING: cint <cimport,nodecl>

-- import SDL functions
local function SDL_Init(flags: uint32): int32 <cimport,nodecl> end
local function SDL_CreateWindow(title: cstring, x: cint, y: cint, w: cint, h: cint, flags: uint32): SDL_Window* <cimport,nodecl> end
local function SDL_CreateWindow(title: cstring, x: cint, y: cint, w: cint, h: cint, flags: uint32): *SDL_Window <cimport,nodecl> end
local function SDL_Quit() <cimport,nodecl> end
local function SDL_DestroyWindow(window: SDL_Window*) <cimport,nodecl> end
local function SDL_PollEvent(event: SDL_Event*): int32 <cimport,nodecl> end
local function SDL_DestroyWindow(window: *SDL_Window) <cimport,nodecl> end
local function SDL_PollEvent(event: *SDL_Event): int32 <cimport,nodecl> end
local function SDL_GetTicks(): uint32 <cimport,nodecl> end
local function SDL_Delay(ms: uint32) <cimport,nodecl> end
local function SDL_CreateRGBSurfaceWithFormatFrom(pixels: pointer, width: cint,height: cint, depth: cint, pitch: cint,format: uint32): SDL_Surface <cimport,nodecl> end
local function SDL_FreeSurface(surface: SDL_Surface) <cimport,nodecl> end
local function SDL_LockSurface(surface: SDL_Surface) <cimport,nodecl> end
local function SDL_GetWindowSurface(window: SDL_Window*): SDL_Surface <cimport,nodecl> end
local function SDL_BlitSurface(src: SDL_Surface, srcrect: SDL_Rect*, dst: SDL_Surface, dstrect: SDL_Rect*): cint <cimport,nodecl> end
local function SDL_UpdateWindowSurface(window: SDL_Window*) <cimport,nodecl> end
local function SDL_CreateRenderer(window: SDL_Window*, index: cint, flags: uint32): SDL_Renderer* <cimport,nodecl> end
local function SDL_DestroyRenderer(renderer: SDL_Renderer*) <cimport,nodecl> end
local function SDL_RenderPresent(renderer: SDL_Renderer*) <cimport,nodecl> end
local function SDL_RenderClear(renderer: SDL_Renderer*) <cimport,nodecl> end
local function SDL_CreateTexture(renderer: SDL_Renderer*, format: uint32, access: cint, w: cint, h: cint): SDL_Texture* <cimport,nodecl> end
local function SDL_DestroyTexture(texture: SDL_Texture*) <cimport,nodecl> end
local function SDL_RenderCopy(renderer: SDL_Renderer*, texture: SDL_Texture*, srcrect: SDL_Rect*, dstrect: SDL_Rect*): cint <cimport,nodecl> end
local function SDL_LockTexture(texture: SDL_Texture*, rect: SDL_Rect*, pixels: pointer*, pitch: cint*): cint <cimport,nodecl> end
local function SDL_UnlockTexture(texture: SDL_Texture*) <cimport,nodecl> end
local function SDL_SetRenderDrawBlendMode(renderer: SDL_Renderer*, blendMode: int32): cint <cimport,nodecl> end
local function SDL_SetTextureBlendMode(texture: SDL_Texture*, blendMode: int32): cint <cimport,nodecl> end
local function SDL_UpdateTexture(texture: SDL_Texture*, rect: SDL_Rect*, pixels: pointer, pitch: cint): cint <cimport,nodecl> end
local function SDL_GetWindowSurface(window: *SDL_Window): SDL_Surface <cimport,nodecl> end
local function SDL_BlitSurface(src: SDL_Surface, srcrect: *SDL_Rect, dst: SDL_Surface, dstrect: *SDL_Rect): cint <cimport,nodecl> end
local function SDL_UpdateWindowSurface(window: *SDL_Window) <cimport,nodecl> end
local function SDL_CreateRenderer(window: *SDL_Window, index: cint, flags: uint32): *SDL_Renderer <cimport,nodecl> end
local function SDL_DestroyRenderer(renderer: *SDL_Renderer) <cimport,nodecl> end
local function SDL_RenderPresent(renderer: *SDL_Renderer) <cimport,nodecl> end
local function SDL_RenderClear(renderer: *SDL_Renderer) <cimport,nodecl> end
local function SDL_CreateTexture(renderer: *SDL_Renderer, format: uint32, access: cint, w: cint, h: cint): *SDL_Texture <cimport,nodecl> end
local function SDL_DestroyTexture(texture: *SDL_Texture) <cimport,nodecl> end
local function SDL_RenderCopy(renderer: *SDL_Renderer, texture: *SDL_Texture, srcrect: *SDL_Rect, dstrect: *SDL_Rect): cint <cimport,nodecl> end
local function SDL_LockTexture(texture: *SDL_Texture, rect: *SDL_Rect, pixels: *pointer, pitch: *cint): cint <cimport,nodecl> end
local function SDL_UnlockTexture(texture: *SDL_Texture) <cimport,nodecl> end
local function SDL_SetRenderDrawBlendMode(renderer: *SDL_Renderer, blendMode: int32): cint <cimport,nodecl> end
local function SDL_SetTextureBlendMode(texture: *SDL_Texture, blendMode: int32): cint <cimport,nodecl> end
local function SDL_UpdateTexture(texture: *SDL_Texture, rect: *SDL_Rect, pixels: pointer, pitch: cint): cint <cimport,nodecl> end
local function SDL_GetError(): cstring <cimport,nodecl> end

--------------------------------------------------------------------------------
Expand All @@ -98,15 +98,15 @@ local window, renderer, texture

local SCREEN_WIDTH <comptime> = 1280
local SCREEN_HEIGHT <comptime> = 720
local pixels: vec4b[SCREEN_WIDTH][SCREEN_HEIGHT]
local pixels: [SCREEN_HEIGHT][SCREEN_WIDTH]vec4b

local NUM_PARTICLES <comptime> = 200
local RADIUS <comptime> = 3
local MIN_DISTANCE <comptime> = 40
local MAX_DISTANCE <comptime> = 80
local MIN_DISTANCE2 <comptime> = MIN_DISTANCE * MIN_DISTANCE
local MAX_DISTANCE2 <comptime> = MAX_DISTANCE * MAX_DISTANCE
local particles: Particle[NUM_PARTICLES]
local particles: [NUM_PARTICLES]Particle

do
for i=0,<NUM_PARTICLES do
Expand Down
14 changes: 7 additions & 7 deletions examples/linkedlist.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ local allocator: auto = general_allocator
## local make_list = generalize(function(T)
local T = @#[T]#
local ListNodeT <codename #['ListNode_'..T.codename]#> = @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
Expand All @@ -19,8 +19,8 @@ local allocator: auto = general_allocator
end

local ListT <codename #['List_'..T.codename]#> = @record{
head: ListNodeT*,
tail: ListNodeT*
head: *ListNodeT,
tail: *ListNodeT
}

function ListT:prepend(value: T)
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/mersenne.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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 <comptime>, M <comptime> = MT19937_N, MT19937_M
local y
Expand Down
8 changes: 4 additions & 4 deletions examples/overview.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -642,7 +642,7 @@ do -- Mixing C code
local function memset(s: pointer, c: int32, n: usize): pointer <cimport'memset',cinclude'<string.h>',nodecl> end
local function free(ptr: pointer) <cimport'free',cinclude'<stdlib.h>',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
Expand Down
18 changes: 9 additions & 9 deletions examples/record_inheretance.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
24 changes: 12 additions & 12 deletions examples/snakesdl.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require 'math'
-- import SDL structures
local SDL_Event <cimport> = @record {
type: uint32,
padding: byte[56]
padding: [56]byte
}
local SDL_Keysym <cimport> = @record {
scancode: cint,
Expand Down Expand Up @@ -45,16 +45,16 @@ local SDL_RENDERER_PRESENTVSYNC <comptime> = 0x4

-- import SDL functions
local function SDL_Init(flags: uint32): int32 <cimport> end
local function SDL_CreateWindow(title: cstring, x: cint, y: cint, w: cint, h: cint, flags: uint32): SDL_Window* <cimport> end
local function SDL_CreateWindow(title: cstring, x: cint, y: cint, w: cint, h: cint, flags: uint32): *SDL_Window <cimport> end
local function SDL_Quit() <cimport> end
local function SDL_DestroyWindow(window: SDL_Window*) <cimport> end
local function SDL_PollEvent(event: SDL_Event*): int32 <cimport> end
local function SDL_CreateRenderer(window: SDL_Window*, index: cint, flags: uint32): SDL_Renderer* <cimport> end
local function SDL_DestroyRenderer(renderer: SDL_Renderer*) <cimport> end
local function SDL_RenderPresent(renderer: SDL_Renderer*) <cimport> end
local function SDL_RenderClear(renderer: SDL_Renderer*) <cimport> end
local function SDL_SetRenderDrawColor(renderer: SDL_Renderer*, r: uint8, g: uint8, b: uint8, a: uint8): cint <cimport> end
local function SDL_RenderFillRect(renderer: SDL_Renderer*, rect: SDL_Rect*): cint <cimport> end
local function SDL_DestroyWindow(window: *SDL_Window) <cimport> end
local function SDL_PollEvent(event: *SDL_Event): int32 <cimport> end
local function SDL_CreateRenderer(window: *SDL_Window, index: cint, flags: uint32): *SDL_Renderer <cimport> end
local function SDL_DestroyRenderer(renderer: *SDL_Renderer) <cimport> end
local function SDL_RenderPresent(renderer: *SDL_Renderer) <cimport> end
local function SDL_RenderClear(renderer: *SDL_Renderer) <cimport> end
local function SDL_SetRenderDrawColor(renderer: *SDL_Renderer, r: uint8, g: uint8, b: uint8, a: uint8): cint <cimport> end
local function SDL_RenderFillRect(renderer: *SDL_Renderer, rect: *SDL_Rect): cint <cimport> end
local function SDL_GetTicks(): uint32 <cimport> end

-- game types
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 556660d

Please sign in to comment.