Fix case_statement/lambda-close-over-variables bug, rename ast_node file to make ast_node:: unambigious, change test_ast to test_compiler and add a little skeleton c_generator file
This commit is contained in:
@@ -36,11 +36,13 @@ NodeTree<T>* RemovalTransformation<T>::transform(NodeTree<T>* from) {
|
|||||||
while(!toProcess.empty()) {
|
while(!toProcess.empty()) {
|
||||||
NodeTree<T>* node = toProcess.front();
|
NodeTree<T>* node = toProcess.front();
|
||||||
toProcess.pop();
|
toProcess.pop();
|
||||||
|
if (!node)
|
||||||
|
continue;
|
||||||
std::vector<NodeTree<T>*> children = node->getChildren();
|
std::vector<NodeTree<T>*> children = node->getChildren();
|
||||||
for (int i = 0; i < children.size(); i++) {
|
for (int i = 0; i < children.size(); i++) {
|
||||||
if (children[i]->getData() == toRemove)
|
if (children[i]->getData() == toRemove)
|
||||||
node->removeChild(children[i]);
|
node->removeChild(children[i]);
|
||||||
else
|
else if (children[i])
|
||||||
toProcess.push(children[i]);
|
toProcess.push(children[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1070,6 +1070,12 @@ std::set<NodeTree<ASTData>*> ASTTransformation::findVariablesToClose(NodeTree<AS
|
|||||||
}
|
}
|
||||||
return closed;
|
return closed;
|
||||||
}
|
}
|
||||||
|
if (stat->getDataRef()->type == case_statement) {
|
||||||
|
// don't try to close over the variant specifier itself, only its statement child
|
||||||
|
auto recClosed = findVariablesToClose(func, stat->getChildren().back(), scope);
|
||||||
|
closed.insert(recClosed.begin(), recClosed.end());
|
||||||
|
return closed;
|
||||||
|
}
|
||||||
if (stat->getDataRef()->type == function_call && (stat->getDataRef()->symbol.getName() == "." || stat->getDataRef()->symbol.getName() == "->")) {
|
if (stat->getDataRef()->type == function_call && (stat->getDataRef()->symbol.getName() == "." || stat->getDataRef()->symbol.getName() == "->")) {
|
||||||
// only search on the left side of access operators like . and ->
|
// only search on the left side of access operators like . and ->
|
||||||
auto recClosed = findVariablesToClose(func, stat->getChildren()[1], scope);
|
auto recClosed = findVariablesToClose(func, stat->getChildren()[1], scope);
|
||||||
|
|||||||
@@ -272,12 +272,14 @@ fun ast_statement_ptr(): *ast_node {
|
|||||||
}
|
}
|
||||||
obj statement (Object) {
|
obj statement (Object) {
|
||||||
var scope: map<string, vector<*ast_node>>
|
var scope: map<string, vector<*ast_node>>
|
||||||
|
var child: *ast_node
|
||||||
fun construct(): *statement {
|
fun construct(): *statement {
|
||||||
scope.construct()
|
scope.construct()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
fun copy_construct(old: *statement) {
|
fun copy_construct(old: *statement) {
|
||||||
scope.copy_construct(&old->scope)
|
scope.copy_construct(&old->scope)
|
||||||
|
child = old->child
|
||||||
}
|
}
|
||||||
fun destruct() {
|
fun destruct() {
|
||||||
scope.destruct()
|
scope.destruct()
|
||||||
@@ -287,7 +289,7 @@ obj statement (Object) {
|
|||||||
copy_construct(&other)
|
copy_construct(&other)
|
||||||
}
|
}
|
||||||
fun operator==(other: ref statement): bool {
|
fun operator==(other: ref statement): bool {
|
||||||
return true
|
return child == other.child
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun ast_if_statement_ptr(): *ast_node {
|
fun ast_if_statement_ptr(): *ast_node {
|
||||||
@@ -584,22 +586,24 @@ fun ast_if_comp_ptr(): *ast_node {
|
|||||||
}
|
}
|
||||||
obj if_comp (Object) {
|
obj if_comp (Object) {
|
||||||
var wanted_generator: string
|
var wanted_generator: string
|
||||||
|
var statement: *ast_node
|
||||||
fun construct(): *if_comp {
|
fun construct(): *if_comp {
|
||||||
wanted_generator.construct()
|
wanted_generator.construct()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
fun copy_construct(old: *if_comp) {
|
fun copy_construct(old: *if_comp) {
|
||||||
wanted_generator.copy_construct(&old->wanted_generator)
|
wanted_generator.copy_construct(&old->wanted_generator)
|
||||||
|
statement = old->statement
|
||||||
}
|
}
|
||||||
fun destruct() {
|
fun destruct() {
|
||||||
wanted_generator.destruct()
|
wanted_generator.destruct()
|
||||||
}
|
}
|
||||||
fun operator=(other: ref if_comp) {
|
fun operator=(other: if_comp) {
|
||||||
destruct()
|
destruct()
|
||||||
copy_construct(&other)
|
copy_construct(&other)
|
||||||
}
|
}
|
||||||
fun operator==(other: ref if_comp): bool {
|
fun operator==(other: if_comp): bool {
|
||||||
return wanted_generator == other.wanted_generator
|
return wanted_generator == other.wanted_generator && statement == other.statement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun ast_simple_passthrough_ptr(): *ast_node {
|
fun ast_simple_passthrough_ptr(): *ast_node {
|
||||||
@@ -610,22 +614,26 @@ fun ast_simple_passthrough_ptr(): *ast_node {
|
|||||||
}
|
}
|
||||||
obj simple_passthrough (Object) {
|
obj simple_passthrough (Object) {
|
||||||
var scope: map<string, vector<*ast_node>>
|
var scope: map<string, vector<*ast_node>>
|
||||||
|
var passthrough_str: string
|
||||||
fun construct(): *simple_passthrough {
|
fun construct(): *simple_passthrough {
|
||||||
scope.construct()
|
scope.construct()
|
||||||
|
passthrough_str.construct()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
fun copy_construct(old: *simple_passthrough) {
|
fun copy_construct(old: *simple_passthrough) {
|
||||||
scope.copy_construct(&old->scope)
|
scope.copy_construct(&old->scope)
|
||||||
|
passthrough_str.copy_construct(&old->passthrough_str)
|
||||||
}
|
}
|
||||||
fun destruct() {
|
fun destruct() {
|
||||||
scope.destruct()
|
scope.destruct()
|
||||||
|
passthrough_str.destruct()
|
||||||
}
|
}
|
||||||
fun operator=(other: ref simple_passthrough) {
|
fun operator=(other: ref simple_passthrough) {
|
||||||
destruct()
|
destruct()
|
||||||
copy_construct(&other)
|
copy_construct(&other)
|
||||||
}
|
}
|
||||||
fun operator==(other: ref simple_passthrough): bool {
|
fun operator==(other: ref simple_passthrough): bool {
|
||||||
return true
|
return scope == other.scope && passthrough_str == other.passthrough_str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun ast_function_call_ptr(): *ast_node {
|
fun ast_function_call_ptr(): *ast_node {
|
||||||
@@ -690,7 +698,7 @@ fun get_ast_children(node: *ast_node): vector<*ast_node> {
|
|||||||
ast_node::adt_def(backing) return vector<*ast_node>()
|
ast_node::adt_def(backing) return vector<*ast_node>()
|
||||||
ast_node::function(backing) return vector<*ast_node>()
|
ast_node::function(backing) return vector<*ast_node>()
|
||||||
ast_node::code_block(backing) return backing.children
|
ast_node::code_block(backing) return backing.children
|
||||||
ast_node::statement(backing) return vector<*ast_node>()
|
ast_node::statement(backing) return vector<*ast_node>(backing.child)
|
||||||
ast_node::if_statement(backing) return vector<*ast_node>()
|
ast_node::if_statement(backing) return vector<*ast_node>()
|
||||||
ast_node::match_statement(backing) return vector<*ast_node>()
|
ast_node::match_statement(backing) return vector<*ast_node>()
|
||||||
ast_node::case_statement(backing) return vector<*ast_node>()
|
ast_node::case_statement(backing) return vector<*ast_node>()
|
||||||
@@ -702,7 +710,7 @@ fun get_ast_children(node: *ast_node): vector<*ast_node> {
|
|||||||
ast_node::defer_statement(backing) return vector<*ast_node>()
|
ast_node::defer_statement(backing) return vector<*ast_node>()
|
||||||
ast_node::assignment_statement(backing) return vector<*ast_node>()
|
ast_node::assignment_statement(backing) return vector<*ast_node>()
|
||||||
ast_node::declaration_statement(backing) return vector<*ast_node>()
|
ast_node::declaration_statement(backing) return vector<*ast_node>()
|
||||||
ast_node::if_comp(backing) return vector<*ast_node>()
|
ast_node::if_comp(backing) return vector<*ast_node>(backing.statement)
|
||||||
ast_node::simple_passthrough(backing) return vector<*ast_node>()
|
ast_node::simple_passthrough(backing) return vector<*ast_node>()
|
||||||
ast_node::function_call(backing) return vector<*ast_node>()
|
ast_node::function_call(backing) return vector<*ast_node>()
|
||||||
ast_node::value(backing) return vector<*ast_node>()
|
ast_node::value(backing) return vector<*ast_node>()
|
||||||
@@ -730,7 +738,7 @@ fun get_ast_name(node: *ast_node): string {
|
|||||||
ast_node::assignment_statement(backing) return string("assignment_statement")
|
ast_node::assignment_statement(backing) return string("assignment_statement")
|
||||||
ast_node::declaration_statement(backing) return string("declaration_statement")
|
ast_node::declaration_statement(backing) return string("declaration_statement")
|
||||||
ast_node::if_comp(backing) return string("if_comp: ") + backing.wanted_generator
|
ast_node::if_comp(backing) return string("if_comp: ") + backing.wanted_generator
|
||||||
ast_node::simple_passthrough(backing) return string("simple_passthrough")
|
ast_node::simple_passthrough(backing) return string("simple_passthrough: , string:") + backing.passthrough_str
|
||||||
ast_node::function_call(backing) return string("function_call")
|
ast_node::function_call(backing) return string("function_call")
|
||||||
ast_node::value(backing) return string("value")
|
ast_node::value(backing) return string("value")
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ import string:*
|
|||||||
import mem:*
|
import mem:*
|
||||||
import io:*
|
import io:*
|
||||||
import importer:*
|
import importer:*
|
||||||
import ast_node:*
|
import ast_nodes:*
|
||||||
|
|
||||||
/*Importer * importer;*/
|
/*Importer * importer;*/
|
||||||
/*NodeTree<ASTData>* builtin_trans_unit; // the top scope for language level stuff*/
|
/*NodeTree<ASTData>* builtin_trans_unit; // the top scope for language level stuff*/
|
||||||
@@ -101,13 +101,20 @@ obj ast_transformation (Object) {
|
|||||||
fun transform_if_comp(node: *tree<symbol>, scope: *ast_node): *ast_node {
|
fun transform_if_comp(node: *tree<symbol>, scope: *ast_node): *ast_node {
|
||||||
var new_if_comp = ast_if_comp_ptr()
|
var new_if_comp = ast_if_comp_ptr()
|
||||||
new_if_comp->if_comp.wanted_generator = concat_symbol_tree(get_node("identifier", node))
|
new_if_comp->if_comp.wanted_generator = concat_symbol_tree(get_node("identifier", node))
|
||||||
|
new_if_comp->if_comp.statement = transform_statement(get_node("statement", node), scope)
|
||||||
return new_if_comp
|
return new_if_comp
|
||||||
}
|
}
|
||||||
fun transform_simple_passthrough(node: *tree<symbol>, scope: *ast_node): *ast_node {
|
fun transform_simple_passthrough(node: *tree<symbol>, scope: *ast_node): *ast_node {
|
||||||
var new_passthrough = ast_simple_passthrough_ptr()
|
var new_passthrough = ast_simple_passthrough_ptr()
|
||||||
// setup passthrough params and string
|
// setup passthrough params and string
|
||||||
|
new_passthrough->simple_passthrough.passthrough_str = concat_symbol_tree(get_node("triple_quoted_string", node))
|
||||||
return new_passthrough
|
return new_passthrough
|
||||||
}
|
}
|
||||||
|
fun transform_statement(node: *tree<symbol>, scope: *ast_node): *ast_node {
|
||||||
|
var new_statement = ast_statement_ptr()
|
||||||
|
new_statement->statement.child = transform(node->children[0], scope)
|
||||||
|
return new_statement
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun concat_symbol_tree(node: *tree<symbol>): string {
|
fun concat_symbol_tree(node: *tree<symbol>): string {
|
||||||
|
|||||||
54
stdlib/c_generator.krak
Normal file
54
stdlib/c_generator.krak
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import io:*
|
||||||
|
import mem:*
|
||||||
|
import map:*
|
||||||
|
import string:*
|
||||||
|
import util:*
|
||||||
|
import tree:*
|
||||||
|
import symbol:*
|
||||||
|
import ast_nodes:*
|
||||||
|
|
||||||
|
|
||||||
|
obj c_generator (Object) {
|
||||||
|
fun construct(): *c_generator {
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
fun copy_construct(old: *c_generator) {
|
||||||
|
}
|
||||||
|
fun operator=(other: ref c_generator) {
|
||||||
|
destruct()
|
||||||
|
copy_construct(&other)
|
||||||
|
}
|
||||||
|
fun destruct() {
|
||||||
|
}
|
||||||
|
fun generate(name_ast_map: map<string, pair<*tree<symbol>,*ast_node>>): pair<string,string> {
|
||||||
|
var linker_string:string = ""
|
||||||
|
var prequal: string = "#include <stdbool.h>\n#include <stdlib.h>\n#include <stdio.h>\n"
|
||||||
|
var plain_typedefs: string = ""
|
||||||
|
var top_level_c_passthrough: string = ""
|
||||||
|
var variable_extern_declarations: string = ""
|
||||||
|
var structs: string = ""
|
||||||
|
var function_typedef_string_pre: string = ""
|
||||||
|
var function_typedef_string: string = ""
|
||||||
|
var function_prototypes: string = ""
|
||||||
|
var variable_declarations: string = ""
|
||||||
|
|
||||||
|
|
||||||
|
// poset generation into structs string
|
||||||
|
// iterate through asts
|
||||||
|
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) {
|
||||||
|
// iterate through children for each ast
|
||||||
|
// assert translation_unit?
|
||||||
|
tree_pair.second->translation_unit.children.for_each(fun(child: *ast_node) {
|
||||||
|
match (*child) {
|
||||||
|
ast_node::if_comp(backing) top_level_c_passthrough += "got an if_comp\n"
|
||||||
|
ast_node::simple_passthrough(backing) top_level_c_passthrough += "got a simple_passthrough\n"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
var function_definitions: string = ""
|
||||||
|
return make_pair(prequal+plain_typedefs+top_level_c_passthrough+variable_extern_declarations+structs+function_typedef_string_pre+function_typedef_string+function_prototypes+variable_declarations+function_definitions, linker_string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -7,7 +7,7 @@ import util:*
|
|||||||
import string:*
|
import string:*
|
||||||
import mem:*
|
import mem:*
|
||||||
import io:*
|
import io:*
|
||||||
import ast_node:*
|
import ast_nodes:*
|
||||||
import ast_transformation:*
|
import ast_transformation:*
|
||||||
import parser:*
|
import parser:*
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import util:*
|
|||||||
import symbol:*
|
import symbol:*
|
||||||
import tree:*
|
import tree:*
|
||||||
import serialize:*
|
import serialize:*
|
||||||
|
import c_generator:*
|
||||||
|
|
||||||
fun main():int {
|
fun main():int {
|
||||||
|
|
||||||
@@ -72,10 +73,15 @@ fun main():int {
|
|||||||
/*println(ast_to_dot(ast))*/
|
/*println(ast_to_dot(ast))*/
|
||||||
/*write_file(string("ast.dot"), ast_to_dot(ast))*/
|
/*write_file(string("ast.dot"), ast_to_dot(ast))*/
|
||||||
|
|
||||||
|
var kraken_file_name = string("to_parse.krak")
|
||||||
var parse.construct(gram): parser
|
var parse.construct(gram): parser
|
||||||
var ast_pass.construct(): ast_transformation
|
var ast_pass.construct(): ast_transformation
|
||||||
var importer.construct(parse, ast_pass): importer
|
var importer.construct(parse, ast_pass): importer
|
||||||
importer.import(string("to_parse.krak"))
|
importer.import(kraken_file_name)
|
||||||
|
var c_generator.construct(): c_generator
|
||||||
|
var c_output_pair = c_generator.generate(importer.name_ast_map)
|
||||||
|
write_file(kraken_file_name + ".c", c_output_pair.first)
|
||||||
|
println(string("linker string: ") + c_output_pair.second)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user