Skip to content

Commit

Permalink
One syntax for variable attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Sep 2, 2019
1 parent f6c561f commit 130fdf6
Show file tree
Hide file tree
Showing 29 changed files with 341 additions and 351 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# AlpineLinux
FROM alpine:3.9
FROM alpine:3.10
RUN apk update
RUN apk upgrade
RUN apk add bash sudo curl build-base git
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/js/highlightjs-nelua.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ hljs.registerLanguage("nelua", function(e) {
//Lua
"function and break do else elseif end for goto if in local not or repeat return then until while " +
//Extended lua
"switch case continue compconst const global" +
"switch case continue global" +
"defer record enum"
,
built_in:
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ then true until while
| `@` | type inference |
| `::` | label definition |
| `--` | comment |
| `{: :}` | pragma |
| `!` | attribute |


48 changes: 21 additions & 27 deletions docs/pages/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ Variables are declared or defined like in lua, but optionally
you can specify it's type when declaring:

```nelua
local a = nil -- of deduced type 'any', initialized to nil
local b = false -- of deduced type 'boolean', initialized to false
local d = 1 -- of type 'integer', initialized to 1
local i = 1 -- of type 'integer', initialized to 1
local e: integer = 1 -- of type 'integer', initialized to 1
local a = nil -- of deduced type 'any', initialized to nil
local s = 'string'
```

Nelua takes advantages of types to make checks and optimizations at compile time.
Expand Down Expand Up @@ -92,7 +93,7 @@ local a -- variable of deduced type 'any', initialized to 'nil'
local b: integer -- variable of type 'integer', initialized to 0
```

This can be optionally be disabled (for optimization reasons) using **pragmas**.
This can be optionally be disabled (for optimization reasons) using **attributes**.

### Auto variables

Expand All @@ -112,7 +113,7 @@ Auto variables are more useful when used in **lazy functions**.
Compconst variables have its value known at compile time:

```nelua
local a: compconst = 1 + 2 -- constant variable of value '3' evaluated and known at compile time
local a !compconst = 1 + 2 -- constant variable of value '3' evaluated and known at compile time
```

The compiler takes advantages of constants to make optimizations, constants are also useful
Expand All @@ -124,10 +125,10 @@ Const variables can be assigned at runtime however it cannot mutate.

```nelua
local x = 1
local a: const = x -- constant variable of value '3' evaluated and known at compile time
local a !const = x
-- doing "a = 2" would throw a compile time error
local function f(x: const integer)
local function f(x: integer !const)
-- cannot assign 'x' here because it's const
end
```
Expand Down Expand Up @@ -537,15 +538,17 @@ a[1] = 0
Union can store multiple types at the same block of memory:

```nelua
local u: union<integer,string> -- variable of type union, initialized to its first type 'integer'
local u: union{integer,string} -- variable of type union, initialized to its first type 'integer'
print(u) -- outputs 0
u = 'string' -- u now holds a string
print(u) -- outputs 'string'
local u: union<uint16>{integer,string} -- union that can hold more runtime types
local u: union<void>{integer,string} -- union that holds all types at the same time (unsafe)
```

Unions are slightly different from C union, because it has an `uint8` internally that holds
the current type pointer at runtime, thus the union size will have at least the
size of the largest type plus the size of `uint8`. Unions cannot hold more than 256 different types.
Unions are slightly different from C union by default, because it has an `uint8` internally that holds the current type at runtime, thus the union size will have at least the
size of the largest type plus the size of `uint8`. By default unions cannot hold more than 256 different types. The internal type

### Nilable

Expand All @@ -558,14 +561,14 @@ Optional type is actually a union of a `nilable` and any other type, it
is used to declare a variable that may hold or not a variable:

```nelua
local a: union<nilable,string> -- variable that may hold a string, initialized to 'nil'
local a: union{nilable,string} -- variable that may hold a string, initialized to 'nil'
assert(a == nil)
assert(not a)
print(a) -- outputs 'nil'
a = 'hi'
print(a) -- outputs 'hi'
-- syntax sugar for union union<nilable,string>
-- syntax sugar for union union{nilable,string}
local v: string?
```

Expand Down Expand Up @@ -1037,7 +1040,7 @@ Lazy functions can make compile time dynamic functions when used in combination
the preprocessor:

```nelua
function pow(x: auto, n: compconst integer)
function pow(x: auto, n: integer !compconst)
## symbols.x.attr.type:is_integral() then
-- x is an integral type (any unsigned/signed integer)
local r: #[symbols.x.attr.type] = 1
Expand Down Expand Up @@ -1065,7 +1068,7 @@ Blocks can be passed to lazy functions, in this case the entire function code wi
inlined in the call placement.

```nelua
local function unroll(count: compconst integer, body: block)
local function unroll(count: integer !compconst, body: block)
## for i=1,symbols.count.attr.value do
body()
## end
Expand All @@ -1077,29 +1080,20 @@ print(a) -- outputs 4
```

--------------------------------------------------------------------------------
## Pragmas
## Attributes

Pragmas are used to inform the compiler different behaviours in the code
Attributes are used to inform the compiler different behaviours in the code
generation.

### Global pragmas

Global pragmas begins with `!!` followed by it's name and parameters.

```nelua
!!cinclude '<stdio.h>' -- include a C header
!!linklib "SDL" -- link SDL library
```

### Function pragmas
### Function attributes

```nelua
function sum(a, b) !inline -- inline function
return a + b
end
```

### Variable pragmas
### Variable attributes

```nelua
local a: integer !noinit-- don't initialize variable to zeros
Expand Down
8 changes: 4 additions & 4 deletions examples/mersenne.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- Mersenne Twister random number generator, implementation taken from
-- http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c

local MT19937_N: compconst uint32 = 624
local MT19937_N: uint32 !compconst = 624

local mt19937 = @record {
i: uint32,
Expand All @@ -23,9 +23,9 @@ end
-- generates a random number on [0, 0xffffffff]
function mt19937:random_uint32()
local MAG01: uint32[2] = {0x0, 0x9908b0df}
local LOWER_MASK: compconst = 0x7fffffff_u32
local UPPER_MASK: compconst = 0x80000000_u32
local N: compconst, M: compconst = MT19937_N, 397_u32
local LOWER_MASK !compconst = 0x7fffffff_u32
local UPPER_MASK !compconst = 0x80000000_u32
local N !compconst, M !compconst = MT19937_N, 397_u32
local y
if self.i >= N then
for j=0_u32,<N-M do
Expand Down
14 changes: 7 additions & 7 deletions examples/snakesdl.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ local Direction = @enum<byte>{NONE=0, UP, DOWN, RIGHT, LEFT}
local Color = @record{r: byte, g: byte, b: byte}

-- game constants
local TILE_SIZE: compconst = 64
local GRID_SIZE: compconst = 12
local SCREEN_SIZE: compconst = @cint(64 * 12) --@cint(GRID_SIZE * TILE_SIZE)
local MOVE_DELAY: compconst = 128
local COLOR_RED: compconst = Color{r=255, g=96, b=96}
local COLOR_GREEN: compconst = Color{r=96, g=255, b=96}
local COLOR_BLACK: compconst = Color{r=0, g=0, b=0}
local TILE_SIZE !compconst = 64
local GRID_SIZE !compconst = 12
local SCREEN_SIZE !compconst = @cint(64 * 12) --@cint(GRID_SIZE * TILE_SIZE)
local MOVE_DELAY !compconst = 128
local COLOR_RED !compconst = Color{r=255, g=96, b=96}
local COLOR_GREEN !compconst = Color{r=96, g=255, b=96}
local COLOR_BLACK !compconst = Color{r=0, g=0, b=0}

-- game state variables
local renderer
Expand Down
6 changes: 3 additions & 3 deletions lib/C/errno.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
global C.errno: cint !cimport 'errno'

-- Constants
const C.EDOM: cint !cimport 'EDOM'
const C.EILSEQ: cint !cimport 'EILSEQ'
const C.ERANGE: cint !cimport 'ERANGE'
global C.EDOM: cint !const !cimport 'EDOM'
global C.EILSEQ: cint !const !cimport 'EILSEQ'
global C.ERANGE: cint !const !cimport 'ERANGE'
12 changes: 6 additions & 6 deletions lib/C/locale.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ function C.setlocale(category: cint, locale: cstring): cstring !cimport 'setloca
function C.localeconv(): lconv* !cimport 'localeconv' end

-- Constants
const C.LC_ALL: cint !cimport 'LC_ALL'
const C.LC_COLLATE: cint !cimport 'LC_COLLATE'
const C.LC_CTYPE: cint !cimport 'LC_CTYPE'
const C.LC_MONETARY: cint !cimport 'LC_MONETARY'
const C.LC_NUMERIC: cint !cimport 'LC_NUMERIC'
const C.LC_TIME: cint !cimport 'LC_TIME'
global C.LC_ALL: cint !const !cimport 'LC_ALL'
global C.LC_COLLATE: cint !const !cimport 'LC_COLLATE'
global C.LC_CTYPE: cint !const !cimport 'LC_CTYPE'
global C.LC_MONETARY: cint !const !cimport 'LC_MONETARY'
global C.LC_NUMERIC: cint !const !cimport 'LC_NUMERIC'
global C.LC_TIME: cint !const !cimport 'LC_TIME'
36 changes: 18 additions & 18 deletions lib/C/math.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -288,26 +288,26 @@ function C.isunorderedf(x: float32, y: float32): cint !cimport 'isunordered' end
function C.isunorderedl(x: clongdouble, y: clongdouble): cint !cimport 'isunordered' end

-- Constants
const C.HUGE_VALF: float32 !cimport 'HUGE_VALF'
const C.HUGE_VAL: float64 !cimport 'HUGE_VAL'
const C.HUGE_VALL: clongdouble !cimport 'HUGE_VALL'
global C.HUGE_VALF: float32 !const !cimport 'HUGE_VALF'
global C.HUGE_VAL: float64 !const !cimport 'HUGE_VAL'
global C.HUGE_VALL: clongdouble !const !cimport 'HUGE_VALL'

const C.INFINITY: cfloat !cimport 'INFINITY'
const C.NAN: cfloat !cimport 'NAN'
global C.INFINITY: cfloat !const !cimport 'INFINITY'
global C.NAN: cfloat !const !cimport 'NAN'

const C.FP_FAST_FMAF: float32 !cimport 'FP_FAST_FMAF'
const C.FP_FAST_FMA: float64 !cimport 'FP_FAST_FMA'
const C.FP_FAST_FMAL: clongdouble !cimport 'FP_FAST_FMAL'
global C.FP_FAST_FMAF: float32 !const !cimport 'FP_FAST_FMAF'
global C.FP_FAST_FMA: float64 !const !cimport 'FP_FAST_FMA'
global C.FP_FAST_FMAL: clongdouble !const !cimport 'FP_FAST_FMAL'

const C.FP_ILOGB0: cint !cimport 'FP_ILOGB0'
const C.FP_ILOGBNAN: cint !cimport 'FP_ILOGBNAN'
global C.FP_ILOGB0: cint !const !cimport 'FP_ILOGB0'
global C.FP_ILOGBNAN: cint !const !cimport 'FP_ILOGBNAN'

const C.math_errhandling: cint !cimport 'math_errhandling'
const C.MATH_ERRNO: cint !cimport 'MATH_ERRNO'
const C.MATH_ERREXCEPT: cint !cimport 'MATH_ERREXCEPT'
global C.math_errhandling: cint !const !cimport 'math_errhandling'
global C.MATH_ERRNO: cint !const !cimport 'MATH_ERRNO'
global C.MATH_ERREXCEPT: cint !const !cimport 'MATH_ERREXCEPT'

const C.FP_NORMAL: cint !cimport 'FP_NORMAL'
const C.FP_SUBNORMAL: cint !cimport 'FP_SUBNORMAL'
const C.FP_ZERO: cint !cimport 'FP_ZERO'
const C.FP_INFINITE: cint !cimport 'FP_INFINITE'
const C.FP_NAN: cint !cimport 'FP_NAN'
global C.FP_NORMAL: cint !const !cimport 'FP_NORMAL'
global C.FP_SUBNORMAL: cint !const !cimport 'FP_SUBNORMAL'
global C.FP_ZERO: cint !const !cimport 'FP_ZERO'
global C.FP_INFINITE: cint !const !cimport 'FP_INFINITE'
global C.FP_NAN: cint !const !cimport 'FP_NAN'
18 changes: 9 additions & 9 deletions lib/C/signal.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
function C.signal(sig: cint, handler: pointer): pointer !cimport 'signal' end
function C.raise(sig: cint): cint !cimport 'raise' end

const C.SIG_DFL: pointer !cimport 'SIG_DFL'
const C.SIG_IGN: pointer !cimport 'SIG_IGN'
const C.SIG_ERR: pointer !cimport 'SIG_ERR'
global C.SIG_DFL: pointer !const !cimport 'SIG_DFL'
global C.SIG_IGN: pointer !const !cimport 'SIG_IGN'
global C.SIG_ERR: pointer !const !cimport 'SIG_ERR'

const C.SIGTERM: cint !cimport 'SIGTERM'
const C.SIGSEGV: cint !cimport 'SIGSEGV'
const C.SIGINT: cint !cimport 'SIGINT'
const C.SIGILL: cint !cimport 'SIGILL'
const C.SIGABRT: cint !cimport 'SIGABRT'
const C.SIGFPE: cint !cimport 'SIGFPE'
global C.SIGTERM: cint !const !cimport 'SIGTERM'
global C.SIGSEGV: cint !const !cimport 'SIGSEGV'
global C.SIGINT: cint !const !cimport 'SIGINT'
global C.SIGILL: cint !const !cimport 'SIGILL'
global C.SIGABRT: cint !const !cimport 'SIGABRT'
global C.SIGFPE: cint !const !cimport 'SIGFPE'
24 changes: 12 additions & 12 deletions lib/C/stdio.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ global C.stdout: FILE* !cimport 'stdout'
global C.stderr: FILE* !cimport 'stderr'

-- Constants
const C.EOF: cint !cimport 'EOF'
const C.BUFSIZ: cint !cimport 'BUFSIZ'
global C.EOF: cint !const !cimport 'EOF'
global C.BUFSIZ: cint !const !cimport 'BUFSIZ'

const C.FOPEN_MAX: cint !cimport 'FOPEN_MAX'
const C.FILENAME_MAX: cint !cimport 'FILENAME_MAX'
global C.FOPEN_MAX: cint !const !cimport 'FOPEN_MAX'
global C.FILENAME_MAX: cint !const !cimport 'FILENAME_MAX'

const C._IOFBF: cint !cimport '_IOFBF'
const C._IOLBF: cint !cimport '_IOLBF'
const C._IONBF: cint !cimport '_IONBF'
global C._IOFBF: cint !const !cimport '_IOFBF'
global C._IOLBF: cint !const !cimport '_IOLBF'
global C._IONBF: cint !const !cimport '_IONBF'

const C.SEEK_SET: cint !cimport 'SEEK_SET'
const C.SEEK_CUR: cint !cimport 'SEEK_CUR'
const C.SEEK_END: cint !cimport 'SEEK_END'
global C.SEEK_SET: cint !const !cimport 'SEEK_SET'
global C.SEEK_CUR: cint !const !cimport 'SEEK_CUR'
global C.SEEK_END: cint !const !cimport 'SEEK_END'

const C.TMP_MAX: cint !cimport 'TMP_MAX'
const C.L_tmpnam: cint !cimport 'L_tmpnam'
global C.TMP_MAX: cint !const !cimport 'TMP_MAX'
global C.L_tmpnam: cint !const !cimport 'L_tmpnam'
8 changes: 4 additions & 4 deletions lib/C/stdlib.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ function C.ldiv(numer: clong, denom: clong): ldiv_t !cimport 'ldiv' end
function C.lldiv(numer: clonglong, denom: clonglong): lldiv_t !cimport 'lldiv' end

-- Constants
const C.EXIT_SUCCESS: cint !cimport 'EXIT_SUCCESS'
const C.EXIT_FAILURE: cint !cimport 'EXIT_FAILURE'
global C.EXIT_SUCCESS: cint !const !cimport 'EXIT_SUCCESS'
global C.EXIT_FAILURE: cint !const !cimport 'EXIT_FAILURE'

const C.RAND_MAX: cint !cimport 'RAND_MAX'
compconst C.NULL = nilptr
global C.RAND_MAX: cint !const !cimport 'RAND_MAX'
global C.NULL !compconst !nodecl = nilptr
2 changes: 1 addition & 1 deletion lib/C/time.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ function C.localtime(timer: time_t*): tm* !cimport 'localtime' end
function C.timespec_get(ts: timespec*, base: cint): cint !cimport 'timespec_get' end

-- Constants
const C.CLOCKS_PER_SEC: clock_t !cimport 'CLOCKS_PER_SEC'
global C.CLOCKS_PER_SEC: clock_t !const !cimport 'CLOCKS_PER_SEC'
10 changes: 5 additions & 5 deletions lib/math.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

global math = @record{}

global math.pi: compconst = 3.141592653589793
global math.maxinteger: compconst = 9223372036854775807
global math.mininteger: compconst = -9223372036854775807-1
global math.huge: const number !cimport 'HUGE_VAL'
global math.pi: number !compconst = 3.141592653589793
global math.maxinteger: integer !compconst = 9223372036854775807
global math.mininteger: integer !compconst = -9223372036854775807-1
global math.huge: number !const !cimport 'HUGE_VAL'

function math.abs(x: number): number !cimport 'fabs' end
function math.ceil(x: number): number !cimport 'ceil' end
Expand Down Expand Up @@ -75,7 +75,7 @@ end

local xoshiro256 = @record{state: uint64[4]}

local compconst FLOAT64_MANT_DIGS = 53
local FLOAT64_MANT_DIGS !compconst = 53

local function rotl(x: uint64, n: int32) !inline
return (x << n) | (x >> (64 - n))
Expand Down
4 changes: 2 additions & 2 deletions lib/mystring.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ local mystringobj !codename 'nelua_mystringobj' = @record{len: usize, data: byte
local mystring !codename 'nelua_mystring' = @mystringobj*

local emptystringobj !codename 'nelua_emptystringobj' = @record{len: usize, zero: byte}
local const emptystringo: emptystringobj !codename 'nelua_emptystringo' = {}
local const emptystring !codename 'nelua_emptystring' = @mystring(&emptystringo)
local emptystringo: emptystringobj !const !codename 'nelua_emptystringo' = {}
local emptystring !const !codename 'nelua_emptystring' = @mystring(&emptystringo)

local function mystring_create(len: usize): mystring
local self = @mystring(malloc(@csize(#mystringobj) + len + 1))
Expand Down
Loading

0 comments on commit 130fdf6

Please sign in to comment.