fix reference type inference

This commit is contained in:
Nathan Braswell
2015-07-15 13:56:57 -04:00
parent 06f36f2a87
commit 51adf491fa
8 changed files with 65 additions and 30 deletions

View File

@@ -709,7 +709,8 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
// for type inferencing // for type inferencing
if (!identifierType) { if (!identifierType) {
if (toAssign) { if (toAssign) {
identifierType = toAssign->getDataRef()->valueType; // no reference variables
identifierType = toAssign->getDataRef()->valueType->withoutReference();
newIdentifier->getDataRef()->valueType = identifierType; newIdentifier->getDataRef()->valueType = identifierType;
} else } else
throw "have to inference but no expression"; throw "have to inference but no expression";

View File

@@ -568,7 +568,7 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
output.value += ValueTypeToCType(children[0]->getData().valueType, assignedTo) + ";\n"; output.value += ValueTypeToCType(children[0]->getData().valueType, assignedTo) + ";\n";
// we put the thing about to be copy constructed in a variable so we can for sure take its address // we put the thing about to be copy constructed in a variable so we can for sure take its address
std::string toAssignTemp = "copy_construct_param" + getID(); std::string toAssignTemp = "copy_construct_param" + getID();
output.value += ValueTypeToCType(children[1]->getData().valueType, toAssignTemp) + " = " + toAssign.value + ";\n"; output.value += ValueTypeToCType(children[1]->getData().valueType->withoutReference(), toAssignTemp) + " = " + toAssign.value + ";\n";
output.value += generateMethodIfExists(children[0]->getDataRef()->valueType, "copy_construct", "&" + assignedTo + ", &" + toAssignTemp, std::vector<Type>{children[1]->getDataRef()->valueType->withIncreasedIndirection()}) + ";\n" + output.postValue; output.value += generateMethodIfExists(children[0]->getDataRef()->valueType, "copy_construct", "&" + assignedTo + ", &" + toAssignTemp, std::vector<Type>{children[1]->getDataRef()->valueType->withIncreasedIndirection()}) + ";\n" + output.postValue;
output.value += toAssign.postValue; output.value += toAssign.postValue;

View File

@@ -139,42 +139,53 @@ obj grammer (Object) {
) )
var first_helper = fun(rhs: vector::vector<symbol::symbol>): set::set<symbol::symbol> { var first_helper = fun(rhs: vector::vector<symbol::symbol>): set::set<symbol::symbol> {
var toRet = set::set<symbol::symbol>() var toRet = set::set<symbol::symbol>()
rhs.for_each(fun(sym: symbol::symbol) { if (rhs.size) {
toRet.add(first_set_map[sym]) for (var i = 0; i < rhs.size; i++;) {
}) var lookahead = first_set_map[rhs[i]]
if (lookahead.contains(symbol::null_symbol())) {
lookahead.remove(symbol::null_symbol())
toRet.add(lookahead)
} else {
toRet.add(lookahead)
break
}
}
} else {
toRet.add(symbol::null_symbol())
}
return toRet return toRet
} }
var changed = true var changed = true
while (changed) { while (changed) {
io::println("//////////current state of map/////////////") /*io::println("//////////current state of map/////////////")*/
first_set_map.keys.for_each(fun(sym: symbol::symbol) { first_set_map.keys.for_each(fun(sym: symbol::symbol) {
io::print("for ") /*io::print("for ")*/
io::println(sym.to_string()) /*io::println(sym.to_string())*/
io::println("map is:") /*io::println("map is:")*/
first_set_map[sym].for_each(fun(look: symbol::symbol) { /*first_set_map[sym].for_each(fun(look: symbol::symbol) {*/
io::print("lookahead: "); io::println(look.to_string()) /*io::print("lookahead: "); io::println(look.to_string())*/
}) /*})*/
}) })
changed = false changed = false
rules.for_each( fun(r: rule) { rules.for_each( fun(r: rule) {
var rule_lookahead = first_helper(r.rhs) var rule_lookahead = first_helper(r.rhs)
if (!changed) { if (!changed) {
io::println(r.to_string()) /*io::println(r.to_string())*/
changed = !first_set_map[r.lhs].contains(rule_lookahead) changed = !first_set_map[r.lhs].contains(rule_lookahead)
io::print("changed: "); io::println(changed) /*io::print("changed: "); io::println(changed)*/
io::print("\tcurrent lookahead is sized:") /*io::print("\tcurrent lookahead is sized:")*/
io::println(first_set_map[r.lhs].size()) /*io::println(first_set_map[r.lhs].size())*/
io::println("\tcurrent lookahead is:") /*io::println("\tcurrent lookahead is:")*/
first_set_map[r.lhs].for_each(fun(look: symbol::symbol) { /*first_set_map[r.lhs].for_each(fun(look: symbol::symbol) {*/
io::print("\t\tlookahead: "); io::println(look.to_string()) /*io::print("\t\tlookahead: "); io::println(look.to_string())*/
}) /*})*/
io::println() /*io::println()*/
io::print("\rule lookahead is sized:") /*io::print("\rule lookahead is sized:")*/
io::println(rule_lookahead.size()) /*io::println(rule_lookahead.size())*/
io::println("\trule lookahead is:") /*io::println("\trule lookahead is:")*/
rule_lookahead.for_each(fun(look: symbol::symbol) { /*rule_lookahead.for_each(fun(look: symbol::symbol) {*/
io::print("\t\tlookahead: "); io::println(look.to_string()) /*io::print("\t\tlookahead: "); io::println(look.to_string())*/
}) /*})*/
} }
first_set_map[r.lhs].add(rule_lookahead) first_set_map[r.lhs].add(rule_lookahead)
}) })

View File

@@ -1,5 +1,10 @@
import string import string
fun null_symbol(): symbol {
var toRet.construct(string::string("$NULL"), false, string::string("$NULL$")): symbol
return toRet
}
fun symbol(nameIn: *char, terminalIn: bool): symbol { fun symbol(nameIn: *char, terminalIn: bool): symbol {
var toRet.construct(string::string(nameIn), terminalIn, string::string("no_value")): symbol var toRet.construct(string::string(nameIn), terminalIn, string::string("no_value")): symbol
return toRet return toRet

10
tests/grammer2.kgm Normal file
View File

@@ -0,0 +1,10 @@
# comment
a = b ;
b = "c":named_c ;
b = c "d":dname ;
c = "a" | d ;
d = e post_null post_non_null ;
e = f | ;
f = ;
post_null = "hi"
post_non_null = "bye"

View File

@@ -8,7 +8,8 @@ import symbol:*
fun main():int { fun main():int {
/*var a = load_grammer(read_file(string("../krakenGrammer.kgm")))*/ /*var a = load_grammer(read_file(string("../krakenGrammer.kgm")))*/
var a = load_grammer(read_file(string("grammer.kgm"))) /*var a = load_grammer(read_file(string("grammer.kgm")))*/
var a = load_grammer(read_file(string("grammer2.kgm")))
println(a.to_string()) println(a.to_string())
var doFirstSet = fun() { var doFirstSet = fun() {
a.calculate_first_set() a.calculate_first_set()
@@ -39,8 +40,9 @@ fun main():int {
var lex = lexer(a.terminals) var lex = lexer(a.terminals)
/*lex.set_input(read_file(string("test_grammer.krak")))*/ /*lex.set_input(read_file(string("test_grammer.krak")))*/
lex.set_input(string("ccdahas spacedhas /*lex.set_input(string("ccdahas spacedhas*/
returndaaaaaaaaaaaaaa")) /*returndaaaaaaaaaaaaaa"))*/
lex.set_input(string("hibyed"))
println("woo lexing:") println("woo lexing:")
range(8).for_each(fun(i: int) { println(lex.next().to_string()); } ) range(8).for_each(fun(i: int) { println(lex.next().to_string()); } )
/*range(80).for_each(fun(i: int) { println(lex.next().to_string()); } )*/ /*range(80).for_each(fun(i: int) { println(lex.next().to_string()); } )*/

View File

@@ -5,4 +5,7 @@
construct construct
do do
do do
copy_construct
do
destruct
destruct destruct

View File

@@ -59,6 +59,9 @@ fun main():int {
var t.construct() : test_cons var t.construct() : test_cons
call(t) call(t)
id(t).do() id(t).do()
var do_copy = id(t)
do_copy.do()
return 0 return 0
} }