zig/doc/langref.md

369 lines
9.5 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 | RootExportDecl | Import | ContainerDecl | GlobalVarDecl | ErrorValueDecl | CImportDecl | TypeDecl)
CImportDecl = "c_import" Block
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") "Symbol" "{" many(StructMember) "}"
StructMember = many(Directive) option(VisibleMod) (StructField | FnDef)
StructField = "Symbol" option(":" Expression) ",")
2016-01-10 10:48:54 -08:00
Import = "import" "String" ";"
2015-12-09 14:20:31 -08:00
RootExportDecl = "export" "Symbol" "String" ";"
2015-12-09 14:20:31 -08:00
ExternDecl = "extern" (FnProto | VariableDeclaration) ";"
2015-12-09 14:20:31 -08:00
FnProto = "fn" option("Symbol") ParamDeclList option("->" TypeExpr)
2015-12-09 14:20:31 -08:00
Directive = "#" "Symbol" "(" "String" ")"
2015-12-09 14:20:31 -08:00
VisibleMod = "pub" | "export"
2015-12-09 14:20:31 -08:00
FnDef = option("inline") 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 ";" | 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 | DeferExpression
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
UnwrapMaybe = "??" BoolOrExpression
2016-01-25 12:53:40 -08:00
UnwrapError = "%%" option("|" "Symbol" "|") BoolOrExpression
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 ")" Expression
ForExpression = "for" "(" Expression ")" option("|" "Symbol" option("," "Symbol") "|") Expression
BoolOrExpression = BoolAndExpression "||" BoolOrExpression | BoolAndExpression
ReturnExpression = option("%" | "?") "return" option(Expression)
2015-12-09 14:20:31 -08:00
DeferExpression = 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") "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
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
u8 uint8_t unsigned 8-bit integer
i16 int16_t signed 16-bit integer
u16 uint16_t unsigned 16-bit integer
i32 int32_t signed 32-bit integer
u32 uint32_t unsigned 32-bit integer
i64 int64_t signed 64-bit integer
u64 uint64_t unsigned 64-bit integer
f32 float 32-bit IEE754 floating point
f64 double 64-bit IEE754 floating point
isize intptr_t signed pointer sized integer
usize uintptr_t unsigned pointer sized integer
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
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
### Error Type
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
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-01-20 00:27:53 -08:00
```
```
Escape Name
\xNN hexadecimal 8-bit character code (exactly 2 digits)
\n Newline
\r Carriage return
\t Tab
\\ Backslash
\0 Null
\' Single quote
\" Double quote
```
2015-12-09 14:20:31 -08:00
### Unicode Escapes
2016-01-20 00:27:53 -08:00
Escape | Name
------------|-----------------------------------------------
\u{NNNNNN} | hexadecimal 24-bit Unicode character code (up to 6 digits)
#### 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
2016-01-20 00:27:53 -08:00
TODO
### Sizeof
2016-01-20 00:27:53 -08:00
TODO
### Overflow Arithmetic
2016-01-20 00:27:53 -08:00
Overflow arithmetic functions have defined behavior on overflow or underflow. TODO what is that behaviour?
The functions take an integer (TODO float?) 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.
```
Function Operation
bool add_with_overflow(type, a: type, b: type, x: &type) *x = a + b
bool sub_with_overflow(type, a: type, b: type, x: &type) *x = a - b
bool mul_with_overflow(type, a: type, b: type, x: &type) *x = a * b
```
### Memory Operations
2016-01-20 00:27:53 -08:00
TODO memset and memcpy
2015-12-09 14:20:31 -08:00
2016-01-20 00:27:53 -08:00
### Value Count
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
### Max and Min Value
2016-01-20 00:27:53 -08:00
TODO