From 90f203bff189706c2dec34482475b89d0a232597 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 19 Nov 2012 22:12:16 +0100 Subject: make goto tables local to macro invocation: they are declared on the C++ callstack since macro invocations result in nested macro_execute() calls otherwise a macro could set labels with program counters which are invalid in other macros/the command line --- goto.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) (limited to 'goto.h') diff --git a/goto.h b/goto.h index 622f58c..b44ffa2 100644 --- a/goto.h +++ b/goto.h @@ -2,9 +2,76 @@ #define __GOTO_H #include +#include #include "sciteco.h" #include "parser.h" +#include "undo.h" +#include "rbtree.h" + +class GotoTable : public RBTree { + 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); +#ifdef DEBUG + table->dump(); +#endif + } + }; + + class Label : public RBEntry { + public: + gchar *name; + gint pc; + + Label(gchar *_name, gint _pc = -1) + : name(g_strdup(_name)), pc(_pc) {} + ~Label() + { + g_free(name); + } + + int + operator <(RBEntry &l2) + { + return g_strcmp0(name, ((Label &)l2).name); + } + }; + +public: + GotoTable() : RBTree() {} + + gint remove(gchar *name); + gint find(gchar *name); + + gint set(gchar *name, gint pc); + inline void + undo_set(gchar *name, gint pc = -1) + { + undo.push(new UndoTokenSet(this, name, pc)); + } + +#ifdef DEBUG + void dump(void); +#endif +}; + +extern GotoTable *goto_table; /* * Command states @@ -28,6 +95,4 @@ namespace States { extern StateGotoCmd gotocmd; } -void goto_table_clear(void); - #endif -- cgit v1.2.3