diff --git a/main.go b/main.go index a2920d8..ea12361 100644 --- a/main.go +++ b/main.go @@ -129,32 +129,16 @@ func thompson(re string) *State { case CONCAT: s2 := mustPop(&nfa) s1 := mustPop(&nfa) - for i := range s1.output { - s1.output[i].transitions[s2.content] = append(s1.output[i].transitions[s2.content], s2) - } - s1.output = s2.output + s1 = concatenate(s1, s2) nfa = append(nfa, s1) case '*': // Create a 0-state, concat the popped state after it, concat the 0-state after the popped state s1 := mustPop(&nfa) - stateToAdd := &State{} - stateToAdd.transitions = make(map[int][]*State) - stateToAdd.content = EPSILON - stateToAdd.isEmpty = true - stateToAdd.isKleene = true - stateToAdd.output = append(stateToAdd.output, stateToAdd) - for i := range s1.output { - s1.output[i].transitions[stateToAdd.content] = append(s1.output[i].transitions[stateToAdd.content], stateToAdd) - } - stateToAdd.transitions[s1.content] = append(stateToAdd.transitions[s1.content], s1) + stateToAdd := kleene(*s1) nfa = append(nfa, stateToAdd) - case '+': + case '+': // a+ is equivalent to aa* s1 := mustPop(&nfa) - for i := range s1.output { - s1.output[i].transitions[s1.content] = append(s1.output[i].transitions[s1.content], s1) - } - // Reset output to s1 (in case s1 was a union operator state, which has multiple outputs) - s1.output = nil - s1.output = append(s1.output, s1) + s2 := kleene(*s1) + s1 = concatenate(s1, s2) nfa = append(nfa, s1) case '|': s1 := mustPop(&nfa)