From 5db0365a63db4699b519580d389ac2c7744e02a6 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sun, 10 Jan 2016 18:26:31 -0500 Subject: [PATCH] Added indirection to types in prep for function calls, full passthrough, and the first real hello world --- stdlib/ast_transformation.krak | 26 ++++++++++++--------- stdlib/c_generator.krak | 24 +++++++++++--------- stdlib/parser.krak | 16 +++++++++++++ stdlib/string.krak | 4 ++++ stdlib/type.krak | 41 ++++++++++++++++++++-------------- tests/to_import.krak | 12 +++++----- tests/to_parse.krak | 4 ++++ 7 files changed, 82 insertions(+), 45 deletions(-) diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index d96c907..d8820ff 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -143,25 +143,31 @@ obj ast_transformation (Object) { // always get to pre-reffed level var real_node = get_node("pre_reffed", node) // check for indirection and step down + var indirection = 0 + while (get_node("pre_reffed", real_node)) { + real_node = get_node("pre_reffed", real_node) + indirection++ + } var type_syntax_str = concat_symbol_tree(real_node) + println(type_syntax_str + " *************************") // should take into account indirection and references... if (type_syntax_str == "void") - return type_ptr(base_type::void_return()) + return type_ptr(base_type::void_return(), indirection) else if (type_syntax_str == "bool") - return type_ptr(base_type::boolean()) + return type_ptr(base_type::boolean(), indirection) else if (type_syntax_str == "int") - return type_ptr(base_type::integer()) + return type_ptr(base_type::integer(), indirection) else if (type_syntax_str == "float") - return type_ptr(base_type::floating()) + return type_ptr(base_type::floating(), indirection) else if (type_syntax_str == "double") - return type_ptr(base_type::double_precision()) + return type_ptr(base_type::double_precision(), indirection) else if (type_syntax_str == "char") - return type_ptr(base_type::character()) - else if (/* check for function type*/ true) - return type_ptr(base_type::function()) + return type_ptr(base_type::character(), indirection) + else if (/* check for function type*/ false) + return type_ptr(base_type::function(), indirection) else { // do lookup for objects, ADTs, templates, etc - return type_ptr(base_type::none()) + return type_ptr(base_type::none(), indirection) } } /*NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree* scope, std::vector types, bool limitToFunction, std::map templateTypeReplacements) {*/ @@ -208,7 +214,7 @@ obj ast_transformation (Object) { var value_str = concat_symbol_tree(node) var value_type = null() if (value_str[0] == '"') - value_type = type_ptr(base_type::character()) // and indirection, sigh + value_type = type_ptr(base_type::character(), 1) else if (value_str[0] == '\'') value_type = type_ptr(base_type::character()) else { diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index ec1f64b..a317cad 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -98,23 +98,25 @@ obj c_generator (Object) { return string("/* COULD NOT GENERATE */") } fun type_to_c(type: *type): string { + var indirection = string() + for (var i = 0; i < type->indirection; i++;) indirection += "*" match (type->base) { - base_type::none() return string("none") - base_type::template() return string("template") - base_type::template_type() return string("template_type") - base_type::void_return() return string("void") - base_type::boolean() return string("bool") - base_type::character() return string("char") - base_type::integer() return string("int") - base_type::floating() return string("float") - base_type::double_precision() return string("double") + base_type::none() return string("none") + indirection + base_type::template() return string("template") + indirection + base_type::template_type() return string("template_type") + indirection + base_type::void_return() return string("void") + indirection + base_type::boolean() return string("bool") + indirection + base_type::character() return string("char") + indirection + base_type::integer() return string("int") + indirection + base_type::floating() return string("float") + indirection + base_type::double_precision() return string("double") + indirection base_type::function() { - var temp = string("function: (") + var temp = indirection + string("function: (") type->parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + " ";) return temp + ")" + type->return_type->to_string() } } - return string("impossible type") + return string("impossible type") + indirection } } diff --git a/stdlib/parser.krak b/stdlib/parser.krak index 07c9fa3..2dc4ad8 100644 --- a/stdlib/parser.krak +++ b/stdlib/parser.krak @@ -79,6 +79,7 @@ obj parser (Object) { } input.addEnd(current_symbol) if (current_symbol == invalid_symbol()) { + println("**PARSE ERROR**") println("lexing failed for ") println(name) return null>() @@ -104,11 +105,14 @@ obj parser (Object) { for (var i = 0; i < input.size; i++;) { if (gss.frontier_is_empty(i)) { + println("**PARSE ERROR**") print(i) print("th frontier is empty in file '") print(name) print("' with txt ") print(input[i].to_string()) + print(" line number: ") + print(find_line(i)) println() return null>() } @@ -127,9 +131,13 @@ obj parser (Object) { return gss.get_edge(acc_state, v0) } + println("**PARSE ERROR**") println("REJECTED") println("parsing (not lexing) failed for ") println(name) + print(" line number: ") + print(find_line(input.size)) + println("(minus 2?)") return null>() } fun reducer(i: int) { @@ -363,6 +371,14 @@ obj parser (Object) { return new>()->construct(null_symbol()) return null>() } + fun find_line(token_no: int): int { + var line_no = 1 + for (var i = 0; i < token_no; i++;) + for (var j = 0; j < input[i].data.length(); j++;) + if (input[i].data[j] == '\n') + line_no++ + return line_no + } } obj gss (Object) { diff --git a/stdlib/string.krak b/stdlib/string.krak index c0c1edc..494cbcf 100644 --- a/stdlib/string.krak +++ b/stdlib/string.krak @@ -109,6 +109,8 @@ obj string (Object, Serializable) { return *this == str } + fun operator+(integer: int): string return *this + to_string(integer); + fun operator+(str: *char): string { var newStr.construct(str):string var ret.construct(data + newStr.data):string @@ -121,6 +123,8 @@ obj string (Object, Serializable) { return ret } + fun operator+=(integer: int) *this += to_string(integer); + fun operator+=(character: char): void { data += character } diff --git a/stdlib/type.krak b/stdlib/type.krak index 30c3b4c..4b79520 100644 --- a/stdlib/type.krak +++ b/stdlib/type.krak @@ -20,37 +20,44 @@ adt base_type { fun type_ptr(): *type { return new()->construct() } -fun type_ptr(base: base_type): *type { - return new()->construct(base) +fun type_ptr(base: base_type): *type return type_ptr(base, 0); +fun type_ptr(base: base_type, indirection: int): *type { + return new()->construct(base, indirection) } -fun type_ptr(parameters: vector<*type>, return_type: *type): *type { - return new()->construct(parameters, return_type) +fun type_ptr(parameters: vector<*type>, return_type: *type): *type return type_ptr(parameters, return_type, 0); +fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int): *type { + return new()->construct(parameters, return_type, indirection) } obj type (Object) { var base: base_type var parameter_types: vector<*type> var return_type: *type + var indirection: int fun construct(): *type { base.copy_construct(&base_type::none()) parameter_types.construct() + indirection = 0 return this } - fun construct(base_in: base_type): *type { + fun construct(base_in: base_type, indirection_in: int): *type { base.copy_construct(&base_in) parameter_types.construct() + indirection = indirection_in return this } - fun construct(parameter_types_in: vector<*type>, return_type_in: *type): *type { + fun construct(parameter_types_in: vector<*type>, return_type_in: *type, indirection_in: int): *type { base.copy_construct(&base_type::function()) parameter_types.copy_construct(¶meter_types) return_type = return_type_in + indirection = indirection_in return this } fun copy_construct(old: *type) { base.copy_construct(&old->base) parameter_types.copy_construct(&old->parameter_types) return_type = old->return_type + indirection = old->indirection } fun operator=(other: ref type) { destruct() @@ -62,22 +69,22 @@ obj type (Object) { } fun to_string(): string { match (base) { - base_type::none() return string("none") - base_type::template() return string("template") - base_type::template_type() return string("template_type") - base_type::void_return() return string("void_return") - base_type::boolean() return string("boolean") - base_type::character() return string("character") - base_type::integer() return string("integer") - base_type::floating() return string("floating") - base_type::double_precision() return string("double_precision") + 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::function() { var temp = string("function: (") parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + " ";) - return temp + ")" + return_type->to_string() + return temp + ")" + return_type->to_string() + "indirection: " + indirection } } - return string("impossible type") + return string("impossible type, indirection:") + indirection } } diff --git a/tests/to_import.krak b/tests/to_import.krak index d4f451f..6a443c6 100644 --- a/tests/to_import.krak +++ b/tests/to_import.krak @@ -1,11 +1,9 @@ -__if_comp__ __C__ simple_passthrough """ - #include - int main() { - printf("hello world! (of selfhosting! (silly selfhosting for now))\n"); - return 0; - } -""" +fun simple_print(to_print: *char) { + __if_comp__ __C__ simple_passthrough(to_print::) """ + printf("%s", to_print); + """ +} var a = 1 var b = 2 diff --git a/tests/to_parse.krak b/tests/to_parse.krak index 7d54d63..eafd1e8 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -11,4 +11,8 @@ fun some_function(): int return 0; fun some_other_function(in: bool): float { return 0.0 } +fun main(): int { + simple_print("Hello World!") + return 0 +}