1 /*
2     This file is part of GNU APL, a free implementation of the
3     ISO/IEC Standard 13751, "Programming Language APL, Extended"
4 
5     Copyright (C) 2008-2015  Dr. Jürgen Sauermann
6 
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __Quad_FUNCTION_HH_DEFINED__
22 #define __Quad_FUNCTION_HH_DEFINED__
23 
24 #include <vector>
25 
26 #include "PrimitiveFunction.hh"
27 #include "StateIndicator.hh"
28 
29 //-----------------------------------------------------------------------------
30 /** The various Quad functions.  */
31 /// Base class for all system functions
32 class QuadFunction : public PrimitiveFunction
33 {
34 public:
35    /// Constructor.
QuadFunction(TokenTag tag)36    QuadFunction(TokenTag tag) : PrimitiveFunction(tag) {}
37 
38    /// overloaded Function::has_alpha()
has_alpha() const39    virtual bool has_alpha() const   { return true; }
40 
41    /// overloaded Function::eval_B().
eval_B(Value_P B)42    virtual Token eval_B(Value_P B) { VALENCE_ERROR; }
43 
44    /// overloaded Function::eval_AB().
eval_AB(Value_P A,Value_P B)45    virtual Token eval_AB(Value_P A, Value_P B) { VALENCE_ERROR; }
46 
47    /// overloaded Function::has_result()
has_result() const48    virtual bool has_result() const   { return true; }
49 };
50 //-----------------------------------------------------------------------------
51 /** The system function ⎕AF (Atomic Function) */
52 /// The class implementing ⎕AF
53 class Quad_AF : public QuadFunction
54 {
55 public:
56    /// Constructor.
Quad_AF()57    Quad_AF() : QuadFunction(TOK_Quad_AF) {}
58 
59    /// overloaded Function::eval_B().
60    virtual Token eval_B(Value_P B);
61 
62    static Quad_AF * fun;          ///< Built-in function.
63    static Quad_AF  _fun;          ///< Built-in function.
64 
65 protected:
66 };
67 //-----------------------------------------------------------------------------
68 /** The system function ⎕AT (Attributes) */
69 /// The class implementing ⎕AI
70 class Quad_AT : public QuadFunction
71 {
72 public:
73    /// Constructor.
Quad_AT()74    Quad_AT() : QuadFunction(TOK_Quad_AT) {}
75 
76    /// overloaded Function::eval_AB().
77    virtual Token eval_AB(Value_P A, Value_P B);
78 
79    static Quad_AT * fun;          ///< Built-in function.
80    static Quad_AT  _fun;          ///< Built-in function.
81 
82 protected:
83 };
84 //-----------------------------------------------------------------------------
85 /** The system function ⎕DL (Delay).  */
86 /// The class implementing ⎕DL
87 class Quad_DL : public QuadFunction
88 {
89 public:
90    /// Constructor.
Quad_DL()91    Quad_DL() : QuadFunction(TOK_Quad_DL) {}
92 
93    static Quad_DL * fun;          ///< Built-in function.
94    static Quad_DL  _fun;          ///< Built-in function.
95 
96 protected:
97    /// overloaded Function::eval_B().
98    virtual Token eval_B(Value_P B);
99 };
100 //-----------------------------------------------------------------------------
101 /**
102    The system function ⎕EA (Execute Alternate)
103  */
104 /// The class implementing ⎕EA
105 class Quad_EA : public QuadFunction
106 {
107 public:
108    /// Constructor.
Quad_EA()109    Quad_EA() : QuadFunction(TOK_Quad_EA) {}
110 
111    /// overladed Function::may_push_SI()
may_push_SI() const112    virtual bool may_push_SI() const   { return true; }
113 
114    static Quad_EA * fun;          ///< Built-in function.
115    static Quad_EA  _fun;          ///< Built-in function.
116 
117 protected:
118    /// overloaded Function::eval_AB().
119    virtual Token eval_AB(Value_P A, Value_P B);
120 };
121 //-----------------------------------------------------------------------------
122 /**
123    The system function ⎕EB (Execute Both)
124  */
125 /// The class implementing ⎕EB
126 class Quad_EB : public QuadFunction
127 {
128 public:
129    /// Constructor.
Quad_EB()130    Quad_EB() : QuadFunction(TOK_Quad_EB) {}
131 
132    /// overladed Function::may_push_SI()
may_push_SI() const133    virtual bool may_push_SI() const   { return true; }
134 
135    static Quad_EB * fun;          ///< Built-in function.
136    static Quad_EB  _fun;          ///< Built-in function.
137 
138 protected:
139    /// overloaded Function::eval_AB().
140    virtual Token eval_AB(Value_P A, Value_P B);
141 };
142 //-----------------------------------------------------------------------------
143 /**
144    The system function ⎕EC (Execute Controlled)
145  */
146 /// The class implementing ⎕EC
147 class Quad_EC : public QuadFunction
148 {
149 public:
150    /// Constructor.
Quad_EC()151    Quad_EC() : QuadFunction(TOK_Quad_EC) {}
152 
153    /// overladed Function::may_push_SI()
may_push_SI() const154    virtual bool may_push_SI() const   { return true; }
155 
156    static Quad_EC * fun;          ///< Built-in function.
157    static Quad_EC  _fun;          ///< Built-in function.
158 
159    /// end of context handler for ⎕EC
160    static void eoc(Token & token);
161 
162 protected:
163    /// overloaded Function::eval_B().
164    virtual Token eval_B(Value_P B);
165 
166    /// overloaded Function::eval_fill_B().
167    virtual Token eval_fill_B(Value_P B);
168 };
169 //-----------------------------------------------------------------------------
170 /**
171    The system function ⎕ENV (ENvironment Variables)
172  */
173 /// The class implementing ⎕ENV
174 class Quad_ENV : public QuadFunction
175 {
176 public:
177    /// Constructor.
Quad_ENV()178    Quad_ENV() : QuadFunction(TOK_Quad_ENV) {}
179 
180    static Quad_ENV * fun;          ///< Built-in function.
181    static Quad_ENV  _fun;          ///< Built-in function.
182 
183 protected:
184    /// overloaded Function::eval_B().
185    virtual Token eval_B(Value_P B);
186 };
187 //-----------------------------------------------------------------------------
188 /**
189    The system function ⎕ES (Event Simulate).
190  */
191 /// The class implementing ⎕ES
192 class Quad_ES : public QuadFunction
193 {
194 public:
195    /// Constructor.
Quad_ES()196    Quad_ES() : QuadFunction(TOK_Quad_ES) {}
197 
198    static Quad_ES * fun;          ///< Built-in function.
199    static Quad_ES  _fun;          ///< Built-in function.
200 
201 protected:
202    /// overloaded Function::eval_AB().
203    virtual Token eval_AB(Value_P A, Value_P B);
204 
205    /// overloaded Function::eval_B().
206    virtual Token eval_B(Value_P B);
207 
208    /// common inplementation for eval_AB() and eval_B()
209    Token event_simulate(const UCS_string * A, Value_P B, Error & error);
210 
211    /// compute error code for B
212    static ErrorCode get_error_code(Value_P B);
213 };
214 //-----------------------------------------------------------------------------
215 /**
216    The system function ⎕EX (Expunge).
217  */
218 /// The class implementing ⎕EX
219 class Quad_EX : public QuadFunction
220 {
221 public:
222    /// Constructor.
Quad_EX()223    Quad_EX() : QuadFunction(TOK_Quad_EX) {}
224 
225    static Quad_EX * fun;          ///< Built-in function.
226    static Quad_EX  _fun;          ///< Built-in function.
227 
228 protected:
229    /// overloaded Function::eval_B().
230    virtual Token eval_B(Value_P B);
231 
232    /// disassociate name from value, return 0 on failure or 1 on success.
233    int expunge(UCS_string name);
234 };
235 //-----------------------------------------------------------------------------
236 /**
237    The system function ⎕INP (input from script, aka. HERE document)
238  */
239 /// The class implementing ⎕INP
240 class Quad_INP : public QuadFunction
241 {
242 public:
243    /// constructor.
Quad_INP()244    Quad_INP()
245    : QuadFunction(TOK_Quad_INP),
246      Quad_INP_running(false)
247    {}
248 
249    static Quad_INP * fun;          ///< Built-in function.
250    static Quad_INP  _fun;          ///< Built-in function.
251 
252 protected:
253    /// overloaded Function::eval_AB().
254    virtual Token eval_AB(Value_P A, Value_P B);
255 
256    /// overloaded Function::eval_B().
257    virtual Token eval_B(Value_P B);
258 
259    /// overloaded Function::eval_XB().
260    virtual Token eval_XB(Value_P X, Value_P B);
261 
262    /// extract the esc1 and esc2 strings from \b A
263    void get_esc(Value_P A, UCS_string & esc1, UCS_string & esc2);
264 
265    /// read \b raw_lines from stdin or file, stop at end_marker
266    void read_strings();
267 
268    /// split \b raw_lines into \b prefixes, \b escapes, and \b suffixes
269    void split_strings();
270 
271    /// the end merker for APL escapes (dyadic ⎕INP only)
272    /// the start merker for APL escapes (dyadic ⎕INP only)
273    UCS_string esc1;
274 
275    /// the end merker for APL escapes (dyadic ⎕INP only)
276    UCS_string esc2;
277 
278    /// the end merker for the entire ⎕INP input
279    UCS_string end_marker;
280 
281    /// the raw lines read from stdin
282    UCS_string_vector raw_lines;
283 
284    /// the line parts left of the escapes
285    UCS_string_vector prefixes;
286 
287    /// the line parts to exe executed
288    UCS_string_vector escapes;
289 
290    /// the line parts right of the escapes
291    UCS_string_vector suffixes;
292 
293    /// bool to prevent recursive ⎕INP calls
294    bool Quad_INP_running;
295 };
296 //-----------------------------------------------------------------------------
297 /**
298    The system function ⎕NA (Name Association).
299  */
300 /// The class implementing ⎕NA
301 class Quad_NA : public QuadFunction
302 {
303 public:
304    /// Constructor.
Quad_NA()305    Quad_NA() : QuadFunction(TOK_Quad_NA) {}
306 
307    static Quad_NA * fun;          ///< Built-in function.
308    static Quad_NA  _fun;          ///< Built-in function.
309 
310 protected:
311    /// overloaded Function::eval_AB()
eval_AB(Value_P A,Value_P B)312    virtual Token eval_AB(Value_P A, Value_P B)   { TODO; }
313 
314    /// overloaded Function::eval_B()
eval_B(Value_P B)315    virtual Token eval_B(Value_P B)   { TODO; }
316 };
317 //-----------------------------------------------------------------------------
318 /**
319    The system function ⎕NC (Name class).
320  */
321 /// The class implementing ⎕NC
322 class Quad_NC : public QuadFunction
323 {
324 public:
325    /// Constructor.
Quad_NC()326    Quad_NC() : QuadFunction(TOK_Quad_NC) {}
327 
328    /// overloaded Function::eval_B().
329    virtual Token eval_B(Value_P B);
330 
331    /// return the ⎕NC for variable name \b var
332    static APL_Integer get_NC(const UCS_string var);
333 
334    static Quad_NC * fun;          ///< Built-in function.
335    static Quad_NC  _fun;          ///< Built-in function.
336 
337 protected:
338 };
339 //-----------------------------------------------------------------------------
340 /**
341    The system function ⎕NL (Name List).
342  */
343 /// The class implementing ⎕NL
344 class Quad_NL : public QuadFunction
345 {
346 public:
347    /// Constructor.
Quad_NL()348    Quad_NL() : QuadFunction(TOK_Quad_NL) {}
349 
350    /// overloaded Function::eval_B().
eval_B(Value_P B)351    virtual Token eval_B(Value_P B)
352       { return do_quad_NL(Value_P(), B); }
353 
354    /// overloaded Function::eval_AB().
eval_AB(Value_P A,Value_P B)355    virtual Token eval_AB(Value_P A, Value_P B)
356       { return do_quad_NL(A, B); }
357 
358    static Quad_NL * fun;          ///< Built-in function.
359    static Quad_NL  _fun;          ///< Built-in function.
360 
361 protected:
362    /// return A ⎕NL B
363    Token do_quad_NL(Value_P A, Value_P B);
364 };
365 //-----------------------------------------------------------------------------
366 /**
367    The system function ⎕SI (State Indicator)
368  */
369 /// The class implementing ⎕SI
370 class Quad_SI : public QuadFunction
371 {
372 public:
373    /// Constructor.
Quad_SI()374    Quad_SI() : QuadFunction(TOK_Quad_SI) {}
375 
376    /// overloaded Function::eval_AB().
377    virtual Token eval_AB(Value_P A, Value_P B);
378 
379    /// overloaded Function::eval_AB().
380    virtual Token eval_B(Value_P B);
381 
382    static Quad_SI * fun;          ///< Built-in function.
383    static Quad_SI  _fun;          ///< Built-in function.
384 
385 protected:
386 };
387 //-----------------------------------------------------------------------------
388 /**
389    The system function ⎕UCS (Universal Character Set)
390  */
391 /// The class implementing ⎕UCS
392 class Quad_UCS : public QuadFunction
393 {
394 public:
395    /// Constructor.
Quad_UCS()396    Quad_UCS() : QuadFunction(TOK_Quad_UCS) {}
397 
398    /// overloaded Function::eval_B().
399    virtual Token eval_B(Value_P B);
400 
401    static Quad_UCS * fun;          ///< Built-in function.
402    static Quad_UCS  _fun;          ///< Built-in function.
403 
404 protected:
405 };
406 //-----------------------------------------------------------------------------
407 /// Base class for ⎕STOP and ⎕TRACE
408 class Stop_Trace : public QuadFunction
409 {
410 protected:
411    /// constructor
Stop_Trace(TokenTag tag)412    Stop_Trace(TokenTag tag)
413    : QuadFunction (tag)
414    {}
415 
416    /// find UserFunction named \b fun_name
417    UserFunction * locate_fun(const Value & fun_name);
418 
419    /// return integers in lines
420    Token reference(const std::vector<Function_Line> & lines, bool assigned);
421 
422    /// return assign lines in new_value to stop or trace vector in ufun
423    void assign(UserFunction * ufun, const Value & new_value, bool stop);
424 };
425 //-----------------------------------------------------------------------------
426 /// The class implementing ⎕STOP
427 class Quad_STOP : public Stop_Trace
428 {
429 public:
430    /// Constructor
Quad_STOP()431    Quad_STOP()
432    : Stop_Trace(TOK_Quad_STOP)
433    {}
434 
435    /// Overloaded Function::eval_AB()
436    virtual Token eval_AB(Value_P A, Value_P B);
437 
438    /// Overloaded Function::eval_B()
439    virtual Token eval_B(Value_P B);
440 
441    static Quad_STOP * fun;          ///< Built-in function.
442    static Quad_STOP  _fun;          ///< Built-in function.
443 };
444 //-----------------------------------------------------------------------------
445 /// The class implementing ⎕TRACE
446 class Quad_TRACE : public Stop_Trace
447 {
448 public:
449    /// Constructor
Quad_TRACE()450    Quad_TRACE()
451    : Stop_Trace(TOK_Quad_TRACE)
452    {}
453 
454    /// Overloaded Function::eval_AB()
455    virtual Token eval_AB(Value_P A, Value_P B);
456 
457    /// Overloaded Function::eval_B()
458    virtual Token eval_B(Value_P B);
459 
460    static Quad_TRACE * fun;          ///< Built-in function.
461    static Quad_TRACE  _fun;          ///< Built-in function.
462 };
463 //-----------------------------------------------------------------------------
464 
465 #endif // __Quad_FUNCTION_HH_DEFINED__
466