| 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 | 3 | use "std.zig"; |
| 4 | 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 | 6 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { |
| 11 | 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 | 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 | 2661 | break; |
| 2639 | 2662 | } |
| 2640 | 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 | 1965 | VariableDeclaration : option(FnVisibleMod) (token(Var) | token(Const)) token(Symbol) (token(Eq) Expression | token(Colon) Type option(token(Eq) Expression)) |
| 1966 | 1966 | */ |
| 1967 | 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; | |
| 1971 | if (var_or_const_tok->id == TokenIdKeywordVar) { | |
| 1972 | is_const = false; | |
| 1973 | } else if (var_or_const_tok->id == TokenIdKeywordConst) { | |
| 1974 | is_const = true; | |
| 1972 | if (first_token->id == TokenIdKeywordPub) { | |
| 1973 | *token_index += 1; | |
| 1974 | visib_mod = VisibModPub; | |
| 1975 | } else if (first_token->id == TokenIdKeywordExport) { | |
| 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 | 1982 | } else if (mandatory) { |
| 1976 | ast_invalid_token_error(pc, var_or_const_tok); | |
| 1983 | ast_invalid_token_error(pc, first_token); | |
| 1977 | 1984 | } else { |
| 1978 | 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 | 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 | 1994 | node->data.variable_declaration.is_const = is_const; |
| 1995 | node->data.variable_declaration.visib_mod = visib_mod; | |
| 1985 | 1996 | |
| 1986 | 1997 | Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol); |
| 1987 | 1998 | ast_buf_from_token(pc, name_token, &node->data.variable_declaration.symbol); |
src/parser.hpp+1| ... | ... | @@ -126,6 +126,7 @@ struct AstNodeReturnExpr { |
| 126 | 126 | struct AstNodeVariableDeclaration { |
| 127 | 127 | Buf symbol; |
| 128 | 128 | bool is_const; |
| 129 | VisibMod visib_mod; | |
| 129 | 130 | // one or both of type and expr will be non null |
| 130 | 131 | AstNode *type; |
| 131 | 132 | AstNode *expr; |
std/std.zig+3-4| ... | ... | @@ -1,8 +1,8 @@ |
| 1 | 1 | use "syscall.zig"; |
| 2 | 2 | |
| 3 | const stdin_fileno : isize = 0; | |
| 4 | const stdout_fileno : isize = 1; | |
| 5 | const stderr_fileno : isize = 2; | |
| 3 | pub const stdin_fileno : isize = 0; | |
| 4 | pub const stdout_fileno : isize = 1; | |
| 5 | pub const stderr_fileno : isize = 2; | |
| 6 | 6 | |
| 7 | 7 | // TODO error handling |
| 8 | 8 | pub fn os_get_random_bytes(buf: &u8, count: usize) -> isize { |
| ... | ... | @@ -41,7 +41,6 @@ pub fn print_i64(x: i64) -> isize { |
| 41 | 41 | |
| 42 | 42 | // TODO error handling |
| 43 | 43 | pub fn readline(buf: []u8, out_len: &usize) -> bool { |
| 44 | // TODO unknown size array indexing operator | |
| 45 | 44 | const amt_read = read(stdin_fileno, buf.ptr, buf.len); |
| 46 | 45 | if (amt_read < 0) { |
| 47 | 46 | return true; |