Add the abitlity to assign during a declaration and type inference if no type given but there is an expression

This commit is contained in:
Nathan Braswell
2016-01-17 01:10:09 -05:00
parent 9c41c2fd12
commit bffedcf2fd
4 changed files with 35 additions and 9 deletions

View File

@@ -688,8 +688,8 @@ obj assignment_statement (Object) {
return to == other.to && from == other.from return to == other.to && from == other.from
} }
} }
fun ast_declaration_statement_ptr(ident: *ast_node): *ast_node { fun ast_declaration_statement_ptr(ident: *ast_node, expression: *ast_node): *ast_node {
var to_ret.construct(ident): declaration_statement var to_ret.construct(ident, expression): declaration_statement
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::declaration_statement(to_ret)) ptr->copy_construct(&ast_node::declaration_statement(to_ret))
return ptr return ptr
@@ -702,12 +702,15 @@ fun is_declaration_statement(node: *ast_node): bool {
} }
obj declaration_statement (Object) { obj declaration_statement (Object) {
var identifier: *ast_node 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 identifier = identifier_in
expression = expression_in
return this return this
} }
fun copy_construct(old: *declaration_statement) { fun copy_construct(old: *declaration_statement) {
identifier = old->identifier identifier = old->identifier
expression = old->expression
} }
fun destruct() { fun destruct() {
} }
@@ -716,7 +719,7 @@ obj declaration_statement (Object) {
copy_construct(&other) copy_construct(&other)
} }
fun operator==(other: ref declaration_statement): bool { 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 { 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::continue_statement(backing) return vector<*ast_node>()
ast_node::defer_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::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::if_comp(backing) return vector<*ast_node>(backing.statement)
ast_node::simple_passthrough(backing) return vector<*ast_node>() ast_node::simple_passthrough(backing) return vector<*ast_node>()
ast_node::function_call(backing) return vector(backing.func) + backing.parameters ast_node::function_call(backing) return vector(backing.func) + backing.parameters

View File

@@ -272,8 +272,20 @@ obj ast_transformation (Object) {
} }
fun transform_declaration_statement(node: *tree<symbol>, scope: *ast_node): *ast_node { fun transform_declaration_statement(node: *tree<symbol>, scope: *ast_node): *ast_node {
var name = concat_symbol_tree(get_node("identifier", node)) var name = concat_symbol_tree(get_node("identifier", node))
var identifier = ast_identifier_ptr(name, transform_type(get_node("type", node), scope, map<string, *type>())) // may have type, or an expression, or both
var declaration = ast_declaration_statement_ptr(identifier) var type_syntax_node = get_node("type", node)
var expression_syntax_node = get_node("boolean_expression", node)
var ident_type = null<type>()
var expression = null<ast_node>()
if (type_syntax_node) ident_type = transform_type(type_syntax_node, scope, map<string, *type>())
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) add_to_scope(name, identifier, scope)
return declaration return declaration
} }
@@ -386,4 +398,10 @@ fun add_to_scope(name: string, to_add: *ast_node, add_to: *ast_node) {
else else
add_to_map->set(name, vector(to_add)) 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){}
}

View File

@@ -86,7 +86,9 @@ obj c_generator (Object) {
fun generate_statement(node: *ast_node): string return generate(node->statement.child) + ";\n"; fun generate_statement(node: *ast_node): string return generate(node->statement.child) + ";\n";
fun generate_declaration_statement(node: *ast_node): string { fun generate_declaration_statement(node: *ast_node): string {
var identifier = node->declaration_statement.identifier 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 { fun generate_assignment_statement(node: *ast_node): string {
return generate(node->assignment_statement.to) + " = " + generate(node->assignment_statement.from) return generate(node->assignment_statement.to) + " = " + generate(node->assignment_statement.from)

View File

@@ -15,7 +15,10 @@ fun main(): int {
var a_declaration:int var a_declaration:int
a_declaration = 78 a_declaration = 78
simple_print(a_declaration) 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("Hello World!\n")
simple_print(1337) simple_print(1337)
return 0 return 0