authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-25 15:45:05-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-25 15:45:05-07:00
logdeb35868841fc3eb99228fe102a5b8ed1991d51f
treee84077cf298eec6f95cd3a75f1f05cd4cb0b501f
parentc0dc0ca6c90649f157dfcb43c7ec69fa4b5f6b09

implement %% prefix operator

See #23 also make undefined constants use llvm undef value

9 files changed, 54 insertions(+), 20 deletions(-)

README.md+2-2
......@@ -48,8 +48,8 @@ compromises backward compatibility.
4848### Current Status
4949
5050 * Have a look in the example/ folder to see some code examples.
51 * Basic language features available such as loops, inline assembly,
52 expressions, literals, functions, importing, structs, tagged unions.
51 * Most language features are available, but many edge cases and errors are
52 not yet implemented.
5353 * Linux x86_64 is supported.
5454 * Building for the native target is supported.
5555 * Optimized machine code that Zig produces is indistinguishable from
doc/langref.md+2-2
......@@ -137,7 +137,7 @@ ContainerInitBody : list(StructLiteralField, ",") | list(Expression, ",")
137137
138138StructLiteralField : "." "Symbol" "=" Expression
139139
140PrefixOp : "!" | "-" | "~" | "*" | ("&" option("const")) | "?" | "%"
140PrefixOp : "!" | "-" | "~" | "*" | ("&" option("const")) | "?" | "%" | "%%"
141141
142142PrimaryExpression : "Number" | "String" | "CharLiteral" | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | "Symbol" | ("@" "Symbol" FnCallExpression) | ArrayType | AsmExpression | ("error" "." "Symbol")
143143
......@@ -154,7 +154,7 @@ KeywordLiteral : "true" | "false" | "null" | "break" | "continue" | "undefined"
154154
155155```
156156x() x[] x.y
157!x -x ~x *x &x ?x %x
157!x -x ~x *x &x ?x %x %%x
158158x{}
159159* / %
160160+ -
example/cat/main.zig+3-4
......@@ -5,7 +5,6 @@ import "std.zig";
55// Things to do to make this work:
66// * var args printing
77// * defer
8// * %% binary operator
98// * %% prefix operator
109// * cast err type to string
1110// * string equality
......@@ -21,7 +20,7 @@ pub fn main(args: [][]u8) %void => {
2120 return usage(exe);
2221 } else {
2322 var is: InputStream;
24 is.open(arg, OpenReadOnly) %% (err) => {
23 is.open(arg, OpenReadOnly) %% |err| {
2524 %%stderr.print("Unable to open file: {}", ([]u8])(err));
2625 return err;
2726 }
......@@ -45,7 +44,7 @@ fn cat_stream(is: InputStream) %void => {
4544 var buf: [1024 * 4]u8;
4645
4746 while (true) {
48 const bytes_read = is.read(buf) %% (err) => {
47 const bytes_read = is.read(buf) %% |err| {
4948 %%stderr.print("Unable to read from stream: {}", ([]u8)(err));
5049 return err;
5150 }
......@@ -54,7 +53,7 @@ fn cat_stream(is: InputStream) %void => {
5453 break;
5554 }
5655
57 stdout.write(buf[0...bytes_read]) %% (err) => {
56 stdout.write(buf[0...bytes_read]) %% |err| {
5857 %%stderr.print("Unable to write to stdout: {}", ([]u8)(err));
5958 return err;
6059 }
example/guess_number/main.zig+8-8
......@@ -4,35 +4,35 @@ import "std.zig";
44import "rand.zig";
55
66pub fn main(args: [][]u8) %void => {
7 stderr.print_str("Welcome to the Guess Number Game in Zig.\n");
7 %%stderr.print_str("Welcome to the Guess Number Game in Zig.\n");
88
99 var seed : u32;
1010 const seed_bytes = (&u8)(&seed)[0...4];
11 os_get_random_bytes(seed_bytes) %% unreachable{};
11 %%os_get_random_bytes(seed_bytes);
1212
1313 var rand = rand_new(seed);
1414
1515 const answer = rand.range_u64(0, 100) + 1;
1616
1717 while (true) {
18 stderr.print_str("\nGuess a number between 1 and 100: ");
18 %%stderr.print_str("\nGuess a number between 1 and 100: ");
1919 var line_buf : [20]u8;
2020
2121 const line_len = stdin.read(line_buf) %% |err| {
22 stderr.print_str("Unable to read from stdin.\n");
22 %%stderr.print_str("Unable to read from stdin.\n");
2323 return err;
2424 };
2525
2626 const guess = parse_u64(line_buf[0...line_len - 1], 10) %% {
27 stderr.print_str("Invalid number.\n");
27 %%stderr.print_str("Invalid number.\n");
2828 continue;
2929 };
3030 if (guess > answer) {
31 stderr.print_str("Guess lower.\n");
31 %%stderr.print_str("Guess lower.\n");
3232 } else if (guess < answer) {
33 stderr.print_str("Guess higher.\n");
33 %%stderr.print_str("Guess higher.\n");
3434 } else {
35 stderr.print_str("You win!\n");
35 %%stderr.print_str("You win!\n");
3636 return;
3737 }
3838 }
src/all_types.hpp+1
......@@ -405,6 +405,7 @@ enum PrefixOp {
405405 PrefixOpDereference,
406406 PrefixOpMaybe,
407407 PrefixOpError,
408 PrefixOpUnwrapError,
408409};
409410
410411struct AstNodePrefixOpExpr {
src/analyze.cpp+14
......@@ -3656,6 +3656,20 @@ static TypeTableEntry *analyze_prefix_op_expr(CodeGen *g, ImportTableEntry *impo
36563656 }
36573657
36583658 }
3659 case PrefixOpUnwrapError:
3660 {
3661 TypeTableEntry *type_entry = analyze_expression(g, import, context, nullptr, expr_node);
3662
3663 if (type_entry->id == TypeTableEntryIdInvalid) {
3664 return type_entry;
3665 } else if (type_entry->id == TypeTableEntryIdErrorUnion) {
3666 return type_entry->data.error.child_type;
3667 } else {
3668 add_node_error(g, expr_node,
3669 buf_sprintf("expected error type, got '%s'", buf_ptr(&type_entry->name)));
3670 return g->builtin_types.entry_invalid;
3671 }
3672 }
36593673 }
36603674 zig_unreachable();
36613675}
src/codegen.cpp+19-1
......@@ -833,6 +833,24 @@ static LLVMValueRef gen_prefix_op_expr(CodeGen *g, AstNode *node) {
833833 {
834834 zig_panic("TODO codegen PrefixOpError");
835835 }
836 case PrefixOpUnwrapError:
837 {
838 LLVMValueRef expr_val = gen_expr(g, expr_node);
839 TypeTableEntry *expr_type = get_expr_type(expr_node);
840 assert(expr_type->id == TypeTableEntryIdErrorUnion);
841 TypeTableEntry *child_type = expr_type->data.error.child_type;
842 // TODO in debug mode, put a panic here if the error is not 0
843 if (child_type->size_in_bits > 0) {
844 LLVMValueRef child_val_ptr = LLVMBuildStructGEP(g->builder, expr_val, 1, "");
845 if (handle_is_ptr(child_type)) {
846 return child_val_ptr;
847 } else {
848 return expr_val;
849 }
850 } else {
851 return nullptr;
852 }
853 }
836854 }
837855 zig_unreachable();
838856}
......@@ -2219,7 +2237,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
22192237 assert(const_val->ok);
22202238
22212239 if (const_val->undef) {
2222 return LLVMConstNull(type_entry->type_ref);
2240 return LLVMGetUndef(type_entry->type_ref);
22232241 }
22242242
22252243 if (type_entry->id == TypeTableEntryIdInt) {
src/parser.cpp+2
......@@ -64,6 +64,7 @@ static const char *prefix_op_str(PrefixOp prefix_op) {
6464 case PrefixOpDereference: return "*";
6565 case PrefixOpMaybe: return "?";
6666 case PrefixOpError: return "%";
67 case PrefixOpUnwrapError: return "%%";
6768 }
6869 zig_unreachable();
6970}
......@@ -1664,6 +1665,7 @@ static PrefixOp tok_to_prefix_op(Token *token) {
16641665 case TokenIdStar: return PrefixOpDereference;
16651666 case TokenIdMaybe: return PrefixOpMaybe;
16661667 case TokenIdPercent: return PrefixOpError;
1668 case TokenIdPercentPercent: return PrefixOpUnwrapError;
16671669 case TokenIdBoolAnd: return PrefixOpAddressOf;
16681670 default: return PrefixOpInvalid;
16691671 }
std/bootstrap.zig+3-3
......@@ -3,9 +3,9 @@ import "syscall.zig";
33// The compiler treats this file special by implicitly importing the function `main`
44// from the root source file.
55
6var argc: isize;
7var argv: &&u8;
8var env: &&u8;
6var argc: isize = undefined;
7var argv: &&u8 = undefined;
8var env: &&u8 = undefined;
99
1010#attribute("naked")
1111export fn _start() unreachable => {