Added indirection to types in prep for function calls, full passthrough, and the first real hello world

This commit is contained in:
Nathan Braswell
2016-01-10 18:26:31 -05:00
parent 7f20a42178
commit 5db0365a63
7 changed files with 82 additions and 45 deletions

View File

@@ -143,25 +143,31 @@ obj ast_transformation (Object) {
// always get to pre-reffed level // always get to pre-reffed level
var real_node = get_node("pre_reffed", node) var real_node = get_node("pre_reffed", node)
// check for indirection and step down // 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) var type_syntax_str = concat_symbol_tree(real_node)
println(type_syntax_str + " *************************")
// should take into account indirection and references... // should take into account indirection and references...
if (type_syntax_str == "void") 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") 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") 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") 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") 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") else if (type_syntax_str == "char")
return type_ptr(base_type::character()) return type_ptr(base_type::character(), indirection)
else if (/* check for function type*/ true) else if (/* check for function type*/ false)
return type_ptr(base_type::function()) return type_ptr(base_type::function(), indirection)
else { else {
// do lookup for objects, ADTs, templates, etc // do lookup for objects, ADTs, templates, etc
return type_ptr(base_type::none()) return type_ptr(base_type::none(), indirection)
} }
} }
/*NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope, std::vector<Type> types, bool limitToFunction, std::map<std::string, Type*> templateTypeReplacements) {*/ /*NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope, std::vector<Type> types, bool limitToFunction, std::map<std::string, Type*> templateTypeReplacements) {*/
@@ -208,7 +214,7 @@ obj ast_transformation (Object) {
var value_str = concat_symbol_tree(node) var value_str = concat_symbol_tree(node)
var value_type = null<type>() var value_type = null<type>()
if (value_str[0] == '"') 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] == '\'') else if (value_str[0] == '\'')
value_type = type_ptr(base_type::character()) value_type = type_ptr(base_type::character())
else { else {

View File

@@ -98,23 +98,25 @@ obj c_generator (Object) {
return string("/* COULD NOT GENERATE */") return string("/* COULD NOT GENERATE */")
} }
fun type_to_c(type: *type): string { fun type_to_c(type: *type): string {
var indirection = string()
for (var i = 0; i < type->indirection; i++;) indirection += "*"
match (type->base) { match (type->base) {
base_type::none() return string("none") base_type::none() return string("none") + indirection
base_type::template() return string("template") base_type::template() return string("template") + indirection
base_type::template_type() return string("template_type") base_type::template_type() return string("template_type") + indirection
base_type::void_return() return string("void") base_type::void_return() return string("void") + indirection
base_type::boolean() return string("bool") base_type::boolean() return string("bool") + indirection
base_type::character() return string("char") base_type::character() return string("char") + indirection
base_type::integer() return string("int") base_type::integer() return string("int") + indirection
base_type::floating() return string("float") base_type::floating() return string("float") + indirection
base_type::double_precision() return string("double") base_type::double_precision() return string("double") + indirection
base_type::function() { 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() + " ";) type->parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + " ";)
return temp + ")" + type->return_type->to_string() return temp + ")" + type->return_type->to_string()
} }
} }
return string("impossible type") return string("impossible type") + indirection
} }
} }

View File

@@ -79,6 +79,7 @@ obj parser (Object) {
} }
input.addEnd(current_symbol) input.addEnd(current_symbol)
if (current_symbol == invalid_symbol()) { if (current_symbol == invalid_symbol()) {
println("**PARSE ERROR**")
println("lexing failed for ") println("lexing failed for ")
println(name) println(name)
return null<tree<symbol>>() return null<tree<symbol>>()
@@ -104,11 +105,14 @@ obj parser (Object) {
for (var i = 0; i < input.size; i++;) { for (var i = 0; i < input.size; i++;) {
if (gss.frontier_is_empty(i)) { if (gss.frontier_is_empty(i)) {
println("**PARSE ERROR**")
print(i) print(i)
print("th frontier is empty in file '") print("th frontier is empty in file '")
print(name) print(name)
print("' with txt ") print("' with txt ")
print(input[i].to_string()) print(input[i].to_string())
print(" line number: ")
print(find_line(i))
println() println()
return null<tree<symbol>>() return null<tree<symbol>>()
} }
@@ -127,9 +131,13 @@ obj parser (Object) {
return gss.get_edge(acc_state, v0) return gss.get_edge(acc_state, v0)
} }
println("**PARSE ERROR**")
println("REJECTED") println("REJECTED")
println("parsing (not lexing) failed for ") println("parsing (not lexing) failed for ")
println(name) println(name)
print(" line number: ")
print(find_line(input.size))
println("(minus 2?)")
return null<tree<symbol>>() return null<tree<symbol>>()
} }
fun reducer(i: int) { fun reducer(i: int) {
@@ -363,6 +371,14 @@ obj parser (Object) {
return new<tree<symbol>>()->construct(null_symbol()) return new<tree<symbol>>()->construct(null_symbol())
return null<tree<symbol>>() return null<tree<symbol>>()
} }
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) { obj gss (Object) {

View File

@@ -109,6 +109,8 @@ obj string (Object, Serializable) {
return *this == str return *this == str
} }
fun operator+(integer: int): string return *this + to_string(integer);
fun operator+(str: *char): string { fun operator+(str: *char): string {
var newStr.construct(str):string var newStr.construct(str):string
var ret.construct(data + newStr.data):string var ret.construct(data + newStr.data):string
@@ -121,6 +123,8 @@ obj string (Object, Serializable) {
return ret return ret
} }
fun operator+=(integer: int) *this += to_string(integer);
fun operator+=(character: char): void { fun operator+=(character: char): void {
data += character data += character
} }

View File

@@ -20,37 +20,44 @@ adt base_type {
fun type_ptr(): *type { fun type_ptr(): *type {
return new<type>()->construct() return new<type>()->construct()
} }
fun type_ptr(base: base_type): *type { fun type_ptr(base: base_type): *type return type_ptr(base, 0);
return new<type>()->construct(base) fun type_ptr(base: base_type, indirection: int): *type {
return new<type>()->construct(base, indirection)
} }
fun type_ptr(parameters: vector<*type>, return_type: *type): *type { fun type_ptr(parameters: vector<*type>, return_type: *type): *type return type_ptr(parameters, return_type, 0);
return new<type>()->construct(parameters, return_type) fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int): *type {
return new<type>()->construct(parameters, return_type, indirection)
} }
obj type (Object) { obj type (Object) {
var base: base_type var base: base_type
var parameter_types: vector<*type> var parameter_types: vector<*type>
var return_type: *type var return_type: *type
var indirection: int
fun construct(): *type { fun construct(): *type {
base.copy_construct(&base_type::none()) base.copy_construct(&base_type::none())
parameter_types.construct() parameter_types.construct()
indirection = 0
return this return this
} }
fun construct(base_in: base_type): *type { fun construct(base_in: base_type, indirection_in: int): *type {
base.copy_construct(&base_in) base.copy_construct(&base_in)
parameter_types.construct() parameter_types.construct()
indirection = indirection_in
return this 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()) base.copy_construct(&base_type::function())
parameter_types.copy_construct(&parameter_types) parameter_types.copy_construct(&parameter_types)
return_type = return_type_in return_type = return_type_in
indirection = indirection_in
return this return this
} }
fun copy_construct(old: *type) { fun copy_construct(old: *type) {
base.copy_construct(&old->base) base.copy_construct(&old->base)
parameter_types.copy_construct(&old->parameter_types) parameter_types.copy_construct(&old->parameter_types)
return_type = old->return_type return_type = old->return_type
indirection = old->indirection
} }
fun operator=(other: ref type) { fun operator=(other: ref type) {
destruct() destruct()
@@ -62,22 +69,22 @@ obj type (Object) {
} }
fun to_string(): string { fun to_string(): string {
match (base) { match (base) {
base_type::none() return string("none") base_type::none() return string("none, indirection: ") + indirection
base_type::template() return string("template") base_type::template() return string("template, indirection:") + indirection
base_type::template_type() return string("template_type") base_type::template_type() return string("template_type, indirection:") + indirection
base_type::void_return() return string("void_return") base_type::void_return() return string("void_return, indirection:") + indirection
base_type::boolean() return string("boolean") base_type::boolean() return string("boolean, indirection:") + indirection
base_type::character() return string("character") base_type::character() return string("character, indirection:") + indirection
base_type::integer() return string("integer") base_type::integer() return string("integer, indirection:") + indirection
base_type::floating() return string("floating") base_type::floating() return string("floating, indirection:") + indirection
base_type::double_precision() return string("double_precision") base_type::double_precision() return string("double_precision, indirection:") + indirection
base_type::function() { base_type::function() {
var temp = string("function: (") var temp = string("function: (")
parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + " ";) 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
} }
} }

View File

@@ -1,11 +1,9 @@
__if_comp__ __C__ simple_passthrough """ fun simple_print(to_print: *char) {
#include <stdio.h> __if_comp__ __C__ simple_passthrough(to_print::) """
int main() { printf("%s", to_print);
printf("hello world! (of selfhosting! (silly selfhosting for now))\n"); """
return 0; }
}
"""
var a = 1 var a = 1
var b = 2 var b = 2

View File

@@ -11,4 +11,8 @@ fun some_function(): int return 0;
fun some_other_function(in: bool): float { fun some_other_function(in: bool): float {
return 0.0 return 0.0
} }
fun main(): int {
simple_print("Hello World!")
return 0
}