| author | |
| committer | |
| log | bdca82ea66259de136ce9374e172f567ee93ef89 |
| tree | 090b73d70f8270f5f803b771d99b8cfbfbf592da |
| parent | 0c24ed8a818dddc1c77a41c07815b036a94279ca |
5 files changed, 46 insertions(+), 16 deletions(-)
example/guess_number/main.zig-4| ... | @@ -3,10 +3,6 @@ export executable "guess_number"; | ... | @@ -3,10 +3,6 @@ export executable "guess_number"; |
| 3 | use "std.zig"; | 3 | use "std.zig"; |
| 4 | use "rand.zig"; | 4 | use "rand.zig"; |
| 5 | 5 | ||
| 6 | // TODO don't duplicate these; implement pub const | ||
| 7 | const stdout_fileno : isize = 1; | ||
| 8 | const stderr_fileno : isize = 2; | ||
| 9 | |||
| 10 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | 6 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { |
| 11 | print_str("Welcome to the Guess Number Game in Zig.\n"); | 7 | print_str("Welcome to the Guess Number Game in Zig.\n"); |
| 12 | 8 |
src/analyze.cpp+23| ... | @@ -2635,6 +2635,29 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import, | ... | @@ -2635,6 +2635,29 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import, |
| 2635 | } | 2635 | } |
| 2636 | } | 2636 | } |
| 2637 | } | 2637 | } |
| 2638 | |||
| 2639 | // import all the public variables | ||
| 2640 | { | ||
| 2641 | auto it = target_import->block_context->variable_table.entry_iterator(); | ||
| 2642 | for (;;) { | ||
| 2643 | auto *entry = it.next(); | ||
| 2644 | if (!entry) | ||
| 2645 | break; | ||
| 2646 | |||
| 2647 | VariableTableEntry *var = entry->value; | ||
| 2648 | bool is_pub = (var->decl_node->data.variable_declaration.visib_mod != VisibModPrivate); | ||
| 2649 | if (is_pub) { | ||
| 2650 | auto existing_entry = import->type_table.maybe_get(entry->key); | ||
| 2651 | if (existing_entry) { | ||
| 2652 | add_node_error(g, node, | ||
| 2653 | buf_sprintf("import of variable '%s' overrides existing definition", | ||
| 2654 | buf_ptr(&var->name))); | ||
| 2655 | } else { | ||
| 2656 | import->block_context->variable_table.put(entry->key, entry->value); | ||
| 2657 | } | ||
| 2658 | } | ||
| 2659 | } | ||
| 2660 | } | ||
| 2638 | break; | 2661 | break; |
| 2639 | } | 2662 | } |
| 2640 | case NodeTypeStructDecl: | 2663 | case NodeTypeStructDecl: |
src/parser.cpp+19-8| ... | @@ -1965,23 +1965,34 @@ static AstNode *ast_parse_return_expr(ParseContext *pc, int *token_index, bool m | ... | @@ -1965,23 +1965,34 @@ static AstNode *ast_parse_return_expr(ParseContext *pc, int *token_index, bool m |
| 1965 | VariableDeclaration : option(FnVisibleMod) (token(Var) | token(Const)) token(Symbol) (token(Eq) Expression | token(Colon) Type option(token(Eq) Expression)) | 1965 | VariableDeclaration : option(FnVisibleMod) (token(Var) | token(Const)) token(Symbol) (token(Eq) Expression | token(Colon) Type option(token(Eq) Expression)) |
| 1966 | */ | 1966 | */ |
| 1967 | static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, int *token_index, bool mandatory) { | 1967 | static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, int *token_index, bool mandatory) { |
| 1968 | Token *var_or_const_tok = &pc->tokens->at(*token_index); | 1968 | Token *first_token = &pc->tokens->at(*token_index); |
| 1969 | |||
| 1970 | VisibMod visib_mod; | ||
| 1969 | 1971 | ||
| 1970 | bool is_const; | 1972 | if (first_token->id == TokenIdKeywordPub) { |
| 1971 | if (var_or_const_tok->id == TokenIdKeywordVar) { | 1973 | *token_index += 1; |
| 1972 | is_const = false; | 1974 | visib_mod = VisibModPub; |
| 1973 | } else if (var_or_const_tok->id == TokenIdKeywordConst) { | 1975 | } else if (first_token->id == TokenIdKeywordExport) { |
| 1974 | is_const = true; | 1976 | *token_index += 1; |
| 1977 | visib_mod = VisibModExport; | ||
| 1978 | } else if (first_token->id == TokenIdKeywordVar || | ||
| 1979 | first_token->id == TokenIdKeywordConst) | ||
| 1980 | { | ||
| 1981 | visib_mod = VisibModPrivate; | ||
| 1975 | } else if (mandatory) { | 1982 | } else if (mandatory) { |
| 1976 | ast_invalid_token_error(pc, var_or_const_tok); | 1983 | ast_invalid_token_error(pc, first_token); |
| 1977 | } else { | 1984 | } else { |
| 1978 | return nullptr; | 1985 | return nullptr; |
| 1979 | } | 1986 | } |
| 1980 | 1987 | ||
| 1988 | Token *var_or_const_tok = &pc->tokens->at(*token_index); | ||
| 1989 | bool is_const = (var_or_const_tok->id == TokenIdKeywordConst); | ||
| 1981 | *token_index += 1; | 1990 | *token_index += 1; |
| 1982 | AstNode *node = ast_create_node(pc, NodeTypeVariableDeclaration, var_or_const_tok); | 1991 | |
| 1992 | AstNode *node = ast_create_node(pc, NodeTypeVariableDeclaration, first_token); | ||
| 1983 | 1993 | ||
| 1984 | node->data.variable_declaration.is_const = is_const; | 1994 | node->data.variable_declaration.is_const = is_const; |
| 1995 | node->data.variable_declaration.visib_mod = visib_mod; | ||
| 1985 | 1996 | ||
| 1986 | Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol); | 1997 | Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol); |
| 1987 | ast_buf_from_token(pc, name_token, &node->data.variable_declaration.symbol); | 1998 | ast_buf_from_token(pc, name_token, &node->data.variable_declaration.symbol); |
src/parser.hpp+1| ... | @@ -126,6 +126,7 @@ struct AstNodeReturnExpr { | ... | @@ -126,6 +126,7 @@ struct AstNodeReturnExpr { |
| 126 | struct AstNodeVariableDeclaration { | 126 | struct AstNodeVariableDeclaration { |
| 127 | Buf symbol; | 127 | Buf symbol; |
| 128 | bool is_const; | 128 | bool is_const; |
| 129 | VisibMod visib_mod; | ||
| 129 | // one or both of type and expr will be non null | 130 | // one or both of type and expr will be non null |
| 130 | AstNode *type; | 131 | AstNode *type; |
| 131 | AstNode *expr; | 132 | AstNode *expr; |
std/std.zig+3-4| ... | @@ -1,8 +1,8 @@ | ... | @@ -1,8 +1,8 @@ |
| 1 | use "syscall.zig"; | 1 | use "syscall.zig"; |
| 2 | 2 | ||
| 3 | const stdin_fileno : isize = 0; | 3 | pub const stdin_fileno : isize = 0; |
| 4 | const stdout_fileno : isize = 1; | 4 | pub const stdout_fileno : isize = 1; |
| 5 | const stderr_fileno : isize = 2; | 5 | pub const stderr_fileno : isize = 2; |
| 6 | 6 | ||
| 7 | // TODO error handling | 7 | // TODO error handling |
| 8 | pub fn os_get_random_bytes(buf: &u8, count: usize) -> isize { | 8 | pub fn os_get_random_bytes(buf: &u8, count: usize) -> isize { |
| ... | @@ -41,7 +41,6 @@ pub fn print_i64(x: i64) -> isize { | ... | @@ -41,7 +41,6 @@ pub fn print_i64(x: i64) -> isize { |
| 41 | 41 | ||
| 42 | // TODO error handling | 42 | // TODO error handling |
| 43 | pub fn readline(buf: []u8, out_len: &usize) -> bool { | 43 | pub fn readline(buf: []u8, out_len: &usize) -> bool { |
| 44 | // TODO unknown size array indexing operator | ||
| 45 | const amt_read = read(stdin_fileno, buf.ptr, buf.len); | 44 | const amt_read = read(stdin_fileno, buf.ptr, buf.len); |
| 46 | if (amt_read < 0) { | 45 | if (amt_read < 0) { |
| 47 | return true; | 46 | return true; |