From d126cbf24ba8b26e3814e2260d555ecaee86508c Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Fri, 29 Apr 2016 16:19:23 -0400 Subject: [PATCH] Added short/long and unsigned types for all int based types --- captain.sh | 2 +- krakenGrammer.kgm | 2 +- stdlib/ast_transformation.krak | 17 +++++++- stdlib/c_generator.krak | 14 ++++++- stdlib/mem.krak | 75 +++++++++------------------------- stdlib/string.krak | 4 -- stdlib/type.krak | 28 ++++++++++--- 7 files changed, 73 insertions(+), 69 deletions(-) diff --git a/captain.sh b/captain.sh index 5779d52..046f7b1 100755 --- a/captain.sh +++ b/captain.sh @@ -1,7 +1,7 @@ #!/bin/bash kraken="kraken" -bootstrap_commits=(cf46fb13afe66ba475db9725e9269c9c1cd3bbc3 2cd43e5a217318c70097334b3598d2924f64b362 2051f54b559ac5edf67277d4f1134aca2cb9215d) +bootstrap_commits=(cf46fb13afe66ba475db9725e9269c9c1cd3bbc3 2cd43e5a217318c70097334b3598d2924f64b362 2051f54b559ac5edf67277d4f1134aca2cb9215d ecbbcb4eda56e2467efb0a04e7d668b95856aa4b) # Echo version string to a file included by kraken.krak # There is a default version string in the file in case kraken is not built with captain diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index af7dbc9..28a1f9d 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -4,7 +4,7 @@ translation_unit = WS unorderd_list_part WS ; unorderd_list_part = import WS unorderd_list_part | function WS unorderd_list_part | type_def line_end WS unorderd_list_part | adt_def line_end WS unorderd_list_part | if_comp WS unorderd_list_part | simple_passthrough WS unorderd_list_part | declaration_statement line_end WS unorderd_list_part | import | function | type_def line_end | adt_def line_end | if_comp | simple_passthrough | declaration_statement line_end ; type = "ref" WS pre_reffed | pre_reffed ; -pre_reffed = "\*" WS pre_reffed | "void" | "int" | "float" | "double" | "char" | scoped_identifier | scoped_identifier WS template_inst | function_type ; +pre_reffed = "\*" WS pre_reffed | "void" | "char" | "uchar" | "short" | "ushort" | "int" | "uint" | "long" | "ulong" | "float" | "double" | scoped_identifier | scoped_identifier WS template_inst | function_type ; function_type = "fun" WS "\(" WS opt_type_list WS "\)" WS ":" WS type ; dec_type = ":" WS type ; diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index 4caa163..388e152 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -396,14 +396,26 @@ obj ast_transformation (Object) { return type_ptr(base_type::void_return(), indirection, is_ref) else if (type_syntax_str == "bool") return type_ptr(base_type::boolean(), indirection, is_ref) + else if (type_syntax_str == "char") + return type_ptr(base_type::character(), indirection, is_ref) + else if (type_syntax_str == "uchar") + return type_ptr(base_type::ucharacter(), indirection, is_ref) + else if (type_syntax_str == "short") + return type_ptr(base_type::short_int(), indirection, is_ref) + else if (type_syntax_str == "ushort") + return type_ptr(base_type::ushort_int(), indirection, is_ref) else if (type_syntax_str == "int") return type_ptr(base_type::integer(), indirection, is_ref) + else if (type_syntax_str == "uint") + return type_ptr(base_type::uinteger(), indirection, is_ref) + else if (type_syntax_str == "long") + return type_ptr(base_type::long_int(), indirection, is_ref) + else if (type_syntax_str == "ulong") + return type_ptr(base_type::ulong_int(), indirection, is_ref) else if (type_syntax_str == "float") return type_ptr(base_type::floating(), indirection, is_ref) else if (type_syntax_str == "double") return type_ptr(base_type::double_precision(), indirection, is_ref) - else if (type_syntax_str == "char") - return type_ptr(base_type::character(), indirection, is_ref) else if (get_node("function_type", real_node)) { var types = get_nodes("type", get_node("function_type", real_node)).map(fun(node: *tree): *type transform_type(node, scope, template_replacements);) return type_ptr(types.slice(0,-2), types.last(), indirection, is_ref) @@ -766,6 +778,7 @@ obj ast_transformation (Object) { parameters = get_nodes("intrinsic_parameter", node).map(fun(child: *tree): string return concat_symbol_tree(child);) if (get_nodes("type", node).size) type_parameters = get_nodes("type", node).map(fun(child: *tree): *type return transform_type(child, scope, template_replacements);) + /*return ast_compiler_intrinsic_ptr(concat_symbol_tree(get_node("identifier", node)), parameters, type_parameters, type_ptr(base_type::ulong_int()))*/ return ast_compiler_intrinsic_ptr(concat_symbol_tree(get_node("identifier", node)), parameters, type_parameters, type_ptr(base_type::integer())) } fun transform_lambda(node: *tree, scope: *ast_node, template_replacements: map): *ast_node { diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index 7b7affe..ad763ba 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -970,7 +970,13 @@ obj c_generator (Object) { base_type::void_return() return indirection + string("void") base_type::boolean() return indirection + string("bool") base_type::character() return indirection + string("char") + base_type::ucharacter() return indirection + string("uchar") + base_type::short_int() return indirection + string("short") + base_type::ushort_int() return indirection + string("ushort") base_type::integer() return indirection + string("int") + base_type::uinteger() return indirection + string("uint") + base_type::long_int() return indirection + string("long") + base_type::ulong_int() return indirection + string("ulong") base_type::floating() return indirection + string("float") base_type::double_precision() return indirection + string("double") base_type::object() return cify_name(type->type_def->type_def.name) @@ -994,7 +1000,13 @@ obj c_generator (Object) { base_type::void_return() return string("void") + indirection base_type::boolean() return string("bool") + indirection base_type::character() return string("char") + indirection + base_type::ucharacter() return string("unsigned char") + indirection + base_type::short_int() return string("short") + indirection + base_type::ushort_int() return string("unsigned short") + indirection base_type::integer() return string("int") + indirection + base_type::uinteger() return string("unsigned int") + indirection + base_type::long_int() return string("long") + indirection + base_type::ulong_int() return string("unsigned long") + indirection base_type::floating() return string("float") + indirection base_type::double_precision() return string("double") + indirection base_type::object() return get_name(type->type_def) + indirection @@ -1058,7 +1070,7 @@ obj c_generator (Object) { // TODO keyword avoid seems not to work if (ast_name_map.contains_value(result) || c_keyword_avoid.contains(result)) result += get_id() - println("HERE: " + result) + /*println("HERE: " + result)*/ ast_name_map.set(node, result) return result } diff --git a/stdlib/mem.krak b/stdlib/mem.krak index a471c5c..e0dbc69 100644 --- a/stdlib/mem.krak +++ b/stdlib/mem.krak @@ -1,47 +1,20 @@ -__if_comp__ __C__ simple_passthrough """ - #include -""" - -/* we have a template versions so we don't have to cast (because we don't have that yet) */ fun null(): *T { - __if_comp__ __C__ { - simple_passthrough(::) """ - return (void*)0; - """ - } + return (0) cast *T } -fun malloc(size: int): *T { - var memPtr: *T; - __if_comp__ __C__ { - simple_passthrough( size = size, memPtr = memPtr : memPtr = memPtr :) """ - memPtr = malloc(size); - """ - } - return memPtr; -} +ext fun malloc(size: int): *void +ext fun free(size: *void) -fun free(memPtr: *T): void { - __if_comp__ __C__ { - simple_passthrough(memPtr = memPtr ::) """ - free(memPtr); - """ - } -} +fun new(count: int): *T + return (malloc( #sizeof * count )) cast *T -fun new(count: int): *T { - return malloc( #sizeof * count ); -} - -fun new(): *T { - return new(1); -} +fun new(): *T + return new(1) /* We specilize on the trait Object to decide on whether or not the destructor should be called */ -fun delete(toDelete: *T, itemCount: int): void { - delete(toDelete); -} +fun delete(toDelete: *T, itemCount: int) + delete(toDelete) /* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */ fun delete(toDelete: *T, itemCount: int): void { @@ -49,45 +22,38 @@ fun delete(toDelete: *T, itemCount: int): void { // finishes the pointer for (var i: int = 0; i < itemCount; i++;) toDelete[i].destruct(); - free(toDelete); - //delete(toDelete); + free((toDelete) cast *void); } /* We specilize on the trait Object to decide on whether or not the destructor should be called */ -fun delete(toDelete: *T): void { - free(toDelete); -} +fun delete(toDelete: *T) + free((toDelete) cast *void) fun delete(toDelete: *T): void { toDelete->destruct(); - free(toDelete); + free((toDelete) cast *void); } // a wrapper for construct if it has the Object trait -fun maybe_construct(it:*T):*T { +fun maybe_construct(it:*T):*T return it -} -fun maybe_construct(it:*T):*T { +fun maybe_construct(it:*T):*T return it->construct() -} - // a wrapper for copy constructing if it has the Object trait -fun maybe_copy_construct(to:*T, from:*T):void { +fun maybe_copy_construct(to:*T, from:*T) *to = *from -} -fun maybe_copy_construct(to:*T, from:*T):void { + +fun maybe_copy_construct(to:*T, from:*T) to->copy_construct(from) -} // a wrapper for destruct if it has the Object trait -fun maybe_destruct(it:*T):void {} +fun maybe_destruct(it:*T) {} -fun maybe_destruct(it:*T):void { +fun maybe_destruct(it:*T) it->destruct() -} obj shared_ptr (Object){ var data: *T; @@ -151,4 +117,3 @@ obj shared_ptr (Object){ }; //end shared_ptr class - diff --git a/stdlib/string.krak b/stdlib/string.krak index 9a0ac5c..a8580c8 100644 --- a/stdlib/string.krak +++ b/stdlib/string.krak @@ -19,10 +19,6 @@ fun operator+(first: *char, second: ref string): string { return string(first) + second } -/*fun operator+(first: *char, second: T): string {*/ - /*return string(first) + second*/ -/*}*/ - fun string(in:*char):string { var out.construct(in):string return out diff --git a/stdlib/type.krak b/stdlib/type.krak index 81c3e4b..b3a3f53 100644 --- a/stdlib/type.krak +++ b/stdlib/type.krak @@ -12,15 +12,21 @@ adt base_type { object, adt, no_type_adt_option, + function, template, template_type, void_return, boolean, character, + ucharacter, + short_int, + ushort_int, integer, + uinteger, + long_int, + ulong_int, floating, - double_precision, - function + double_precision } fun type_ptr(): *type { @@ -161,7 +167,13 @@ obj type (Object) { base_type::void_return() return indr_string + string("void_return") + trait_string base_type::boolean() return indr_string + string("boolean") + trait_string base_type::character() return indr_string + string("character") + trait_string + base_type::short_int() return indr_string + string("short") + trait_string base_type::integer() return indr_string + string("integer") + trait_string + base_type::long_int() return indr_string + string("long") + trait_string + base_type::ucharacter() return indr_string + string("ucharacter") + trait_string + base_type::ushort_int() return indr_string + string("ushort") + trait_string + base_type::uinteger() return indr_string + string("uinteger") + trait_string + base_type::ulong_int() return indr_string + string("ulong") + trait_string base_type::floating() return indr_string + string("floating") + trait_string base_type::double_precision() return indr_string + string("double_precision") + trait_string base_type::function() { @@ -177,9 +189,15 @@ obj type (Object) { return 5 match (base) { base_type::character() return 1 - base_type::integer() return 2 - base_type::floating() return 3 - base_type::double_precision() return 4 + base_type::ucharacter() return 1 + base_type::short_int() return 2 + base_type::ushort_int() return 2 + base_type::integer() return 3 + base_type::uinteger() return 3 + base_type::long_int() return 4 + base_type::ulong_int() return 4 + base_type::floating() return 5 + base_type::double_precision() return 6 } return 0 }