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 #ifndef NEDIT_INTERPRET_H_INCLUDED
28 #define NEDIT_INTERPRET_H_INCLUDED
29
30 #include "nedit.h"
31 #include "../util/rbTree.h"
32
33 #define STACK_SIZE 1024
34 #define MAX_SYM_LEN 100
35 #define MACRO_EVENT_MARKER 2
36
37
38
39 enum symTypes {
CONST_SYM,
GLOBAL_SYM,
LOCAL_SYM,
ARG_SYM,
PROC_VALUE_SYM,
40 C_FUNCTION_SYM,
MACRO_FUNCTION_SYM,
ACTION_ROUTINE_SYM};
41 #define N_OPS 43
42 enum operations {
OP_RETURN_NO_VAL,
OP_RETURN,
OP_PUSH_SYM,
OP_DUP,
OP_ADD,
43 OP_SUB,
OP_MUL,
OP_DIV,
OP_MOD,
OP_NEGATE,
OP_INCR,
OP_DECR,
OP_GT,
OP_LT,
44 OP_GE,
OP_LE,
OP_EQ,
OP_NE,
OP_BIT_AND,
OP_BIT_OR,
OP_AND,
OP_OR,
OP_NOT,
45 OP_POWER,
OP_CONCAT,
OP_ASSIGN,
OP_SUBR_CALL,
OP_FETCH_RET_VAL,
OP_BRANCH,
46 OP_BRANCH_TRUE,
OP_BRANCH_FALSE,
OP_BRANCH_NEVER,
OP_ARRAY_REF,
47 OP_ARRAY_ASSIGN,
OP_BEGIN_ARRAY_ITER,
OP_ARRAY_ITER,
OP_IN_ARRAY,
48 OP_ARRAY_DELETE,
OP_PUSH_ARRAY_SYM,
OP_ARRAY_REF_ASSIGN_SETUP,
OP_PUSH_ARG,
49 OP_PUSH_ARG_COUNT,
OP_PUSH_ARG_ARRAY};
50
51 enum typeTags {
NO_TAG,
INT_TAG,
STRING_TAG,
ARRAY_TAG};
52
53 enum execReturnCodes {
MACRO_TIME_LIMIT,
MACRO_PREEMPT,
MACRO_DONE,
MACRO_ERROR};
54
55 #define ARRAY_DIM_SEP "\034"
56
57 struct DataValueTag;
58 struct SparseArrayEntryTag;
59 struct ProgramTag;
60 struct SymbolRec;
61
62 typedef union InstTag {
63 int (*func)(
void);
64 int value;
65 struct SymbolRec *sym;
66 } Inst;
67
68 typedef int (*BuiltInSubr)(WindowInfo *window,
struct DataValueTag *argList,
69 int nArgs,
struct DataValueTag *result,
char **errMsg);
70
71 typedef struct NStringTag {
72 char *rep;
73 size_t len;
74 } NString;
75
76 typedef struct DataValueTag {
77 enum typeTags tag;
78 union {
79 int n;
80 struct NStringTag str;
81 BuiltInSubr subr;
82 struct ProgramTag* prog;
83 XtActionProc xtproc;
84 Inst* inst;
85 struct DataValueTag* dataval;
86 struct SparseArrayEntryTag *arrayPtr;
87 } val;
88 } DataValue;
89
90 typedef struct SparseArrayEntryTag {
91 rbTreeNode nodePtrs;
92 char *key;
93 DataValue value;
94 } SparseArrayEntry;
95
96
97 typedef struct SymbolRec {
98 char *name;
99 enum symTypes type;
100 DataValue value;
101 struct SymbolRec *next;
102 } Symbol;
103
104 typedef struct ProgramTag {
105 Symbol *localSymList;
106 Inst *code;
107 } Program;
108
109
110 typedef struct {
111 DataValue *stack;
112 DataValue *stackP;
113 DataValue *frameP;
114 Inst *pc;
115 WindowInfo *runWindow;
116 WindowInfo *focusWindow;
117 } RestartData;
118
119 void InitMacroGlobals(
void);
120
121 SparseArrayEntry *arrayIterateFirst(DataValue *theArray);
122 SparseArrayEntry *arrayIterateNext(SparseArrayEntry *iterator);
123 SparseArrayEntry *ArrayNew(
void);
124 Boolean ArrayInsert(DataValue* theArray,
char* keyStr, DataValue* theValue);
125 void ArrayDelete(DataValue *theArray,
char *keyStr);
126 void ArrayDeleteAll(DataValue *theArray);
127 unsigned ArraySize(DataValue *theArray);
128 Boolean ArrayGet(DataValue* theArray,
char* keyStr, DataValue* theValue);
129 int ArrayCopy(DataValue *dstArray, DataValue *srcArray);
130
131
132
133 void BeginCreatingProgram(
void);
134 int AddOp(
int op,
char **msg);
135 int AddSym(Symbol *sym,
char **msg);
136 int AddImmediate(
int value,
char **msg);
137 int AddBranchOffset(Inst *to,
char **msg);
138 Inst *GetPC(
void);
139 Symbol *InstallIteratorSymbol(
void);
140 Symbol *LookupStringConstSymbol(
const char *value);
141 Symbol *InstallStringConstSymbol(
const char *str);
142 Symbol *LookupSymbol(
const char *name);
143 Symbol *InstallSymbol(
const char *name,
enum symTypes type, DataValue value);
144 Program *FinishCreatingProgram(
void);
145 void SwapCode(Inst *start, Inst *boundary, Inst *end);
146 void StartLoopAddrList(
void);
147 int AddBreakAddr(Inst *addr);
148 int AddContinueAddr(Inst *addr);
149 void FillLoopAddrs(Inst *breakAddr, Inst *continueAddr);
150
151
152 #define PERM_ALLOC_STR(xStr) (((
char *)(
"\001" xStr)) +
1)
153
154
155 int ExecuteMacro(WindowInfo *window, Program *prog,
int nArgs, DataValue *args,
156 DataValue *result, RestartData **continuation,
char **msg);
157 int ContinueMacro(RestartData *continuation, DataValue *result,
char **msg);
158 void RunMacroAsSubrCall(Program *prog);
159 void PreemptMacro(
void);
160 char *AllocString(
int length);
161 char *AllocStringNCpy(
const char *s,
int length);
162 char *AllocStringCpy(
const char *s);
163 int AllocNString(NString *string,
int length);
164 int AllocNStringNCpy(NString *string,
const char *s,
int length);
165 int AllocNStringCpy(NString *string,
const char *s);
166 void GarbageCollectStrings(
void);
167 void FreeRestartData(RestartData *context);
168 Symbol *PromoteToGlobal(Symbol *sym);
169 void FreeProgram(Program *prog);
170 void ModifyReturnedValue(RestartData *context, DataValue dv);
171 WindowInfo *MacroRunWindow(
void);
172 WindowInfo *MacroFocusWindow(
void);
173 void SetMacroFocusWindow(WindowInfo *window);
174
175 int StringToNum(
const char *string,
int *number);
176
177 #endif
178