| ... | @@ -0,0 +1,189 @@ |
| 1 | # Language Reference |
| 2 | |
| 3 | ## Primitive Numeric Types: |
| 4 | |
| 5 | zig | C equivalent | Description |
| 6 | -------------|------------------------|------------------------------- |
| 7 | bool | bool | unsigned 1-bit integer |
| 8 | i8 | int8_t | signed 8-bit integer |
| 9 | u8 | uint8_t | unsigned 8-bit integer |
| 10 | i16 | int16_t | signed 16-bit integer |
| 11 | u16 | uint16_t | unsigned 16-bit integer |
| 12 | i32 | int32_t | signed 32-bit integer |
| 13 | u32 | uint32_t | unsigned 32-bit integer |
| 14 | i64 | int64_t | signed 64-bit integer |
| 15 | u64 | uint64_t | unsigned 64-bit integer |
| 16 | f32 | float | 32-bit IEE754 floating point |
| 17 | f64 | double | 64-bit IEE754 floating point |
| 18 | f128 | long double | 128-bit IEE754 floating point |
| 19 | isize | intptr_t | signed pointer sized integer |
| 20 | usize | uintptr_t | unsigned pointer sized integer |
| 21 | c_short | short | for API compatibility with C |
| 22 | c_ushort | unsigned short | for API compatibility with C |
| 23 | c_int | int | for API compatibility with C |
| 24 | c_uint | unsigned int | for API compatibility with C |
| 25 | c_long | long | for API compatibility with C |
| 26 | c_ulong | unsigned long | for API compatibility with C |
| 27 | c_longlong | long long | for API compatibility with C |
| 28 | c_ulonglong | unsigned long long | for API compatibility with C |
| 29 | |
| 30 | ## Grammar |
| 31 | |
| 32 | ``` |
| 33 | Root : many(TopLevelDecl) token(EOF) |
| 34 | |
| 35 | TopLevelDecl : FnDef | ExternBlock | RootExportDecl | Use |
| 36 | |
| 37 | Use : many(Directive) token(Use) token(String) token(Semicolon) |
| 38 | |
| 39 | RootExportDecl : many(Directive) token(Export) token(Symbol) token(String) token(Semicolon) |
| 40 | |
| 41 | ExternBlock : many(Directive) token(Extern) token(LBrace) many(FnDecl) token(RBrace) |
| 42 | |
| 43 | FnProto : many(Directive) option(FnVisibleMod) token(Fn) token(Symbol) ParamDeclList option(token(Arrow) Type) |
| 44 | |
| 45 | Directive : token(NumberSign) token(Symbol) token(LParen) token(String) token(RParen) |
| 46 | |
| 47 | FnVisibleMod : token(Pub) | token(Export) |
| 48 | |
| 49 | FnDecl : FnProto token(Semicolon) |
| 50 | |
| 51 | FnDef : FnProto Block |
| 52 | |
| 53 | ParamDeclList : token(LParen) list(ParamDecl, token(Comma)) token(RParen) |
| 54 | |
| 55 | ParamDecl : token(Symbol) token(Colon) Type | token(Ellipsis) |
| 56 | |
| 57 | Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType |
| 58 | |
| 59 | PointerType : token(Star) (token(Const) | token(Mut)) Type |
| 60 | |
| 61 | ArrayType : token(LBracket) Type token(Semicolon) Expression token(RBracket) |
| 62 | |
| 63 | Block : token(LBrace) list(option(Statement), token(Semicolon)) token(RBrace) |
| 64 | |
| 65 | Statement : Label | VariableDeclaration token(Semicolon) | NonBlockExpression token(Semicolon) | BlockExpression |
| 66 | |
| 67 | Label: token(Symbol) token(Colon) |
| 68 | |
| 69 | VariableDeclaration : token(Let) option(token(Mut)) token(Symbol) (token(Eq) Expression | token(Colon) Type option(token(Eq) Expression)) |
| 70 | |
| 71 | Expression : BlockExpression | NonBlockExpression |
| 72 | |
| 73 | NonBlockExpression : ReturnExpression | AssignmentExpression |
| 74 | |
| 75 | AssignmentExpression : BoolOrExpression token(Equal) BoolOrExpression | BoolOrExpression |
| 76 | |
| 77 | BlockExpression : IfExpression | Block |
| 78 | |
| 79 | BoolOrExpression : BoolAndExpression token(BoolOr) BoolAndExpression | BoolAndExpression |
| 80 | |
| 81 | ReturnExpression : token(Return) option(Expression) |
| 82 | |
| 83 | IfExpression : token(If) Expression Block option(Else | ElseIf) |
| 84 | |
| 85 | ElseIf : token(Else) IfExpression |
| 86 | |
| 87 | Else : token(Else) Block |
| 88 | |
| 89 | BoolAndExpression : ComparisonExpression token(BoolAnd) ComparisonExpression | ComparisonExpression |
| 90 | |
| 91 | ComparisonExpression : BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression |
| 92 | |
| 93 | ComparisonOperator : token(BoolEq) | token(BoolNotEq) | token(BoolLessThan) | token(BoolGreaterThan) | token(BoolLessEqual) | token(BoolGreaterEqual) |
| 94 | |
| 95 | BinaryOrExpression : BinaryXorExpression token(BinOr) BinaryXorExpression | BinaryXorExpression |
| 96 | |
| 97 | BinaryXorExpression : BinaryAndExpression token(BinXor) BinaryAndExpression | BinaryAndExpression |
| 98 | |
| 99 | BinaryAndExpression : BitShiftExpression token(BinAnd) BitShiftExpression | BitShiftExpression |
| 100 | |
| 101 | BitShiftExpression : AdditionExpression BitShiftOperator AdditionExpression | AdditionExpression |
| 102 | |
| 103 | BitShiftOperator : token(BitShiftLeft | token(BitShiftRight) |
| 104 | |
| 105 | AdditionExpression : MultiplyExpression AdditionOperator MultiplyExpression | MultiplyExpression |
| 106 | |
| 107 | AdditionOperator : token(Plus) | token(Minus) |
| 108 | |
| 109 | MultiplyExpression : CastExpression MultiplyOperator CastExpression | CastExpression |
| 110 | |
| 111 | MultiplyOperator : token(Star) | token(Slash) | token(Percent) |
| 112 | |
| 113 | CastExpression : PrefixOpExpression token(as) Type | PrefixOpExpression |
| 114 | |
| 115 | PrefixOpExpression : PrefixOp SuffixOpExpression | SuffixOpExpression |
| 116 | |
| 117 | SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression) |
| 118 | |
| 119 | FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen) |
| 120 | |
| 121 | ArrayAccessExpression : token(LBracket) Expression token(RBracket) |
| 122 | |
| 123 | PrefixOp : token(Not) | token(Dash) | token(Tilde) |
| 124 | |
| 125 | PrimaryExpression : token(Number) | token(String) | KeywordLiteral | GroupedExpression | token(Symbol) | Goto |
| 126 | |
| 127 | Goto: token(Goto) token(Symbol) |
| 128 | |
| 129 | GroupedExpression : token(LParen) Expression token(RParen) |
| 130 | |
| 131 | KeywordLiteral : token(Unreachable) | token(Void) | token(True) | token(False) |
| 132 | ``` |
| 133 | |
| 134 | ## Operator Precedence |
| 135 | |
| 136 | ``` |
| 137 | x() x[] |
| 138 | !x -x ~x |
| 139 | as |
| 140 | * / % |
| 141 | + - |
| 142 | << >> |
| 143 | & |
| 144 | ^ |
| 145 | | |
| 146 | == != < > <= >= |
| 147 | && |
| 148 | || |
| 149 | = += -= |
| 150 | ``` |
| 151 | |
| 152 | ## Literals |
| 153 | |
| 154 | ### Characters and Strings |
| 155 | |
| 156 | | Example | Characters | Escapes | Null Terminated |
| 157 | ------------------------------------------------------------------------------- |
| 158 | Byte | 'H' | All ASCII | Byte | No |
| 159 | UTF-8 Bytes | "hello" | All Unicode | Byte & Unicode | No |
| 160 | UTF-8 C string | c"hello" | All Unicode | Byte & Unicode | Yes |
| 161 | |
| 162 | ### Byte Escapes |
| 163 | |
| 164 | | Name |
| 165 | ----------------------------------------------- |
| 166 | \x7F | 8-bit character code (exactly 2 digits) |
| 167 | \n | Newline |
| 168 | \r | Carriage return |
| 169 | \t | Tab |
| 170 | \\ | Backslash |
| 171 | \0 | Null |
| 172 | \' | Single quote |
| 173 | \" | Double quote |
| 174 | |
| 175 | ### Unicode Escapes |
| 176 | |
| 177 | | Name |
| 178 | ---------------------------------------------------------- |
| 179 | \u{7FFF} | 24-bit Unicode character code (up to 6 digits) |
| 180 | |
| 181 | ### Numbers |
| 182 | |
| 183 | Number literals | Example | Exponentiation |
| 184 | ------------------------------------------- |
| 185 | Decimal integer | 98222 | N/A |
| 186 | Hex integer | 0xff | N/A |
| 187 | Octal integer | 0o77 | N/A |
| 188 | Binary integer | 0b11110000 | N/A |
| 189 | Floating-point | 123.0E+77 | Optional |