aboutsummaryrefslogtreecommitdiffhomepage
path: root/goto.cpp
blob: f4d9497bd04ea59d8165f28619efd85330b54dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <bsd/sys/tree.h>

#include <glib.h>
#include <glib/gprintf.h>

#include "sciteco.h"
#include "parser.h"
#include "undo.h"
#include "goto.h"

static gchar *skip_label = NULL;

class GotoTable {
	class UndoTokenSet : public UndoToken {
		GotoTable *table;

		gchar	*name;
		gint	pc;

	public:
		UndoTokenSet(GotoTable *_table, gchar *_name, gint _pc = -1)
			    : table(_table), name(g_strdup(_name)), pc(_pc) {}
		~UndoTokenSet()
		{
			g_free(name);
		}

		void
		run(void)
		{
			table->set(name, pc);
			g_free(name);
			name = NULL;
#if 0
			table->dump();
#endif
		}
	};

	class Label {
	public:
		RB_ENTRY(Label) nodes;

		gchar	*name;
		gint	pc;

		Label(gchar *_name, gint _pc = -1)
		     : name(g_strdup(_name)), pc(_pc) {}
		~Label()
		{
			g_free(name);
		}

		static inline int
		cmp(Label *l1, Label *l2)
		{
			return g_strcmp0(l1->name, l2->name);
		}
	};

	RB_HEAD(Table, Label) head;

	RB_GENERATE(Table, Label, nodes, Label::cmp);

public:
	GotoTable()
	{
		RB_INIT(&head);
	}

	~GotoTable()
	{
		clear();
	}

	gint
	remove(gchar *name)
	{
		gint existing_pc = -1;

		Label label(name);
		Label *existing = RB_FIND(Table, &head, &label);

		if (existing) {
			existing_pc = existing->pc;
			RB_REMOVE(Table, &head, existing);
			delete existing;
		}

		return existing_pc;
	}

	gint
	find(gchar *name)
	{
		Label label(name);
		Label *existing = RB_FIND(Table, &head, &label);

		return existing ? existing->pc : -1;
	}

	gint
	set(gchar *name, gint pc)
	{
		if (pc < 0)
			return remove(name);

		Label *label = new Label(name, pc);
		Label *existing;
		gint existing_pc = -1;

		existing = RB_FIND(Table, &head, label);
		if (existing) {
			existing_pc = existing->pc;
			g_free(existing->name);
			existing->name = label->name;
			existing->pc = label->pc;

			label->name = NULL;
			delete label;
		} else {
			RB_INSERT(Table, &head, label);
		}

#if 0
		dump();
#endif
		return existing_pc;
	}

	inline void
	undo_set(gchar *name, gint pc = -1)
	{
		undo.push(new UndoTokenSet(this, name, pc));
	}

	void
	clear(void)
	{
		Label *cur;

		while ((cur = RB_MIN(Table, &head))) {
			RB_REMOVE(Table, &head, cur);
			delete cur;
		}
	}

#if 0
	void
	dump(void)
	{
		Label *cur;

		RB_FOREACH(cur, Table, &head)
			g_printf("table[\"%s\"] = %d\n", cur->name, cur->pc);
		g_printf("---END---\n");
	}
#endif
};

static GotoTable table;

void
goto_table_clear(void)
{
	table.clear();
}

StateLabel::StateLabel() : State()
{
	transitions['\0'] = this;
}

State *
StateLabel::custom(gchar chr)
{
	gchar *new_str;

	if (chr == '!') {
		table.undo_set(strings[0], table.set(strings[0], macro_pc));

		if (!g_strcmp0(strings[0], skip_label)) {
			undo.push_str(skip_label);
			g_free(skip_label);
			skip_label = NULL;

			undo.push_var<Mode>(mode);
			mode = MODE_NORMAL;
		}

		undo.push_str(strings[0]);
		g_free(strings[0]);
		strings[0] = NULL;

		return &states.start;
	}

	undo.push_str(strings[0]);
	new_str = g_strdup_printf("%s%c", strings[0] ? : "", chr);
	g_free(strings[0]);
	strings[0] = new_str;

	return this;
}