mirror of
https://github.com/ziglang/zig.git
synced 2024-11-15 00:26:57 +00:00
langref: always start code on a separate line in a syntax_block
In a syntax_block the code always start on a separate code, expect for C, JavaScript, Peg and with Zig inline assembly. Ensure that the code starts on a separate line, even in cases where there is only one line. Ensure that the end_syntax_block is always on a separate line and that the indentation is consistent.
This commit is contained in:
parent
d87a58dfab
commit
efbb6128bb
@ -6045,14 +6045,16 @@ const optional_int: ?i32 = 5678;
|
|||||||
Task: call malloc, if the result is null, return null.
|
Task: call malloc, if the result is null, return null.
|
||||||
</p>
|
</p>
|
||||||
<p>C code</p>
|
<p>C code</p>
|
||||||
{#syntax_block|c|call_malloc_in_c.c#}// malloc prototype included for reference
|
{#syntax_block|c|call_malloc_in_c.c#}
|
||||||
|
// malloc prototype included for reference
|
||||||
void *malloc(size_t size);
|
void *malloc(size_t size);
|
||||||
|
|
||||||
struct Foo *do_a_thing(void) {
|
struct Foo *do_a_thing(void) {
|
||||||
char *ptr = malloc(1234);
|
char *ptr = malloc(1234);
|
||||||
if (!ptr) return NULL;
|
if (!ptr) return NULL;
|
||||||
// ...
|
// ...
|
||||||
}{#end_syntax_block#}
|
}
|
||||||
|
{#end_syntax_block#}
|
||||||
<p>Zig code</p>
|
<p>Zig code</p>
|
||||||
{#syntax_block|zig|call_malloc_from_zig.zig#}
|
{#syntax_block|zig|call_malloc_from_zig.zig#}
|
||||||
// malloc prototype included for reference
|
// malloc prototype included for reference
|
||||||
@ -6072,7 +6074,8 @@ fn doAThing() ?*Foo {
|
|||||||
<p>
|
<p>
|
||||||
The other form of checking against NULL you might see looks like this:
|
The other form of checking against NULL you might see looks like this:
|
||||||
</p>
|
</p>
|
||||||
{#syntax_block|c|checking_null_in_c.c#}void do_a_thing(struct Foo *foo) {
|
{#syntax_block|c|checking_null_in_c.c#}
|
||||||
|
void do_a_thing(struct Foo *foo) {
|
||||||
// do some stuff
|
// do some stuff
|
||||||
|
|
||||||
if (foo) {
|
if (foo) {
|
||||||
@ -6080,7 +6083,8 @@ fn doAThing() ?*Foo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// do some stuff
|
// do some stuff
|
||||||
}{#end_syntax_block#}
|
}
|
||||||
|
{#end_syntax_block#}
|
||||||
<p>
|
<p>
|
||||||
In Zig you can accomplish the same thing:
|
In Zig you can accomplish the same thing:
|
||||||
</p>
|
</p>
|
||||||
@ -7443,7 +7447,8 @@ pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
|
|||||||
<p>
|
<p>
|
||||||
Dissecting the syntax:
|
Dissecting the syntax:
|
||||||
</p>
|
</p>
|
||||||
{#syntax_block|zig|Assembly Syntax Explained#}// Inline assembly is an expression which returns a value.
|
{#syntax_block|zig|Assembly Syntax Explained#}
|
||||||
|
// Inline assembly is an expression which returns a value.
|
||||||
// the `asm` keyword begins the expression.
|
// the `asm` keyword begins the expression.
|
||||||
_ = asm
|
_ = asm
|
||||||
// `volatile` is an optional modifier that tells Zig this
|
// `volatile` is an optional modifier that tells Zig this
|
||||||
@ -7498,7 +7503,8 @@ volatile (
|
|||||||
// output. In this example we list $rcx and $r11 because it is known the
|
// output. In this example we list $rcx and $r11 because it is known the
|
||||||
// kernel syscall does not preserve these registers.
|
// kernel syscall does not preserve these registers.
|
||||||
: "rcx", "r11"
|
: "rcx", "r11"
|
||||||
);{#end_syntax_block#}
|
);
|
||||||
|
{#end_syntax_block#}
|
||||||
<p>
|
<p>
|
||||||
For x86 and x86_64 targets, the syntax is AT&T syntax, rather than the more
|
For x86 and x86_64 targets, the syntax is AT&T syntax, rather than the more
|
||||||
popular Intel syntax. This is due to technical constraints; assembly parsing is
|
popular Intel syntax. This is due to technical constraints; assembly parsing is
|
||||||
@ -10688,12 +10694,15 @@ const c = @cImport({
|
|||||||
or <kbd>-cflags</kbd> could result in clang or Zig parse failures, or subtle ABI incompatibilities
|
or <kbd>-cflags</kbd> could result in clang or Zig parse failures, or subtle ABI incompatibilities
|
||||||
when linking with C code.
|
when linking with C code.
|
||||||
</p>
|
</p>
|
||||||
{#syntax_block|c|varytarget.h#}long FOO = __LONG_MAX__;{#end_syntax_block#}
|
{#syntax_block|c|varytarget.h#}
|
||||||
|
long FOO = __LONG_MAX__;
|
||||||
|
{#end_syntax_block#}
|
||||||
{#shell_samp#}$ zig translate-c -target thumb-freestanding-gnueabihf varytarget.h|grep FOO
|
{#shell_samp#}$ zig translate-c -target thumb-freestanding-gnueabihf varytarget.h|grep FOO
|
||||||
pub export var FOO: c_long = 2147483647;
|
pub export var FOO: c_long = 2147483647;
|
||||||
$ zig translate-c -target x86_64-macos-gnu varytarget.h|grep FOO
|
$ zig translate-c -target x86_64-macos-gnu varytarget.h|grep FOO
|
||||||
pub export var FOO: c_long = 9223372036854775807;{#end_shell_samp#}
|
pub export var FOO: c_long = 9223372036854775807;{#end_shell_samp#}
|
||||||
{#syntax_block|c|varycflags.h#}enum FOO { BAR };
|
{#syntax_block|c|varycflags.h#}
|
||||||
|
enum FOO { BAR };
|
||||||
int do_something(enum FOO foo);
|
int do_something(enum FOO foo);
|
||||||
{#end_syntax_block#}
|
{#end_syntax_block#}
|
||||||
{#shell_samp#}$ zig translate-c varycflags.h|grep -B1 do_something
|
{#shell_samp#}$ zig translate-c varycflags.h|grep -B1 do_something
|
||||||
@ -10784,7 +10793,8 @@ pub fn main() void {
|
|||||||
Zig.
|
Zig.
|
||||||
</p>
|
</p>
|
||||||
<p>Consider the following example:</p>
|
<p>Consider the following example:</p>
|
||||||
{#syntax_block|c|macro.c#}#define MAKELOCAL(NAME, INIT) int NAME = INIT
|
{#syntax_block|c|macro.c#}
|
||||||
|
#define MAKELOCAL(NAME, INIT) int NAME = INIT
|
||||||
int foo(void) {
|
int foo(void) {
|
||||||
MAKELOCAL(a, 1);
|
MAKELOCAL(a, 1);
|
||||||
MAKELOCAL(b, 2);
|
MAKELOCAL(b, 2);
|
||||||
@ -10905,7 +10915,8 @@ export fn add(a: i32, b: i32) i32 {
|
|||||||
<p>To make a shared library:</p>
|
<p>To make a shared library:</p>
|
||||||
{#shell_samp#}$ zig build-lib mathtest.zig -dynamic{#end_shell_samp#}
|
{#shell_samp#}$ zig build-lib mathtest.zig -dynamic{#end_shell_samp#}
|
||||||
<p>Here is an example with the {#link|Zig Build System#}:</p>
|
<p>Here is an example with the {#link|Zig Build System#}:</p>
|
||||||
{#syntax_block|c|test.c#}// This header is generated by zig from mathtest.zig
|
{#syntax_block|c|test.c#}
|
||||||
|
// This header is generated by zig from mathtest.zig
|
||||||
#include "mathtest.h"
|
#include "mathtest.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@ -10959,7 +10970,8 @@ export fn decode_base_64(
|
|||||||
return decoded_size;
|
return decoded_size;
|
||||||
}
|
}
|
||||||
{#code_end#}
|
{#code_end#}
|
||||||
{#syntax_block|c|test.c#}// This header is generated by zig from base64.zig
|
{#syntax_block|c|test.c#}
|
||||||
|
// This header is generated by zig from base64.zig
|
||||||
#include "base64.h"
|
#include "base64.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -11009,7 +11021,8 @@ export fn add(a: i32, b: i32) void {
|
|||||||
print(a + b);
|
print(a + b);
|
||||||
}
|
}
|
||||||
{#code_end#}
|
{#code_end#}
|
||||||
{#syntax_block|javascript|test.js#}const fs = require('fs');
|
{#syntax_block|javascript|test.js#}
|
||||||
|
const fs = require('fs');
|
||||||
const source = fs.readFileSync("./math.wasm");
|
const source = fs.readFileSync("./math.wasm");
|
||||||
const typedArray = new Uint8Array(source);
|
const typedArray = new Uint8Array(source);
|
||||||
|
|
||||||
@ -11019,8 +11032,9 @@ WebAssembly.instantiate(typedArray, {
|
|||||||
}}).then(result => {
|
}}).then(result => {
|
||||||
const add = result.instance.exports.add;
|
const add = result.instance.exports.add;
|
||||||
add(1, 2);
|
add(1, 2);
|
||||||
});{#end_syntax_block#}
|
});
|
||||||
{#shell_samp#}$ node test.js
|
{#end_syntax_block#}
|
||||||
|
{#shell_samp#}$ node test.js
|
||||||
The result is 3{#end_shell_samp#}
|
The result is 3{#end_shell_samp#}
|
||||||
{#header_close#}
|
{#header_close#}
|
||||||
{#header_open|WASI#}
|
{#header_open|WASI#}
|
||||||
@ -12065,7 +12079,8 @@ fn readU32Be() u32 {}
|
|||||||
{#header_close#}
|
{#header_close#}
|
||||||
|
|
||||||
{#header_open|Grammar#}
|
{#header_open|Grammar#}
|
||||||
{#syntax_block|peg|grammar.y#}Root <- skip container_doc_comment? ContainerMembers eof
|
{#syntax_block|peg|grammar.y#}
|
||||||
|
Root <- skip container_doc_comment? ContainerMembers eof
|
||||||
|
|
||||||
# *** Top level ***
|
# *** Top level ***
|
||||||
ContainerMembers <- ContainerDeclarations (ContainerField COMMA)* (ContainerField / ContainerDeclarations)
|
ContainerMembers <- ContainerDeclarations (ContainerField COMMA)* (ContainerField / ContainerDeclarations)
|
||||||
@ -12631,7 +12646,7 @@ keyword <- KEYWORD_addrspace / KEYWORD_align / KEYWORD_allowzero / KEYWORD_and
|
|||||||
/ KEYWORD_struct / KEYWORD_suspend / KEYWORD_switch / KEYWORD_test
|
/ KEYWORD_struct / KEYWORD_suspend / KEYWORD_switch / KEYWORD_test
|
||||||
/ KEYWORD_threadlocal / KEYWORD_try / KEYWORD_union / KEYWORD_unreachable
|
/ KEYWORD_threadlocal / KEYWORD_try / KEYWORD_union / KEYWORD_unreachable
|
||||||
/ KEYWORD_usingnamespace / KEYWORD_var / KEYWORD_volatile / KEYWORD_while
|
/ KEYWORD_usingnamespace / KEYWORD_var / KEYWORD_volatile / KEYWORD_while
|
||||||
{#end_syntax_block#}
|
{#end_syntax_block#}
|
||||||
{#header_close#}
|
{#header_close#}
|
||||||
{#header_open|Zen#}
|
{#header_open|Zen#}
|
||||||
<ul>
|
<ul>
|
||||||
|
Loading…
Reference in New Issue
Block a user