Make the grammer test work again by updating grammer and parser to use the new adt syntax - I also messed up commenting out one of the cout lines making lambdas not work.

This commit is contained in:
Nathan Braswell
2015-11-14 20:13:42 -05:00
parent e7a49bf2e5
commit 466b2310db
4 changed files with 38 additions and 28 deletions

View File

@@ -555,7 +555,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
auto statement = transform(getNode("statement", children), scope, types, false, templateTypeReplacements); // definitly do not limit this statement to functions
if (name == "lambda")
newNode->getDataRef()->closedVariables = findVariablesToClose(newNode, statement, scope);
for (auto i : newNode->getDataRef()->closedVariables)
//for (auto i : newNode->getDataRef()->closedVariables)
//std::cout << "OK, CLOSED: " << i->getDataRef()->toString() << std::endl;
newNode->addChild(statement);
//std::cout << "finished function" << functionName << std::endl;

View File

@@ -512,14 +512,24 @@ obj action {
return act == other.act && state_or_rule == other.state_or_rule && rule_position == other.rule_position
}
fun print() {
if (act == action_type::push)
io::print("push ")
else if (act == action_type::reduce)
io::print("reduce ")
else if (act == action_type::accept)
io::print("accept ")
else if (act == action_type::reject)
io::print("reject ")
match (act) {
action_type::push()
io::print("push ")
action_type::reduce()
io::print("reduce ")
action_type::accept()
io::print("accept ")
action_type::reject()
io::print("reject ")
}
/*if (act == action_type::push)*/
/*io::print("push ")*/
/*else if (act == action_type::reduce)*/
/*io::print("reduce ")*/
/*else if (act == action_type::accept)*/
/*io::print("accept ")*/
/*else if (act == action_type::reject)*/
/*io::print("reject ")*/
io::print(state_or_rule)
io::print(" ")
io::print(rule_position)
@@ -566,25 +576,25 @@ obj table (Object, Serializable) {
expand_to(from_state)
var cleaned_symbol = clean_symbol(on_symbol)
if (items[from_state].contains_key(cleaned_symbol))
items[from_state][cleaned_symbol].addEnd(action(action_type::push, to_state))
items[from_state][cleaned_symbol].addEnd(action(action_type::push(), to_state))
else
items[from_state].set(cleaned_symbol, vector::vector(action(action_type::push, to_state)))
items[from_state].set(cleaned_symbol, vector::vector(action(action_type::push(), to_state)))
}
fun add_reduce(from_state: int, on_symbol: ref symbol::symbol, by_rule_no: int, rule_position: int) {
expand_to(from_state)
var cleaned_symbol = clean_symbol(on_symbol)
if (items[from_state].contains_key(cleaned_symbol))
items[from_state][cleaned_symbol].addEnd(action(action_type::reduce, by_rule_no, rule_position))
items[from_state][cleaned_symbol].addEnd(action(action_type::reduce(), by_rule_no, rule_position))
else
items[from_state].set(cleaned_symbol, vector::vector(action(action_type::reduce, by_rule_no, rule_position)))
items[from_state].set(cleaned_symbol, vector::vector(action(action_type::reduce(), by_rule_no, rule_position)))
}
fun add_accept(from_state: int, on_symbol: ref symbol::symbol) {
expand_to(from_state)
var cleaned_symbol = clean_symbol(on_symbol)
if (items[from_state].contains_key(cleaned_symbol))
items[from_state][cleaned_symbol].addEnd(action(action_type::accept, 0))
items[from_state][cleaned_symbol].addEnd(action(action_type::accept(), 0))
else
items[from_state].set(cleaned_symbol, vector::vector(action(action_type::accept, 0)))
items[from_state].set(cleaned_symbol, vector::vector(action(action_type::accept(), 0)))
}
fun get(state: int, on_symbol: symbol::symbol): vector::vector<action> {
var cleaned_symbol = clean_symbol(on_symbol)
@@ -593,17 +603,17 @@ obj table (Object, Serializable) {
fun get_shift(state: int, on_symbol: symbol::symbol): action {
var actions = get(state, on_symbol)
for (var i = 0; i < actions.size; i++;)
if (actions[i].act == action_type::push)
if (actions[i].act == action_type::push())
return actions[i]
io::println("tried to get a shift when none existed")
io::print("for state ")
io::print(state)
io::print(" and symbol ")
io::println(on_symbol.to_string())
return action(action_type::invalid,-1)
return action(action_type::invalid(),-1)
}
fun get_reduces(state: int, on_symbol: symbol::symbol): vector::vector<action> {
return get(state, on_symbol).filter(fun(act: action):bool { return act.act == action_type::reduce; })
return get(state, on_symbol).filter(fun(act: action):bool { return act.act == action_type::reduce(); })
}
fun print_string() {
/*return string::string("woo a table of size: ") + items.size*/

View File

@@ -63,7 +63,7 @@ obj parser (Object) {
// if the zero state contains any reductions for state 0 and eof, then
// it must be reducing to the goal state
println("checking the bidness")
if (inputStr == "" && gram.parse_table.get(0, eof_symbol()).contains(action(action_type::reduce, 0))) {
if (inputStr == "" && gram.parse_table.get(0, eof_symbol()).contains(action(action_type::reduce(), 0))) {
println("Accept on no input for ")
println(name)
return new<tree<symbol>>()->construct(null_symbol())
@@ -92,11 +92,11 @@ obj parser (Object) {
gram.parse_table.get(0, input[0]).for_each(fun(act: action) {
println("for each action")
act.print()
if (act.act == action_type::push)
if (act.act == action_type::push())
to_shift.push(make_pair(v0, act.state_or_rule))
/*else if (act.act == reduce && fully_reduces_to_null(gram.rules[act.state_or_rule])) {*/
else if (act.act == action_type::reduce && act.rule_position == 0) {
print("act == reduce && == 0 Adding reduction from state: ")
else if (act.act == action_type::reduce() && act.rule_position == 0) {
print("act == reduce() && == 0 Adding reduction from state: ")
println(v0->data)
to_reduce.push(reduction(v0, gram.rules[act.state_or_rule].lhs, 0, null_symbol_tree, null_symbol_tree))
}
@@ -197,7 +197,7 @@ obj parser (Object) {
gram.parse_table.get(shift_to, input[i]).for_each(fun(act: action) {
var reduce_rule = gram.rules[act.state_or_rule]
/*if (act.act == reduce && !fully_reduces_to_null(reduce_rule)) {*/
if (act.act == action_type::reduce && act.rule_position != 0) {
if (act.act == action_type::reduce() && act.rule_position != 0) {
to_reduce.push(reduction(curr_reached, reduce_rule.lhs,
act.rule_position,
get_nullable_parts(reduce_rule),
@@ -213,7 +213,7 @@ obj parser (Object) {
gss.add_to_frontier(i, shift_to_node)
gss.add_edge(shift_to_node, curr_reached, new_label)
gram.parse_table.get(shift_to, input[i]).for_each(fun(act: action) {
if (act.act == action_type::push) {
if (act.act == action_type::push()) {
to_shift.push(make_pair(shift_to_node, act.state_or_rule))
} else {
var action_rule = gram.rules[act.state_or_rule]
@@ -278,7 +278,7 @@ obj parser (Object) {
println("post add edger")
gram.parse_table.get(shift.second, input[i+1]).for_each(fun(action: action) {
println("looking at an action")
if (action.act == action_type::push) {
if (action.act == action_type::push()) {
println("is push")
next_shifts.push(make_pair(shift_to_node, action.state_or_rule))
} else {

View File

@@ -33,7 +33,7 @@ obj regexState (Object) {
fun destruct():void {
next_states.destruct()
}
fun match(input: char): vector::vector<*regexState> {
fun match_char(input: char): vector::vector<*regexState> {
return next_states.filter(fun(it:*regexState):bool { return it->character == input; })
}
fun is_end():bool {
@@ -190,8 +190,8 @@ obj regex (Object, Serializable) {
return longest
if (next.any_true(fun(state: *regexState):bool { return state->is_end(); }))
longest = i
//next = next.flatten_map<*regexState>(fun(state: *regexState): vector::vector<*regexState> { return state->match(to_match[i]); })
next = next.flatten_map(fun(state: *regexState): vector::vector<*regexState> { return state->match(to_match[i]); })
//next = next.flatten_map<*regexState>(fun(state: *regexState): vector::vector<*regexState> { return state->match_char(to_match[i]); })
next = next.flatten_map(fun(state: *regexState): vector::vector<*regexState> { return state->match_char(to_match[i]); })
}
if (next.any_true(fun(state: *regexState):bool { return state->is_end(); }))
return to_match.length()