authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-20 02:16:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-20 02:16:36-07:00
log961adc0909feb1d1b49ca18bdf555f6fc757a342
tree59b611fab3bed1923be9d4ac67e10419072760f3
parentad9759bc8e993364490d7268d491620ee4a2cc24
parentf0f56a4a9bd416d4d6ecf9ff4766f70926715aa9

Merge branch 'master' of github.com:andrewrk/zig


5 files changed, 146 insertions(+), 63 deletions(-)

doc/langref.md+140-57
......@@ -1,32 +1,5 @@
11# Language Reference
22
3## Primitive Numeric Types:
4
5zig | 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
303## Grammar
314
325```
......@@ -190,42 +163,152 @@ x{}
190163= *= /= %= += -= <<= >>= &= ^= |= &&= ||=
191164```
192165
193## Literals
166## Types
167
168### Numeric Types
169
170```
171Type name C equivalent Description
172
173i8 int8_t signed 8-bit integer
174u8 uint8_t unsigned 8-bit integer
175i16 int16_t signed 16-bit integer
176u16 uint16_t unsigned 16-bit integer
177i32 int32_t signed 32-bit integer
178u32 uint32_t unsigned 32-bit integer
179i64 int64_t signed 64-bit integer
180u64 uint64_t unsigned 64-bit integer
181
182f32 float 32-bit IEE754 floating point
183f64 double 64-bit IEE754 floating point
184f128 long double 128-bit IEE754 floating point
185
186isize intptr_t signed pointer sized integer
187usize uintptr_t unsigned pointer sized integer
188
189c_short short for ABI compatibility with C
190c_ushort unsigned short for ABI compatibility with C
191c_int int for ABI compatibility with C
192c_uint unsigned int for ABI compatibility with C
193c_long long for ABI compatibility with C
194c_ulong unsigned long for ABI compatibility with C
195c_longlong long long for ABI compatibility with C
196c_ulonglong unsigned long long for ABI compatibility with C
197```
198
199### Boolean Type
200The boolean type has the name `bool` and represents either true or false.
201
202### Function Types
203TODO
204
205### Array Types
206TODO
207Also, are there slices?
208
209### Struct Types
210TODO
211
212### Pointer Types
213TODO
194214
195### Characters and Strings
215### Unreachable Type
216The unreachable type has the name `unreachable`. TODO explanation
196217
197 | Example | Characters | Escapes | Null Term | Type
198----------------|----------|-------------|----------------|-----------|----------
199 Byte | 'H' | All ASCII | Byte | No | u8
200 UTF-8 Bytes | "hello" | All Unicode | Byte & Unicode | No | [5]u8
201 UTF-8 C string | c"hello" | All Unicode | Byte & Unicode | Yes | &const u8
218### Void Type
219The void type has the name `void`. TODO explanation
202220
203### Byte Escapes
204221
205 | Name
206------|----------------------------------------
207 \x7F | 8-bit character code (exactly 2 digits)
208 \n | Newline
209 \r | Carriage return
210 \t | Tab
211 \\ | Backslash
212 \0 | Null
213 \' | Single quote
214 \" | Double quote
222## Expressions
223
224### Literals
225
226#### Character and String Literals
227```
228Literal Example Characters Escapes Null Term Type
229
230Byte 'H' All ASCII Byte No u8
231UTF-8 Bytes "hello" All Unicode Byte & Unicode No [5]u8
232UTF-8 C string c"hello" All Unicode Byte & Unicode Yes &const u8
233```
234
235```
236Escape Name
237
238\xNN hexadecimal 8-bit character code (exactly 2 digits)
239\n Newline
240\r Carriage return
241\t Tab
242\\ Backslash
243\0 Null
244\' Single quote
245\" Double quote
246```
215247
216248### Unicode Escapes
217249
218 | Name
219----------|-----------------------------------------------
220 \u{7FFF} | 24-bit Unicode character code (up to 6 digits)
250 Escape | Name
251------------|-----------------------------------------------
252 \u{NNNNNN} | hexadecimal 24-bit Unicode character code (up to 6 digits)
253
254#### Numeric Literals
255
256```
257Number literals Example Exponentiation
258
259Decimal integer 98222 N/A
260Hex integer 0xff N/A
261Octal integer 0o77 N/A
262Binary integer 0b11110000 N/A
263Floating-point 123.0E+77 Optional
264Hex floating point TODO TODO
265```
266
267### Identifiers
268TODO
269
270### Declarations
271Declarations have type `void`.
272
273#### Function Declarations
274TODO
275
276#### Variable Declarations
277TODO
278
279#### Struct Declarations
280TODO
281
282#### Enum Declarations
283TODO
284
285
286## Built-in Functions
287Built-in functions are prefixed with `@`.
288
289### Typeof
290TODO
291
292### Sizeof
293TODO
294
295### Overflow Arithmetic
296Overflow arithmetic functions have defined behavior on overflow or underflow. TODO what is that behaviour?
297
298The 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.
299
300```
301Function Operation
302bool add_with_overflow(type, a: type, b: type, x: &type) *x = a + b
303bool sub_with_overflow(type, a: type, b: type, x: &type) *x = a - b
304bool mul_with_overflow(type, a: type, b: type, x: &type) *x = a * b
305```
306
307### Memory Operations
308TODO memset and memcpy
221309
222### Numbers
310### Value Count
311TODO
223312
224 Number literals | Example | Exponentiation
225--------------------|-------------|---------------
226 Decimal integer | 98222 | N/A
227 Hex integer | 0xff | N/A
228 Octal integer | 0o77 | N/A
229 Binary integer | 0b11110000 | N/A
230 Floating-point | 123.0E+77 | Optional
231 Hex floating point | TODO | TODO
313### Max and Min Value
314TODO
src/all_types.hpp+1-1
......@@ -861,7 +861,7 @@ enum BuiltinFnId {
861861 BuiltinFnIdSizeof,
862862 BuiltinFnIdMaxValue,
863863 BuiltinFnIdMinValue,
864 BuiltinFnIdValueCount,
864 BuiltinFnIdMemberCount,
865865 BuiltinFnIdTypeof,
866866 BuiltinFnIdAddWithOverflow,
867867 BuiltinFnIdSubWithOverflow,
src/analyze.cpp+1-1
......@@ -2726,7 +2726,7 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
27262726 case BuiltinFnIdMinValue:
27272727 return analyze_min_max_value(g, import, context, node,
27282728 "no min value available for type '%s'", false);
2729 case BuiltinFnIdValueCount:
2729 case BuiltinFnIdMemberCount:
27302730 {
27312731 AstNode *type_node = node->data.fn_call_expr.params.at(0);
27322732 TypeTableEntry *type_entry = analyze_type_expr(g, import, context, type_node);
src/codegen.cpp+2-2
......@@ -315,7 +315,7 @@ static LLVMValueRef gen_builtin_fn_call_expr(CodeGen *g, AstNode *node) {
315315 zig_unreachable();
316316 }
317317 }
318 case BuiltinFnIdValueCount:
318 case BuiltinFnIdMemberCount:
319319 {
320320 assert(node->data.fn_call_expr.params.length == 1);
321321 AstNode *type_node = node->data.fn_call_expr.params.at(0);
......@@ -2620,7 +2620,7 @@ static void define_builtin_fns(CodeGen *g) {
26202620 create_builtin_fn_with_arg_count(g, BuiltinFnIdSizeof, "sizeof", 1);
26212621 create_builtin_fn_with_arg_count(g, BuiltinFnIdMaxValue, "max_value", 1);
26222622 create_builtin_fn_with_arg_count(g, BuiltinFnIdMinValue, "min_value", 1);
2623 create_builtin_fn_with_arg_count(g, BuiltinFnIdValueCount, "value_count", 1);
2623 create_builtin_fn_with_arg_count(g, BuiltinFnIdMemberCount, "member_count", 1);
26242624 create_builtin_fn_with_arg_count(g, BuiltinFnIdTypeof, "typeof", 1);
26252625 create_builtin_fn_with_arg_count(g, BuiltinFnIdAddWithOverflow, "add_with_overflow", 4);
26262626 create_builtin_fn_with_arg_count(g, BuiltinFnIdSubWithOverflow, "sub_with_overflow", 4);
test/run_tests.cpp+2-2
......@@ -1082,11 +1082,11 @@ pub fn main(args: [][]u8) i32 => {
10821082 print_str("BAD\n");
10831083 }
10841084
1085 if (@value_count(Foo) != 3) {
1085 if (@member_count(Foo) != 3) {
10861086 print_str("BAD\n");
10871087 }
10881088
1089 if (@value_count(Bar) != 4) {
1089 if (@member_count(Bar) != 4) {
10901090 print_str("BAD\n");
10911091 }
10921092