zig/doc/langref.md

394 lines
11 KiB
Markdown
Raw Normal View History

2015-12-09 14:20:31 -08:00
# Language Reference
## Grammar
```
Root = many(TopLevelDecl) "EOF"
2015-12-09 14:20:31 -08:00
TopLevelDecl = many(Directive) option(VisibleMod) (FnDef | ExternDecl | ContainerDecl | GlobalVarDecl | ErrorValueDecl | TypeDecl | UseDecl)
TypeDecl = "type" "Symbol" "=" TypeExpr ";"
ErrorValueDecl = "error" "Symbol" ";"
GlobalVarDecl = VariableDeclaration ";"
2016-01-04 15:57:22 -08:00
VariableDeclaration = ("var" | "const") "Symbol" option(":" TypeExpr) "=" Expression
ContainerDecl = ("struct" | "enum" | "union") "Symbol" option(ParamDeclList) "{" many(StructMember) "}"
StructMember = many(Directive) option(VisibleMod) (StructField | FnDef | GlobalVarDecl | ContainerDecl)
StructField = "Symbol" option(":" Expression) ",")
2016-01-10 10:48:54 -08:00
UseDecl = "use" Expression ";"
2015-12-09 14:20:31 -08:00
ExternDecl = "extern" (FnProto | VariableDeclaration) ";"
2015-12-09 14:20:31 -08:00
2016-04-06 11:38:12 -07:00
FnProto = "fn" option("Symbol") option(ParamDeclList) ParamDeclList option("->" TypeExpr)
2015-12-09 14:20:31 -08:00
Directive = "#" "Symbol" "(" Expression ")"
2015-12-09 14:20:31 -08:00
VisibleMod = "pub" | "export"
2015-12-09 14:20:31 -08:00
FnDef = option("inline" | "extern") FnProto Block
2015-12-09 14:20:31 -08:00
ParamDeclList = "(" list(ParamDecl, ",") ")"
2015-12-09 14:20:31 -08:00
ParamDecl = option("noalias") option("Symbol" ":") TypeExpr | "..."
2015-12-09 14:20:31 -08:00
Block = "{" list(option(Statement), ";") "}"
2015-12-09 14:20:31 -08:00
Statement = Label | VariableDeclaration ";" | Defer ";" | NonBlockExpression ";" | BlockExpression
2015-12-09 14:20:31 -08:00
Label = "Symbol" ":"
2015-12-09 14:20:31 -08:00
Expression = BlockExpression | NonBlockExpression
2015-12-09 14:20:31 -08:00
TypeExpr = PrefixOpExpression
NonBlockExpression = ReturnExpression | AssignmentExpression
2015-12-10 14:34:38 -08:00
AsmExpression = "asm" option("volatile") "(" "String" option(AsmOutput) ")"
2015-12-10 14:34:38 -08:00
AsmOutput = ":" list(AsmOutputItem, ",") option(AsmInput)
2015-12-10 14:34:38 -08:00
AsmInput = ":" list(AsmInputItem, ",") option(AsmClobbers)
2015-12-10 14:34:38 -08:00
AsmOutputItem = "[" "Symbol" "]" "String" "(" ("Symbol" | "->" TypeExpr) ")"
2015-12-11 02:55:26 -08:00
AsmInputItem = "[" "Symbol" "]" "String" "(" Expression ")"
2015-12-11 02:55:26 -08:00
AsmClobbers= ":" list("String", ",")
2015-12-09 14:20:31 -08:00
UnwrapExpression = BoolOrExpression (UnwrapMaybe | UnwrapError) | BoolOrExpression
2016-02-06 18:28:11 -08:00
UnwrapMaybe = "??" Expression
2016-01-25 12:53:40 -08:00
2016-02-06 18:28:11 -08:00
UnwrapError = "%%" option("|" "Symbol" "|") Expression
2016-01-25 12:53:40 -08:00
AssignmentExpression = UnwrapExpression AssignmentOperator UnwrapExpression | UnwrapExpression
2015-12-12 17:17:27 -08:00
AssignmentOperator = "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" | "&&=" | "||="
2015-12-09 14:20:31 -08:00
BlockExpression = IfExpression | Block | WhileExpression | ForExpression | SwitchExpression
2015-12-24 13:37:43 -08:00
SwitchExpression = "switch" "(" Expression ")" "{" many(SwitchProng) "}"
2015-12-09 14:20:31 -08:00
SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" "Symbol" "|") Expression ","
SwitchItem = Expression | (Expression "..." Expression)
2015-12-09 14:20:31 -08:00
WhileExpression = "while" "(" Expression option(";" Expression) ")" Expression
ForExpression = "for" "(" Expression ")" option("|" option("*") "Symbol" option("," "Symbol") "|") Expression
BoolOrExpression = BoolAndExpression "||" BoolOrExpression | BoolAndExpression
ReturnExpression = option("%" | "?") "return" option(Expression)
2015-12-09 14:20:31 -08:00
Defer = option("%" | "?") "defer" option(Expression)
IfExpression = IfVarExpression | IfBoolExpression
2015-12-26 14:05:27 -08:00
IfBoolExpression = "if" "(" Expression ")" Expression option(Else)
2015-12-09 14:20:31 -08:00
IfVarExpression = "if" "(" ("const" | "var") option("*") "Symbol" option(":" TypeExpr) "?=" Expression ")" Expression Option(Else)
2015-12-09 14:20:31 -08:00
Else = "else" Expression
2015-12-09 14:20:31 -08:00
BoolAndExpression = ComparisonExpression "&&" BoolAndExpression | ComparisonExpression
2015-12-09 14:20:31 -08:00
ComparisonExpression = BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression
2015-12-09 14:20:31 -08:00
ComparisonOperator = "==" | "!=" | "<" | ">" | "<=" | ">="
2015-12-09 14:20:31 -08:00
BinaryOrExpression = BinaryXorExpression "|" BinaryOrExpression | BinaryXorExpression
2015-12-09 14:20:31 -08:00
BinaryXorExpression = BinaryAndExpression "^" BinaryXorExpression | BinaryAndExpression
2015-12-09 14:20:31 -08:00
BinaryAndExpression = BitShiftExpression "&" BinaryAndExpression | BitShiftExpression
2015-12-09 14:20:31 -08:00
BitShiftExpression = AdditionExpression BitShiftOperator BitShiftExpression | AdditionExpression
2015-12-09 14:20:31 -08:00
BitShiftOperator = "<<" | ">>"
2015-12-09 14:20:31 -08:00
AdditionExpression = MultiplyExpression AdditionOperator AdditionExpression | MultiplyExpression
2015-12-09 14:20:31 -08:00
AdditionOperator = "+" | "-" | "++"
2015-12-09 14:20:31 -08:00
MultiplyExpression = CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpression
CurlySuffixExpression = TypeExpr option(ContainerInitExpression)
2015-12-09 14:20:31 -08:00
2016-04-28 18:03:44 -07:00
MultiplyOperator = "*" | "/" | "%" | "**"
2015-12-09 14:20:31 -08:00
PrefixOpExpression = PrefixOp PrefixOpExpression | SuffixOpExpression
2015-12-09 14:20:31 -08:00
SuffixOpExpression = PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
FieldAccessExpression = "." "Symbol"
2015-12-09 14:20:31 -08:00
FnCallExpression = "(" list(Expression, ",") ")"
2015-12-09 14:20:31 -08:00
ArrayAccessExpression = "[" Expression "]"
2015-12-09 14:20:31 -08:00
SliceExpression = "[" Expression "..." option(Expression) "]" option("const")
ContainerInitExpression = "{" ContainerInitBody "}"
2016-01-13 17:15:51 -08:00
ContainerInitBody = list(StructLiteralField, ",") | list(Expression, ",")
2016-01-13 17:15:51 -08:00
StructLiteralField = "." "Symbol" "=" Expression
2015-12-09 14:20:31 -08:00
2016-02-01 01:11:46 -08:00
PrefixOp = "!" | "-" | "~" | "*" | ("&" option("const")) | "?" | "%" | "%%" | "??"
2015-12-23 23:00:23 -08:00
PrimaryExpression = "Number" | "String" | "CharLiteral" | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | "Symbol" | ("@" "Symbol" FnCallExpression) | ArrayType | (option("extern") FnProto) | AsmExpression | ("error" "." "Symbol")
2015-12-23 23:00:23 -08:00
ArrayType = "[" option(Expression) "]" option("const") TypeExpr
2015-12-09 14:20:31 -08:00
GotoExpression = "goto" "Symbol"
2015-12-09 14:20:31 -08:00
GroupedExpression = "(" Expression ")"
2015-12-09 14:20:31 -08:00
KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "error" | "type"
2015-12-09 14:20:31 -08:00
```
## Operator Precedence
```
x() x[] x.y
!x -x ~x *x &x ?x %x %%x
x{}
2015-12-09 14:20:31 -08:00
* / %
+ - ++
2015-12-09 14:20:31 -08:00
<< >>
&
^
|
== != < > <= >=
&&
||
2016-01-25 12:53:40 -08:00
?? %%
2015-12-12 17:17:27 -08:00
= *= /= %= += -= <<= >>= &= ^= |= &&= ||=
2015-12-09 14:20:31 -08:00
```
2016-01-20 00:27:53 -08:00
## Types
### Numeric Types
```
Type name C equivalent Description
i8 int8_t signed 8-bit integer
2016-05-05 17:19:01 -07:00
u8 (none) unsigned 8-bit integer
2016-01-20 00:27:53 -08:00
i16 int16_t signed 16-bit integer
2016-05-05 17:19:01 -07:00
u16 (none) unsigned 16-bit integer
2016-01-20 00:27:53 -08:00
i32 int32_t signed 32-bit integer
2016-05-05 17:19:01 -07:00
u32 (none) unsigned 32-bit integer
2016-01-20 00:27:53 -08:00
i64 int64_t signed 64-bit integer
2016-05-05 17:19:01 -07:00
u64 (none) unsigned 64-bit integer
2016-01-20 00:27:53 -08:00
isize intptr_t signed pointer sized integer
2016-05-05 17:19:01 -07:00
usize (none) unsigned pointer sized integer
i8w (none) wrapping signed 8-bit integer
u8w uint8_t wrapping unsigned 8-bit integer
i16w (none) wrapping signed 16-bit integer
u16w uint16_t wrapping unsigned 16-bit integer
i32w (none) wrapping signed 32-bit integer
u32w uint32_t wrapping unsigned 32-bit integer
i64w (none) wrapping signed 64-bit integer
u64w uint64_t wrapping unsigned 64-bit integer
isizew (none) wrapping signed pointer sized integer
usizew uintptr_t wrapping unsigned pointer sized integer
2016-01-20 00:27:53 -08:00
c_short short for ABI compatibility with C
c_ushort unsigned short for ABI compatibility with C
c_int int for ABI compatibility with C
c_uint unsigned int for ABI compatibility with C
c_long long for ABI compatibility with C
c_ulong unsigned long for ABI compatibility with C
c_longlong long long for ABI compatibility with C
c_ulonglong unsigned long long for ABI compatibility with C
c_long_double long double for ABI compatibility with C
c_void void for ABI compatibility with C
2016-05-05 17:19:01 -07:00
f32 float 32-bit IEE754 floating point
f64 double 64-bit IEE754 floating point
2016-01-20 00:27:53 -08:00
```
### Boolean Type
2016-01-20 00:27:53 -08:00
The boolean type has the name `bool` and represents either true or false.
### Function Type
2016-01-20 00:27:53 -08:00
TODO
### Fixed-Size Array Type
Example: The string `"aoeu"` has type `[4]u8`.
The size is known at compile time and is part of the type.
### Slice Type
A slice can be obtained with the slicing syntax: `array[start...end]`
Example: `"aoeu"[0...2]` has type `[]u8`.
2016-01-20 00:27:53 -08:00
2016-01-22 15:05:29 -08:00
### Struct Type
2016-01-20 00:27:53 -08:00
TODO
2016-01-22 15:05:29 -08:00
### Enum Type
TODO
### Maybe Type
TODO
### Pure Error Type
TODO
### Error Union Type
2016-01-22 15:05:29 -08:00
TODO
### Pointer Type
2016-01-20 00:27:53 -08:00
TODO
2015-12-09 14:20:31 -08:00
2016-01-20 00:27:53 -08:00
### Unreachable Type
2016-01-20 00:27:53 -08:00
The unreachable type has the name `unreachable`. TODO explanation
2015-12-09 14:20:31 -08:00
2016-01-20 00:27:53 -08:00
### Void Type
The void type has the name `void`. void types are zero bits and are omitted
from codegen.
2015-12-09 14:20:31 -08:00
2016-01-20 00:27:53 -08:00
## Expressions
### Literals
#### Character and String Literals
```
Literal Example Characters Escapes Null Term Type
2016-01-20 00:27:53 -08:00
Byte 'H' All ASCII Byte No u8
UTF-8 Bytes "hello" All Unicode Byte & Unicode No [5]u8
UTF-8 C string c"hello" All Unicode Byte & Unicode Yes &const u8
2016-05-01 14:53:48 -07:00
UTF-8 Raw String r"X(hello)X" All Unicode None No [5]u8
UTF-8 Raw C String rc"X(hello)X" All Unicode None Yes &const u8
2016-01-20 00:27:53 -08:00
```
2016-05-01 14:53:48 -07:00
### Escapes
Escape | Name
----------|-------------------------------------------------------------------
\n | Newline
\r | Carriage Return
\t | Tab
\\ | Backslash
\' | Single Quote
\" | Double Quote
\xNN | hexadecimal 8-bit character code (2 digits)
\uNNNN | hexadecimal 16-bit Unicode character code UTF-8 encoded (4 digits)
\UNNNNNN | hexadecimal 24-bit Unicode character code UTF-8 encoded (6 digits)
Note that the maximum valid Unicode point is 0x10ffff.
##### Raw Strings
Raw string literals have no escapes and can span across multiple lines. To
start a raw string, use 'r"' or 'rc"' followed by unique bytes followed by '('.
To end a raw string, use ')' followed by the same unique bytes, followed by '"'.
2016-01-20 00:27:53 -08:00
#### Numeric Literals
```
Number literals Example Exponentiation
Decimal integer 98222 N/A
Hex integer 0xff N/A
Octal integer 0o77 N/A
Binary integer 0b11110000 N/A
Floating-point 123.0E+77 Optional
Hex floating point TODO TODO
```
### Identifiers
2016-01-20 00:27:53 -08:00
TODO
### Declarations
2016-01-20 00:27:53 -08:00
Declarations have type `void`.
#### Function Declarations
2016-01-20 00:27:53 -08:00
TODO
#### Variable Declarations
2016-01-20 00:27:53 -08:00
TODO
#### Struct Declarations
2016-01-20 00:27:53 -08:00
TODO
#### Enum Declarations
2016-01-20 00:27:53 -08:00
TODO
## Built-in Functions
2016-01-20 00:27:53 -08:00
Built-in functions are prefixed with `@`.
### @typeof
`@typeof(expression)`
2016-01-20 00:27:53 -08:00
### @sizeof
`@sizeof(type)`
2016-01-20 00:27:53 -08:00
### Overflow Arithmetic
Overflow arithmetic functions have defined behavior on overflow or underflow.
2016-01-20 00:27:53 -08:00
The functions take an integer type, two variables of the specified type, and a
pointer to a variable of the specified type where the result is stored. The
functions return a boolean value: true of overflow/underflow occurred, false
otherwise.
2016-01-20 00:27:53 -08:00
```
Function Operation
@add_with_overflow(T: type, a: T, b: T, x: &T) -> bool *x = a + b
@sub_with_overflow(T: type, a: T, b: T, x: &T) -> bool *x = a - b
@mul_with_overflow(T: type, a: T, b: T, x: &T) -> bool *x = a * b
2016-01-20 00:27:53 -08:00
```
### @memset
`@memset(dest, char, len)`
2015-12-09 14:20:31 -08:00
### @memcpy
`@memcpy(dest, source, len)`
### @member_count
`@member_count(enum_type)`
2015-12-09 14:20:31 -08:00
2016-01-20 00:27:53 -08:00
### Max and Min Value
`@max_value(type)`
`@min_value(type)`