2015-12-09 14:20:31 -08:00
|
|
|
# Language Reference
|
|
|
|
|
|
|
|
## Primitive Numeric Types:
|
|
|
|
|
|
|
|
zig | C equivalent | Description
|
|
|
|
-------------|------------------------|-------------------------------
|
|
|
|
bool | bool | unsigned 1-bit integer
|
|
|
|
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 API compatibility with C
|
|
|
|
c_ushort | unsigned short | for API compatibility with C
|
|
|
|
c_int | int | for API compatibility with C
|
|
|
|
c_uint | unsigned int | for API compatibility with C
|
|
|
|
c_long | long | for API compatibility with C
|
|
|
|
c_ulong | unsigned long | for API compatibility with C
|
|
|
|
c_longlong | long long | for API compatibility with C
|
|
|
|
c_ulonglong | unsigned long long | for API compatibility with C
|
|
|
|
|
|
|
|
## Grammar
|
|
|
|
|
|
|
|
```
|
2016-01-19 18:15:36 -08:00
|
|
|
Root : many(TopLevelDecl) "EOF"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-15 17:45:52 -08:00
|
|
|
TopLevelDecl : FnDef | ExternBlock | RootExportDecl | Import | ContainerDecl | VariableDeclaration
|
2015-12-14 21:01:39 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
VariableDeclaration : option(FnVisibleMod) ("var" | "const") "symbol" ("=" Expression | ":" PrefixOpExpression option("=" Expression))
|
2015-12-11 23:10:37 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ContainerDecl : many(Directive) option(FnVisibleMod) ("struct" | "enum") "Symbol" "{" many(StructMember) "}"
|
2016-01-04 15:57:22 -08:00
|
|
|
|
|
|
|
StructMember: StructField | FnDecl
|
2015-12-11 23:10:37 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
StructField : "Symbol" option(":" Expression) ",")
|
2016-01-10 10:48:54 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
Import : many(Directive) "import" "String" ";"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
RootExportDecl : many(Directive) "export" "Symbol" "String" ";"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ExternBlock : many(Directive) "extern" "{" many(FnDecl) "}"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
FnProto : many(Directive) option(FnVisibleMod) "fn" "Symbol" ParamDeclList option(PrefixOpExpression)
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
Directive : "#" "Symbol" "(" "String" ")"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
FnVisibleMod : "pub" | "export"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
FnDecl : FnProto ";"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
FnDef : FnProto "=>" Block
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ParamDeclList : "(" list(ParamDecl, ",") ")"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ParamDecl : option("noalias") "Symbol" ":" PrefixOpExpression | "..."
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
Block : "{" list(option(Statement), ";") "}"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
Statement : Label | VariableDeclaration ";" | NonBlockExpression ";" | BlockExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -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
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
AsmExpression : "asm" option("volatile") "(" "String" option(AsmOutput) ")"
|
2015-12-10 14:34:38 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
AsmOutput : ":" list(AsmOutputItem, ",") option(AsmInput)
|
2015-12-10 14:34:38 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
AsmInput : ":" list(AsmInputItem, ",") option(AsmClobbers)
|
2015-12-10 14:34:38 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
AsmOutputItem : "[" "Symbol" "]" "String" "(" ("Symbol" | "->" PrefixOpExpression) ")"
|
2015-12-11 02:55:26 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
AsmInputItem : "[" "Symbol" "]" "String" "(" Expression ")"
|
2015-12-11 02:55:26 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
AsmClobbers: ":" list("String", ",")
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
UnwrapMaybeExpression : BoolOrExpression "??" BoolOrExpression | BoolOrExpression
|
2016-01-07 02:23:38 -08:00
|
|
|
|
|
|
|
AssignmentExpression : UnwrapMaybeExpression AssignmentOperator UnwrapMaybeExpression | UnwrapMaybeExpression
|
2015-12-12 17:17:27 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
AssignmentOperator : "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" | "&&=" | "||="
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
BlockExpression : IfExpression | Block | WhileExpression | ForExpression | SwitchExpression
|
2015-12-24 13:37:43 -08:00
|
|
|
|
2016-01-19 18:15:36 -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 ","
|
2016-01-18 06:00:45 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
SwitchItem : Expression | (Expression "..." Expression)
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
WhileExpression : "while" "(" Expression ")" Expression
|
|
|
|
|
|
|
|
ForExpression : "for" "(" "Symbol" "," Expression option("," "Symbol") ")" Expression
|
|
|
|
|
|
|
|
BoolOrExpression : BoolAndExpression "||" BoolOrExpression | BoolAndExpression
|
|
|
|
|
|
|
|
ReturnExpression : "return" option(Expression)
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2015-12-26 14:05:27 -08:00
|
|
|
IfExpression : IfVarExpression | IfBoolExpression
|
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
IfBoolExpression : "if" "(" Expression ")" Expression option(Else)
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
IfVarExpression : "if" "(" ("const" | "var") "Symbol" option(":" PrefixOpExpression) "?=" Expression ")" Expression Option(Else)
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
Else : "else" Expression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
BoolAndExpression : ComparisonExpression "&&" BoolAndExpression | ComparisonExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
|
|
|
ComparisonExpression : BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression
|
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ComparisonOperator : "==" | "!=" | "<" | ">" | "<=" | ">="
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
BinaryOrExpression : BinaryXorExpression "|" BinaryOrExpression | BinaryXorExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
BinaryXorExpression : BinaryAndExpression "^" BinaryXorExpression | BinaryAndExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
BinaryAndExpression : BitShiftExpression "&" BinaryAndExpression | BitShiftExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2015-12-12 16:03:44 -08:00
|
|
|
BitShiftExpression : AdditionExpression BitShiftOperator BitShiftExpression | AdditionExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
BitShiftOperator : "<<" | ">>"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2015-12-12 16:03:44 -08:00
|
|
|
AdditionExpression : MultiplyExpression AdditionOperator AdditionExpression | MultiplyExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
AdditionOperator : "+" | "-"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-15 16:40:12 -08:00
|
|
|
MultiplyExpression : CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpression
|
|
|
|
|
|
|
|
CurlySuffixExpression : PrefixOpExpression option(ContainerInitExpression)
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
MultiplyOperator : "*" | "/" | "%"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2015-12-12 16:03:44 -08:00
|
|
|
PrefixOpExpression : PrefixOp PrefixOpExpression | SuffixOpExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-15 16:40:12 -08:00
|
|
|
SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
|
2015-12-11 23:10:37 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
FieldAccessExpression : "." "Symbol"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
FnCallExpression : "(" list(Expression, ",") ")"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ArrayAccessExpression : "[" Expression "]"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
SliceExpression : "[" Expression "..." option(Expression) "]" option("const")
|
2016-01-07 04:29:11 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ContainerInitExpression : "{" ContainerInitBody "}"
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ContainerInitBody : list(StructLiteralField, ",") | list(Expression, ",")
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
StructLiteralField : "." "Symbol" "=" Expression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
PrefixOp : "!" | "-" | "~" | "*" | ("&" option("const")) | "?"
|
2015-12-23 23:00:23 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
PrimaryExpression : "Number" | "String" | "CharLiteral" | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | "Symbol" | ("@" "Symbol" FnCallExpression) | ArrayType | AsmExpression
|
2015-12-23 23:00:23 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
ArrayType : "[" option(Expression) "]" option("const") PrefixOpExpression
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
GotoExpression: "goto" "Symbol"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
GroupedExpression : "(" Expression ")"
|
2015-12-09 14:20:31 -08:00
|
|
|
|
2016-01-19 18:15:36 -08:00
|
|
|
KeywordLiteral : "true" | "false" | "null" | "break" | "continue"
|
2015-12-09 14:20:31 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
## Operator Precedence
|
|
|
|
|
|
|
|
```
|
2016-01-15 16:40:12 -08:00
|
|
|
x() x[] x.y
|
|
|
|
!x -x ~x *x &x ?x
|
|
|
|
x{}
|
2015-12-09 14:20:31 -08:00
|
|
|
* / %
|
|
|
|
+ -
|
|
|
|
<< >>
|
|
|
|
&
|
|
|
|
^
|
|
|
|
|
|
|
|
|
== != < > <= >=
|
|
|
|
&&
|
|
|
|
||
|
2016-01-07 02:23:38 -08:00
|
|
|
??
|
2015-12-12 17:17:27 -08:00
|
|
|
= *= /= %= += -= <<= >>= &= ^= |= &&= ||=
|
2015-12-09 14:20:31 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
## Literals
|
|
|
|
|
|
|
|
### Characters and Strings
|
|
|
|
|
2015-12-11 23:10:37 -08:00
|
|
|
| Example | Characters | Escapes | Null Term | Type
|
2016-01-07 02:23:38 -08:00
|
|
|
----------------|----------|-------------|----------------|-----------|----------
|
2015-12-11 23:10:37 -08:00
|
|
|
Byte | 'H' | All ASCII | Byte | No | u8
|
2016-01-20 01:12:24 -08:00
|
|
|
UTF-8 Bytes | "hello" | All Unicode | Byte & Unicode | No | [5]u8
|
|
|
|
UTF-8 C string | c"hello" | All Unicode | Byte & Unicode | Yes | &const u8
|
2015-12-09 14:20:31 -08:00
|
|
|
|
|
|
|
### Byte Escapes
|
|
|
|
|
|
|
|
| Name
|
2016-01-07 02:23:38 -08:00
|
|
|
------|----------------------------------------
|
2015-12-09 14:20:31 -08:00
|
|
|
\x7F | 8-bit character code (exactly 2 digits)
|
|
|
|
\n | Newline
|
|
|
|
\r | Carriage return
|
|
|
|
\t | Tab
|
|
|
|
\\ | Backslash
|
|
|
|
\0 | Null
|
|
|
|
\' | Single quote
|
|
|
|
\" | Double quote
|
|
|
|
|
|
|
|
### Unicode Escapes
|
|
|
|
|
|
|
|
| Name
|
2016-01-07 02:23:38 -08:00
|
|
|
----------|-----------------------------------------------
|
2015-12-09 14:20:31 -08:00
|
|
|
\u{7FFF} | 24-bit Unicode character code (up to 6 digits)
|
|
|
|
|
|
|
|
### Numbers
|
|
|
|
|
2015-12-14 01:46:37 -08:00
|
|
|
Number literals | Example | Exponentiation
|
2016-01-07 02:23:38 -08:00
|
|
|
--------------------|-------------|---------------
|
2015-12-14 01:46:37 -08:00
|
|
|
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
|