mirror of
https://github.com/ziglang/zig.git
synced 2026-01-21 06:45:24 +00:00
implement pub const
This commit is contained in:
parent
0c24ed8a81
commit
bdca82ea66
@ -3,10 +3,6 @@ export executable "guess_number";
|
||||
use "std.zig";
|
||||
use "rand.zig";
|
||||
|
||||
// TODO don't duplicate these; implement pub const
|
||||
const stdout_fileno : isize = 1;
|
||||
const stderr_fileno : isize = 2;
|
||||
|
||||
pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
|
||||
print_str("Welcome to the Guess Number Game in Zig.\n");
|
||||
|
||||
|
||||
@ -2635,6 +2635,29 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// import all the public variables
|
||||
{
|
||||
auto it = target_import->block_context->variable_table.entry_iterator();
|
||||
for (;;) {
|
||||
auto *entry = it.next();
|
||||
if (!entry)
|
||||
break;
|
||||
|
||||
VariableTableEntry *var = entry->value;
|
||||
bool is_pub = (var->decl_node->data.variable_declaration.visib_mod != VisibModPrivate);
|
||||
if (is_pub) {
|
||||
auto existing_entry = import->type_table.maybe_get(entry->key);
|
||||
if (existing_entry) {
|
||||
add_node_error(g, node,
|
||||
buf_sprintf("import of variable '%s' overrides existing definition",
|
||||
buf_ptr(&var->name)));
|
||||
} else {
|
||||
import->block_context->variable_table.put(entry->key, entry->value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NodeTypeStructDecl:
|
||||
|
||||
@ -1965,23 +1965,34 @@ static AstNode *ast_parse_return_expr(ParseContext *pc, int *token_index, bool m
|
||||
VariableDeclaration : option(FnVisibleMod) (token(Var) | token(Const)) token(Symbol) (token(Eq) Expression | token(Colon) Type option(token(Eq) Expression))
|
||||
*/
|
||||
static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, int *token_index, bool mandatory) {
|
||||
Token *var_or_const_tok = &pc->tokens->at(*token_index);
|
||||
Token *first_token = &pc->tokens->at(*token_index);
|
||||
|
||||
bool is_const;
|
||||
if (var_or_const_tok->id == TokenIdKeywordVar) {
|
||||
is_const = false;
|
||||
} else if (var_or_const_tok->id == TokenIdKeywordConst) {
|
||||
is_const = true;
|
||||
VisibMod visib_mod;
|
||||
|
||||
if (first_token->id == TokenIdKeywordPub) {
|
||||
*token_index += 1;
|
||||
visib_mod = VisibModPub;
|
||||
} else if (first_token->id == TokenIdKeywordExport) {
|
||||
*token_index += 1;
|
||||
visib_mod = VisibModExport;
|
||||
} else if (first_token->id == TokenIdKeywordVar ||
|
||||
first_token->id == TokenIdKeywordConst)
|
||||
{
|
||||
visib_mod = VisibModPrivate;
|
||||
} else if (mandatory) {
|
||||
ast_invalid_token_error(pc, var_or_const_tok);
|
||||
ast_invalid_token_error(pc, first_token);
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Token *var_or_const_tok = &pc->tokens->at(*token_index);
|
||||
bool is_const = (var_or_const_tok->id == TokenIdKeywordConst);
|
||||
*token_index += 1;
|
||||
AstNode *node = ast_create_node(pc, NodeTypeVariableDeclaration, var_or_const_tok);
|
||||
|
||||
AstNode *node = ast_create_node(pc, NodeTypeVariableDeclaration, first_token);
|
||||
|
||||
node->data.variable_declaration.is_const = is_const;
|
||||
node->data.variable_declaration.visib_mod = visib_mod;
|
||||
|
||||
Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol);
|
||||
ast_buf_from_token(pc, name_token, &node->data.variable_declaration.symbol);
|
||||
|
||||
@ -126,6 +126,7 @@ struct AstNodeReturnExpr {
|
||||
struct AstNodeVariableDeclaration {
|
||||
Buf symbol;
|
||||
bool is_const;
|
||||
VisibMod visib_mod;
|
||||
// one or both of type and expr will be non null
|
||||
AstNode *type;
|
||||
AstNode *expr;
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
use "syscall.zig";
|
||||
|
||||
const stdin_fileno : isize = 0;
|
||||
const stdout_fileno : isize = 1;
|
||||
const stderr_fileno : isize = 2;
|
||||
pub const stdin_fileno : isize = 0;
|
||||
pub const stdout_fileno : isize = 1;
|
||||
pub const stderr_fileno : isize = 2;
|
||||
|
||||
// TODO error handling
|
||||
pub fn os_get_random_bytes(buf: &u8, count: usize) -> isize {
|
||||
@ -41,7 +41,6 @@ pub fn print_i64(x: i64) -> isize {
|
||||
|
||||
// TODO error handling
|
||||
pub fn readline(buf: []u8, out_len: &usize) -> bool {
|
||||
// TODO unknown size array indexing operator
|
||||
const amt_read = read(stdin_fileno, buf.ptr, buf.len);
|
||||
if (amt_read < 0) {
|
||||
return true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user