Spec tweaks
This commit is contained in:
parent
cad6c06607
commit
8661ab6ace
1 changed files with 45 additions and 39 deletions
84
spec.txt
84
spec.txt
|
|
@ -19,15 +19,19 @@ Built-in types:
|
|||
N.B.: there is no dedicated 'char' type, strings operate on u8 (utf-8) or u32 (utf-32)
|
||||
|
||||
Pointer types:
|
||||
*T (pointer to const T)
|
||||
*mut T (pointer to mutable T)
|
||||
**T (pointer to const pointer to const T)
|
||||
*mut *T (pointer to mutable pointer to const T)
|
||||
**mut T (pointer to const pointer to mutable T)
|
||||
*mut *mut T (pointer to mutable pointer to mutable T)
|
||||
T* (pointer to const T)
|
||||
T mut* (pointer to mutable T)
|
||||
T** (pointer to const pointer to const T)
|
||||
T* mut* (pointer to mutable pointer to const T)
|
||||
T mut** (pointer to const pointer to mutable T)
|
||||
T mut* mut* (pointer to mutable pointer to mutable T)
|
||||
|
||||
Array types:
|
||||
T[N] array of N elements of type T (N must be a compile-time value)
|
||||
T[N] array of N elements of type T (N must be a compile-time value)
|
||||
|
||||
Function types:
|
||||
(arg1, arg2, arg3) -> result
|
||||
(arg1, arg2) -> unit // no return value
|
||||
|
||||
======== LITERALS ========
|
||||
|
||||
|
|
@ -63,10 +67,10 @@ Variable declaration:
|
|||
|
||||
Array declaration:
|
||||
let arr: i32[4] = [12, 15, 65, 42]
|
||||
let arr: i32[] = [56, 23] // size inferred as 2
|
||||
let arr: i32[] = [56, 23] // size inferred as 2 - maybe forbid this?
|
||||
let arr = [2, 5, 6] // size and type inferred as i32[3]
|
||||
|
||||
Variables must always be initialized. (TODO: really? What about arrays?)
|
||||
Variables must always be initialized. (TODO: really? What about arrays? Maybe need special syntax for zero-initialization or mass-initialization)
|
||||
Const variables must be initialized with a const expression (any expression that doesn't include non-const values).
|
||||
|
||||
======== OPERATORS ========
|
||||
|
|
@ -118,40 +122,14 @@ Any integer/floating-point types can be cast to each other.
|
|||
Any pointer types can be cast to each other (TODO: alignment? UB or safe fallback?).
|
||||
|
||||
Address:
|
||||
&x // returns *T
|
||||
&x // returns *T, fails if x is a const variable
|
||||
&mut x // returns *mut T, fails if x is non-mut variable
|
||||
|
||||
Assignment:
|
||||
x = 15 // requires x to be a mut variable
|
||||
*p = 15 // p must be a pointer to mut
|
||||
|
||||
======== STRUCTS ========
|
||||
|
||||
Struct types:
|
||||
struct rect:
|
||||
width: u32
|
||||
height: u32
|
||||
|
||||
Creating a struct value:
|
||||
let x = rect(10u, 20u)
|
||||
let y = rect(width = 10u, height = 20u)
|
||||
|
||||
Struct field access:
|
||||
let r = rect(1u, 2u)
|
||||
let x = r.width
|
||||
let p = &r
|
||||
let y = p.height // field access through pointer is the same
|
||||
|
||||
Function types:
|
||||
(T1, T2, T3) -> i32
|
||||
(T1, T2) -> unit // no return value
|
||||
|
||||
Function definition:
|
||||
func foo(x: i32, y: i32) -> i32:
|
||||
return x * y
|
||||
|
||||
func bar(x: f32): // deduced return type unit
|
||||
print(x)
|
||||
======== FLOW CONTROL ========
|
||||
|
||||
Flow control:
|
||||
if condition:
|
||||
|
|
@ -166,6 +144,34 @@ Flow control:
|
|||
|
||||
TODO: for loops? iterator/range interface?
|
||||
|
||||
======== STRUCTS ========
|
||||
|
||||
Struct types:
|
||||
struct rect:
|
||||
width: u32
|
||||
height: u32
|
||||
|
||||
Creating a struct value:
|
||||
let x = rect(10u, 20u)
|
||||
let y = rect(width = 10u, height = 20u)
|
||||
|
||||
Struct field access:
|
||||
let r = rect(1u, 2u)
|
||||
let x = r.width
|
||||
let p = &r
|
||||
let y = p.height // field access through pointer is the same
|
||||
|
||||
======== FUNCTIONS ========
|
||||
|
||||
Function definition:
|
||||
func foo(x: i32, y: i32) -> i32:
|
||||
return x * y
|
||||
|
||||
func bar(x: f32): // deduced return type unit
|
||||
print(x)
|
||||
|
||||
TODO: function overloading? Probably requires selecting a specific overload using `as` operator to save to a value (but not on call site)
|
||||
|
||||
======== TYPE OF TYPES ========
|
||||
|
||||
Types are also considered to be values. The keyword `type` denotes the type of all types.
|
||||
|
|
@ -182,14 +188,14 @@ E.g.
|
|||
======== CONST EXPRESSIONS ========
|
||||
|
||||
// TODO
|
||||
// Auto-upgrading values to compile-time when a function is executed from const-only values?
|
||||
// Auto-upgrading values to compile-time when a pure function is executed from const-only values?
|
||||
|
||||
======== METAPROGRAMMING ========
|
||||
|
||||
// TODO
|
||||
// Functions returning functions/structs
|
||||
// Syntactic sugar for common cases
|
||||
// Figure out: max(a,b) - how to deduce type parameters?
|
||||
// Figure out: max(a,b) - how to deduce type parameters? How does it play with overloading?
|
||||
// func max(t: type):
|
||||
// return func(x : t, y : t):
|
||||
// if x > y:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue