zig/doc/langref.md

328 lines
9.1 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 : FnDef | ExternBlock | RootExportDecl | Import | ContainerDecl | VariableDeclaration | ErrorValueDecl
ErrorValueDecl : option(FnVisibleMod) "%." "Symbol"
VariableDeclaration : option(FnVisibleMod) ("var" | "const") "Symbol" ("=" Expression | ":" PrefixOpExpression option("=" Expression))
ContainerDecl : many(Directive) option(FnVisibleMod) ("struct" | "enum") "Symbol" "{" many(StructMember) "}"
2016-01-04 15:57:22 -08:00
StructMember: StructField | FnDecl
StructField : "Symbol" option(":" Expression) ",")
2016-01-10 10:48:54 -08:00
Import : many(Directive) "import" "String" ";"
2015-12-09 14:20:31 -08:00
RootExportDecl : many(Directive) "export" "Symbol" "String" ";"
2015-12-09 14:20:31 -08:00
ExternBlock : many(Directive) "extern" "{" many(FnDecl) "}"
2015-12-09 14:20:31 -08:00
FnProto : many(Directive) option(FnVisibleMod) "fn" "Symbol" ParamDeclList option(PrefixOpExpression)
2015-12-09 14:20:31 -08:00
Directive : "#" "Symbol" "(" "String" ")"
2015-12-09 14:20:31 -08:00
FnVisibleMod : "pub" | "export"
2015-12-09 14:20:31 -08:00
FnDecl : FnProto ";"
2015-12-09 14:20:31 -08:00
FnDef : FnProto "=>" Block
2015-12-09 14:20:31 -08:00
ParamDeclList : "(" list(ParamDecl, ",") ")"
2015-12-09 14:20:31 -08:00
ParamDecl : option("noalias") "Symbol" ":" PrefixOpExpression | "..."
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
2016-01-13 17:15:51 -08:00
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" | "->" PrefixOpExpression) ")"
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
UnwrapMaybeExpression : BoolOrExpression "??" BoolOrExpression | BoolOrExpression
AssignmentExpression : UnwrapMaybeExpression AssignmentOperator UnwrapMaybeExpression | UnwrapMaybeExpression
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
2016-01-20 01:12:24 -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" "(" "Symbol" "," Expression option("," "Symbol") ")" Expression
BoolOrExpression : BoolAndExpression "||" BoolOrExpression | BoolAndExpression
ReturnExpression : option("%" | "?") "return" option(Expression)
2015-12-09 14:20:31 -08:00
2015-12-26 14:05:27 -08:00
IfExpression : IfVarExpression | IfBoolExpression
IfBoolExpression : "if" "(" Expression ")" Expression option(Else)
2015-12-09 14:20:31 -08:00
IfVarExpression : "if" "(" ("const" | "var") "Symbol" option(":" PrefixOpExpression) "?=" 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
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 : PrefixOpExpression 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
PrefixOp : "!" | "-" | "~" | "*" | ("&" option("const")) | "?"
2015-12-23 23:00:23 -08:00
PrimaryExpression : "Number" | "String" | "CharLiteral" | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | "Symbol" | ("@" "Symbol" FnCallExpression) | ArrayType | AsmExpression | ("%." "Symbol")
2015-12-23 23:00:23 -08:00
ArrayType : "[" option(Expression) "]" option("const") PrefixOpExpression
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"
2015-12-09 14:20:31 -08:00
```
## Operator Precedence
```
x() x[] x.y
!x -x ~x *x &x ?x %x
x{}
2015-12-09 14:20:31 -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
f128 long double 128-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
```
### Boolean Type
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
### Struct Types
TODO
### Pointer Types
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
TODO
### Declarations
Declarations have type `void`.
#### Function Declarations
TODO
#### Variable Declarations
TODO
#### Struct Declarations
TODO
#### Enum Declarations
TODO
## Built-in Functions
Built-in functions are prefixed with `@`.
### Typeof
TODO
### Sizeof
TODO
### Overflow Arithmetic
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
TODO memset and memcpy
2015-12-09 14:20:31 -08:00
2016-01-20 00:27:53 -08:00
### Value Count
TODO
2015-12-09 14:20:31 -08:00
2016-01-20 00:27:53 -08:00
### Max and Min Value
TODO