make grammer/parser use simple adts, fix it so adt literals aren't closed over by accident

This commit is contained in:
Nathan Braswell
2015-08-30 01:53:11 -04:00
parent 5f3f3e5a66
commit 13c6044193
4 changed files with 39 additions and 29 deletions

View File

@@ -1002,7 +1002,10 @@ std::set<NodeTree<ASTData>*> ASTTransformation::findVariablesToClose(NodeTree<AS
closed.insert(recClosed.begin(), recClosed.end());
return closed;
}
if (stat->getDataRef()->type == identifier && !inScopeChain(stat, func))
// if it's an identifier and not in the scope chain, and isn't an enum name
if (stat->getDataRef()->type == identifier && !inScopeChain(stat, func) &&
(!stat->getDataRef()->valueType->typeDefinition ||
stat->getDataRef()->valueType->typeDefinition->getDataRef()->type != adt_def) )
closed.insert(stat);
for (auto child: stat->getChildren()) {
auto recClosed = findVariablesToClose(func, child, scope);

View File

@@ -479,22 +479,25 @@ obj state (Object) {
}
}
// REALLY need those enums
var push = 0
var reduce = 1
adt action_type {
push,
reduce,
// note that these two are not actually currently used
// accept is the reduce of the goal rule and reject is the
// absence of actions
var accept = 2
var reject = 3
fun action(act: int, state_or_rule: int): action {
accept,
reject,
invalid
}
fun action(act: action_type, state_or_rule: int): action {
var toRet: action
toRet.act = act
toRet.state_or_rule = state_or_rule
toRet.rule_position = -1
return toRet
}
fun action(act: int, state_or_rule: int, rule_position: int): action {
fun action(act: action_type, state_or_rule: int, rule_position: int): action {
var toRet: action
toRet.act = act
toRet.state_or_rule = state_or_rule
@@ -502,20 +505,20 @@ fun action(act: int, state_or_rule: int, rule_position: int): action {
return toRet
}
obj action {
var act: int // really need those enums
var act: action_type
var state_or_rule: int // sigh
var rule_position: int // sigh
fun operator==(other: action): bool {
return act == other.act && state_or_rule == other.state_or_rule && rule_position == other.rule_position
}
fun print() {
if (act == push)
if (act == action_type::push)
io::print("push ")
else if (act == reduce)
else if (act == action_type::reduce)
io::print("reduce ")
else if (act == accept)
else if (act == action_type::accept)
io::print("accept ")
else if (act == reject)
else if (act == action_type::reject)
io::print("reject ")
io::print(state_or_rule)
io::print(" ")
@@ -563,25 +566,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(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(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(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(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(accept, 0))
items[from_state][cleaned_symbol].addEnd(action(action_type::accept, 0))
else
items[from_state].set(cleaned_symbol, vector::vector(action(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)
@@ -590,17 +593,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 == 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(-1,-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 == 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(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,10 +92,10 @@ obj parser (Object) {
gram.parse_table.get(0, input[0]).for_each(fun(act: action) {
println("for each action")
act.print()
if (act.act == 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 == reduce && act.rule_position == 0) {
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 == 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 == 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 == 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

@@ -5,8 +5,12 @@ adt options {
option1
}
fun can_pass(it: options): options {
return options::option1
}
fun main():int {
var it: options = options::option1
var it: options = can_pass(options::option0)
if (it == options::option0)
println("nope")
if (it == options::option1)