-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathREP.cc
1411 lines (1211 loc) · 33.5 KB
/
REP.cc
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
File: REP.cc
Contains: Read-Evaluate-Print functions.
Written by: Newton Research Group.
*/
#include <stdarg.h>
#include "Objects.h"
#include "Globals.h"
#include "NewtGlobals.h"
#include "REPTranslators.h"
#include "Funcs.h"
#include "ROMResources.h"
/*----------------------------------------------------------------------
D e c l a r a t i o n s
----------------------------------------------------------------------*/
DeclareException(exMessage, exRootException);
DeclareException(exCompiler, exRootException);
DeclareException(exInterpreter, exRootException);
DeclareException(exTranslator, exRootException);
/*----------------------------------------------------------------------
F u n c t i o n P r o t o t y p e s
----------------------------------------------------------------------*/
extern void PrintObjectAux(Ref inObj, int indent, int inDepth);
extern "C" void PrintObject(Ref inObj, int indent);
extern Ref ParseString(RefArg inStr);
NewtonErr CreateHammerInTranslator(PInTranslator ** outTranslator);
NewtonErr CreateHammerOutTranslator(POutTranslator ** outTranslator);
NewtonErr CreateNullInTranslator(PInTranslator ** outTranslator);
NewtonErr CreateNullOutTranslator(POutTranslator ** outTranslator);
NewtonErr CreateStdioInTranslator(PInTranslator ** outTranslator);
NewtonErr CreateStdioOutTranslator(POutTranslator ** outTranslator);
/*------------------------------------------------------------------------------
P l a i n C F u n c t i o n I n t e r f a c e
------------------------------------------------------------------------------*/
extern "C" {
Ref FBreakLoop(RefArg inRcvr);
Ref FExitBreakLoop(RefArg inRcvr);
}
/*------------------------------------------------------------------------------
D a t a
------------------------------------------------------------------------------*/
ULong gNewtConfig = 0x42; // 000013FC
bool gPrintLiterals = false;
Ref gREPContext; // 0C101810
int gREPLevel; // 0C101814
PInTranslator * gREPin; // 0C10181C
POutTranslator * gREPout; // 0C101820
bool * gBreakLoopDone; // 0C105178
#pragma mark -
/*------------------------------------------------------------------------------
E r r o r M e s s a g e s
GetFramesErrorString is defined in ErrorString.m since it uses Cocoa.
------------------------------------------------------------------------------*/
extern "C" const char * GetFramesErrorString(NewtonErr inErr);
void
PrintFramesErrorMsg(const char * inStr, RefArg inData)
{
char tagName[16]; // original allocs this when tagLen is known, but tag names are short
const char * s;
for (s = inStr; *s != 0; s++)
{
if (*s == '%')
{
const char * tagStart = s+1;
const char * tagEnd = strchr(tagStart, '.');
if (tagEnd)
{
size_t tagLen = tagEnd - tagStart;
unwind_protect
{
strncpy(tagName, tagStart, tagLen);
tagName[tagLen] = 0;
PrintObject(GetFrameSlot(inData, MakeSymbol(tagName)), 0);
}
on_unwind
{
s = tagEnd;
}
end_unwind;
}
else
gREPout->putc(*s);
}
else
gREPout->putc(*s);
}
}
#pragma mark -
/*------------------------------------------------------------------------------
R e a d - E v a l u a t e - P r i n t L o o p
------------------------------------------------------------------------------*/
// for NCX, NTX
FILE * logout = NULL;
void RedirectStdioOutTranslator(FILE * inFRef);
void
CaptureStdioOutTranslator(const char * inFilename)
{
logout = fopen(inFilename, "w");
if (logout)
RedirectStdioOutTranslator(logout);
}
void
EndCaptureStdioOutTranslator(void)
{
if (logout)
{
fflush(logout);
fclose(logout), logout = NULL;
RedirectStdioOutTranslator(NULL);
}
}
void
RedirectStdioOutTranslator(FILE * inFRef)
{
if (inFRef == NULL)
{
inFRef = logout ? logout : stdout;
}
StdioTranslatorInfo args = { inFRef };
gREPout->init(&args);
}
/*------------------------------------------------------------------------------
Initialize the REP input stream.
Args: --
Return: input translator
------------------------------------------------------------------------------*/
PInTranslator *
InitREPIn(void)
{
PInTranslator * translator = NULL;
if (gNewtConfig & 0x02)
CreateStdioInTranslator(&translator);
else
CreateNullInTranslator(&translator);
return translator;
}
/*------------------------------------------------------------------------------
Initialize the REP output stream.
Args: --
Return: output translator
------------------------------------------------------------------------------*/
POutTranslator *
InitREPOut(void)
{
POutTranslator * translator = NULL;
if (gNewtConfig & 0x02)
CreateStdioOutTranslator(&translator);
else
CreateNullOutTranslator(&translator);
return translator;
}
/*------------------------------------------------------------------------------
Reset the NTK event handler idle.
Args: --
Return: --
------------------------------------------------------------------------------*/
void
ResetREPIdler(void)
{
/*
Timeout idleTime = REPTime();
if (idleTime != 0)
gREPEventHandler->resetIdle(idleTime);
else
gREPEventHandler->stopIdle();
*/
}
/*------------------------------------------------------------------------------
Initialize the Read-Evaluate-Print context.
Set up key NS variables in the global vars frame (see also InitScriptGlobals
and RunInitScript which happen later at CNotebook init).
Args: --
Return: --
------------------------------------------------------------------------------*/
void
REPInit(void)
{
if (gREPin == NULL)
ThrowErr(exCompiler, kNSErrNoREP);
DefGlobalVar(SYMA(vars), RA(gVarFrame));
DefGlobalVar(SYMA(functions), RA(gFunctionFrame));
DefGlobalVar(SYMA(trace), RA(NILREF));
DefGlobalVar(SYMA(printDepth), MAKEINT(3));
DefGlobalVar(SYMA(prettyPrint), RA(TRUEREF));
gREPContext = gVarFrame;
AddGCRoot(&gREPContext);
gREPLevel = 0;
gREPout->print("\nWelcome to NewtonScript!\n\n");
gREPout->prompt(gREPLevel);
gREPout->flush();
}
/*------------------------------------------------------------------------------
Print a NewtonScript object.
Args: inObj the object
indent number of spaces to indent after new line
Return: --
------------------------------------------------------------------------------*/
void
PrintObject(Ref inObj, int indent)
{
gREPout->consumeFrame(inObj, 0, indent);
}
/*------------------------------------------------------------------------------
Enter Read-Evaluate-Print break loop.
Args: --
Return: --
------------------------------------------------------------------------------*/
void BreakLoop(void);
void
REPBreakLoop(void)
{
unwind_protect
{
gREPout->enterBreakLoop(gREPLevel);
BreakLoop();
}
on_unwind
{
gREPout->exitBreakLoop();
}
end_unwind;
}
void
BreakLoop(void)
{
while (!*gBreakLoopDone)
REPIdle();
}
/*------------------------------------------------------------------------------
Handle Read-Evaluate-Print idle time.
Args: --
Return: --
------------------------------------------------------------------------------*/
void
REPIdle(void)
{
gREPin->idle();
gREPout->idle();
REPAcceptLine();
}
Timeout
REPTime(void)
{
if (gREPin != NULL
&& gREPout != NULL)
{
Timeout timeIn = gREPin->idle();
Timeout timeOut = gREPout->idle();
if (timeIn == 0)
return timeOut;
if (timeOut == 0)
return timeIn;
if (timeIn > timeOut)
return timeOut;
else
return timeIn;
}
return 0;
}
/*------------------------------------------------------------------------------
Read input and compile it;
Evaluate the resulting codeblock;
Print the result.
Args: --
Return: --
------------------------------------------------------------------------------*/
void
REPAcceptLine(void)
{
IncrementCurrentStackPos();
newton_try
{
if (gREPin->frameAvailable())
{
int indent;
RefVar result;
RefVar codeBlock(gREPin->produceFrame(0));
if (NOTNIL(GetGlobalVar(MakeSymbol("showCodeBlocks"))))
{
gREPout->consumeFrame(codeBlock, 0, 0);
gREPout->putc(0x0D);
}
if (NOTNIL(codeBlock))
{
result = InterpretBlock(codeBlock, gREPContext);
indent = gREPout->print("#%-8lX ", (Ref)result);
gREPout->consumeFrame(result, 0, indent);
gREPout->putc(0x0D);
}
gREPout->prompt(gREPLevel);
gREPout->flush();
}
}
newton_catch_all
{
gREPout->exceptionNotify(CurrentException());
gREPout->prompt(gREPLevel);
gREPout->flush();
}
end_try;
DecrementCurrentStackPos();
ClearRefHandles();
}
/*------------------------------------------------------------------------------
Read-Evaluate-Print output.
Is this ever actually used in favour of direct calls to gREPout methods?
The problem is passing on the variable number of args -- we create a vprint
member function to handle this; you might also do it with assembler glue.
Args: inFormat as for printf
Return: number of chars printed or error code (as for printf)
------------------------------------------------------------------------------*/
int
REPprintf(const char * inFormat, ...)
{
int result = 0;
va_list args;
va_start(args, inFormat);
result = gREPout->vprint(inFormat, args);
va_end(args);
return result;
}
void
REPflush(void)
{
gREPout->flush();
}
/*------------------------------------------------------------------------------
Trace Read-Evaluate-Print stack.
Implemented in DebugAPI.cc which better encapsulates stack details.
Args: interpreter
Return: --
------------------------------------------------------------------------------
void
REPStackTrace(void * interpreter)
{}*/
/*------------------------------------------------------------------------------
Print Read-Evaluate-Print exception.
Args: --
Return: --
------------------------------------------------------------------------------*/
#define kExceptionPrefixStr " !!! Exception: "
#define kExceptionErrorStr " !!! Exception: %s\n"
#define kExceptionErrorCodeStr " !!! Exception: %s (%d)\n"
#define kExceptionBuildErrorStr " File \"%s\"; Line %d !!! Exception: "
void
REPExceptionNotify(Exception * inException)
{
const char * str;
if (Subexception(inException->name, exMessage))
gREPout->print(kExceptionErrorStr, (char *)inException->data);
else if (Subexception(inException->name, exRefException))
{
RefVar data(*(RefStruct *)inException->data);
if (IsFrame(data))
{
RefVar filename(GetFrameSlot(data, SYMA(filename)));
RefVar lineNumber(GetFrameSlot(data, SYMA(lineNumber)));
RefVar err(GetFrameSlot(data, SYMA(errorCode)));
if (NOTNIL(filename) && NOTNIL(lineNumber))
{
RemoveSlot(data, SYMA(filename));
RemoveSlot(data, SYMA(lineNumber));
if (ISINT(err) && (str = GetFramesErrorString(RINT(err))) != NULL)
{
CDataPtr filenameStr(ASCIIString(filename));
gREPout->print(kExceptionBuildErrorStr, (char *)filenameStr, RINT(lineNumber));
PrintFramesErrorMsg(str, data);
gREPout->print("\n");
}
}
else if (ISINT(err) && (str = GetFramesErrorString(RINT(err))) != NULL)
{
gREPout->print(kExceptionPrefixStr);
PrintFramesErrorMsg(str, data);
gREPout->print("\n");
}
else
{
int indent = gREPout->print(kExceptionPrefixStr);
PrintObject(data, indent);
gREPout->print("\n");
}
}
else
{
int indent = gREPout->print(kExceptionPrefixStr);
PrintObject(data, indent);
gREPout->print("\n");
}
}
else if ((str = GetFramesErrorString((NewtonErr)(long)inException->data)) != NULL)
gREPout->print(kExceptionErrorStr, str);
else
gREPout->print(kExceptionErrorCodeStr, (char *)inException->name, (NewtonErr)(long)inException->data);
}
#pragma mark -
/*------------------------------------------------------------------------------
B r e a k L o o p
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
Enter break loop from NewtonScript.
Args: inRcvr evaluation context
Return: NULL
------------------------------------------------------------------------------*/
Ref
FBreakLoop(RefArg inRcvr)
{
RefVar savedContext(gREPContext);
gREPContext = inRcvr;
gREPLevel++;
bool isDone = false;
bool * savedDone = gBreakLoopDone;
gBreakLoopDone = &isDone;
unwind_protect
{
REPBreakLoop();
}
on_unwind
{
gREPContext = savedContext;
gREPLevel--;
gBreakLoopDone = savedDone;
}
end_unwind;
return NILREF;
}
/*------------------------------------------------------------------------------
Exit break loop to NewtonScript.
Args: inRcvr ignored
Return: NULL
------------------------------------------------------------------------------*/
Ref
FExitBreakLoop(RefArg inRcvr)
{
if (gBreakLoopDone != NULL)
*gBreakLoopDone = true;
else
ThrowErr(exInterpreter, kNSErrNotInBreakLoop);
return NILREF;
}
#pragma mark -
/*------------------------------------------------------------------------------
T r a n s l a t o r s
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
P H a m m e r I n T r a n s l a t o r
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
Create the Hammer input stream.
Args: --
Return: Hammer input translator
------------------------------------------------------------------------------*/
NewtonErr
CreateHammerInTranslator(PInTranslator ** outTranslator)
{
NewtonErr err = noErr;
XTRY
{
XFAILNOT(outTranslator, err = -1;)
*outTranslator = (PInTranslator *)MakeByName("PInTranslator", "PHammerInTranslator");
XFAILNOT(*outTranslator, err = MemError();)
TranslatorInfo args = { "%NewtonScript Listener", 1024 };
XFAILIF(err = (*outTranslator)->init(&args), (*outTranslator)->destroy(); *outTranslator = NULL;)
}
XENDTRY;
return err;
}
/* ----------------------------------------------------------------
PHammerInTranslator implementation class info.
---------------------------------------------------------------- */
const CClassInfo *
PHammerInTranslator::classInfo(void)
{
__asm__ (
CLASSINFO_BEGIN
" .long 0 \n"
" .long 1f - . \n"
" .long 2f - . \n"
" .long 3f - . \n"
" .long 4f - . \n"
" .long 5f - . \n"
" .long __ZN19PHammerInTranslator6sizeOfEv - 0b \n"
" .long 0 \n"
" .long 0 \n"
" .long __ZN19PHammerInTranslator4makeEv - 0b \n"
" .long __ZN19PHammerInTranslator7destroyEv - 0b \n"
" .long 0 \n"
" .long 0 \n"
" .long 0 \n"
" .long 6f - 0b \n"
"1: .asciz \"PHammerInTranslator\" \n"
"2: .asciz \"PInTranslator\" \n"
"3: .byte 0 \n"
" .align 2 \n"
"4: .long 0 \n"
" .long __ZN19PHammerInTranslator9classInfoEv - 4b \n"
" .long __ZN19PHammerInTranslator4makeEv - 4b \n"
" .long __ZN19PHammerInTranslator7destroyEv - 4b \n"
" .long __ZN19PHammerInTranslator4initEPv - 4b \n"
" .long __ZN19PHammerInTranslator4idleEv - 4b \n"
" .long __ZN19PHammerInTranslator14frameAvailableEv - 4b \n"
" .long __ZN19PHammerInTranslator12produceFrameEi - 4b \n"
CLASSINFO_END
);
}
PROTOCOL_IMPL_SOURCE_MACRO(PHammerInTranslator)
PHammerInTranslator *
PHammerInTranslator::make(void)
{
fileRef = NULL;
fBuf = NULL;
fBufSize = 0;
f1C = 0;
f20 = 0;
return this;
}
void
PHammerInTranslator::destroy(void)
{
if (fileRef != NULL)
fclose(fileRef);
if (fBuf != NULL)
FreePtr(fBuf), fBuf = NULL;
}
NewtonErr
PHammerInTranslator::init(void * inArgs)
{
NewtonErr err = noErr;
XTRY
{
fBufSize = ((TranslatorInfo*)inArgs)->bufSize;
fBuf = NewPtr(((TranslatorInfo*)inArgs)->bufSize);
XFAILNOT(fBuf, err = MemError();)
// SetPtrName(fBuf, 'REPb');
fileRef = fopen(((TranslatorInfo*)inArgs)->filename, "r");
// if (fileRef != NULL)
// set_input_notify(fileRef, &f1C);
}
XENDTRY;
return err;
}
Timeout
PHammerInTranslator::idle(void)
{
return 250*kMilliseconds;
}
bool
PHammerInTranslator::frameAvailable(void)
{
return fileRef != NULL
/* && (f1C - f20) != 0 */;
}
Ref
PHammerInTranslator::produceFrame(int inLevel)
{
RefVar result;
if (fileRef != NULL
&& fgets(fBuf, fBufSize, fileRef) != NULL)
{
f20 = f1C;
result = ParseString(MakeStringFromCString(fBuf));
}
return result;
}
#pragma mark -
/*------------------------------------------------------------------------------
P H a m m e r O u t T r a n s l a t o r
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
Create the Hammer output stream.
Args: --
Return: Hammer output translator
------------------------------------------------------------------------------*/
NewtonErr
CreateHammerOutTranslator(POutTranslator ** outTranslator)
{
NewtonErr err = noErr;
XTRY
{
XFAILNOT(outTranslator, err = -1;)
*outTranslator = (POutTranslator *)MakeByName("POutTranslator", "PHammerOutTranslator");
XFAILNOT(*outTranslator, err = MemError();)
TranslatorInfo args = { "%NewtonScript Listener", 256 };
XFAILIF(err = (*outTranslator)->init(&args), (*outTranslator)->destroy(); *outTranslator = NULL;)
}
XENDTRY;
return err;
}
/* ----------------------------------------------------------------
PHammerOutTranslator implementation class info.
---------------------------------------------------------------- */
const CClassInfo *
PHammerOutTranslator::classInfo(void)
{
__asm__ (
CLASSINFO_BEGIN
" .long 0 \n"
" .long 1f - . \n"
" .long 2f - . \n"
" .long 3f - . \n"
" .long 4f - . \n"
" .long 5f - . \n"
" .long __ZN20PHammerOutTranslator6sizeOfEv - 0b \n"
" .long 0 \n"
" .long 0 \n"
" .long __ZN20PHammerOutTranslator4makeEv - 0b \n"
" .long __ZN20PHammerOutTranslator7destroyEv - 0b \n"
" .long 0 \n"
" .long 0 \n"
" .long 0 \n"
" .long 6f - 0b \n"
"1: .asciz \"PHammerOutTranslator\" \n"
"2: .asciz \"POutTranslator\" \n"
"3: .byte 0 \n"
" .align 2 \n"
"4: .long 0 \n"
" .long __ZN20PHammerOutTranslator9classInfoEv - 4b \n"
" .long __ZN20PHammerOutTranslator4makeEv - 4b \n"
" .long __ZN20PHammerOutTranslator7destroyEv - 4b \n"
" .long __ZN20PHammerOutTranslator4initEPv - 4b \n"
" .long __ZN20PHammerOutTranslator4idleEv - 4b \n"
" .long __ZN20PHammerOutTranslator12consumeFrameERK6RefVarii - 4b \n"
" .long __ZN20PHammerOutTranslator6promptEi - 4b \n"
" .long __ZN20PHammerOutTranslator5printEPKcz - 4b \n"
" .long __ZN20PHammerOutTranslator6vprintEPKcP13__va_list_tag - 4b \n"
" .long __ZN20PHammerOutTranslator4putcEi - 4b \n"
" .long __ZN20PHammerOutTranslator5flushEv - 4b \n"
" .long __ZN20PHammerOutTranslator14enterBreakLoopEi - 4b \n"
" .long __ZN20PHammerOutTranslator13exitBreakLoopEv - 4b \n"
" .long __ZN20PHammerOutTranslator10stackTraceEPv - 4b \n"
" .long __ZN20PHammerOutTranslator15exceptionNotifyEP9Exception - 4b \n"
CLASSINFO_END
);
}
PROTOCOL_IMPL_SOURCE_MACRO(PHammerOutTranslator)
PHammerOutTranslator *
PHammerOutTranslator::make(void)
{
fileRef = NULL;
fBuf = NULL;
return this;
}
void
PHammerOutTranslator::destroy(void)
{
if (fileRef != NULL)
fclose(fileRef);
if (fBuf != NULL)
FreePtr(fBuf), fBuf = NULL;
}
NewtonErr
PHammerOutTranslator::init(void * inArgs)
{
NewtonErr err = noErr;
XTRY
{
fBuf = NewPtr(((TranslatorInfo*)inArgs)->bufSize);
XFAILNOT(fBuf, err = MemError();)
// SetPtrName(fBuf, 'REPb');
fileRef = fopen(((TranslatorInfo*)inArgs)->filename, "w");
if (fileRef != NULL)
setvbuf(fileRef, fBuf, 0x0200 /*_IOFBF?*/, ((TranslatorInfo*)inArgs)->bufSize);
fileRef = stdout;
}
XENDTRY;
return err;
}
Timeout
PHammerOutTranslator::idle(void)
{
return 250*kMilliseconds;
}
void
PHammerOutTranslator::consumeFrame(RefArg inObj, int inDepth, int indent)
{
if (fileRef != NULL)
PrintObjectAux(inObj, indent, inDepth);
}
void
PHammerOutTranslator::prompt(int inLevel)
{
if (fileRef != NULL)
print("%7d > ", inLevel);
}
int
PHammerOutTranslator::vprint(const char * inFormat, va_list args)
{
return vfprintf(fileRef, inFormat, args);
}
int
PHammerOutTranslator::print(const char * inFormat, ...)
{
if (fileRef != NULL)
{
int result;
va_list args;
va_start(args, inFormat);
result = vprint(inFormat, args);
va_end(args);
return result;
}
return 0;
}
int
PHammerOutTranslator::putc(int inCh)
{
if (fileRef != NULL)
return fputc(inCh, fileRef);
return 0;
}
void
PHammerOutTranslator::flush(void)
{
if (fileRef != NULL)
{
NewtonErr err;
if ((err = fflush(fileRef)) != noErr)
ThrowErr(exTranslator, err);
}
}
void
PHammerOutTranslator::enterBreakLoop(int inLevel)
{
print("Entering break loop\n");
prompt(inLevel);
flush();
}
void
PHammerOutTranslator::exitBreakLoop(void)
{
print("Exiting break loop\n");
}
void
PHammerOutTranslator::stackTrace(void * interpreter)
{
REPStackTrace(interpreter);
}
void
PHammerOutTranslator::exceptionNotify(Exception * inException)
{
REPExceptionNotify(inException);
}
#pragma mark -
/*------------------------------------------------------------------------------
P N u l l I n T r a n s l a t o r
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
Create a null input stream.
Args: --
Return: null input translator
------------------------------------------------------------------------------*/
NewtonErr
CreateNullInTranslator(PInTranslator ** outTranslator)
{
NewtonErr err = noErr;
XTRY
{
XFAILNOT(outTranslator, err = -1;)
*outTranslator = (PInTranslator *)MakeByName("PInTranslator", "PNullInTranslator");
XFAILNOT(*outTranslator, err = MemError();)
XFAILIF(err = (*outTranslator)->init(NULL), (*outTranslator)->destroy(); *outTranslator = NULL;)
}
XENDTRY;
return err;
}
/* ----------------------------------------------------------------
PNullInTranslator implementation class info.
---------------------------------------------------------------- */
const CClassInfo *
PNullInTranslator::classInfo(void)
{
__asm__ (
CLASSINFO_BEGIN
" .long 0 \n"
" .long 1f - . \n"
" .long 2f - . \n"
" .long 3f - . \n"
" .long 4f - . \n"
" .long 5f - . \n"
" .long __ZN17PNullInTranslator6sizeOfEv - 0b \n"
" .long 0 \n"
" .long 0 \n"
" .long __ZN17PNullInTranslator4makeEv - 0b \n"
" .long __ZN17PNullInTranslator7destroyEv - 0b \n"
" .long 0 \n"
" .long 0 \n"
" .long 0 \n"
" .long 6f - 0b \n"
"1: .asciz \"PNullInTranslator\" \n"
"2: .asciz \"PInTranslator\" \n"
"3: .byte 0 \n"
" .align 2 \n"
"4: .long 0 \n"
" .long __ZN17PNullInTranslator9classInfoEv - 4b \n"
" .long __ZN17PNullInTranslator4makeEv - 4b \n"
" .long __ZN17PNullInTranslator7destroyEv - 4b \n"
" .long __ZN17PNullInTranslator4initEPv - 4b \n"
" .long __ZN17PNullInTranslator4idleEv - 4b \n"
" .long __ZN17PNullInTranslator14frameAvailableEv - 4b \n"
" .long __ZN17PNullInTranslator12produceFrameEi - 4b \n"
CLASSINFO_END
);
}
PROTOCOL_IMPL_SOURCE_MACRO(PNullInTranslator)
PNullInTranslator *
PNullInTranslator::make(void)
{ return this; }
void
PNullInTranslator::destroy(void)
{ }
NewtonErr
PNullInTranslator::init(void * inArgs)
{ return noErr; }
Timeout
PNullInTranslator::idle(void)
{ return kNoTimeout; }
bool
PNullInTranslator::frameAvailable(void)
{ return false; }
Ref
PNullInTranslator::produceFrame(int inLevel)
{ return NILREF; }
#pragma mark -
/*------------------------------------------------------------------------------
P N u l l O u t T r a n s l a t o r
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
Create a null output stream.
Args: --
Return: null output translator
------------------------------------------------------------------------------*/
NewtonErr
CreateNullOutTranslator(POutTranslator ** outTranslator)
{
NewtonErr err = noErr;
XTRY
{
XFAILNOT(outTranslator, err = -1;)
*outTranslator = (POutTranslator *)MakeByName("POutTranslator", "PNullOutTranslator");
XFAILNOT(*outTranslator, err = MemError();)
XFAILIF(err = (*outTranslator)->init(NULL), (*outTranslator)->destroy(); *outTranslator = NULL;)
}
XENDTRY;
return err;
}
/* ----------------------------------------------------------------
PNullOutTranslator implementation class info.
---------------------------------------------------------------- */
const CClassInfo *
PNullOutTranslator::classInfo(void)
{
__asm__ (
CLASSINFO_BEGIN
" .long 0 \n"