diff --git a/stdlib/ast_nodes.krak b/stdlib/ast_nodes.krak index 7db697a..e96fcf9 100644 --- a/stdlib/ast_nodes.krak +++ b/stdlib/ast_nodes.krak @@ -688,8 +688,8 @@ obj assignment_statement (Object) { return to == other.to && from == other.from } } -fun ast_declaration_statement_ptr(ident: *ast_node): *ast_node { - var to_ret.construct(ident): declaration_statement +fun ast_declaration_statement_ptr(ident: *ast_node, expression: *ast_node): *ast_node { + var to_ret.construct(ident, expression): declaration_statement var ptr = new() ptr->copy_construct(&ast_node::declaration_statement(to_ret)) return ptr @@ -702,12 +702,15 @@ fun is_declaration_statement(node: *ast_node): bool { } obj declaration_statement (Object) { var identifier: *ast_node - fun construct(identifier_in: *ast_node): *declaration_statement { + var expression: *ast_node + fun construct(identifier_in: *ast_node, expression_in: *ast_node): *declaration_statement { identifier = identifier_in + expression = expression_in return this } fun copy_construct(old: *declaration_statement) { identifier = old->identifier + expression = old->expression } fun destruct() { } @@ -716,7 +719,7 @@ obj declaration_statement (Object) { copy_construct(&other) } fun operator==(other: ref declaration_statement): bool { - return identifier == other.identifier + return identifier == other.identifier && expression == other.expression } } fun ast_if_comp_ptr(): *ast_node { @@ -884,7 +887,7 @@ fun get_ast_children(node: *ast_node): vector<*ast_node> { ast_node::continue_statement(backing) return vector<*ast_node>() ast_node::defer_statement(backing) return vector<*ast_node>() ast_node::assignment_statement(backing) return vector(backing.to, backing.from) - ast_node::declaration_statement(backing) return vector(backing.identifier) + ast_node::declaration_statement(backing) return vector(backing.identifier, backing.expression) ast_node::if_comp(backing) return vector<*ast_node>(backing.statement) ast_node::simple_passthrough(backing) return vector<*ast_node>() ast_node::function_call(backing) return vector(backing.func) + backing.parameters diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index ab669b1..f57f28c 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -272,8 +272,20 @@ obj ast_transformation (Object) { } fun transform_declaration_statement(node: *tree, scope: *ast_node): *ast_node { var name = concat_symbol_tree(get_node("identifier", node)) - var identifier = ast_identifier_ptr(name, transform_type(get_node("type", node), scope, map())) - var declaration = ast_declaration_statement_ptr(identifier) + // may have type, or an expression, or both + var type_syntax_node = get_node("type", node) + var expression_syntax_node = get_node("boolean_expression", node) + var ident_type = null() + var expression = null() + if (type_syntax_node) ident_type = transform_type(type_syntax_node, scope, map()) + if (expression_syntax_node) { + expression = transform(expression_syntax_node, scope) + if (!type_syntax_node) + ident_type = get_ast_type(expression) + } + if (!ident_type) error("declaration statement with no type or expression from which to inference type") + var identifier = ast_identifier_ptr(name, ident_type) + var declaration = ast_declaration_statement_ptr(identifier, expression) add_to_scope(name, identifier, scope) return declaration } @@ -386,4 +398,10 @@ fun add_to_scope(name: string, to_add: *ast_node, add_to: *ast_node) { else add_to_map->set(name, vector(to_add)) } +fun error(message: *char) error(string(message)); +fun error(message: string) { + println("****ERROR****") + println(message) + while (true){} +} diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index df39434..61f9173 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -86,7 +86,9 @@ obj c_generator (Object) { fun generate_statement(node: *ast_node): string return generate(node->statement.child) + ";\n"; fun generate_declaration_statement(node: *ast_node): string { var identifier = node->declaration_statement.identifier - return type_to_c(identifier->identifier.type) + " " + identifier->identifier.name + var to_ret = type_to_c(identifier->identifier.type) + " " + identifier->identifier.name + if (node->declaration_statement.expression) to_ret += string(" = ") + generate(node->declaration_statement.expression) + return to_ret } fun generate_assignment_statement(node: *ast_node): string { return generate(node->assignment_statement.to) + " = " + generate(node->assignment_statement.from) diff --git a/tests/to_parse.krak b/tests/to_parse.krak index 70520b2..625de8c 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -15,7 +15,10 @@ fun main(): int { var a_declaration:int a_declaration = 78 simple_print(a_declaration) - /*var another_declaration = 9*/ + var another_declaration: int = 8.0 + simple_print(another_declaration) + var yet_another_declaration = "Hello Marcus\n" + simple_print(yet_another_declaration) simple_print("Hello World!\n") simple_print(1337) return 0