Make --parse-only not highlight and redo format of parse errors so that kraken.vim syntax checking works, fix naming to allow multiple instantiations of object templates, fix so that template type replacements go through to bodies of methods of object templates in the fourth_pass
This commit is contained in:
@@ -28,7 +28,7 @@ class Parser {
|
|||||||
virtual void loadGrammer(std::string grammerInputString);
|
virtual void loadGrammer(std::string grammerInputString);
|
||||||
virtual void createStateSet();
|
virtual void createStateSet();
|
||||||
virtual std::string stateSetToString();
|
virtual std::string stateSetToString();
|
||||||
virtual NodeTree<Symbol>* parseInput(std::string inputString, std::string filename) = 0; // filename for error reporting
|
virtual NodeTree<Symbol>* parseInput(std::string inputString, std::string filename, bool highlight_errors) = 0; // filename for error reporting
|
||||||
virtual std::string grammerToString();
|
virtual std::string grammerToString();
|
||||||
virtual std::string grammerToDOT();
|
virtual std::string grammerToDOT();
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class RNGLRParser: public Parser {
|
|||||||
public:
|
public:
|
||||||
RNGLRParser();
|
RNGLRParser();
|
||||||
~RNGLRParser();
|
~RNGLRParser();
|
||||||
NodeTree<Symbol>* parseInput(std::string inputString, std::string filename); // filename for error reporting
|
NodeTree<Symbol>* parseInput(std::string inputString, std::string filename, bool highlight_errors); // filename for error reporting
|
||||||
void printReconstructedFrontier(int frontier);
|
void printReconstructedFrontier(int frontier);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ NodeTree<Symbol>* Importer::parseAndTrim(std::string fileName) {
|
|||||||
programInFile.close();
|
programInFile.close();
|
||||||
|
|
||||||
//std::cout << programInputFileString << std::endl;
|
//std::cout << programInputFileString << std::endl;
|
||||||
NodeTree<Symbol>* parseTree = parser->parseInput(programInputFileString, inputFileName);
|
NodeTree<Symbol>* parseTree = parser->parseInput(programInputFileString, inputFileName, !only_parse);
|
||||||
|
|
||||||
if (parseTree) {
|
if (parseTree) {
|
||||||
//std::cout << parseTree->DOTGraphString() << std::endl;
|
//std::cout << parseTree->DOTGraphString() << std::endl;
|
||||||
@@ -208,6 +208,8 @@ NodeTree<Symbol>* Importer::parseAndTrim(std::string fileName) {
|
|||||||
throw "unexceptablblllll";
|
throw "unexceptablblllll";
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
if (only_parse)
|
||||||
|
return parseTree;
|
||||||
//outFile.close();
|
//outFile.close();
|
||||||
|
|
||||||
//Remove Transformations
|
//Remove Transformations
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ void RNGLRParser::printReconstructedFrontier(int frontier) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString, std::string filename) {
|
NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString, std::string filename, bool highlight_errors) {
|
||||||
input.clear();
|
input.clear();
|
||||||
gss.clear();
|
gss.clear();
|
||||||
while(!toReduce.empty()) toReduce.pop();
|
while(!toReduce.empty()) toReduce.pop();
|
||||||
@@ -118,21 +118,28 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString, std::string f
|
|||||||
//std::cout << "Frontier " << i << " is empty." << std::endl;
|
//std::cout << "Frontier " << i << " is empty." << std::endl;
|
||||||
//std::cerr << "Parsing failed on " << input[i].toString() << std::endl;
|
//std::cerr << "Parsing failed on " << input[i].toString() << std::endl;
|
||||||
//std::cerr << "Problem is on line: " << findLine(i) << std::endl;
|
//std::cerr << "Problem is on line: " << findLine(i) << std::endl;
|
||||||
// std::cerr << filename << ":" << findLine(i) << std::endl;
|
// std::cerr << filename << ":" << findLine(i) << std::endl;
|
||||||
errord = true;
|
errord = true;
|
||||||
std::cout << BOLDMAGENTA << "parse error" << std::endl;
|
if (highlight_errors)
|
||||||
|
std::cout << BOLDBLUE;
|
||||||
std::cout << BOLDWHITE << "Error at: " << BOLDBLUE << filename << ":" << findLine(i) << std::endl;
|
std::cout << filename << ":" << findLine(i) << std::endl;
|
||||||
|
if (highlight_errors)
|
||||||
|
std::cout << BOLDMAGENTA;
|
||||||
|
std::cout << ": parse error" << std::endl;
|
||||||
|
|
||||||
std::ifstream infile(filename);
|
std::ifstream infile(filename);
|
||||||
std::string line;
|
std::string line;
|
||||||
int linecount = 0;
|
int linecount = 0;
|
||||||
while(std::getline(infile,line))
|
while(std::getline(infile,line))
|
||||||
{
|
{
|
||||||
if(linecount == findLine(i) - 1)
|
if(linecount == findLine(i) - 1) {
|
||||||
std::cout << BOLDRED << line << std::endl;
|
if (highlight_errors)
|
||||||
|
std::cout << BOLDRED;
|
||||||
|
std::cout << line << std::endl;
|
||||||
|
}
|
||||||
linecount++;
|
linecount++;
|
||||||
}
|
}
|
||||||
|
if (highlight_errors)
|
||||||
std::cout << RESET << std::endl;
|
std::cout << RESET << std::endl;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -204,8 +204,13 @@ obj ast_transformation (Object) {
|
|||||||
while (!fourth_pass_worklist.empty()) {
|
while (!fourth_pass_worklist.empty()) {
|
||||||
var partially_inst_type_def = fourth_pass_worklist.pop()
|
var partially_inst_type_def = fourth_pass_worklist.pop()
|
||||||
partially_inst_type_def->type_def.methods.for_each(fun(method: *ast_node) {
|
partially_inst_type_def->type_def.methods.for_each(fun(method: *ast_node) {
|
||||||
// this is the wrong map
|
var template = partially_inst_type_def->type_def.scope[string("~enclosing_scope")][0]
|
||||||
method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, map<string, *type>())
|
var template_types = template->template.template_types
|
||||||
|
var real_types = template->template.instantiated_map.reverse_get(partially_inst_type_def)
|
||||||
|
var replacements = map<string, *type>()
|
||||||
|
for (var i = 0; i < template_types.size; i++;)
|
||||||
|
replacements.set(template_types[i], real_types[i].clone())
|
||||||
|
method->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[method]), method, replacements)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,7 +118,6 @@ obj c_generator (Object) {
|
|||||||
var defer_stack = stack<pair<bool,stack<*ast_node>>>(make_pair(false, stack<*ast_node>()))
|
var defer_stack = stack<pair<bool,stack<*ast_node>>>(make_pair(false, stack<*ast_node>()))
|
||||||
|
|
||||||
var decorated_name = generate_function(child).one_string()
|
var decorated_name = generate_function(child).one_string()
|
||||||
// also add in name decoration
|
|
||||||
backing.parameters.for_each(fun(parameter: *ast_node) {
|
backing.parameters.for_each(fun(parameter: *ast_node) {
|
||||||
if (parameter_types != "") { parameter_types += ", "; parameters += ", ";}
|
if (parameter_types != "") { parameter_types += ", "; parameters += ", ";}
|
||||||
parameter_types += type_to_c(parameter->identifier.type)
|
parameter_types += type_to_c(parameter->identifier.type)
|
||||||
@@ -183,8 +182,10 @@ obj c_generator (Object) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
type_poset.get_sorted().for_each(fun(vert: *ast_node) {
|
type_poset.get_sorted().for_each(fun(vert: *ast_node) {
|
||||||
plain_typedefs += string("typedef struct ") + vert->type_def.name + "_dummy " + vert->type_def.name + ";\n"
|
/*var base_name = vert->type_def.name*/
|
||||||
structs += string("struct ") + vert->type_def.name + "_dummy {\n"
|
var base_name = get_name(vert)
|
||||||
|
plain_typedefs += string("typedef struct ") + base_name + "_dummy " + base_name + ";\n"
|
||||||
|
structs += string("struct ") + base_name + "_dummy {\n"
|
||||||
vert->type_def.variables.for_each(fun(variable_declaration: *ast_node) structs += generate_declaration_statement(variable_declaration, null<ast_node>(), null<stack<pair<bool,stack<*ast_node>>>>(), true).one_string() + ";\n";)
|
vert->type_def.variables.for_each(fun(variable_declaration: *ast_node) structs += generate_declaration_statement(variable_declaration, null<ast_node>(), null<stack<pair<bool,stack<*ast_node>>>>(), true).one_string() + ";\n";)
|
||||||
structs += "};\n"
|
structs += "};\n"
|
||||||
// generate the methods
|
// generate the methods
|
||||||
@@ -317,9 +318,7 @@ obj c_generator (Object) {
|
|||||||
}
|
}
|
||||||
// this generates the function as a value, not the actual function
|
// this generates the function as a value, not the actual function
|
||||||
fun generate_function(node: *ast_node): code_triple {
|
fun generate_function(node: *ast_node): code_triple {
|
||||||
var str = code_triple(node->function.name)
|
return code_triple(get_name(node))
|
||||||
node->function.parameters.for_each(fun(param: *ast_node) str += string("_") + type_decoration(param->identifier.type);)
|
|
||||||
return str
|
|
||||||
}
|
}
|
||||||
fun generate_function_call(node: *ast_node, enclosing_object: *ast_node): code_triple {
|
fun generate_function_call(node: *ast_node, enclosing_object: *ast_node): code_triple {
|
||||||
var func_name = string()
|
var func_name = string()
|
||||||
@@ -461,7 +460,7 @@ obj c_generator (Object) {
|
|||||||
base_type::floating() return string("float") + indirection
|
base_type::floating() return string("float") + indirection
|
||||||
base_type::double_precision() return string("double") + indirection
|
base_type::double_precision() return string("double") + indirection
|
||||||
base_type::object() {
|
base_type::object() {
|
||||||
return type->type_def->type_def.name + indirection
|
return get_name(type->type_def) + indirection
|
||||||
}
|
}
|
||||||
base_type::function() {
|
base_type::function() {
|
||||||
var temp = indirection + string("function: (")
|
var temp = indirection + string("function: (")
|
||||||
@@ -471,6 +470,28 @@ obj c_generator (Object) {
|
|||||||
}
|
}
|
||||||
return string("impossible type") + indirection
|
return string("impossible type") + indirection
|
||||||
}
|
}
|
||||||
|
fun get_name(node: *ast_node): string {
|
||||||
|
match (*node) {
|
||||||
|
ast_node::type_def(backing) {
|
||||||
|
var upper = backing.scope[string("~enclosing_scope")][0]
|
||||||
|
var result = backing.name
|
||||||
|
if (is_template(upper))
|
||||||
|
upper->template.instantiated_map.reverse_get(node).for_each(fun(t: ref type) result += string("_") + type_decoration(&t);)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
ast_node::function(backing) {
|
||||||
|
// be careful, operators like . come through this
|
||||||
|
var upper = backing.scope.get_with_default(string("~enclosing_scope"), vector(null<ast_node>()))[0]
|
||||||
|
var str = string()
|
||||||
|
if (upper && is_type_def(upper))
|
||||||
|
str += get_name(upper)
|
||||||
|
str += node->function.name
|
||||||
|
node->function.parameters.for_each(fun(param: *ast_node) str += string("_") + type_decoration(param->identifier.type);)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string("impossible name")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -74,6 +74,20 @@ obj map<T,U> (Object, Serializable) {
|
|||||||
}
|
}
|
||||||
return values.get(key_loc)
|
return values.get(key_loc)
|
||||||
}
|
}
|
||||||
|
fun get_with_default(key: T, default_val: ref U): ref U {
|
||||||
|
if (contains_key(key))
|
||||||
|
return get(key)
|
||||||
|
return default_val
|
||||||
|
}
|
||||||
|
fun reverse_get(value: U): ref T {
|
||||||
|
/*return values.get(keys.find(key))*/
|
||||||
|
var value_loc = values.find(value)
|
||||||
|
if (value_loc == -1) {
|
||||||
|
io::println("trying to access nonexistant value-key!")
|
||||||
|
while (true) {}
|
||||||
|
}
|
||||||
|
return keys.get(value_loc)
|
||||||
|
}
|
||||||
fun remove(key: T) {
|
fun remove(key: T) {
|
||||||
var idx = keys.find(key)
|
var idx = keys.find(key)
|
||||||
if (idx < 0) {
|
if (idx < 0) {
|
||||||
|
|||||||
@@ -150,6 +150,7 @@ obj type (Object) {
|
|||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
fun clone(): *type return clone_with_indirection(indirection);
|
||||||
fun clone_with_increased_indirection(): *type return clone_with_indirection(indirection+1);
|
fun clone_with_increased_indirection(): *type return clone_with_indirection(indirection+1);
|
||||||
fun clone_with_increased_indirection(more: int): *type return clone_with_indirection(indirection+more);
|
fun clone_with_increased_indirection(more: int): *type return clone_with_indirection(indirection+more);
|
||||||
fun clone_with_decreased_indirection(): *type return clone_with_indirection(indirection-1);
|
fun clone_with_decreased_indirection(): *type return clone_with_indirection(indirection-1);
|
||||||
|
|||||||
1
tests/test_obj_create_scope.expected_results
Normal file
1
tests/test_obj_create_scope.expected_results
Normal file
@@ -0,0 +1 @@
|
|||||||
|
7
|
||||||
23
tests/test_obj_create_scope.krak
Normal file
23
tests/test_obj_create_scope.krak
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import simple_print:*
|
||||||
|
|
||||||
|
obj ConTest {
|
||||||
|
var a: int
|
||||||
|
fun construct(a: int): *ConTest {
|
||||||
|
ConTest::a = a
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main(): int {
|
||||||
|
var b.construct(7): ConTest
|
||||||
|
println(b.a)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -56,11 +56,21 @@ fun some_other_function(in: bool): float {
|
|||||||
*/
|
*/
|
||||||
obj SimpleContainer<T> {
|
obj SimpleContainer<T> {
|
||||||
var data: T
|
var data: T
|
||||||
|
fun print_data() {
|
||||||
|
var indirection: T
|
||||||
|
indirection = data
|
||||||
|
println(indirection)
|
||||||
|
}
|
||||||
|
fun construct(dataIn: T) data = dataIn
|
||||||
}
|
}
|
||||||
fun main(): int {
|
fun main(): int {
|
||||||
var it: SimpleContainer<*char>
|
var it: SimpleContainer<*char>
|
||||||
it.data = "Wooo object template"
|
it.data = "Wooo object template"
|
||||||
println(it.data)
|
println(it.data)
|
||||||
|
it.data = "Wooo object template methods"
|
||||||
|
it.print_data()
|
||||||
|
var it2.construct(3): SimpleContainer<int>
|
||||||
|
it2.print_data()
|
||||||
/*println(other_id("Wooo function template inference"))*/
|
/*println(other_id("Wooo function template inference"))*/
|
||||||
/*var a = id<int>(7)*/
|
/*var a = id<int>(7)*/
|
||||||
/*println(a)*/
|
/*println(a)*/
|
||||||
|
|||||||
Reference in New Issue
Block a user