Placeholder/passthrough for all the expressions, simple value node passthrough (though if Kraken has the same literal syntax as C it'll work for a while, though it really needs to encode the type...

This commit is contained in:
Nathan Braswell
2016-01-08 00:33:05 -05:00
parent daae39fe19
commit 16aa01a76e
4 changed files with 62 additions and 24 deletions

View File

@@ -519,8 +519,8 @@ obj for_loop (Object) {
return true return true
} }
} }
fun ast_return_statement_ptr(): *ast_node { fun ast_return_statement_ptr(return_value: *ast_node): *ast_node {
var to_ret.construct(): return_statement var to_ret.construct(return_value): return_statement
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::return_statement(to_ret)) ptr->copy_construct(&ast_node::return_statement(to_ret))
return ptr return ptr
@@ -532,12 +532,15 @@ fun is_return_statement(node: *ast_node): bool {
return false return false
} }
obj return_statement (Object) { obj return_statement (Object) {
var return_value: *ast_node
var scope: map<string, vector<*ast_node>> var scope: map<string, vector<*ast_node>>
fun construct(): *return_statement { fun construct(return_value_in: *ast_node): *return_statement {
return_value = return_value_in
scope.construct() scope.construct()
return this return this
} }
fun copy_construct(old: *return_statement) { fun copy_construct(old: *return_statement) {
return_value = old->return_value
scope.copy_construct(&old->scope) scope.copy_construct(&old->scope)
} }
fun destruct() { fun destruct() {
@@ -548,7 +551,7 @@ obj return_statement (Object) {
copy_construct(&other) copy_construct(&other)
} }
fun operator==(other: ref return_statement): bool { fun operator==(other: ref return_statement): bool {
return true return return_value == other.return_value
} }
} }
fun ast_break_statement_ptr(): *ast_node { fun ast_break_statement_ptr(): *ast_node {
@@ -813,8 +816,8 @@ obj function_call (Object) {
return true return true
} }
} }
fun ast_value_ptr(): *ast_node { fun ast_value_ptr(string_value: string): *ast_node {
var to_ret.construct(): value var to_ret.construct(string_value): value
var ptr = new<ast_node>() var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::value(to_ret)) ptr->copy_construct(&ast_node::value(to_ret))
return ptr return ptr
@@ -826,23 +829,27 @@ fun is_value(node: *ast_node): bool {
return false return false
} }
obj value (Object) { obj value (Object) {
var string_value: string
var scope: map<string, vector<*ast_node>> var scope: map<string, vector<*ast_node>>
fun construct(): *value { fun construct(string_value_in: string): *value {
scope.construct() scope.construct()
string_value.copy_construct(&string_value_in)
return this return this
} }
fun copy_construct(old: *value) { fun copy_construct(old: *value) {
scope.copy_construct(&old->scope) scope.copy_construct(&old->scope)
string_value.copy_construct(&old->string_value)
} }
fun destruct() { fun destruct() {
scope.destruct() scope.destruct()
string_value.destruct()
} }
fun operator=(other: ref value) { fun operator=(other: ref value) {
destruct() destruct()
copy_construct(&other) copy_construct(&other)
} }
fun operator==(other: ref value): bool { fun operator==(other: ref value): bool {
return true return string_value == other.string_value
} }
} }

View File

@@ -177,6 +177,20 @@ obj ast_transformation (Object) {
return transform_simple_passthrough(node, scope) return transform_simple_passthrough(node, scope)
} else if (name == "statement") { } else if (name == "statement") {
return transform_statement(node, scope) return transform_statement(node, scope)
} else if (name == "return_statement") {
return transform_return_statement(node, scope)
} else if (name == "boolean_expression" || name == "and_boolean_expression"
|| name == "bool_exp" || name == "expression"
|| name == "shiftand" || name == "term"
|| name == "factor" || name == "unarad"
) {
// for now, assume passthrough and just transform underneath
return transform_expression(node, scope)
} else if (name == "bool" || name == "string"
|| name == "character" || name == "number"
) {
println(string("transforming value: ") + name)
return transform_value(node, scope)
} }
print("FAILED TO TRANSFORM: "); println(concat_symbol_tree(node)) print("FAILED TO TRANSFORM: "); println(concat_symbol_tree(node))
return null<ast_node>() return null<ast_node>()
@@ -190,6 +204,13 @@ obj ast_transformation (Object) {
var name = concat_symbol_tree(node) var name = concat_symbol_tree(node)
return ast_identifier_ptr(name, type_ptr(base_type::void_return())) return ast_identifier_ptr(name, type_ptr(base_type::void_return()))
} }
fun transform_value(node: *tree<symbol>, scope: *ast_node): *ast_node return ast_value_ptr(concat_symbol_tree(node))
fun transform_expression(node: *tree<symbol>, 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<symbol>, scope: *ast_node): *ast_node { fun transform_code_block(node: *tree<symbol>, scope: *ast_node): *ast_node {
var new_block = ast_code_block_ptr() var new_block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", scope, new_block) add_to_scope("~enclosing_scope", scope, new_block)
@@ -213,6 +234,9 @@ obj ast_transformation (Object) {
new_statement->statement.child = transform(node->children[0], scope) new_statement->statement.child = transform(node->children[0], scope)
return new_statement return new_statement
} }
fun transform_return_statement(node: *tree<symbol>, scope: *ast_node): *ast_node {
return ast_return_statement_ptr(transform(node->children[0], scope))
}
} }
fun concat_symbol_tree(node: *tree<symbol>): string { fun concat_symbol_tree(node: *tree<symbol>): string {

View File

@@ -77,10 +77,12 @@ obj c_generator (Object) {
return node->simple_passthrough.passthrough_str return node->simple_passthrough.passthrough_str
} }
fun generate_statement(node: *ast_node): string return generate(node->statement.child) + ";\n"; fun generate_statement(node: *ast_node): string return generate(node->statement.child) + ";\n";
fun generate_return_statement(node: *ast_node): string return string("return ") + generate(node->return_statement.return_value);
fun generate_value(node: *ast_node): string return node->value.string_value;
fun generate_code_block(node: *ast_node): string { fun generate_code_block(node: *ast_node): string {
var to_ret = string("{\n") var to_ret = string("{\n")
node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child);) node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child);)
return to_ret + "}\n" return to_ret + "}"
} }
// for now, anyway // for now, anyway
@@ -90,6 +92,8 @@ obj c_generator (Object) {
ast_node::simple_passthrough(backing) return generate_simple_passthrough(node) ast_node::simple_passthrough(backing) return generate_simple_passthrough(node)
ast_node::statement(backing) return generate_statement(node) ast_node::statement(backing) return generate_statement(node)
ast_node::code_block(backing) return generate_code_block(node) ast_node::code_block(backing) return generate_code_block(node)
ast_node::return_statement(backing) return generate_return_statement(node)
ast_node::value(backing) return generate_value(node)
} }
return string("/* COULD NOT GENERATE */") return string("/* COULD NOT GENERATE */")
} }

View File

@@ -78,25 +78,27 @@ obj importer (Object) {
fun trim(parse_tree: *tree<symbol>) { fun trim(parse_tree: *tree<symbol>) {
remove_node(symbol("$NULL$", false), parse_tree) remove_node(symbol("$NULL$", false), parse_tree)
remove_node(symbol("WS", false), parse_tree) remove_node(symbol("WS", false), parse_tree)
remove_node(symbol("\\(", true), parse_tree) // the terminals have " around them, which we have to escape
remove_node(symbol("\\)", true), parse_tree) remove_node(symbol("\"\\(\"", true), parse_tree)
remove_node(symbol("var", true), parse_tree) remove_node(symbol("\"\\)\"", true), parse_tree)
remove_node(symbol("fun", true), parse_tree) remove_node(symbol("\"var\"", true), parse_tree)
remove_node(symbol(";", true), parse_tree) remove_node(symbol("\"fun\"", true), parse_tree)
remove_node(symbol("\"template\"", true), parse_tree)
remove_node(symbol("\"return\"", true), parse_tree)
remove_node(symbol("\";\"", true), parse_tree)
remove_node(symbol("line_end", false), parse_tree) remove_node(symbol("line_end", false), parse_tree)
remove_node(symbol("{", true), parse_tree) remove_node(symbol("\"{\"", true), parse_tree)
remove_node(symbol("}", true), parse_tree) remove_node(symbol("\"}\"", true), parse_tree)
remove_node(symbol("(", true), parse_tree) remove_node(symbol("\"(\"", true), parse_tree)
remove_node(symbol(")", true), parse_tree) remove_node(symbol("\")\"", true), parse_tree)
remove_node(symbol("if", true), parse_tree) remove_node(symbol("\"if\"", true), parse_tree)
remove_node(symbol("while", true), parse_tree) remove_node(symbol("\"while\"", true), parse_tree)
remove_node(symbol("__if_comp__", true), parse_tree) remove_node(symbol("\"__if_comp__\"", true), parse_tree)
remove_node(symbol("comp_simple_passthrough", true), parse_tree) remove_node(symbol("\"comp_simple_passthrough\"", true), parse_tree)
remove_node(symbol("def_nonterm", false), parse_tree) remove_node(symbol("def_nonterm", false), parse_tree)
remove_node(symbol("obj_nonterm", false), parse_tree) remove_node(symbol("obj_nonterm", false), parse_tree)
remove_node(symbol("adt_nonterm", false), parse_tree) remove_node(symbol("adt_nonterm", false), parse_tree)
remove_node(symbol("template", true), parse_tree) remove_node(symbol("\"\\|\"", true), parse_tree)
remove_node(symbol("\\|", true), parse_tree)
collapse_node(symbol("case_statement_list", false), parse_tree) collapse_node(symbol("case_statement_list", false), parse_tree)
collapse_node(symbol("opt_param_assign_list", false), parse_tree) collapse_node(symbol("opt_param_assign_list", false), parse_tree)
@@ -132,6 +134,7 @@ obj importer (Object) {
node->children.remove(i) node->children.remove(i)
i--; i--;
} else { } else {
/*println(remove.to_string() + " not equal " + node->children[i]->data.to_string())*/
to_process.push(node->children[i]) to_process.push(node->children[i])
} }
} }