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:
@@ -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<ast_node>()
|
||||
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
|
||||
|
||||
@@ -272,8 +272,20 @@ obj ast_transformation (Object) {
|
||||
}
|
||||
fun transform_declaration_statement(node: *tree<symbol>, 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<string, *type>()))
|
||||
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<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)
|
||||
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){}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user