From ac34a550d5ecadb362df96013fe67ea544b100b2 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Mon, 18 Jan 2016 18:04:34 -0500 Subject: [PATCH] Basic math expressions will passthrough now --- stdlib/ast_nodes.krak | 2 +- stdlib/ast_transformation.krak | 33 +++++++++++++++++++++++---------- stdlib/c_generator.krak | 8 ++++++-- stdlib/type.krak | 26 ++++++++++++++------------ tests/to_parse.krak | 5 +++-- 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/stdlib/ast_nodes.krak b/stdlib/ast_nodes.krak index e96fcf9..812cf61 100644 --- a/stdlib/ast_nodes.krak +++ b/stdlib/ast_nodes.krak @@ -952,7 +952,7 @@ fun get_ast_type(node: *ast_node): *type { match (*node) { ast_node::identifier() return node->identifier.type ast_node::function() return node->function.type - ast_node::function_call() return get_ast_type(node->function_call.func) + ast_node::function_call() return get_ast_type(node->function_call.func)->return_type ast_node::value() return node->value.value_type } } diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index f57f28c..b6d06a1 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -241,12 +241,6 @@ obj ast_transformation (Object) { } return ast_value_ptr(value_str, value_type) } - fun transform_expression(node: *tree, scope: *ast_node): *ast_node { - // figure out what the expression is, handle overloads, or you know - // ignore everything and do a passthrough - /*println(string("passing through: ") + node->data.name)*/ - return transform(node->children[0], scope) - } fun transform_code_block(node: *tree, scope: *ast_node): *ast_node { var new_block = ast_code_block_ptr() add_to_scope("~enclosing_scope", scope, new_block) @@ -308,15 +302,33 @@ obj ast_transformation (Object) { f->function_call.parameters.for_each(fun(param: *ast_node) print(param);) return f } + fun transform_expression(node: *tree, scope: *ast_node): *ast_node { + // figure out what the expression is, handle overloads, or you know + // ignore everything and do a passthrough + if (node->children.size == 1) + return transform(node->children[0], scope) + else if (node->children.size == 2) { + return transform(node->children[0], scope) + } + var parameters = vector(transform(node->children[0], scope), transform(node->children[2], scope)) + var parameter_types = parameters.map(fun(param: *ast_node): *type return get_ast_type(param);) + return ast_function_call_ptr(get_builtin_function(concat_symbol_tree(node->children[1]), parameter_types), parameters) + } + fun get_builtin_function(name: string, param_types: vector<*type>): *ast_node { + return ast_function_ptr(name, type_ptr(param_types, param_types[0]), vector<*ast_node>()) + } fun function_lookup(name: string, scope: *ast_node, param_types: vector<*type>): *ast_node { var param_string = string() param_types.for_each(fun(t: *type) param_string += t->to_string() + ", ";) var results = scope_lookup(name, scope) - for (var i = 0; i < results.size; i++;) + print(results.size); println(" number of results") + for (var i = 0; i < results.size; i++;) { if (is_function(results[i])) { var func_param_types = get_ast_type(results[i])->parameter_types - if (func_param_types.size != param_types.size) + if (func_param_types.size != param_types.size) { + println(string("type sizes don't match") + get_ast_type(results[i])->to_string() + " with needed " + param_string) continue + } var param_types_match = true for (var j = 0; j < param_types.size; j++;) { if (*func_param_types[j] != *param_types[j]) { @@ -326,8 +338,9 @@ obj ast_transformation (Object) { } if (param_types_match) return results[i] - } else - println(string("either isn't function or types don't match ") + get_ast_type(results[i])->to_string() + " with needed " + param_string) + } + println(string("either isn't function or types don't match ") + get_ast_type(results[i])->to_string() + " with needed " + param_string) + } println(string("function lookup failed for ") + name) return null() } diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index 61f9173..d68d487 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -111,13 +111,17 @@ obj c_generator (Object) { } fun generate_function_call(node: *ast_node): string { + var func_name = generate(node->function_call.func) + var parameters = node->function_call.parameters + if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/" || func_name == "||" || func_name == "&&") + return generate(parameters[0]) + func_name + generate(parameters[1]) var call_string = string() - node->function_call.parameters.for_each(fun(param: *ast_node) { + parameters.for_each(fun(param: *ast_node) { if (call_string != "") call_string += ", " call_string += generate(param) }) - return generate(node->function_call.func) + "(" + call_string + ")" + return func_name + "(" + call_string + ")" } // for now, anyway diff --git a/stdlib/type.krak b/stdlib/type.krak index a0709bf..016ccf8 100644 --- a/stdlib/type.krak +++ b/stdlib/type.krak @@ -76,20 +76,22 @@ obj type (Object) { return base == other.base && parameter_types == other.parameter_types && indirection == other.indirection } fun to_string(): string { + var indirection_str = string() + for (var i = 0; i < indirection; i++;) indirection_str += "*" match (base) { - base_type::none() return string("none, indirection: ") + indirection - base_type::template() return string("template, indirection:") + indirection - base_type::template_type() return string("template_type, indirection:") + indirection - base_type::void_return() return string("void_return, indirection:") + indirection - base_type::boolean() return string("boolean, indirection:") + indirection - base_type::character() return string("character, indirection:") + indirection - base_type::integer() return string("integer, indirection:") + indirection - base_type::floating() return string("floating, indirection:") + indirection - base_type::double_precision() return string("double_precision, indirection:") + indirection + base_type::none() return indirection_str + string("none") + base_type::template() return indirection_str + string("template") + base_type::template_type() return indirection_str + string("template_type") + base_type::void_return() return indirection_str + string("void_return") + base_type::boolean() return indirection_str + string("boolean") + base_type::character() return indirection_str + string("character") + base_type::integer() return indirection_str + string("integer") + base_type::floating() return indirection_str + string("floating") + base_type::double_precision() return indirection_str + string("double_precision") base_type::function() { - var temp = string("function: (") - parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + " ";) - return temp + ")" + return_type->to_string() + "indirection: " + indirection + var temp = indirection_str + string("fun(") + parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + ", ";) + return temp + ")" + return_type->to_string() } } return string("impossible type, indirection:") + indirection diff --git a/tests/to_parse.krak b/tests/to_parse.krak index 625de8c..6d8f5b3 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -13,8 +13,9 @@ fun some_other_function(in: bool): float { } fun main(): int { var a_declaration:int - a_declaration = 78 - simple_print(a_declaration) + simple_print(1 + 2) + var again = 2 + 4 - 1 * 400 + simple_print(again) var another_declaration: int = 8.0 simple_print(another_declaration) var yet_another_declaration = "Hello Marcus\n"