UNIXworkcode

1 /******************************************************************************* 2 * * 3 * interpret.h -- Nirvana Editor Interpreter Header File * 4 * * 5 * Copyright 2004 The NEdit Developers * 6 * * 7 * This is free software; you can redistribute it and/or modify it under the * 8 * terms of the GNU General Public License as published by the Free Software * 9 * Foundation; either version 2 of the License, or (at your option) any later * 10 * version. In addition, you may distribute versions of this program linked to * 11 * Motif or Open Motif. See README for details. * 12 * * 13 * This software is distributed in the hope that it will be useful, but WITHOUT * 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * 16 * more details. * 17 * * 18 * You should have received a copy of the GNU General Public License along with * 19 * software; if not, write to the Free Software Foundation, Inc., 59 Temple * 20 * Place, Suite 330, Boston, MA 02111-1307 USA * 21 * * 22 * Nirvana Text Editor * 23 * July 31, 2001 * 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 /* Maximum stack size */ 34 #define MAX_SYM_LEN 100 /* Max. symbol name length */ 35 #define MACRO_EVENT_MARKER 2 /* Special value for the send_event field of 36 events passed to action routines. Tells 37 them that they were called from a macro */ 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; /* MUST BE FIRST ENTRY */ 92 char *key; 93 DataValue value; 94 } SparseArrayEntry; 95 96 /* symbol table entry */ 97 typedef struct SymbolRec { 98 char *name; 99 enum symTypes type; 100 DataValue value; 101 struct SymbolRec *next; /* to link to another */ 102 } Symbol; 103 104 typedef struct ProgramTag { 105 Symbol *localSymList; 106 Inst *code; 107 } Program; 108 109 /* Information needed to re-start a preempted macro */ 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 /* Routines for creating a program, (accumulated beginning with 132 BeginCreatingProgram and returned via FinishCreatingProgram) */ 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 /* create a permanently allocated static string (only for use with static strings) */ 152 #define PERM_ALLOC_STR(xStr) (((char *)("\001" xStr)) + 1) 153 154 /* Routines for executing programs */ 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 /* function used for implicit conversion from string to number */ 175 int StringToNum(const char *string, int *number); 176 177 #endif /* NEDIT_INTERPRET_H_INCLUDED */ 178