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()); closed.insert(recClosed.begin(), recClosed.end());
return closed; 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); closed.insert(stat);
for (auto child: stat->getChildren()) { for (auto child: stat->getChildren()) {
auto recClosed = findVariablesToClose(func, child, scope); auto recClosed = findVariablesToClose(func, child, scope);

View File

@@ -479,22 +479,25 @@ obj state (Object) {
} }
} }
// REALLY need those enums adt action_type {
var push = 0 push,
var reduce = 1 reduce,
// note that these two are not actually currently used // note that these two are not actually currently used
// accept is the reduce of the goal rule and reject is the // accept is the reduce of the goal rule and reject is the
// absence of actions // absence of actions
var accept = 2 accept,
var reject = 3 reject,
fun action(act: int, state_or_rule: int): action { invalid
}
fun action(act: action_type, state_or_rule: int): action {
var toRet: action var toRet: action
toRet.act = act toRet.act = act
toRet.state_or_rule = state_or_rule toRet.state_or_rule = state_or_rule
toRet.rule_position = -1 toRet.rule_position = -1
return toRet 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 var toRet: action
toRet.act = act toRet.act = act
toRet.state_or_rule = state_or_rule 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 return toRet
} }
obj action { obj action {
var act: int // really need those enums var act: action_type
var state_or_rule: int // sigh var state_or_rule: int // sigh
var rule_position: int // sigh var rule_position: int // sigh
fun operator==(other: action): bool { fun operator==(other: action): bool {
return act == other.act && state_or_rule == other.state_or_rule && rule_position == other.rule_position return act == other.act && state_or_rule == other.state_or_rule && rule_position == other.rule_position
} }
fun print() { fun print() {
if (act == push) if (act == action_type::push)
io::print("push ") io::print("push ")
else if (act == reduce) else if (act == action_type::reduce)
io::print("reduce ") io::print("reduce ")
else if (act == accept) else if (act == action_type::accept)
io::print("accept ") io::print("accept ")
else if (act == reject) else if (act == action_type::reject)
io::print("reject ") io::print("reject ")
io::print(state_or_rule) io::print(state_or_rule)
io::print(" ") io::print(" ")
@@ -563,25 +566,25 @@ obj table (Object, Serializable) {
expand_to(from_state) expand_to(from_state)
var cleaned_symbol = clean_symbol(on_symbol) var cleaned_symbol = clean_symbol(on_symbol)
if (items[from_state].contains_key(cleaned_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 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) { fun add_reduce(from_state: int, on_symbol: ref symbol::symbol, by_rule_no: int, rule_position: int) {
expand_to(from_state) expand_to(from_state)
var cleaned_symbol = clean_symbol(on_symbol) var cleaned_symbol = clean_symbol(on_symbol)
if (items[from_state].contains_key(cleaned_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 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) { fun add_accept(from_state: int, on_symbol: ref symbol::symbol) {
expand_to(from_state) expand_to(from_state)
var cleaned_symbol = clean_symbol(on_symbol) var cleaned_symbol = clean_symbol(on_symbol)
if (items[from_state].contains_key(cleaned_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 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> { fun get(state: int, on_symbol: symbol::symbol): vector::vector<action> {
var cleaned_symbol = clean_symbol(on_symbol) 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 { fun get_shift(state: int, on_symbol: symbol::symbol): action {
var actions = get(state, on_symbol) var actions = get(state, on_symbol)
for (var i = 0; i < actions.size; i++;) for (var i = 0; i < actions.size; i++;)
if (actions[i].act == push) if (actions[i].act == action_type::push)
return actions[i] return actions[i]
io::println("tried to get a shift when none existed") io::println("tried to get a shift when none existed")
io::print("for state ") io::print("for state ")
io::print(state) io::print(state)
io::print(" and symbol ") io::print(" and symbol ")
io::println(on_symbol.to_string()) 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> { 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() { fun print_string() {
/*return string::string("woo a table of size: ") + items.size*/ /*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 // if the zero state contains any reductions for state 0 and eof, then
// it must be reducing to the goal state // it must be reducing to the goal state
println("checking the bidness") 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("Accept on no input for ")
println(name) println(name)
return new<tree<symbol>>()->construct(null_symbol()) 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) { gram.parse_table.get(0, input[0]).for_each(fun(act: action) {
println("for each action") println("for each action")
act.print() act.print()
if (act.act == push) if (act.act == action_type::push)
to_shift.push(make_pair(v0, act.state_or_rule)) 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 && 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: ") print("act == reduce && == 0 Adding reduction from state: ")
println(v0->data) println(v0->data)
to_reduce.push(reduction(v0, gram.rules[act.state_or_rule].lhs, 0, null_symbol_tree, null_symbol_tree)) 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) { gram.parse_table.get(shift_to, input[i]).for_each(fun(act: action) {
var reduce_rule = gram.rules[act.state_or_rule] var reduce_rule = gram.rules[act.state_or_rule]
/*if (act.act == reduce && !fully_reduces_to_null(reduce_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, to_reduce.push(reduction(curr_reached, reduce_rule.lhs,
act.rule_position, act.rule_position,
get_nullable_parts(reduce_rule), get_nullable_parts(reduce_rule),
@@ -213,7 +213,7 @@ obj parser (Object) {
gss.add_to_frontier(i, shift_to_node) gss.add_to_frontier(i, shift_to_node)
gss.add_edge(shift_to_node, curr_reached, new_label) gss.add_edge(shift_to_node, curr_reached, new_label)
gram.parse_table.get(shift_to, input[i]).for_each(fun(act: action) { 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)) to_shift.push(make_pair(shift_to_node, act.state_or_rule))
} else { } else {
var action_rule = gram.rules[act.state_or_rule] var action_rule = gram.rules[act.state_or_rule]
@@ -278,7 +278,7 @@ obj parser (Object) {
println("post add edger") println("post add edger")
gram.parse_table.get(shift.second, input[i+1]).for_each(fun(action: action) { gram.parse_table.get(shift.second, input[i+1]).for_each(fun(action: action) {
println("looking at an action") println("looking at an action")
if (action.act == push) { if (action.act == action_type::push) {
println("is push") println("is push")
next_shifts.push(make_pair(shift_to_node, action.state_or_rule)) next_shifts.push(make_pair(shift_to_node, action.state_or_rule))
} else { } else {

View File

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