1 /* @(#) pf_text.c 98/01/26 1.3 */
2 /***************************************************************
3 ** Text Strings for Error Messages
6 ** For PForth based on 'C'
9 ** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
11 ** The pForth software code is dedicated to the public domain,
12 ** and any third party may reproduce, distribute and modify
13 ** the pForth software code or any derivative works thereof
14 ** without any compensation or license. The pForth software
15 ** code is provided on an "as is" basis without any warranty
16 ** of any kind, including, without limitation, the implied
17 ** warranties of merchantability and fitness for a particular
18 ** purpose and their equivalents under the laws of any jurisdiction.
20 ****************************************************************
21 ** 19970702 PLB Fixed ConvertNumberToText for unsigned numbers.
22 ** 19980522 PLB Expand PAD for ConvertNumberToText so "-1 binary .s" doesn't crash.
23 ***************************************************************/
30 ** Define array of error messages.
31 ** These are defined in one place to make it easier to translate them.
34 /***************************************************************/
35 void pfReportError( const char *FunctionName, Err ErrCode )
43 switch(ErrCode & 0xFF)
45 case PF_ERR_NO_MEM & 0xFF:
46 s = "insufficient memory"; break;
47 case PF_ERR_TOO_BIG & 0xFF:
48 s = "data chunk too large"; break;
49 case PF_ERR_NUM_PARAMS & 0xFF:
50 s = "incorrect number of parameters"; break;
51 case PF_ERR_OPEN_FILE & 0xFF:
52 s = "could not open file"; break;
53 case PF_ERR_WRONG_FILE & 0xFF:
54 s = "wrong type of file format"; break;
55 case PF_ERR_BAD_FILE & 0xFF:
56 s = "badly formatted file"; break;
57 case PF_ERR_READ_FILE & 0xFF:
58 s = "file read failed"; break;
59 case PF_ERR_WRITE_FILE & 0xFF:
60 s = "file write failed"; break;
61 case PF_ERR_CORRUPT_DIC & 0xFF:
62 s = "corrupted dictionary"; break;
63 case PF_ERR_NOT_SUPPORTED & 0xFF:
64 s = "not supported in this version"; break;
65 case PF_ERR_VERSION_FUTURE & 0xFF:
66 s = "version from future"; break;
67 case PF_ERR_VERSION_PAST & 0xFF:
68 s = "version is obsolete. Rebuild new one."; break;
69 case PF_ERR_COLON_STACK & 0xFF:
70 s = "stack depth changed between : and ; . Probably unbalanced conditional"; break;
71 case PF_ERR_HEADER_ROOM & 0xFF:
72 s = "no room left in header space"; break;
73 case PF_ERR_CODE_ROOM & 0xFF:
74 s = "no room left in code space"; break;
75 case PF_ERR_NO_SHELL & 0xFF:
76 s = "attempt to use names in forth compiled with PF_NO_SHELL"; break;
77 case PF_ERR_NO_NAMES & 0xFF:
78 s = "dictionary has no names"; break;
79 case PF_ERR_OUT_OF_RANGE & 0xFF:
80 s = "parameter out of range"; break;
81 case PF_ERR_ENDIAN_CONFLICT & 0xFF:
82 s = "endian-ness of dictionary does not match code"; break;
83 case PF_ERR_FLOAT_CONFLICT & 0xFF:
84 s = "float support mismatch between .dic file and code"; break;
85 case PF_ERR_CELL_SIZE_CONFLICT & 0xFF:
86 s = "cell size mismatch between .dic file and code"; break;
88 s = "unrecognized error code!"; break;
94 void pfReportThrow( ThrowCode code )
100 case THROW_ABORT_QUOTE:
102 case THROW_STACK_OVERFLOW:
103 s = "Stack overflow!"; break;
104 case THROW_STACK_UNDERFLOW:
105 s = "Stack underflow!"; break;
106 case THROW_EXECUTING:
107 s = "Executing a compile-only word!"; break;
108 case THROW_FLOAT_STACK_UNDERFLOW:
109 s = "Float Stack underflow!"; break;
110 case THROW_UNDEFINED_WORD:
111 s = "Undefined word!"; break;
113 s = "Conditional control structure mismatch!"; break;
117 case THROW_SEMICOLON:
118 s = "Stack depth changed between : and ; . Probably unbalanced conditional!"; break;
120 s = "Not a DEFERred word!"; break;
122 s = "Unrecognized throw code!"; break;
127 MSG_NUM_D("THROW code = ", code );
134 /**************************************************************
135 ** Copy a Forth String to a 'C' string.
138 char *ForthStringToC( char *dst, const char *FString, cell_t dstSize )
142 Len = (cell_t) *FString;
143 /* Make sure the text + NUL can fit. */
148 pfCopyMemory( dst, FString+1, Len );
154 /**************************************************************
155 ** Copy a NUL terminated string to a Forth counted string.
157 char *CStringToForth( char *dst, const char *CString, cell_t dstSize )
161 /* Make sure the SIZE+text can fit. */
162 for( i=1; i<dstSize; i++ )
174 /**************************************************************
175 ** Compare two test strings, case sensitive.
176 ** Return TRUE if they match.
178 cell_t ffCompareText( const char *s1, const char *s2, cell_t len )
183 for( i=0; i<len; i++ )
185 DBUGX(("ffCompareText: *s1 = 0x%x, *s2 = 0x%x\n", *s1, *s2 ));
192 DBUGX(("ffCompareText: return 0x%x\n", Result ));
196 /**************************************************************
197 ** Compare two test strings, case INsensitive.
198 ** Return TRUE if they match.
200 cell_t ffCompareTextCaseN( const char *s1, const char *s2, cell_t len )
206 for( i=0; i<len; i++ )
208 c1 = pfCharToLower(*s1++);
209 c2 = pfCharToLower(*s2++);
210 DBUGX(("ffCompareText: c1 = 0x%x, c2 = 0x%x\n", c1, c2 ));
217 DBUGX(("ffCompareText: return 0x%x\n", Result ));
221 /**************************************************************
222 ** Compare two strings, case sensitive.
223 ** Return zero if they match, -1 if s1<s2, +1 is s1>s2;
225 cell_t ffCompare( const char *s1, cell_t len1, const char *s2, cell_t len2 )
227 cell_t i, result, n, diff;
233 if( (diff = (*s2++ - *s1++)) != 0 )
235 result = (diff > 0) ? -1 : 1 ;
239 if( result == 0 ) /* Match up to MIN(len1,len2) */
245 else if ( len1 > len2 )
253 /***************************************************************
254 ** Convert number to text.
256 #define CNTT_PAD_SIZE ((sizeof(cell_t)*8)+2) /* PLB 19980522 - Expand PAD so "-1 binary .s" doesn't crash. */
257 static char cnttPad[CNTT_PAD_SIZE];
259 char *ConvertNumberToText( cell_t Num, cell_t Base, int32_t IfSigned, int32_t MinChars )
261 cell_t IfNegative = 0;
263 ucell_t NewNum, Rem, uNum;
269 /* Convert to positive and keep sign. */
277 /* Point past end of Pad */
278 p = cnttPad + CNTT_PAD_SIZE;
279 *(--p) = (char) 0; /* NUL terminate */
281 while( (i++<MinChars) || (uNum != 0) )
283 NewNum = uNum / Base;
284 Rem = uNum - (NewNum * Base);
285 c = (char) (( Rem < 10 ) ? (Rem + '0') : (Rem - 10 + 'A'));
292 if( IfNegative ) *(--p) = '-';
297 /***************************************************************
298 ** Diagnostic routine that prints memory in table format.
300 void DumpMemory( void *addr, cell_t cnt)
302 cell_t ln, cn, nlines;
303 unsigned char *ptr, *cptr, c;
305 nlines = (cnt + 15) / 16;
307 ptr = (unsigned char *) addr;
311 for (ln=0; ln<nlines; ln++)
313 MSG( ConvertNumberToText( (cell_t) ptr, 16, FALSE, 8 ) );
316 for (cn=0; cn<16; cn++)
318 MSG( ConvertNumberToText( (cell_t) *cptr++, 16, FALSE, 2 ) );
322 for (cn=0; cn<16; cn++)
325 if ((c < ' ') || (c > '}')) c = '.';
333 /* Print name, mask off any dictionary bits. */
334 void TypeName( const char *Name )
336 const char *FirstChar;
342 ioType( FirstChar, Len );
348 /* Unit test for string conversion routines. */
349 #define ASSERT_PAD_IS( index, value, msg ) \
350 if( pad[index] != ((char)(value)) ) \
352 ERR(( "ERROR text test failed: " msg "\n")); \
356 cell_t pfUnitTestText( void )
358 cell_t numErrors = 0;
362 /* test CStringToForth */
363 pfSetMemory(pad,0xA5,sizeof(pad));
364 CStringToForth( pad, "frog", 6 );
365 ASSERT_PAD_IS( 0, 4, "CS len 6" );
366 ASSERT_PAD_IS( 4, 'g', "CS end 6" );
367 ASSERT_PAD_IS( 5, 0xA5, "CS past 6" );
369 pfSetMemory(pad,0xA5,sizeof(pad));
370 CStringToForth( pad, "frog", 5 );
371 ASSERT_PAD_IS( 0, 4, "CS len 5" );
372 ASSERT_PAD_IS( 4, 'g', "CS end 5" );
373 ASSERT_PAD_IS( 5, 0xA5, "CS past 5" );
375 pfSetMemory(pad,0xA5,sizeof(pad));
376 CStringToForth( pad, "frog", 4 );
377 ASSERT_PAD_IS( 0, 3, "CS len 4" );
378 ASSERT_PAD_IS( 3, 'o', "CS end 4" );
379 ASSERT_PAD_IS( 4, 0xA5, "CS past 4" );
381 /* Make a Forth string for testing ForthStringToC. */
382 CStringToForth( fpad, "frog", sizeof(fpad) );
384 pfSetMemory(pad,0xA5,sizeof(pad));
385 ForthStringToC( pad, fpad, 6 );
386 ASSERT_PAD_IS( 0, 'f', "FS len 6" );
387 ASSERT_PAD_IS( 3, 'g', "FS end 6" );
388 ASSERT_PAD_IS( 4, 0, "FS nul 6" );
389 ASSERT_PAD_IS( 5, 0xA5, "FS past 6" );
391 pfSetMemory(pad,0xA5,sizeof(pad));
392 ForthStringToC( pad, fpad, 5 );
393 ASSERT_PAD_IS( 0, 'f', "FS len 5" );
394 ASSERT_PAD_IS( 3, 'g', "FS end 5" );
395 ASSERT_PAD_IS( 4, 0, "FS nul 5" );
396 ASSERT_PAD_IS( 5, 0xA5, "FS past 5" );
398 pfSetMemory(pad,0xA5,sizeof(pad));
399 ForthStringToC( pad, fpad, 4 );
400 ASSERT_PAD_IS( 0, 'f', "FS len 4" );
401 ASSERT_PAD_IS( 2, 'o', "FS end 4" );
402 ASSERT_PAD_IS( 3, 0, "FS nul 4" );
403 ASSERT_PAD_IS( 4, 0xA5, "FS past 4" );