Some bugfixes/added errors, convert most to not use simple_passthrough

This commit is contained in:
Nathan Braswell
2016-04-30 15:38:28 -04:00
parent d126cbf24b
commit 7aa1d9983b
77 changed files with 260 additions and 600 deletions

View File

@@ -615,7 +615,7 @@ obj ast_transformation (Object) {
identifier->identifier.type = get_ast_type(expression)->clone_without_ref()
}
if (!identifier->identifier.type) error(node, "declaration statement with no type or expression from which to inference type")
if (identifier->identifier.type->is_none()) error(node, "declaration statement with bad type")
if (identifier->identifier.type->is_none() || (identifier->identifier.type->indirection == 0 && identifier->identifier.type->is_void())) error(node, "declaration statement with bad type")
var declaration = ast_declaration_statement_ptr(identifier, expression)
// ok, deal with the possible init position method call
if (identifiers.size == 2) {
@@ -716,9 +716,13 @@ obj ast_transformation (Object) {
}
fun transform_return_statement(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node {
var return_value = get_node("boolean_expression", node)
var to_ret: *ast_node
if (return_value)
return ast_return_statement_ptr(transform(return_value, scope, template_replacements))
return ast_return_statement_ptr(null<ast_node>())
to_ret = ast_return_statement_ptr(transform(return_value, scope, template_replacements))
else
to_ret = ast_return_statement_ptr(null<ast_node>())
ast_to_syntax.set(to_ret, node)
return to_ret
}
fun transform_branching_statement(node: *tree<symbol>, scope: *ast_node): *ast_node {
if (node->data.name == "break_statement")
@@ -778,8 +782,8 @@ obj ast_transformation (Object) {
parameters = get_nodes("intrinsic_parameter", node).map(fun(child: *tree<symbol>): string return concat_symbol_tree(child);)
if (get_nodes("type", node).size)
type_parameters = get_nodes("type", node).map(fun(child: *tree<symbol>): *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()))
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<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node {
var function_node = second_pass_function(node, scope, template_replacements, false)

View File

@@ -75,6 +75,7 @@ obj code_triple (Object) {
obj c_generator (Object) {
var id_counter: int
var ast_to_syntax: map<*ast_node, *tree<symbol>>
var ast_name_map: map<*ast_node, string>
var closure_struct_map: map<set<*ast_node>, string>
var function_type_map: map<type, string>
@@ -86,6 +87,7 @@ obj c_generator (Object) {
var linker_string: string
fun construct(): *c_generator {
id_counter = 0
ast_to_syntax.construct()
ast_name_map.construct()
closure_struct_map.construct()
function_type_map.construct()
@@ -144,6 +146,7 @@ obj c_generator (Object) {
}
fun copy_construct(old: *c_generator) {
id_counter = old->id_counter
ast_to_syntax.copy_construct(&old->ast_to_syntax)
ast_name_map.copy_construct(&old->ast_name_map)
closure_struct_map.copy_construct(&old->closure_struct_map)
function_type_map.copy_construct(&old->function_type_map)
@@ -159,6 +162,7 @@ obj c_generator (Object) {
copy_construct(&other)
}
fun destruct() {
ast_to_syntax.destruct()
ast_name_map.destruct()
closure_struct_map.destruct()
function_type_map.destruct()
@@ -205,8 +209,9 @@ obj c_generator (Object) {
return make_pair(type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameter_types + ");\n",
type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameters + ")")
}
fun generate_c(name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>): pair<string,string> {
var prequal: string = "#include <stdbool.h>\n#include <stdlib.h>\n#include <stdio.h>\n"
fun generate_c(name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax_in: map<*ast_node, *tree<symbol>> ): pair<string,string> {
ast_to_syntax = ast_to_syntax_in
var prequal: string = "#include <stdbool.h>\n"
var plain_typedefs: string = "\n/**Plain Typedefs**/\n"
var top_level_c_passthrough: string = ""
var variable_extern_declarations: string = ""
@@ -371,8 +376,7 @@ obj c_generator (Object) {
ast_node::declaration_statement(backing) variable_declarations += generate_declaration_statement(child, null<ast_node>(), null<ast_node>(), null<stack<pair<bool,stack<*ast_node>>>>(), false).one_string() + ";\n" // false - don't do defer
ast_node::function(backing) {
// check for and add to parameters if a closure
if (!backing.is_extern)
generate_function_definition(child, null<ast_node>(), false)
generate_function_definition(child, null<ast_node>(), false)
}
ast_node::template(backing) {
backing.instantiated.for_each(fun(node: *ast_node) {
@@ -446,7 +450,8 @@ obj c_generator (Object) {
structs += "};\n"
})
return make_pair(prequal+plain_typedefs+top_level_c_passthrough+variable_extern_declarations+structs+function_typedef_string+closure_struct_definitions+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)
/*return make_pair(prequal+plain_typedefs+top_level_c_passthrough+variable_extern_declarations+structs+function_typedef_string+closure_struct_definitions+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)*/
return make_pair(prequal+plain_typedefs+function_typedef_string+top_level_c_passthrough+variable_extern_declarations+structs+closure_struct_definitions+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)
}
fun get_closure_struct_type(closed_variables: set<*ast_node>): string {
if (!closure_struct_map.contains_key(closed_variables)) {
@@ -599,7 +604,7 @@ obj c_generator (Object) {
to_ret.pre = generate_declaration_statement(declaration, enclosing_object, enclosing_func, defer_stack, false).one_string() + ";\n"
if ((function_return_type->is_object() || return_value_type->is_object()) && !function_return_type->equality(return_value_type, false))
// note the clone with decreased indirection because of the clone with increased indirection above
error(string("return type does not match: ") + function_return_type->to_string() + ", " + return_value_type->to_string());
error(ast_to_syntax[node], string("return type does not match: ") + function_return_type->to_string() + ", " + return_value_type->to_string());
if (!function_return_type->is_ref && return_value_type->indirection == 0 && (return_value_type->is_adt() || (return_value_type->is_object() && has_method(return_value_type->type_def, "copy_construct", vector(return_value_type->clone_with_indirection(1)))))) {
to_ret.pre += generate_statement(ast_statement_ptr(make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(return_value))))), enclosing_object, enclosing_func, defer_stack).one_string()
} else {
@@ -724,7 +729,7 @@ obj c_generator (Object) {
if (as_value) {
var closed_vars = node->function.closed_variables
if (closed_vars.size() == 0)
return code_triple(string("((") + type_to_c(node->function.type) + "){(void*)NULL,(void*)" + get_name(node) + "})")
return code_triple(string("((") + type_to_c(node->function.type) + "){(void*)0,(void*)" + get_name(node) + "})")
var temp_closure_struct = string("closure_struct_temp") + get_id()
var to_ret = code_triple()
var closure_type_str = get_closure_struct_type(closed_vars)

View File

@@ -4,9 +4,7 @@ __if_comp__ __C__ simple_passthrough(::"-pthread") """
#include <pthread.h>
"""
def pthread_t float
fun pthread_create(thrd : *pthread_t, strt_routine : fun() : void) : int {
fun pthread_create(thrd : *ulong, strt_routine : fun() : void) : int {
__if_comp__ __C__ {
simple_passthrough(thrd,strt_routine::) """
pthread_attr_t attr;
@@ -20,7 +18,7 @@ fun pthread_create(thrd : *pthread_t, strt_routine : fun() : void) : int {
return 0
}
fun pthread_join(thrd : *pthread_t) : int {
fun pthread_join(thrd : *ulong) : int {
__if_comp__ __C__ { simple_passthrough(thrd::) """
pthread_t thread = *((pthread_t*)thrd);
return pthread_join(thread, NULL);
@@ -48,7 +46,7 @@ obj future<T> {
var status : int
var psy : fun() : T
var wrapper : fun() : void
var thread : pthread_t
var thread : ulong
fun construct(in : fun() : T) : *future<T> {
status = 0

View File

@@ -65,7 +65,7 @@ obj hash_map<T,U> (Object, Serializable) {
/*io::println((this) cast int)*/
/*io::print("size of data:")*/
/*io::println(data.size)*/
var key_hash = util::hash(key)
var key_hash = (util::hash(key)) cast int
if (!data[key_hash%data.size].contains_key(key)) {
size++
if (size > data.size) {
@@ -75,7 +75,7 @@ obj hash_map<T,U> (Object, Serializable) {
for (var i = 0; i < size*2; i++;)
new_data.addEnd(map::map<T,U>())
for_each(fun(key: T, value: U) {
new_data[util::hash(key)%new_data.size].set(key, value)
new_data[(util::hash(key)) cast int%new_data.size].set(key, value)
})
data = new_data
}
@@ -83,10 +83,10 @@ obj hash_map<T,U> (Object, Serializable) {
data[key_hash%data.size].set(key, value)
}
fun get(key: T): ref U {
return data[util::hash(key)%data.size].get(key)
return data[(util::hash(key)) cast int%data.size].get(key)
}
fun contains_key(key: T): bool {
return data[util::hash(key)%data.size].contains_key(key)
return data[(util::hash(key)) cast int%data.size].contains_key(key)
}
fun contains_value(value: U): bool {
for (var i = 0; i < data.size; i++;) {
@@ -103,7 +103,7 @@ obj hash_map<T,U> (Object, Serializable) {
io::println("trying to reverse get a value that is not in the hash_map")
}
fun remove(key: T) {
data[util::hash(key)%data.size].remove(key)
data[(util::hash(key)) cast int%data.size].remove(key)
}
fun for_each(func: fun(T, U):void) {
for (var i = 0; i < data.size; i++;)

View File

@@ -2,10 +2,6 @@ import string;
import vector;
import mem:*
__if_comp__ __C__ simple_passthrough """
#include <stdio.h>
"""
fun println() : void {
print("\n");
}
@@ -15,35 +11,18 @@ fun println<T>(toPrint: T) : void {
print("\n")
}
fun print<T>(toPrint: *T) : void{
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
printf("%p", (void*)toPrint);
fflush(0);
"""
}
return;
}
fun print<T>(toPrint: *T)
print((toPrint) cast ulong)
ext fun printf(fmt_str: *char, to_print: *char): int
ext fun fflush(file: int): int
fun print(toPrint: *char) : void {
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
printf("%s", toPrint);
fflush(0);
"""
}
return;
printf("%s", toPrint)
fflush(0)
}
fun print(toPrint: char) : void {
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
printf("%c", toPrint);
fflush(0);
"""
}
return;
}
fun print(toPrint: char) : void
print(string::string(toPrint))
fun print(toPrint: string::string) : void {
var charArr = toPrint.toCharArray()
@@ -59,52 +38,39 @@ fun print(toPrint: bool): void {
return;
}
fun print(toPrint: int): void {
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
printf("%d", toPrint);
fflush(0);
"""
}
return;
}
fun print(toPrint: int): void
print(string::to_string(toPrint))
fun print(toPrint: ulong): void
print(string::to_string(toPrint))
fun print(toPrint: float): void {
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
printf("%f", toPrint);
fflush(0);
"""
}
return;
}
ext fun sprintf(to_str: *char, format: *char, d: double)
fun print(toPrint: float)
print((toPrint) cast double)
fun print(toPrint: double) : void{
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
printf("%f", toPrint);
fflush(0);
"""
}
return;
fun print(toPrint: double) {
var int_str = new<char>((#sizeof<double>) cast int)
sprintf(int_str, "%f", toPrint)
print(int_str)
delete(int_str)
}
// Ok, just some DEAD simple file io for now
ext fun fopen(path: *char, mode: *char): *void
ext fun fclose(file: *void): int
ext fun fprintf(file: *void, format: *char, data: *char): int
ext fun ftell(file: *void): long
ext fun fseek(file: *void, offset: long, whence: int): int
ext fun fread(ptr: *void, size: ulong, nmemb: ulong, file: *void): ulong
ext fun fwrite(ptr: *void, size: ulong, nmemb: ulong, file: *void): ulong
fun file_exists(path: string::string): bool {
var char_path = path.toCharArray()
defer delete(char_path)
var result = false
__if_comp__ __C__ {
simple_passthrough(char_path:result:) """
bool result = false;
FILE *fp = fopen(char_path, "r");
if (fp) {
result = true;
fclose(fp);
}
"""
var fp = fopen(char_path, "r")
if (fp) {
fclose(fp)
return true
}
return result
return false
}
fun read_file(path: string::string): string::string {
if (!file_exists(path))
@@ -117,40 +83,25 @@ fun write_file(path: string::string, data: string::string) {
defer delete(char_path)
var char_data = data.toCharArray()
defer delete(char_data)
__if_comp__ __C__ {
simple_passthrough(char_path,char_data::) """
FILE *fp = fopen(char_path, "w");
fprintf(fp, "%s", char_data);
fclose(fp);
"""
}
var fp = fopen(char_path, "w")
fprintf(fp, "%s", char_data)
fclose(fp)
}
fun read_file_binary(path: string::string): vector::vector<char> {
var char_path = path.toCharArray()
defer delete(char_path)
var data: *char
var size: int
__if_comp__ __C__ {
simple_passthrough(char_path = char_path:data = data, size = size:) """
FILE *fp = fopen(char_path, "r");
fseek(fp, 0L, SEEK_END);
long size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char *data = malloc(size+1);
size_t readSize = fread(data, 1, size, fp);
data[readSize] = 0;
fclose(fp);
"""
}
var toRet.construct(size): vector::vector<char>
var fp = fopen(char_path, "r")
fseek(fp, (0) cast long, 2)// fseek(fp, 0L, SEEK_END)
var size = ftell(fp)
fseek(fp, (0) cast long, 0)//fseek(fp, 0L, SEEK_SET)
var data = new<char>((size+1) cast int)
var readSize = fread((data) cast *void, (1) cast ulong, (size) cast ulong, fp)
fclose(fp)
data[readSize] = 0
var toRet.construct((size) cast int): vector::vector<char>
for (var i = 0; i < size; i++;)
toRet.add(data[i])
__if_comp__ __C__ {
simple_passthrough(data = data::) """
free(data);
"""
}
delete(data)
return toRet
}
fun write_file_binary(path: string::string, vdata: vector::vector<char>) {
@@ -158,13 +109,9 @@ fun write_file_binary(path: string::string, vdata: vector::vector<char>) {
defer delete(char_path)
var data = vdata.getBackingMemory()
var size = vdata.size
__if_comp__ __C__ {
simple_passthrough(char_path, data, size::) """
FILE *fp = fopen(char_path, "w");
fwrite(data, 1, size, fp);
fclose(fp);
"""
}
var fp = fopen(char_path, "w")
fwrite((data) cast *void, (1) cast ulong, (size) cast ulong, fp)
fclose(fp)
}
fun BoldRed(): void{
@@ -195,7 +142,3 @@ fun Reset(): void{
print("\033[0m");
}

View File

@@ -1,139 +1,27 @@
__if_comp__ __C__ simple_passthrough(::"-lm") """
#include <math.h>
"""
fun fibanacci(num: int): int {
if (num < 2)
return 1;
return fibanacci(num-1) + fibanacci(num-2);
var l1 = 1
var l2 = 1
for (var i = 0; i < num; i++;) {
var next = l1 + l2
l2 = l1
l1 = next
}
return l1
}
/*********************
* Trig Functions
********************/
fun atan(arg: double): double
{
var ans: double = 0;
__if_comp__ __C__{
simple_passthrough(arg = arg, ans = ans : ans = ans :) """
ans = atan(arg);
"""
}//end C wrapper
return ans;
}//end atan function
fun atan2(x: double, y: double): double
{
var ans: double = 0;
__if_comp__ __C__{
simple_passthrough(x = x, y = y, ans = ans : ans = ans :) """
ans = atan2(x,y);
"""
}//end C wrapper
return ans;
}//end atan2 function
fun acos(arg: double): double
{
var ans: double = 0;
__if_comp__ __C__{
simple_passthrough(arg = arg, ans = ans : ans = ans :) """
ans = acos(arg);
"""
}//end C wrapper
return ans;
}//end acos function
fun asin(arg: double): double
{
var ans: double = 0;
__if_comp__ __C__{
simple_passthrough(arg = arg, ans = ans : ans = ans :) """
ans = asin(arg);
"""
}//end C wrapper
return ans;
}//end asin function
fun tan(arg: double): double
{
var ans: double = 0;
__if_comp__ __C__{
simple_passthrough(arg = arg, ans = ans : ans = ans :) """
ans = tan(arg);
"""
}//end C wrapper
return ans;
}//end tan function
fun cos(arg: double): double
{
var ans: double = 0;
__if_comp__ __C__{
simple_passthrough(arg = arg, ans = ans : ans = ans :) """
ans = cos(arg);
"""
}//end C wrapper
return ans;
}//end cos function
fun sin(arg: double): double
{
var ans: double = 0;
__if_comp__ __C__{
simple_passthrough(arg = arg, ans = ans : ans = ans :) """
ans = sin(arg);
"""
}//end C wrapper
return ans;
}//end sin function
ext fun atan(arg: double): double
ext fun atan2(x: double, y: double): double
ext fun acos(arg: double): double
ext fun asin(arg: double): double
ext fun tan(arg: double): double
ext fun cos(arg: double): double
ext fun sin(arg: double): double
fun mod(x: double, y: double): double
{
var ans: double;
var intAns: int;
intAns = x / y;
ans = x - intAns*y;
return ans;
var intAns = x / y;
return x - intAns*y;
}
val M_PI = acos(-1);

View File

@@ -1,13 +1,12 @@
fun null<T>(): *T {
return (0) cast *T
}
ext fun malloc(size: int): *void
ext fun malloc(size: ulong): *void
ext fun free(size: *void)
fun null<T>(): *T
return (0) cast *T
fun new<T>(count: int): *T
return (malloc( #sizeof<T> * count )) cast *T
return (malloc( (#sizeof<T> * count ) cast ulong )) cast *T
fun new<T>(): *T
return new<T>(1)

View File

@@ -7,22 +7,7 @@ fun system(call_string: string):int {
delete(c_call_string)
return result
}
fun system(call_string: *char): int {
__if_comp__ __C__ {
simple_passthrough(call_string::) """
return system(call_string);
"""
}
}
ext fun system(call_string: *char): int
ext fun exit(code: int):void
fun exit() exit(0);
fun exit(code: int) {
__if_comp__ __C__ {
simple_passthrough(code::) """
exit(code);
"""
}
}

View File

@@ -19,7 +19,7 @@ fun unserialize<T>(it: ref vector::vector<char>): T {
return unserialize<T>(it, 0).first
}
fun unserialize<T>(it: ref vector::vector<char>, pos: int): util::pair<T,int> {
return util::make_pair(*(it.getBackingMemory()+pos) cast *T, pos + #sizeof<T>)
return util::make_pair(*(it.getBackingMemory()+pos) cast *T, pos + (#sizeof<T>) cast int)
}
fun unserialize<T(Serializable)>(it: ref vector::vector<char>, pos: int): util::pair<T,int> {
var toRet: T

View File

@@ -1,59 +0,0 @@
fun println() {
print("\n")
}
fun println(to_print: *char) {
print(to_print)
print("\n")
}
fun println(to_print: char) {
print(to_print)
print("\n")
}
fun println(to_print: int) {
print(to_print)
print("\n")
}
fun println(to_print: float) {
print(to_print)
print("\n")
}
fun println(to_print: double) {
print(to_print)
print("\n")
}
fun println(to_print: bool) {
print(to_print)
print("\n")
}
fun print(to_print: *char) {
__if_comp__ __C__ simple_passthrough(to_print::) """
printf("%s", to_print);
"""
}
fun print(to_print: char) {
__if_comp__ __C__ simple_passthrough(to_print::) """
printf("%c", to_print);
"""
}
fun print(to_print: int) {
__if_comp__ __C__ simple_passthrough(to_print::) """
printf("%d", to_print);
"""
}
fun print(to_print: float) {
__if_comp__ __C__ simple_passthrough(to_print::) """
printf("%f", to_print);
"""
}
fun print(to_print: double) {
__if_comp__ __C__ simple_passthrough(to_print::) """
printf("%f", to_print);
"""
}
fun print(to_print: bool) {
if (to_print) print("true")
else print("false")
}

View File

@@ -2,16 +2,41 @@ import vector
import util
import mem
import serialize
import io
fun to_string(in: int): string {
var dest = mem::new<char>(#sizeof<int> * 8)
defer mem::delete(dest)
__if_comp__ __C__ {
simple_passthrough(dest = dest, in = in::) """
sprintf(dest, "%d", in);
"""
}
var ret = string(dest)
fun to_string(in: uchar): string
return to_string_num(in)
fun to_string(in: short): string
return to_string_num(in)
fun to_string(in: ushort): string
return to_string_num(in)
fun to_string(in: int): string
return to_string_num(in)
fun to_string(in: uint): string
return to_string_num(in)
fun to_string(in: long): string
return to_string_num(in)
fun to_string(in: ulong): string
return to_string_num(in)
fun to_string_num<T>(in: T): string {
if (in == 0)
return string("0")
var ret = string()
var pos = 0
var is_neg = false
if (in > 0) {
pos = in
} else {
pos = -in
is_neg = true
}
while (pos) {
ret = string((pos%10 + '0') cast char) + ret
pos = pos / 10
}
if (is_neg)
return string("-") + ret
return ret
}

View File

@@ -27,12 +27,18 @@ fun min<T>(a: T, b: T): T {
return a;
}
fun hash<T(Hashable)>(item: T): int return item.hash()
fun hash<T>(item: *T): int return (item) cast int
fun hash(item: int): int return item
fun hash(item: char): int return item
fun hash<T(Hashable)>(item: T): ulong return item.hash()
fun hash<T>(item: *T): ulong return (item) cast ulong
fun hash(item: char): ulong return item
fun hash(item: uchar): ulong return item
fun hash(item: short): ulong return item
fun hash(item: ushort): ulong return item
fun hash(item: int): ulong return item
fun hash(item: uint): ulong return item
fun hash(item: long): ulong return item
fun hash(item: ulong): ulong return item
// default hash
fun hash<T>(item: T): int {
fun hash<T>(item: T): ulong {
io::println("using empty hash - please do not do!")
return 0
}
@@ -95,8 +101,7 @@ obj pair<T,U> (Object, Serializable, Hashable) {
return second_pair.second
}
fun hash():int return hash(first) ^ hash(second)
/*fun hash():int return 0*/
fun hash():ulong return hash(first) ^ hash(second)
// the old unnecessary template to prevent generation
// if not used trick (in this case, changing out U with V)