More variable stuff
This commit is contained in:
+39
-9
@@ -242,7 +242,6 @@ static bool parse_alias_declaration(Parser* p, Module* module, bool is_public) {
|
||||
|
||||
AliasDeclaration* alias = &module->aliases[module->alias_count - 1];
|
||||
memset(alias, 0, sizeof(AliasDeclaration));
|
||||
alias->is_public = is_public;
|
||||
|
||||
if (!parser_require(p, TOKEN_IDENTIFIER, "expected alias identifier")) {
|
||||
return false;
|
||||
@@ -264,15 +263,33 @@ static bool parse_alias_declaration(Parser* p, Module* module, bool is_public) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an expression.
|
||||
*/
|
||||
static bool parse_expression(Parser* p, Expression* expr) {
|
||||
if (parser_accept(p, TOKEN_INTEGER)) {
|
||||
expr->tag = EXPRESSION_INTEGER;
|
||||
expr->integer = atoi(p->token.text.data);
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_STRING)) {
|
||||
expr->tag = EXPRESSION_STRING;
|
||||
expr->string = parser_to_text(p);
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_TRUE)) {
|
||||
expr->tag = EXPRESSION_BOOLEAN;
|
||||
expr->boolean = true;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_FALSE)) {
|
||||
expr->tag = EXPRESSION_BOOLEAN;
|
||||
expr->boolean = false;
|
||||
return true;
|
||||
}
|
||||
log_on_line(&p->token.location, "expected expression");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a variable declaration.
|
||||
*
|
||||
* @param p The parser state.
|
||||
* @param module The module to add the variable to.
|
||||
* @param is_public Whether the variable is public.
|
||||
* @param is_static Whether the variable is static.
|
||||
* @param is_const Whether the variable is constant.
|
||||
* @return true if successful, false otherwise.
|
||||
*/
|
||||
static bool parse_variable_declaration(Parser* p, Module* module, bool is_public, bool is_static, bool is_const) {
|
||||
module->variable_count++;
|
||||
@@ -293,6 +310,13 @@ static bool parse_variable_declaration(Parser* p, Module* module, bool is_public
|
||||
}
|
||||
var->name = parser_to_text(p);
|
||||
|
||||
if (parser_accept(p, TOKEN_ASSIGN)) {
|
||||
var->initializer = malloc(sizeof(Expression));
|
||||
if (!parse_expression(p, var->initializer)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!parser_expect(p, TOKEN_SEMICOLON, "expected ';' after variable declaration")) {
|
||||
return false;
|
||||
}
|
||||
@@ -406,7 +430,7 @@ void parser_free(Module* module) {
|
||||
|
||||
if (module->aliases != NULL) {
|
||||
for(size_t i = 0; i < module->alias_count; i++) {
|
||||
free(module->aliases[i].name);
|
||||
free((void*)module->aliases[i].name);
|
||||
free_type_expression(&module->aliases[i].value);
|
||||
}
|
||||
free(module->aliases);
|
||||
@@ -416,6 +440,12 @@ void parser_free(Module* module) {
|
||||
for(size_t i = 0; i < module->variable_count; i++) {
|
||||
free(module->variables[i].name);
|
||||
free_type_expression(&module->variables[i].type);
|
||||
if (module->variables[i].initializer) {
|
||||
if (module->variables[i].initializer->tag == EXPRESSION_STRING) {
|
||||
free((void*)module->variables[i].initializer->string);
|
||||
}
|
||||
free(module->variables[i].initializer);
|
||||
}
|
||||
}
|
||||
free(module->variables);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user