Commit f17e20d5fe

Andrew Kelley <superjoe30@gmail.com>
2015-12-15 02:10:25
instead of *mut and *const, & and &const
closes #33
1 parent 7dd2929
doc/langref.md
@@ -60,7 +60,7 @@ ParamDecl : token(Symbol) token(Colon) Type | token(Ellipsis)
 
 Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType
 
-PointerType : token(Star) (token(Const) | token(Mut)) Type
+PointerType : token(Ampersand) option(token(Const)) Type
 
 ArrayType : token(LBracket) Type token(Semicolon) Expression token(RBracket)
 
@@ -114,7 +114,7 @@ BinaryOrExpression : BinaryXorExpression token(BinOr) BinaryOrExpression | Binar
 
 BinaryXorExpression : BinaryAndExpression token(BinXor) BinaryXorExpression | BinaryAndExpression
 
-BinaryAndExpression : BitShiftExpression token(BinAnd) BinaryAndExpression | BitShiftExpression
+BinaryAndExpression : BitShiftExpression token(Ampersand) BinaryAndExpression | BitShiftExpression
 
 BitShiftExpression : AdditionExpression BitShiftOperator BitShiftExpression | AdditionExpression
 
example/arrays/arrays.zig
@@ -2,7 +2,7 @@ export executable "arrays";
 
 use "std.zig";
 
-export fn main(argc: isize, argv: *mut *mut u8, env: *mut *mut u8) -> i32 {
+export fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
     let mut array : [i32; 5];
 
     let mut i : i32 = 0;
@@ -30,7 +30,7 @@ loop_2_start:
 loop_2_end:
 
     if accumulator == 15 {
-        print_str("OK" as string);
+        print_str("OK\n" as string);
     }
 
 
example/expressions/expressions.zig
@@ -2,7 +2,7 @@ export executable "expressions";
 
 #link("c")
 extern {
-    fn puts(s: *const u8) -> i32;
+    fn puts(s: &const u8) -> i32;
     fn exit(code: i32) -> unreachable;
 }
 
example/hello_world/hello.zig
@@ -2,7 +2,7 @@ export executable "hello";
 
 use "std.zig";
 
-export fn main(argc: isize, argv: *mut *mut u8, env: *mut *mut u8) -> i32 {
+export fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
     // TODO implicit coercion from array to string
     print_str("Hello, world!\n" as string);
     return 0;
example/hello_world/hello_libc.zig
@@ -2,7 +2,7 @@ export executable "hello";
 
 #link("c")
 extern {
-    fn printf(__format: *const u8, ...) -> i32;
+    fn printf(__format: &const u8, ...) -> i32;
     fn exit(__status: i32) -> unreachable;
 }
 
example/multiple_files/libc.zig
@@ -1,5 +1,5 @@
 #link("c")
 extern {
-    pub fn puts(s: *const u8) -> i32;
+    pub fn puts(s: &const u8) -> i32;
     pub fn exit(code: i32) -> unreachable;
 }
example/structs/structs.zig
@@ -2,7 +2,7 @@ export executable "structs";
 
 use "std.zig";
 
-export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
+export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
     let mut foo : Foo;
 
     foo.a = foo.a + 1;
src/analyze.cpp
@@ -111,7 +111,7 @@ TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool
         TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPointer);
         entry->type_ref = LLVMPointerType(child_type->type_ref, 0);
         buf_resize(&entry->name, 0);
-        buf_appendf(&entry->name, "*%s %s", is_const ? "const" : "mut", buf_ptr(&child_type->name));
+        buf_appendf(&entry->name, "&%s%s", is_const ? "const " : "", buf_ptr(&child_type->name));
         entry->size_in_bits = g->pointer_size_bytes * 8;
         entry->align_in_bits = g->pointer_size_bytes * 8;
         entry->di_type = LLVMZigCreateDebugPointerType(g->dbuilder, child_type->di_type,
src/parseh.cpp
@@ -250,9 +250,9 @@ static Buf *to_zig_type(ParseH *p, CXType raw_type) {
                 CXType pointee_type = clang_getArrayElementType(raw_type);
                 Buf *pointee_buf = to_zig_type(p, pointee_type);
                 if (clang_isConstQualifiedType(pointee_type)) {
-                    return buf_sprintf("*const %s", buf_ptr(pointee_buf));
+                    return buf_sprintf("&const %s", buf_ptr(pointee_buf));
                 } else {
-                    return buf_sprintf("*mut %s", buf_ptr(pointee_buf));
+                    return buf_sprintf("&%s", buf_ptr(pointee_buf));
                 }
             }
         case CXType_Pointer:
@@ -265,9 +265,9 @@ static Buf *to_zig_type(ParseH *p, CXType raw_type) {
                     pointee_buf = to_zig_type(p, pointee_type);
                 }
                 if (clang_isConstQualifiedType(pointee_type)) {
-                    return buf_sprintf("*const %s", buf_ptr(pointee_buf));
+                    return buf_sprintf("&const %s", buf_ptr(pointee_buf));
                 } else {
-                    return buf_sprintf("*mut %s", buf_ptr(pointee_buf));
+                    return buf_sprintf("&%s", buf_ptr(pointee_buf));
                 }
             }
         case CXType_Record:
src/parser.cpp
@@ -781,6 +781,7 @@ static AstNode *ast_parse_expression(ParseContext *pc, int *token_index, bool ma
 static AstNode *ast_parse_block(ParseContext *pc, int *token_index, bool mandatory);
 static AstNode *ast_parse_if_expr(ParseContext *pc, int *token_index, bool mandatory);
 static AstNode *ast_parse_block_expr(ParseContext *pc, int *token_index, bool mandatory);
+static AstNode *ast_parse_type(ParseContext *pc, int *token_index);
 
 static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) {
     if (token->id != token_id) {
@@ -841,10 +842,21 @@ static void ast_parse_directives(ParseContext *pc, int *token_index,
     zig_unreachable();
 }
 
+static void ast_parse_type_assume_amp(ParseContext *pc, int *token_index, AstNode *node) {
+    node->data.type.type = AstNodeTypeTypePointer;
+    Token *first_type_token = &pc->tokens->at(*token_index);
+    if (first_type_token->id == TokenIdKeywordConst) {
+        node->data.type.is_const = true;
+        *token_index += 1;
+        first_type_token = &pc->tokens->at(*token_index);
+    }
+
+    node->data.type.child_type = ast_parse_type(pc, token_index);
+}
 
 /*
 Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType
-PointerType : token(Star) (token(Const) | token(Mut)) Type
+PointerType : token(Ampersand) option(token(Const)) Type
 ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket)
 */
 static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
@@ -862,19 +874,17 @@ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
     } else if (token->id == TokenIdSymbol) {
         node->data.type.type = AstNodeTypeTypePrimitive;
         ast_buf_from_token(pc, token, &node->data.type.primitive_name);
-    } else if (token->id == TokenIdStar) {
+    } else if (token->id == TokenIdAmpersand) {
+        ast_parse_type_assume_amp(pc, token_index, node);
+    } else if (token->id == TokenIdBoolAnd) {
+        // Pretend that we got 2 ampersand tokens
         node->data.type.type = AstNodeTypeTypePointer;
-        Token *const_or_mut = &pc->tokens->at(*token_index);
-        *token_index += 1;
-        if (const_or_mut->id == TokenIdKeywordMut) {
-            node->data.type.is_const = false;
-        } else if (const_or_mut->id == TokenIdKeywordConst) {
-            node->data.type.is_const = true;
-        } else {
-            ast_invalid_token_error(pc, const_or_mut);
-        }
 
-        node->data.type.child_type = ast_parse_type(pc, token_index);
+        node->data.type.child_type = ast_create_node_no_line_info(pc, NodeTypeType);
+        node->data.type.child_type->line = token->start_line;
+        node->data.type.child_type->column = token->start_column + 1;
+
+        ast_parse_type_assume_amp(pc, token_index, node->data.type.child_type);
     } else if (token->id == TokenIdLBracket) {
         node->data.type.type = AstNodeTypeTypeArray;
 
@@ -1341,7 +1351,7 @@ static AstNode *ast_parse_bit_shift_expr(ParseContext *pc, int *token_index, boo
 
 
 /*
-BinaryAndExpression : BitShiftExpression token(BinAnd) BinaryAndExpression | BitShiftExpression
+BinaryAndExpression : BitShiftExpression token(Ampersand) BinaryAndExpression | BitShiftExpression
 */
 static AstNode *ast_parse_bin_and_expr(ParseContext *pc, int *token_index, bool mandatory) {
     AstNode *operand_1 = ast_parse_bit_shift_expr(pc, token_index, mandatory);
@@ -1350,7 +1360,7 @@ static AstNode *ast_parse_bin_and_expr(ParseContext *pc, int *token_index, bool
 
     while (true) {
         Token *token = &pc->tokens->at(*token_index);
-        if (token->id != TokenIdBinAnd)
+        if (token->id != TokenIdAmpersand)
             return operand_1;
         *token_index += 1;
 
src/tokenizer.cpp
@@ -320,7 +320,7 @@ void tokenize(Buf *buf, Tokenization *out) {
                         t.state = TokenizeStateSawDash;
                         break;
                     case '&':
-                        begin_token(&t, TokenIdBinAnd);
+                        begin_token(&t, TokenIdAmpersand);
                         t.state = TokenizeStateSawAmpersand;
                         break;
                     case '^':
@@ -832,7 +832,7 @@ static const char * token_name(Token *token) {
         case TokenIdDash: return "Dash";
         case TokenIdNumberSign: return "NumberSign";
         case TokenIdBinOr: return "BinOr";
-        case TokenIdBinAnd: return "BinAnd";
+        case TokenIdAmpersand: return "Ampersand";
         case TokenIdBinXor: return "BinXor";
         case TokenIdBoolOr: return "BoolOr";
         case TokenIdBoolAnd: return "BoolAnd";
src/tokenizer.hpp
@@ -52,7 +52,7 @@ enum TokenId {
     TokenIdBoolOr,
     TokenIdBoolAnd,
     TokenIdBinOr,
-    TokenIdBinAnd,
+    TokenIdAmpersand,
     TokenIdBinXor,
     TokenIdEq,
     TokenIdTimesEq,
std/std.zig
@@ -14,7 +14,7 @@ fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
 }
 
 // TODO constants for SYS_write and stdout_fileno
-pub fn write(fd: isize, buf: *const u8, count: usize) -> isize {
+pub fn write(fd: isize, buf: &const u8, count: usize) -> isize {
     let SYS_write : isize = 1;
     return syscall3(SYS_write, fd, buf as isize, count as isize);
 }
test/run_tests.cpp
@@ -99,7 +99,7 @@ static void add_compiling_test_cases(void) {
     add_simple_case("hello world with libc", R"SOURCE(
         #link("c")
         extern {
-            fn puts(s: *const u8) -> i32;
+            fn puts(s: &const u8) -> i32;
             fn exit(code: i32) -> unreachable;
         }
 
@@ -112,7 +112,7 @@ static void add_compiling_test_cases(void) {
     add_simple_case("function call", R"SOURCE(
         #link("c")
         extern {
-            fn puts(s: *const u8) -> i32;
+            fn puts(s: &const u8) -> i32;
             fn exit(code: i32) -> unreachable;
         }
 
@@ -134,7 +134,7 @@ static void add_compiling_test_cases(void) {
     add_simple_case("comments", R"SOURCE(
         #link("c")
         extern {
-            fn puts(s: *const u8) -> i32;
+            fn puts(s: &const u8) -> i32;
             fn exit(code: i32) -> unreachable;
         }
 
@@ -169,7 +169,7 @@ static void add_compiling_test_cases(void) {
         add_source_file(tc, "libc.zig", R"SOURCE(
             #link("c")
             extern {
-                pub fn puts(s: *const u8) -> i32;
+                pub fn puts(s: &const u8) -> i32;
                 pub fn exit(code: i32) -> unreachable;
             }
         )SOURCE");
@@ -192,7 +192,7 @@ static void add_compiling_test_cases(void) {
     add_simple_case("if statements", R"SOURCE(
         #link("c")
         extern {
-            fn puts(s: *const u8) -> i32;
+            fn puts(s: &const u8) -> i32;
             fn exit(code: i32) -> unreachable;
         }
 
@@ -217,7 +217,7 @@ static void add_compiling_test_cases(void) {
     add_simple_case("params", R"SOURCE(
         #link("c")
         extern {
-            fn puts(s: *const u8) -> i32;
+            fn puts(s: &const u8) -> i32;
             fn exit(code: i32) -> unreachable;
         }
 
@@ -236,7 +236,7 @@ static void add_compiling_test_cases(void) {
     add_simple_case("goto", R"SOURCE(
         #link("c")
         extern {
-            fn puts(s: *const u8) -> i32;
+            fn puts(s: &const u8) -> i32;
             fn exit(code: i32) -> unreachable;
         }
 
@@ -260,7 +260,7 @@ static void add_compiling_test_cases(void) {
     add_simple_case("local variables", R"SOURCE(
 #link("c")
 extern {
-    fn puts(s: *const u8) -> i32;
+    fn puts(s: &const u8) -> i32;
     fn exit(code: i32) -> unreachable;
 }
 
@@ -277,7 +277,7 @@ export fn _start() -> unreachable {
     add_simple_case("bool literals", R"SOURCE(
 #link("c")
 extern {
-    fn puts(s: *const u8) -> i32;
+    fn puts(s: &const u8) -> i32;
     fn exit(code: i32) -> unreachable;
 }
 
@@ -293,7 +293,7 @@ export fn _start() -> unreachable {
     add_simple_case("separate block scopes", R"SOURCE(
 #link("c")
 extern {
-    fn puts(s: *const u8) -> i32;
+    fn puts(s: &const u8) -> i32;
     fn exit(code: i32) -> unreachable;
 }
 
@@ -315,7 +315,7 @@ export fn _start() -> unreachable {
     add_simple_case("void parameters", R"SOURCE(
 #link("c")
 extern {
-    fn puts(s: *const u8) -> i32;
+    fn puts(s: &const u8) -> i32;
     fn exit(code: i32) -> unreachable;
 }
 
@@ -335,7 +335,7 @@ fn void_fun(a : i32, b : void, c : i32) {
     add_simple_case("mutable local variables", R"SOURCE(
 #link("c")
 extern {
-    fn puts(s: *const u8) -> i32;
+    fn puts(s: &const u8) -> i32;
     fn exit(code: i32) -> unreachable;
 }
 
@@ -359,7 +359,7 @@ done:
     add_simple_case("arrays", R"SOURCE(
 #link("c")
 extern {
-    fn puts(s: *const u8) -> i32;
+    fn puts(s: &const u8) -> i32;
     fn exit(code: i32) -> unreachable;
 }
 
@@ -402,7 +402,7 @@ loop_2_end:
     add_simple_case("hello world without libc", R"SOURCE(
 use "std.zig";
 
-export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
+export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
     print_str("Hello, world!\n" as string);
     return 0;
 }
@@ -412,7 +412,7 @@ export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
     add_simple_case("a + b + c", R"SOURCE(
 use "std.zig";
 
-export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
+export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
     if false || false || false { print_str("BAD 1\n" as string); }
     if true && true && false   { print_str("BAD 2\n" as string); }
     if 1 | 2 | 4 != 7          { print_str("BAD 3\n" as string); }
@@ -434,7 +434,7 @@ export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
     add_simple_case("short circuit", R"SOURCE(
 use "std.zig";
 
-export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
+export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
     if true || { print_str("BAD 1\n" as string); false } {
       print_str("OK 1\n" as string);
     }
@@ -457,7 +457,7 @@ export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
     add_simple_case("modify operators", R"SOURCE(
 use "std.zig";
 
-export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
+export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
     let mut i : i32 = 0;
     i += 5;  if i != 5  { print_str("BAD +=\n" as string); }
     i -= 2;  if i != 3  { print_str("BAD -=\n" as string); }
@@ -480,7 +480,7 @@ export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
     add_simple_case("structs", R"SOURCE(
 use "std.zig";
 
-export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
+export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
     let mut foo : Foo;
     foo.a = foo.a + 1;
     foo.b = foo.a == 1;
@@ -542,7 +542,7 @@ fn a() -> bogus {}
     )SOURCE", 1, ".tmp_source.zig:2:11: error: invalid type name: 'bogus'");
 
     add_compile_fail_case("pointer to unreachable", R"SOURCE(
-fn a() -> *mut unreachable {}
+fn a() -> &unreachable {}
     )SOURCE", 1, ".tmp_source.zig:2:11: error: pointer to unreachable not allowed");
 
     add_compile_fail_case("unreachable code", R"SOURCE(
@@ -605,7 +605,7 @@ fn f() -> i32 {
     let a = c"a";
     a
 }
-    )SOURCE", 1, ".tmp_source.zig:2:15: error: expected type 'i32', got '*const u8'");
+    )SOURCE", 1, ".tmp_source.zig:2:15: error: expected type 'i32', got '&const u8'");
 
     add_compile_fail_case("if condition is bool, not int", R"SOURCE(
 fn f() {
@@ -682,7 +682,6 @@ fn f() {
     add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
 fn f(...) {}
     )SOURCE", 1, ".tmp_source.zig:2:1: error: variadic arguments only allowed in extern functions");
-
 }
 
 static void print_compiler_invocation(TestCase *test_case) {