authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-08 00:13:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-08 00:13:54-04:00
log304cfb7122a9f32d11b1f699fc9768ce81b7b9ca
tree4cb3899a4c00e50bca285e7b83699f65fa27dc53
parent2f20fe6ecd15581e356b3dcd3b254611b132c096

move docs to ziglang.org


3 files changed, 2 insertions(+), 575 deletions(-)

README.md+2
......@@ -5,6 +5,8 @@ clarity.
55
66[ziglang.org](http://ziglang.org)
77
8[Documentation](http://ziglang.org/documentation/)
9
810## Feature Highlights
911
1012 * Small, simple language. Focus on debugging your application rather than
doc/langref.md-500
......@@ -177,503 +177,3 @@ or
177177?? %%
178178= *= /= %= += -= <<= >>= &= ^= |=
179179```
180
181## Types
182
183### Numeric Types
184
185```
186Type name C equivalent Description
187
188i8 int8_t signed 8-bit integer
189u8 uint8_t unsigned 8-bit integer
190i16 int16_t signed 16-bit integer
191u16 uint16_t unsigned 16-bit integer
192i32 int32_t signed 32-bit integer
193u32 uint32_t unsigned 32-bit integer
194i64 int64_t signed 64-bit integer
195u64 uint64_t unsigned 64-bit integer
196isize intptr_t signed pointer sized integer
197usize uintptr_t unsigned pointer sized integer
198
199c_short short for ABI compatibility with C
200c_ushort unsigned short for ABI compatibility with C
201c_int int for ABI compatibility with C
202c_uint unsigned int for ABI compatibility with C
203c_long long for ABI compatibility with C
204c_ulong unsigned long for ABI compatibility with C
205c_longlong long long for ABI compatibility with C
206c_ulonglong unsigned long long for ABI compatibility with C
207c_longdouble long double for ABI compatibility with C
208c_void void for ABI compatibility with C
209
210f32 float 32-bit floating point
211f64 double 64-bit floating point
212```
213
214## Expressions
215
216### Literals
217
218#### Character and String Literals
219
220```
221Literal Example Characters Escapes Null Term Type
222
223Byte 'H' All ASCII Byte No u8
224UTF-8 Bytes "hello" All Unicode Byte & Unicode No [5]u8
225UTF-8 C string c"hello" All Unicode Byte & Unicode Yes &const u8
226```
227
228### Escapes
229
230 Escape | Name
231----------|-------------------------------------------------------------------
232 \n | Newline
233 \r | Carriage Return
234 \t | Tab
235 \\ | Backslash
236 \' | Single Quote
237 \" | Double Quote
238 \xNN | hexadecimal 8-bit character code (2 digits)
239 \uNNNN | hexadecimal 16-bit Unicode character code UTF-8 encoded (4 digits)
240 \UNNNNNN | hexadecimal 24-bit Unicode character code UTF-8 encoded (6 digits)
241
242Note that the maximum valid Unicode point is 0x10ffff.
243
244##### Multiline String Literals
245
246Multiline string literals have no escapes and can span across multiple lines.
247To start a multiline string literal, use the `\\` token. Just like a comment,
248the string literal goes until the end of the line. The end of the line is not
249included in the string literal.
250
251However, if the next line begins with `\\` then a newline is appended and
252the string literal continues.
253
254Example:
255
256```zig
257const hello_world_in_c =
258 \\#include <stdio.h>
259 \\
260 \\int main(int argc, char **argv) {
261 \\ printf("hello world\n");
262 \\ return 0;
263 \\}
264;
265```
266
267For a multiline C string literal, prepend `c` to each `\\`. Example:
268
269```zig
270const c_string_literal =
271 c\\#include <stdio.h>
272 c\\
273 c\\int main(int argc, char **argv) {
274 c\\ printf("hello world\n");
275 c\\ return 0;
276 c\\}
277;
278```
279
280In this example the variable `c_string_literal` has type `&const char` and
281has a terminating null byte.
282
283#### Number Literals
284
285 Number literals | Example | Exponentiation
286--------------------|-------------|--------------
287 Decimal integer | 98222 | N/A
288 Hex integer | 0xff | N/A
289 Octal integer | 0o77 | N/A
290 Binary integer | 0b11110000 | N/A
291 Floating point | 123.0E+77 | Optional
292 Hex floating point | 0x103.70p-5 | Optional
293
294## Built-in Functions
295
296Built-in functions are prefixed with `@`. Remember that the `comptime` keyword on
297a parameter means that the parameter must be known at compile time.
298
299### @typeOf(expression) -> type
300
301This function returns a compile-time constant, which is the type of the
302expression passed as an argument. The expression is *not evaluated*.
303
304### @sizeOf(comptime T: type) -> (number literal)
305
306This function returns the number of bytes it takes to store T in memory.
307
308The result is a target-specific compile time constant.
309
310### @alignOf(comptime T: type) -> (number literal)
311
312This function returns the number of bytes that this type should be aligned to
313for the current target.
314
315The result is a target-specific compile time constant.
316
317### @offsetOf(comptime T: type, comptime field_name: [] const u8) -> (number literal)
318
319This function returns the byte offset of a field relative to its containing struct.
320
321### Overflow Arithmetic
322
323These functions take an integer type, two variables of the specified type,
324and a pointer to memory of the specified type where the result is stored.
325
326The functions return a boolean value: true if overflow or underflow occurred,
327false otherwise.
328
329```
330Function Operation
331@addWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool *x = a + b
332@subWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool *x = a - b
333@mulWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool *x = a * b
334@shlWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool *x = a << b
335```
336
337### @memset(dest: &u8, c: u8, byte_count: usize)
338
339This function sets a region of memory to `c`. `dest` is a pointer.
340
341This function is a low level intrinsic with no safety mechanisms. Most higher
342level code will not use this function, instead using something like this:
343
344```zig
345for (destSlice) |*b| *b = c;
346```
347
348The optimizer is intelligent enough to turn the above snippet into a memset.
349
350### @memcpy(noalias dest: &u8, noalias source: &const u8, byte_count: usize)
351
352This function copies bytes from one region of memory to another. `dest` and
353`source` are both pointers and must not overlap.
354
355This function is a low level intrinsic with no safety mechanisms. Most higher
356level code will not use this function, instead using something like this:
357
358```zig
359const mem = @import("std").mem;
360mem.copy(destSlice, sourceSlice);
361```
362
363The optimizer is intelligent enough to turn the above snippet into a memcpy.
364
365### @breakpoint()
366
367This function inserts a platform-specific debug trap instruction which causes
368debuggers to break there.
369
370This function is only valid within function scope.
371
372### @returnAddress()
373
374This function returns a pointer to the return address of the current stack
375frame.
376
377The implications of this are target specific and not consistent across
378all platforms.
379
380This function is only valid within function scope.
381
382### @frameAddress()
383
384This function returns the base pointer of the current stack frame.
385
386The implications of this are target specific and not consistent across all
387platforms. The frame address may not be available in release mode due to
388aggressive optimizations.
389
390This function is only valid within function scope.
391
392### @maxValue(comptime T: type) -> (number literal)
393
394This function returns the maximum integer value of the integer type T.
395
396The result is a compile time constant. For some types such as `c_long`, the
397result is marked as depending on a compile variable.
398
399### @minValue(comptime T: type) -> (number literal)
400
401This function returns the minimum integer value of the integer type T.
402
403The result is a compile time constant. For some types such as `c_long`, the
404result is marked as depending on a compile variable.
405
406### @memberCount(comptime T: type) -> (number literal)
407
408This function returns the number of enum values in an enum type.
409
410The result is a compile time constant.
411
412### @import(comptime path: []u8) -> (namespace)
413
414This function finds a zig file corresponding to `path` and imports all the
415public top level declarations into the resulting namespace.
416
417`path` can be a relative or absolute path, or it can be the name of a package,
418such as "std".
419
420This function is only valid at top level scope.
421
422### @cImport(expression) -> (namespace)
423
424This function parses C code and imports the functions, types, variables, and
425compatible macro definitions into the result namespace.
426
427`expression` is interpreted at compile time. The builtin functions
428`@c_include`, `@c_define`, and `@c_undef` work within this expression,
429appending to a temporary buffer which is then parsed as C code.
430
431This function is only valid at top level scope.
432
433### @cInclude(comptime path: []u8)
434
435This function can only occur inside `@c_import`.
436
437This appends `#include <$path>\n` to the `c_import` temporary buffer.
438
439### @cDefine(comptime name: []u8, value)
440
441This function can only occur inside `@c_import`.
442
443This appends `#define $name $value` to the `c_import` temporary buffer.
444
445### @cUndef(comptime name: []u8)
446
447This function can only occur inside `@c_import`.
448
449This appends `#undef $name` to the `c_import` temporary buffer.
450
451### @generatedCode(expression) -> @typeOf(expression)
452
453This function wraps an expression and returns the result of the expression
454unmodified.
455
456Inside the expression, code is considered generated, which means that the
457following compile errors are disabled:
458
459 * unnecessary if statement error
460
461The result of the expression is marked as depending on a compile variable.
462
463### @ctz(x: T) -> T
464
465This function counts the number of trailing zeroes in x which is an integer
466type T.
467
468### @clz(x: T) -> T
469
470This function counts the number of leading zeroes in x which is an integer
471type T.
472
473### @errorName(err: error) -> []u8
474
475This function returns the string representation of an error. If an error
476declaration is:
477
478```zig
479error OutOfMem;
480```
481
482Then the string representation is "OutOfMem".
483
484If there are no calls to `@errorName` in an entire application, then no error
485name table will be generated.
486
487### @typeName(T: type) -> []u8
488
489This function returns the string representation of a type.
490
491### @embedFile(comptime path: []u8) -> [X]u8
492
493This function returns a compile time constant fixed-size array with length
494equal to the byte count of the file given by `path`. The contents of the array
495are the contents of the file.
496
497### @cmpxchg(ptr: &T, cmp: T, new: T, success_order: MemoryOrder, fail_order: MemoryOrder) -> bool
498
499This function performs an atomic compare exchange operation.
500
501### @fence(order: MemoryOrder)
502
503The `fence` function is used to introduce happens-before edges between operations.
504
505### @truncate(comptime T: type, integer) -> T
506
507This function truncates bits from an integer type, resulting in a smaller
508integer type.
509
510The following produces a crash in debug mode and undefined behavior in
511release mode:
512
513```zig
514const a: u16 = 0xabcd;
515const b: u8 = u8(a);
516```
517
518However this is well defined and working code:
519
520```zig
521const a: u16 = 0xabcd;
522const b: u8 = @truncate(u8, a);
523// b is now 0xcd
524```
525
526This function always truncates the significant bits of the integer, regardless
527of endianness on the target platform.
528
529This function also performs a twos complement cast. For example, the following
530produces a crash in debug mode and undefined behavior in release mode:
531
532```zig
533const a = i16(-1);
534const b = u16(a);
535```
536
537However this is well defined and working code:
538
539```zig
540const a = i16(-1);
541const b = @truncate(u16, a);
542// b is now 0xffff
543```
544
545### @compileError(comptime msg: []u8)
546
547This function, when semantically analyzed, causes a compile error with the
548message `msg`.
549
550There are several ways that code avoids being semantically checked, such as
551using `if` or `switch` with compile time constants, and comptime functions.
552
553### @compileLog(args: ...)
554
555This function, when semantically analyzed, causes a compile error, but it does
556not prevent compile-time code from continuing to run, and it otherwise does not
557interfere with analysis.
558
559Each of the arguments will be serialized to a printable debug value and output
560to stderr, and then a newline at the end.
561
562This function can be used to do "printf debugging" on compile-time executing
563code.
564
565### @IntType(comptime is_signed: bool, comptime bit_count: u8) -> type
566
567This function returns an integer type with the given signness and bit count.
568
569### @setDebugSafety(scope, safety_on: bool)
570
571Sets a whether we want debug safety checks on for a given scope.
572
573### @isInteger(comptime T: type) -> bool
574
575Returns whether a given type is an integer.
576
577### @isFloat(comptime T: type) -> bool
578
579Returns whether a given type is a float.
580
581### @canImplicitCast(comptime T: type, value) -> bool
582
583Returns whether a value can be implicitly casted to a given type.
584
585### @setGlobalAlign(global_variable_name, byte_count: usize) -> bool
586
587Sets the alignment property of a global variable.
588
589### @setGlobalSection(global_variable_name, section_name: []u8) -> bool
590
591Puts the global variable in the specified section.
592
593### @panic(message: []const u8) -> noreturn
594
595Invokes the panic handler function. By default the panic handler function
596calls the public `panic` function exposed in the root source file, or
597if there is not one specified, invokes the one provided in
598`std/special/panic.zig`.
599
600### @ptrCast(comptime DestType: type, value: var) -> DestType
601
602Converts a pointer of one type to a pointer of another type.
603
604### @intToPtr(comptime DestType: type, int: usize) -> DestType
605
606Converts an integer to a pointer. To convert the other way, use `usize(ptr)`.
607
608### @enumTagName(value: var) -> []const u8
609
610Converts an enum tag name to a slice of bytes. Example:
611
612### @fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8, field_ptr: &T) -> &ParentType
613
614Given a pointer to a field, returns the base pointer of a struct.
615
616### @rem(numerator: T, denominator: T) -> T
617
618Remainder division. For unsigned integers this is the same as
619`numerator % denominator`. Caller guarantees `denominator > 0`.
620
621 * `@rem(-5, 3) == -2`
622 * `@divTrunc(a, b) + @rem(a, b) == a`
623
624See also:
625 * `std.math.rem`
626 * `@mod`
627
628### @mod(numerator: T, denominator: T) -> T
629
630Modulus division. For unsigned integers this is the same as
631`numerator % denominator`. Caller guarantees `denominator > 0`.
632
633 * `@mod(-5, 3) == 1`
634 * `@divFloor(a, b) + @mod(a, b) == a`
635
636See also:
637 * `std.math.mod`
638 * `@rem`
639
640### @divTrunc(numerator: T, denominator: T) -> T
641
642Truncated division. Rounds toward zero. For unsigned integers it is
643the same as `numerator / denominator`. Caller guarantees `denominator != 0` and
644`!(@isInteger(T) and T.is_signed and numerator == @minValue(T) and denominator == -1)`.
645
646 * `@divTrunc(-5, 3) == -1`
647 * `@divTrunc(a, b) + @rem(a, b) == a`
648
649See also:
650 * `std.math.divTrunc`
651 * `@divFloor`
652 * `@divExact`
653
654### @divFloor(numerator: T, denominator: T) -> T
655
656Floored division. Rounds toward negative infinity. For unsigned integers it is
657the same as `numerator / denominator`. Caller guarantees `denominator != 0` and
658`!(@isInteger(T) and T.is_signed and numerator == @minValue(T) and denominator == -1)`.
659
660 * `@divFloor(-5, 3) == -2`
661 * `@divFloor(a, b) + @mod(a, b) == a`
662
663See also:
664 * `std.math.divFloor`
665 * `@divTrunc`
666 * `@divExact`
667
668### @divExact(numerator: T, denominator: T) -> T
669
670Exact division. Caller guarantees `denominator != 0` and
671`@divTrunc(numerator, denominator) * denominator == numerator`.
672
673 * `@divExact(6, 3) == 2`
674 * `@divExact(a, b) * b == a`
675
676See also:
677 * `std.math.divExact`
678 * `@divTrunc`
679 * `@divFloor`
doc/style.md deleted-75
......@@ -1,75 +0,0 @@
1# Official Style Guide
2
3These conventions are not enforced by the compiler, but they are shipped in
4this documentation along with the compiler in order to provide a point of
5reference, should anyone wish to point to an authority on agreed upon Zig
6coding style.
7
8## Whitespace
9
10 * 4 space indentation
11 * Open braces on same line, unless you need to wrap.
12 * If a list of things is longer than 2, put each item on its own line and
13 exercise the abilty to put an extra comma at the end.
14 * Line length: aim for 100; use common sense.
15
16## Names
17
18Roughly speaking: `camelCaseFunctionName`, `TitleCaseTypeName`,
19`snake_case_variable_name`. More precisely:
20
21 * If `x` is a `struct` (or an alias of a `struct`), then `x` should be `TitleCase`.
22 * If `x` otherwise identifies a type, `x` should have `snake_case`.
23 * If `x` is callable, and `x`'s return type is `type`, then `x` should be `TitleCase`.
24 * If `x` is otherwise callable, then `x` should be `camelCase`.
25 * Otherwise, `x` should be `snake_case`.
26
27Acronyms, initialisms, proper nouns, or any other word that has capitalization
28rules in written English are subject to naming conventions just like any other
29word. Even acronyms that are only 2 letters long are subject to these
30conventions.
31
32These are general rules of thumb; if it makes sense to do something different,
33do what makes sense.
34
35Examples:
36
37```zig
38const namespace_name = @import("dir_name/file_name.zig");
39var global_var: i32;
40const const_name = 42;
41const primitive_type_alias = f32;
42const string_alias = []u8;
43
44struct StructName {}
45const StructAlias = StructName;
46
47fn functionName(param_name: TypeName) {
48 var functionPointer = functionName;
49 functionPointer();
50 functionPointer = otherFunction;
51 functionPointer();
52}
53const functionAlias = functionName;
54
55fn ListTemplateFunction(ChildType: type, inline fixed_size: usize) -> type {
56 struct ShortList(T: type, n: usize) {
57 field_name: [n]T,
58 fn methodName() {}
59 }
60 return List(ChildType, fixed_size);
61}
62
63// The word XML loses its casing when used in Zig identifiers.
64const xml_document =
65 \\<?xml version="1.0" encoding="UTF-8"?>
66 \\<document>
67 \\</document>
68;
69struct XmlParser {}
70
71// The initials BE (Big Endian) are just another word in Zig identifier names.
72fn readU32Be() -> u32 {}
73```
74
75See Zig standard library for examples.