File: STBlock.m

package info (click to toggle)
steptalk 0.10.0%2Bgit20200629-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,732 kB
  • sloc: objc: 12,182; yacc: 400; makefile: 40; sh: 34; csh: 4; awk: 3; lisp: 3
file content (256 lines) | stat: -rw-r--r-- 6,380 bytes parent folder | download
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
    STBlock.m
    Wrapper for STBlockContext to make it invisible from the user  
 
    Copyright (c) 2002 Free Software Foundation
 
    This file is part of the StepTalk project.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 */


#import "Externs.h"
#import "STBlock.h"
#import "STLiterals.h"
#import "STBlockContext.h"
#import "STBytecodeInterpreter.h"
#import "STStack.h"

#import <StepTalk/STExterns.h>

#import <Foundation/NSArray.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSException.h>

Class STBlockContextClass = nil;

@implementation STBlock
+ (void)initialize
{
    STBlockContextClass = [STBlockContext class];
}
- initWithInterpreter:(STBytecodeInterpreter *)anInterpreter
          homeContext:(STMethodContext *)context
            initialIP:(NSUInteger)ptr
        argumentCount:(NSUInteger)count
            stackSize:(NSUInteger)size
{
    if ((self = [super init]) != nil)
    {
	homeContext = RETAIN(context);
	argCount  = count; 
	stackSize = size;
	initialIP = ptr;
	interpreter = RETAIN(anInterpreter);
    }
    return self;
}

- (void)dealloc
{
    RELEASE(homeContext);
    RELEASE(interpreter);
    RELEASE(cachedContext);
    [super dealloc];
}
- (NSUInteger)argumentCount
{
    return argCount;
}

- value
{
    if(argCount != 0)
    {
        [NSException  raise:STScriptingException
                     format:@"Block needs %lu arguments",
                            (unsigned long)argCount];
        return nil;
    }
    return [self valueWithArgs:(id*)0 count:0];
}

- value:arg
{
    return [self valueWithArguments:[NSArray arrayWithObject:arg]];
}

- value:arg1 value:arg2
{
    return [self valueWithArguments:[NSArray arrayWithObjects:arg1,arg2,nil]];
}

- value:arg1 value:arg2 value:arg3
{
    return [self valueWithArguments:[NSArray arrayWithObjects:arg1,arg2,arg3,nil]];
}

- valueWith:arg
{
    id  args[1] = {arg};
    return [self valueWithArgs:args count:1];
}

- valueWith:arg1 with:arg2
{
    id  args[2] = {arg1,arg2};
    return [self valueWithArgs:args count:2];
}

- valueWith:arg1 with:arg2 with:arg3
{
    id  args[3] = {arg1,arg2,arg3};
    return [self valueWithArgs:args count:3];
}

- valueWith:arg1 with:arg2 with:arg3 with:arg4
{
    id  args[4] = {arg1,arg2,arg3,arg4};
    return [self valueWithArgs:args count:4];
}

- valueWithArgs:(id *)args count:(NSUInteger)count
{
    NSArray *arguments = [NSArray arrayWithObjects:args count:count];
    return [self valueWithArguments:arguments];
}

- valueWithArguments:(NSArray *)arguments
{
    STExecutionContext *parentContext;
    STBlockContext     *context;
    STStack            *stack;
    NSUInteger          i, count;
    id                  retval;

    count = [arguments count];
    if (argCount != count)
    {
        [NSException raise:STScriptingException
                    format:@"Invalid block argument count %lu, "
                           @"wants to be %lu", (unsigned long)count,
                           (unsigned long)argCount];
        return nil;
    }

    if (!usingCachedContext)
    {
        /* In case of recursive block nesting */
        usingCachedContext = YES;

        if (!cachedContext)
        {
            cachedContext = [[STBlockContextClass alloc] 
                                    initWithInitialIP:initialIP
                                            stackSize:stackSize];
        }

        /* Avoid allocation */
        context = cachedContext;
        [[context stack] empty];
        [context resetInstructionPointer];
    }
    else
    {
        /* Create new context */
        context = [[STBlockContextClass alloc] initWithInitialIP:initialIP
                                                       stackSize:stackSize];

        AUTORELEASE(context);
    }

    /* push block arguments to the stack */
    
    stack = [context stack];
    for (i = 0; i<count; i++)
    {
        [stack push:[arguments objectAtIndex:i]];
    }

    [context setHomeContext:homeContext];

    parentContext = [interpreter context];

    [interpreter setContext:context];
    NS_DURING
        retval = [interpreter interpret];
    NS_HANDLER
        if (context == cachedContext)
            usingCachedContext = NO;
        [localException raise];
    NS_ENDHANDLER
    [interpreter setContext:parentContext];

    /* Release cached context */
    if (context == cachedContext)
    {
        if (usingCachedContext)
            usingCachedContext = NO;
        else
            [NSException raise:STInternalInconsistencyException
                        format:@"%@: using cached context %@"
                               @" but usingCachedContext is not set",
                               self, cachedContext];
    }

    return retval;
}

- handler:(STBlock *)handlerBlock
{
    STExecutionContext *context;
    id                  retval;

    /* save the execution context for the case of exception */
    context = [interpreter context];

    NS_DURING
        retval = [self value];
    NS_HANDLER
	/* don't handle the internal STInterpreterReturnException exception */
	if ([[localException name] isEqualToString:STInterpreterReturnException])
	    [localException raise];
        retval = [handlerBlock value:localException];
        /* restore the execution context */
        [interpreter setContext:context];
    NS_ENDHANDLER

    return retval;
}

- whileTrue:(STBlock *)doBlock
{
    id retval = nil;
    
    while([[self value] boolValue])
    {
        retval = [doBlock value];
    }
    return retval;
}

- whileFalse:(STBlock *)doBlock
{
    id retval = nil;
    
    while(! [[self value] boolValue])
    {
        retval = [doBlock value];
    }
    return retval;
}
@end