3 source/interpret.c | 53 +++++++++++++++++++++++++++++++++++++----------------
4 source/interpret.h | 7 ++++---
5 source/parse.y | 36 ++++++++++++++++++++++++++----------
6 3 files changed, 67 insertions(+), 29 deletions(-)
8 diff --quilt old/source/interpret.c new/source/interpret.c
9 --- old/source/interpret.c
10 +++ new/source/interpret.c
11 @@ -80,7 +80,7 @@ static const char CVSID[] = "$Id: interp
13 enum opStatusCodes {STAT_OK=2, STAT_DONE, STAT_ERROR, STAT_PREEMPT};
15 -static void addLoopAddr(Inst *addr);
16 +static int addLoopAddr(Inst *addr, char **msg);
17 static void saveContext(RestartData *context);
18 static void restoreContext(RestartData *context);
20 @@ -387,6 +387,16 @@ int AddBranchOffset(Inst *to, char **msg
24 +** Set a branch offset to an operand previously added with AddBranchOffset()
26 +int SetBranchOffset(Inst *from, Inst *to, char **msg)
28 + from->value = to - from;
34 ** Return the address at which the next instruction will be stored
37 @@ -423,34 +433,45 @@ Inst *SwapCode(Inst *start, Inst *bounda
38 ** address for a break or continue statement, and FillLoopAddrs to fill
39 ** in all the addresses and return to the level of the enclosing loop.
41 -void StartLoopAddrList(void)
42 +int StartLoopAddrList(char **msg)
45 + return addLoopAddr(NULL, msg);
48 -int AddBreakAddr(Inst *addr)
49 +int AddBreakAddr(Inst *addr, char **msg)
51 - if (LoopStackPtr == LoopStack) return 1;
53 + if (LoopStackPtr == LoopStack) {
54 + *msg = "break outside loop";
57 + if (!addLoopAddr(addr, msg)) {
60 addr->value = NEEDS_BREAK;
65 -int AddContinueAddr(Inst *addr)
67 - if (LoopStackPtr == LoopStack) return 1;
69 +int AddContinueAddr(Inst *addr, char **msg)
71 + if (LoopStackPtr == LoopStack) {
72 + *msg = "continue outside loop";
75 + if (!addLoopAddr(addr, msg)) {
78 addr->value = NEEDS_CONTINUE;
83 -static void addLoopAddr(Inst *addr)
84 +static int addLoopAddr(Inst *addr, char **msg)
86 - if (LoopStackPtr > &LoopStack[LOOP_STACK_SIZE-1]) {
87 - fprintf(stderr, "NEdit: loop stack overflow in macro parser");
89 + if (LoopStackPtr >= &LoopStack[LOOP_STACK_SIZE]) {
90 + *msg = "loop stack overflow";
93 *LoopStackPtr++ = addr;
97 void FillLoopAddrs(Inst *breakAddr, Inst *continueAddr)
98 diff --quilt old/source/interpret.h new/source/interpret.h
99 --- old/source/interpret.h
100 +++ new/source/interpret.h
101 @@ -152,6 +152,7 @@ int AddOp(int op, char **msg);
102 int AddSym(Symbol *sym, char **msg);
103 int AddImmediate(int value, char **msg);
104 int AddBranchOffset(Inst *to, char **msg);
105 +int SetBranchOffset(Inst *from, Inst *to, char **msg);
107 Symbol *InstallIteratorSymbol(void);
108 Symbol *LookupStringConstSymbol(const char *value);
109 @@ -161,9 +162,9 @@ Symbol *InstallSymbol(const char *name,
110 Symbol *InstallMultiAssignExpr(void);
111 Program *FinishCreatingProgram(AccumulatorData *acc);
112 Inst *SwapCode(Inst *start, Inst *boundary, Inst *end);
113 -void StartLoopAddrList(void);
114 -int AddBreakAddr(Inst *addr);
115 -int AddContinueAddr(Inst *addr);
116 +int StartLoopAddrList(char **msg);
117 +int AddBreakAddr(Inst *addr, char **msg);
118 +int AddContinueAddr(Inst *addr, char **msg);
119 void FillLoopAddrs(Inst *breakAddr, Inst *continueAddr);
121 /* create a permanently allocated static string (only for use with static strings) */
122 diff --quilt old/source/parse.y new/source/parse.y
123 --- old/source/parse.y
124 +++ new/source/parse.y
127 #define SET_BR_OFF(from, to) \
129 - ((from)->value) = ((Inst *)(to)) - ((Inst *)(from)); \
130 + if (!SetBranchOffset(from, to, &ErrMsg)) { \
134 +#define START_LOOP() \
136 + if (!StartLoopAddrList(&ErrMsg)) { \
140 +#define ADD_BREAK(addr) \
142 + if (!AddBreakAddr(addr, &ErrMsg)) { \
146 +#define ADD_CONTINUE(addr) \
148 + if (!AddContinueAddr(addr, &ErrMsg)) { \
153 /* Max. length for a string constant (... there shouldn't be a maximum) */
154 @@ -309,15 +329,11 @@ stmt: ';' blank
156 | BREAK stmtend blank {
157 ADD_OP(OP_BRANCH); ADD_BR_OFF(0);
158 - if (AddBreakAddr(GetPC()-1)) {
159 - yyerror("break outside loop"); YYERROR;
161 + ADD_BREAK(GetPC() - 1);
163 | CONTINUE stmtend blank {
164 ADD_OP(OP_BRANCH); ADD_BR_OFF(0);
165 - if (AddContinueAddr(GetPC()-1)) {
166 - yyerror("continue outside loop"); YYERROR;
168 + ADD_CONTINUE(GetPC() - 1);
170 | RETURN expr stmtend blank {
172 @@ -771,15 +787,15 @@ numexpropt: blank {
176 - $$ = GetPC(); StartLoopAddrList();
177 + $$ = GetPC(); START_LOOP();
181 - $$ = GetPC(); StartLoopAddrList();
182 + $$ = GetPC(); START_LOOP();
186 - StartLoopAddrList(); $$ = GetPC();
187 + START_LOOP(); $$ = GetPC();