DEADSOFTWARE

Mirror gpcp-32255
[gpcp-linux.git] / libs / csharp / RTS.cs
1 /** This is the body of the GPCP runtime support.
2 *
3 * Written November 1998, John Gough. (for jdk version)
4 * This version for Lightning, May 2000, John Gough.
5 * Support for uplevel addressing RTS.XHR, Aug-2001.
6 * Merged version for N2CPS, gpcp etc. SYChan, KJGough. 19-Aug-2001.
7 */
9 #if !BETA1
10 #define BETA2
11 #endif
13 public class RTS
14 // Known in ILASM as [RTS]RTS
15 {
16 /** This is part of the body of the GPCP runtime support.
17 *
18 * Written November 1998, John Gough.
19 * This version for Lightning, May 2000, John Gough.
20 */
22 /* ------------------------------------------------------------ */
23 /* Support for RTS.cp */
24 /* ------------------------------------------------------------ */
26 public static char[] defaultTarget = {'n','e','t','\0'};
27 public static char[] eol = NativeStrings.mkArr(System.Environment.NewLine);
29 public static double dblPosInfinity = System.Double.PositiveInfinity;
30 public static double dblNegInfinity = System.Double.NegativeInfinity;
31 public static float fltPosInfinity = System.Single.PositiveInfinity;
32 public static float fltNegInfinity = System.Single.NegativeInfinity;
34 private static char[] ChrNaN = {'N','a','N','\0'};
35 private static char[] ChrPosInf = {'I','n','f','i','n','i','t','y','\0'};
36 private static char[] ChrNegInf = {'-','I','n','f','i','n','i','t','y','\0'};
37 private static System.String StrNaN = new System.String(ChrNaN);
38 private static System.String StrPosInf = new System.String(ChrPosInf);
39 private static System.String StrNegInf = new System.String(ChrNegInf);
41 private static System.Type typDouble = System.Type.GetType("System.Double");
42 private static System.Type typSingle = System.Type.GetType("System.Single");
44 private static System.IFormatProvider invarCulture =
45 (System.IFormatProvider) new System.Globalization.CultureInfo("");
46 private static System.IFormatProvider currentCulture =
47 (System.IFormatProvider) System.Globalization.CultureInfo.CurrentCulture;
49 /* -------------------------------------------------------------------- */
50 // PROCEDURE getStr*(x : NativeException) : RTS.CharOpen; END getStr;
51 //
52 // Known in ILASM as [RTS]RTS::getStr
53 public static char[] getStr(System.Exception inp)
54 {
55 return CP_rts.strToChO(inp.ToString());
56 }
58 /* ------------------------------------------------------------ */
59 // PROCEDURE StrToBool(IN str : ARRAY OF CHAR;
60 // OUT b : BOOLEAN;
61 // OUT ok : BOOLEAN);
62 //
63 // Known in ILASM as [RTS]RTS::StrToBool
64 public static void StrToBool(char[] str,
65 out bool o, // OUT param
66 out bool r) // OUT param
67 {
68 System.String bstr = new System.String(str);
69 try {
70 o = System.Boolean.Parse(bstr);
71 r = true;
72 } catch {
73 o = false;
74 r = false;
75 }
76 }
78 /* ------------------------------------------------------------ */
79 // PROCEDURE TypeName(typ : NativeType) : CharOpen
80 // (* Get the name of the argument type *)
81 //
82 public static char[] TypeName(System.Type t) {
83 return NativeStrings.mkArr(t.FullName);
84 }
86 /* ------------------------------------------------------------ */
87 // PROCEDURE CharAtIndex(str : NativeString; idx : INTEGER) : CHAR;
88 // (* Get the character at zero-based index idx *)
89 //
90 public static char CharAtIndex( string s, int i ) { return s[i]; }
92 /* ------------------------------------------------------------ */
93 // PROCEDURE Length(str : NativeString) : INTEGER;
94 // (* Get the length of the native string *)
95 //
96 public static int Length( string s ) { return s.Length; }
98 /* ------------------------------------------------------------ */
99 // PROCEDURE StrToByte(IN str : ARRAY OF CHAR;
100 // OUT b : BYTE;
101 // OUT ok : BOOLEAN);
102 //
103 // Known in ILASM as [RTS]RTS::StrToByte
104 public static void StrToByte(char[] str,
105 out sbyte o, // OUT param
106 out bool r) // OUT param
108 System.String bstr = new System.String(str);
109 try {
110 o = System.SByte.Parse(bstr);
111 r = true;
112 } catch {
113 o = 0;
114 r = false;
118 /* ------------------------------------------------------------ */
119 // PROCEDURE StrToUByte(IN str : ARRAY OF CHAR;
120 // OUT b : BYTE;
121 // OUT ok : BOOLEAN);
122 //
123 // Known in ILASM as [RTS]RTS::StrToUByte
124 public static void StrToUByte(char[] str,
125 out sbyte o, // OUT param
126 out bool r) // OUT param
128 System.String bstr = new System.String(str);
129 try {
130 o = (sbyte)System.Byte.Parse(bstr);
131 r = true;
132 } catch {
133 o = (sbyte)0;
134 r = false;
138 /* ------------------------------------------------------------ */
139 // PROCEDURE HexStrToUByte(IN s : ARRAY OF CHAR; OUT b : BYTE; OUT ok : BOOLEAN);
140 //
141 // Known in ILASM as [RTS]RTS::HexStrToUByte
142 public static void HexStrToUByte(char[] str,
143 out sbyte o, // OUT param
144 out bool r) // OUT param
146 System.String bstr = new System.String(str);
147 try {
148 o = (sbyte)System.Byte.Parse
149 (bstr, System.Globalization.NumberStyles.HexNumber);
150 r = true;
151 } catch {
152 o = (sbyte)0;
153 r = false;
157 /* ------------------------------------------------------------ */
158 // PROCEDURE StrToShort(IN str : ARRAY OF CHAR;
159 // OUT i : SHORTINT;
160 // OUT ok : BOOLEAN);
161 //
162 // Known in ILASM as [RTS]RTS::StrToShort
163 public static void StrToShort(char[] str,
164 out short o, // OUT param
165 out bool r) // OUT param
167 System.String sstr = new System.String(str);
168 try {
169 o = System.Int16.Parse(sstr);
170 r = true;
171 } catch {
172 o = (short) 0;
173 r = false;
177 /* ------------------------------------------------------------ */
178 // PROCEDURE StrToUShort(IN str : ARRAY OF CHAR;
179 // OUT i : SHORTINT;
180 // OUT ok : BOOLEAN);
181 //
182 // Known in ILASM as [RTS]RTS::StrToUShort
183 public static void StrToUShort(char[] str,
184 out short o, // OUT param
185 out bool r) // OUT param
187 System.String sstr = new System.String(str);
188 try {
189 o = (short)System.UInt16.Parse(sstr);
190 r = true;
191 } catch {
192 o = (short) 0;
193 r = false;
197 /* ------------------------------------------------------------ */
198 // PROCEDURE StrToInt(IN str : ARRAY OF CHAR;
199 // OUT i : INTEGER;
200 // OUT ok : BOOLEAN);
201 //
202 // Known in ILASM as [RTS]RTS::StrToInt
203 public static void StrToInt(char[] str,
204 out int o, // OUT param
205 out bool r) // OUT param
207 System.String lstr = new System.String(str);
208 try {
209 o = System.Int32.Parse(lstr);
210 r = true;
211 } catch {
212 o = 0;
213 r = false;
217 /* ------------------------------------------------------------ */
218 // PROCEDURE StrToUInt(IN str : ARRAY OF CHAR;
219 // OUT i : INTEGER;
220 // OUT ok : BOOLEAN);
221 //
222 // Known in ILASM as [RTS]RTS::StrToUInt
223 public static void StrToUInt(char[] str,
224 out int o, // OUT param
225 out bool r) // OUT param
227 System.String lstr = new System.String(str);
228 try {
229 o = (int)System.UInt32.Parse(lstr);
230 r = true;
231 } catch {
232 o = (int)0;
233 r = false;
237 /* ------------------------------------------------------------ */
238 // PROCEDURE StrToLong(IN str : ARRAY OF CHAR;
239 // OUT l : LONGINT;
240 // OUT ok : BOOLEAN);
241 //
242 // Known in ILASM as [RTS]RTS::StrToLong
243 public static void StrToLong(char[] str,
244 out long o, // OUT param
245 out bool r) // OUT param
247 System.String lstr = new System.String(str);
248 try {
249 o = System.Int64.Parse(lstr);
250 r = true;
251 } catch {
252 o = (long) 0;
253 r = false;
257 /* ------------------------------------------------------------ */
258 // PROCEDURE StrToULong(IN str : ARRAY OF CHAR;
259 // OUT l : LONGINT;
260 // OUT ok : BOOLEAN);
261 //
262 // Known in ILASM as [RTS]RTS::StrToULong
263 public static void StrToULong(char[] str,
264 out long o, // OUT param
265 out bool r) // OUT param
267 System.String lstr = new System.String(str);
268 try {
269 o = (long)System.UInt64.Parse(lstr);
270 r = true;
271 } catch {
272 o = (long) 0;
273 r = false;
277 /* ------------------------------------------------------------ */
278 // PROCEDURE StrToSReal(IN str : ARRAY OF CHAR;
279 // OUT r : SHORTREAL;
280 // OUT ok : BOOLEAN);
281 //
282 // Known in ILASM as [RTS]RTS::StrToSReal
283 public static void StrToSReal(char[] str,
284 out float o, // OUT param
285 out bool r) // OUT param
287 System.String rstr = new System.String(str);
288 try {
289 o = System.Single.Parse(rstr);
290 r = true;
291 } catch {
292 if (System.String.Compare(rstr, StrPosInf) == 0) {
293 o = System.Single.PositiveInfinity;
294 r = true;
296 else if (System.String.Compare(rstr, StrNegInf) == 0) {
297 o = System.Single.NegativeInfinity;
298 r = true;
300 else if (System.String.Compare(rstr, StrNaN) == 0) {
301 o = System.Single.NaN;
302 r = true;
304 else {
305 o = 0;
306 r = false;
311 /* ------------------------------------------------------------ */
312 // PROCEDURE StrToReal(IN str : ARRAY OF CHAR;
313 // OUT r : REAL;
314 // OUT ok : BOOLEAN);
315 //
316 // Known in ILASM as [RTS]RTS::StrToReal
317 public static void StrToReal(char[] str,
318 out double o, // OUT param
319 out bool r) // OUT param
321 System.String rstr = new System.String(str);
322 try {
323 o = System.Double.Parse(rstr);
324 r = true;
325 } catch {
326 if (System.String.Compare(rstr, StrPosInf) == 0) {
327 o = System.Double.PositiveInfinity;
328 r = true;
330 else if (System.String.Compare(rstr, StrNegInf) == 0) {
331 o = System.Double.NegativeInfinity;
332 r = true;
334 else if (System.String.Compare(rstr, StrNaN) == 0) {
335 o = System.Double.NaN;
336 r = true;
338 else {
339 o = 0.0;
340 r = false;
345 /* ------------------------------------------------------------ */
346 // PROCEDURE StrToRealInvar(IN str : ARRAY OF CHAR;
347 // OUT r : REAL;
348 // OUT ok : BOOLEAN);
349 //
350 // Known in ILASM as [RTS]RTS::StrToReal
351 public static void StrToRealInvar(char[] str,
352 out double o, // OUT param
353 out bool r) // OUT param
355 System.String rstr = new System.String(str);
356 try {
357 o = System.Double.Parse(rstr, invarCulture);
358 r = true;
359 } catch {
360 if (System.String.Compare(rstr, StrPosInf) == 0) {
361 o = System.Double.PositiveInfinity;
362 r = true;
364 else if (System.String.Compare(rstr, StrNegInf) == 0) {
365 o = System.Double.NegativeInfinity;
366 r = true;
368 else if (System.String.Compare(rstr, StrNaN) == 0) {
369 o = System.Double.NaN;
370 r = true;
372 else {
373 o = 0.0;
374 r = false;
379 /* ------------------------------------------------------------ */
380 // PROCEDURE StrToRealLocal(IN str : ARRAY OF CHAR;
381 // OUT r : REAL;
382 // OUT ok : BOOLEAN);
383 //
384 // Known in ILASM as [RTS]RTS::StrToReal
385 public static void StrToRealLocal(char[] str,
386 out double o, // OUT param
387 out bool r) // OUT param
389 System.String rstr = new System.String(str);
390 try {
391 o = System.Double.Parse(rstr, currentCulture);
392 r = true;
393 } catch {
394 if (System.String.Compare(rstr, StrPosInf) == 0) {
395 o = System.Double.PositiveInfinity;
396 r = true;
398 else if (System.String.Compare(rstr, StrNegInf) == 0) {
399 o = System.Double.NegativeInfinity;
400 r = true;
402 else if (System.String.Compare(rstr, StrNaN) == 0) {
403 o = System.Double.NaN;
404 r = true;
406 else {
407 o = 0.0;
408 r = false;
413 /* ------------------------------------------------------------ */
414 // PROCEDURE ByteToStr*(i : BYTE; OUT s : ARRAY OF CHAR);
415 // (** Decode a CP BYTE into an array *)
416 // BEGIN END ByteToStr;
417 //
418 // Known in ILASM as [RTS]RTS::ByteToStr
419 public static void ByteToStr(sbyte num,
420 char[] str)
422 System.String lls = System.Convert.ToString(num);
423 int len = lls.Length;
424 if (len >= str.Length)
425 len = str.Length - 1;
426 lls.CopyTo(0, str, 0, len);
427 str[len] = '\0';
430 /* ------------------------------------------------------------ */
431 // PROCEDURE ShortToStr*(i : SHORTINT; OUT s : ARRAY OF CHAR);
432 // (** Decode a CP SHORTINT into an array *)
433 // BEGIN END ShortToStr;
434 //
435 // Known in ILASM as [RTS]RTS::ShortToStr
436 public static void ShortToStr(short num,
437 char[] str)
439 System.String lls = System.Convert.ToString(num);
440 int len = lls.Length;
441 if (len >= str.Length)
442 len = str.Length - 1;
443 lls.CopyTo(0, str, 0, len);
444 str[len] = '\0';
447 /* ------------------------------------------------------------ */
448 // PROCEDURE IntToStr*(i : INTEGER; OUT s : ARRAY OF CHAR);
449 // (** Decode a CP INTEGER into an array *)
450 // BEGIN END IntToStr;
451 //
452 // Known in ILASM as [RTS]RTS::IntToStr
453 public static void IntToStr(int num,
454 char[] str)
456 System.String lls = System.Convert.ToString(num);
457 int len = lls.Length;
458 if (len >= str.Length)
459 len = str.Length - 1;
460 lls.CopyTo(0, str, 0, len);
461 str[len] = '\0';
464 /* ------------------------------------------------------------ */
465 // PROCEDURE ObjToStr(obj: mscorlib_System.Object; OUT str: ARRAY OF CHAR);
466 // (** Decode an .NET object into an array of CHAR *)
467 // BEGIN END ObjToStr;
468 //
469 // Known in ILASM as [RTS]RTS::ObjToStr
470 public static void ObjToStr(System.Object obj, char[] str)
472 System.String lls;
473 if (obj.GetType().IsEnum) {
474 #if BETA1
475 lls = obj.ToString();
476 #else //BETA2
477 lls = System.Convert.ToString(System.Convert.ToInt64(obj));
478 #endif
480 else {
481 if (obj.GetType().Equals(typDouble)) {
482 #if BETA1
483 lls = System.Convert.ToDouble(obj).ToString();
484 #else //BETA2
485 lls = System.Convert.ToDouble(obj).ToString("R");
486 #endif
488 else if (obj.GetType().Equals(typSingle)) {
489 #if BETA1
490 lls = System.Convert.ToSingle(obj).ToString();
491 #else //BETA2
492 lls = System.Convert.ToSingle(obj).ToString("R");
493 #endif
495 else {
496 lls = System.Convert.ToString(obj);
500 int len = lls.Length;
501 if (len >= str.Length)
502 len = str.Length - 1;
503 lls.CopyTo(0, str, 0, len);
504 str[len] = '\0';
507 /* ------------------------------------------------------------ */
508 // PROCEDURE LongToStr*(i : LONGINT; OUT s : ARRAY OF CHAR);
509 // (** Decode a CP LONGINT into an array *)
510 // BEGIN END LongToStr;
511 //
512 // Known in ILASM as [RTS]RTS::LongToStr
513 public static void LongToStr(long num,
514 char[] str)
516 System.String lls = System.Convert.ToString(num);
517 int len = lls.Length;
518 if (len >= str.Length)
519 len = str.Length - 1;
520 lls.CopyTo(0, str, 0, len);
521 str[len] = '\0';
524 /* ------------------------------------------------------------ */
525 // PROCEDURE SRealToStr*(r : SHORTREAL; OUT s : ARRAY OF CHAR);
526 // (** Decode a CP REAL into an array *)
527 // BEGIN END SRealToStr;
528 //
529 // Known in ILASM as [RTS]RTS::SRealToStr
530 public static void SRealToStr(float num,
531 char[] str)
533 // System.String lls = System.Convert.ToString(num);
534 #if BETA1
535 System.String lls = ((System.Single) num).ToString();
536 #else //BETA2
537 System.String lls = ((System.Single) num).ToString("R");
538 #endif
539 int len = lls.Length;
540 lls.CopyTo(0, str, 0, len);
541 str[len] = '\0';
544 /* ------------------------------------------------------------ */
545 // PROCEDURE RealToStr*(r : REAL; OUT s : ARRAY OF CHAR);
546 // (** Decode a CP REAL into an array *)
547 // BEGIN END RealToStr;
548 //
549 // Known in ILASM as [RTS]RTS::RealToStr
550 public static void RealToStr(double num,
551 char[] str)
553 #if BETA1
554 System.String lls = System.Convert.ToString(num);
555 #else //BETA2
556 System.String lls = ((System.Double) num).ToString("R");
557 #endif
558 int len = lls.Length;
559 lls.CopyTo(0, str, 0, len);
560 str[len] = '\0';
563 /* ------------------------------------------------------------ */
564 // PROCEDURE RealToStrInvar*(r : REAL; OUT s : ARRAY OF CHAR);
565 // (** Decode a CP REAL into an array *)
566 // BEGIN END RealToStrInvar;
567 //
568 // Known in ILASM as [RTS]RTS::RealToStrInvar
569 public static void RealToStrInvar(double num,
570 char[] str)
572 #if BETA1
573 System.String lls = System.Convert.ToString(num);
574 #else //BETA2
575 System.String lls =
576 ((System.Double) num).ToString("R", invarCulture);
577 #endif
578 int len = lls.Length;
579 lls.CopyTo(0, str, 0, len);
580 str[len] = '\0';
583 /* ------------------------------------------------------------ */
584 // PROCEDURE RealToStrLocal*(r : REAL; OUT s : ARRAY OF CHAR);
585 // (** Decode a CP REAL into an array *)
586 // BEGIN END RealToStrInvar;
587 //
588 // Known in ILASM as [RTS]RTS::RealToStrLocal
589 public static void RealToStrLocal(double num,
590 char[] str)
592 #if BETA1
593 System.String lls = System.Convert.ToString(num);
594 #else //BETA2
595 System.String lls =
596 ((System.Double) num).ToString("R", currentCulture);
597 #endif
598 int len = lls.Length;
599 lls.CopyTo(0, str, 0, len);
600 str[len] = '\0';
603 /* ------------------------------------------------------------ */
604 //
605 // PROCEDURE realToLongBits(r : REAL) : LONGINT;
606 // (** Convert an ieee double into a longint with same bit pattern *)
607 //
608 public static long realToLongBits(double r)
610 byte[] tmp = System.BitConverter.GetBytes(r);
611 return System.BitConverter.ToInt64(tmp,0);
613 //
614 // PROCEDURE longBitsToReal(l : LONGINT) : REAL;
615 // (** Convert a longint into an ieee double with same bit pattern *)
616 //
617 public static double longBitsToReal(long l)
619 byte[] tmp = System.BitConverter.GetBytes(l);
620 return System.BitConverter.ToDouble(tmp,0);
623 /* ------------------------------------------------------------ */
624 //
625 // PROCEDURE shortRealToIntBits(r : SHORTREAL) : INTEGER;
626 // (** Convert an ieee double into a longint with same bit pattern *)
627 //
628 public static int shortRealToIntBits(float r)
630 byte[] tmp = System.BitConverter.GetBytes(r);
631 return System.BitConverter.ToInt32(tmp,0);
633 //
634 // PROCEDURE intBitsToShortReal(l : INTEGER) : SHORTREAL;
635 // (** Convert an int into an ieee float with same bit pattern *)
636 //
637 public static double intBitsToShortReal(int l)
639 byte[] tmp = System.BitConverter.GetBytes(l);
640 return System.BitConverter.ToSingle(tmp,0);
643 /* ------------------------------------------------------------ */
644 //
645 // PROCEDURE hiByte(l : SHORTINT) : BYTE;
646 // (** Get hi-significant byte of short *)
647 //
648 // Known in ILASM as [RTS]RTS::hiByte
649 public static sbyte hiByte(short i)
651 return (sbyte) (i >> 8);
653 //
654 // PROCEDURE loByte(l : SHORTINT) : BYTE;
655 // (** Get lo-significant byte of short *)
656 //
657 // Known in ILASM as [RTS]RTS::loByte
658 public static sbyte loByte(short i)
660 return (sbyte) i;
662 //
663 // PROCEDURE hiShort(l : INTEGER) : SHORTINT;
664 // (** Get hi-significant word of integer *)
665 //
666 // Known in ILASM as [RTS]RTS::hiShort
667 public static short hiShort(int i)
669 return (short) (i >> 16);
671 //
672 // PROCEDURE loShort(l : INTEGER) : SHORTINT;
673 // (** Get lo-significant word of integer *)
674 //
675 // Known in ILASM as [RTS]RTS::loShort
676 public static short loShort(int i)
678 return (short) i;
680 //
681 // PROCEDURE hiInt(l : LONGINT) : INTEGER;
682 // (** Get hi-significant word of long integer *)
683 //
684 // Known in ILASM as [RTS]RTS::hiInt
685 public static int hiInt(long l)
687 return (int) (l >> 32);
689 //
690 // PROCEDURE loInt(l : LONGINT) : INTEGER;
691 // (** Get lo-significant word of long integer *)
692 //
693 // Known in ILASM as [RTS]RTS::loInt
694 public static int loInt(long l)
696 return (int) l;
698 //
699 // PROCEDURE Throw(IN s : ARRAY OF CHAR);
700 // (** Abort execution with an error *)
701 //
702 // Known in ILASM as [RTS]RTS::Throw
703 public static void Throw(char[] s)
705 throw new System.Exception(new System.String(s));
707 //
708 // PROCEDURE GetMillis() : LONGINT;
709 //
710 // Known in ILASM as [RTS]RTS::GetMillis
711 public static long GetMillis()
713 return (System.DateTime.Now.Ticks / 10000);
715 //
716 // PROCEDURE GetDateString(OUT str : ARRAY OF CHAR);
717 //
718 // Known in ILASM as [RTS]RTS::GetDateString
719 public static void GetDateString(char[] arr)
721 System.String str = System.DateTime.Now.ToString();
722 int len = str.Length;
723 if (len >= arr.Length)
724 len = arr.Length - 1;
725 str.CopyTo(0, arr, 0, len);
726 arr[len] = '\0';
729 public static void ClassMarker(System.Object o)
731 System.Console.Write(o.GetType().ToString());
734 /* ------------------------------------------------------------ */
735 /* ------------------------------------------------------------ */
736 /* ------------------------------------------------------------ */
739 public class ProgArgs
740 // Known in ILASM as [RTS]ProgArgs
741 /*
742 * Library module for GP Component Pascal.
743 * This module allows access to the arguments in programs which
744 * import CPmain. It is accessible from modules which do NOT
745 * import CPmain.
747 * Original : kjg December 1999
748 */
750 public static System.String[] argList = null;
752 // Known in ILASM as [RTS]ProgArgs::ArgNumber
753 // PROCEDURE ArgNumber*() : INTEGER
754 public static int ArgNumber()
756 if (ProgArgs.argList == null)
757 return 0;
758 else
759 return argList.Length;
762 // Known in ILASM as [RTS]ProgArgs::GetArg
763 // PROCEDURE GetArg*(num : INTEGER; OUT arg : ARRAY OF CHAR)
764 public static void GetArg(int num, char[] arr)
766 int i;
767 if (argList == null && num < argList.Length) {
768 arr[0] = '\0';
769 } else {
770 System.String str = argList[num];
771 for (i = 0;
772 i < arr.Length && i < argList[num].Length;
773 i++) {
774 arr[i] = str[i];
776 if (i == arr.Length)
777 i--;
778 arr[i] = '\0';
782 public static void GetEnvVar(char[] name, char[] valu) {
783 System.String nam = CP_rts.mkStr(name);
784 System.String val = System.Environment.GetEnvironmentVariable(nam);
785 CP_rts.StrToChF(valu, val);
788 } // end of public class ProgArgs
790 /* ------------------------------------------------------------ */
791 /* ------------------------------------------------------------ */
792 /* ------------------------------------------------------------ */
794 public class CP_rts
795 // Known in ILASM as [RTS]CP_rts
796 /*
797 * It is a fundamental principle that the facilities of CP_rts
798 * are known to the particular compiler backend, but are
799 * not accessible to the Programmer. The programmer-accessible
800 * parts of the runtime are known via the module RTS.
801 */
803 /* ==================================================================== *
804 * MOD and DIV helpers. With correction factors *
805 * ==================================================================== */
807 // Known in ILASM as [RTS]CP_rts::CpModI
808 public static int CpModI(int lVal, int rVal)
810 // A correction is required if the signs of
811 // the two operands are different, but the
812 // remainder is non-zero. Inc rem by rVal.
813 int rslt = lVal % rVal;
814 if ((lVal < 0 != rVal < 0) && (rslt != 0))
815 rslt += rVal;
816 return rslt;
819 // Known in ILASM as [RTS]CP_rts::CpDivI
820 public static int CpDivI(int lVal, int rVal)
822 // A correction is required if the signs of
823 // the two operands are different, but the
824 // remainder is non-zero. Dec quo by 1.
825 int rslt = lVal / rVal;
826 int remV = lVal % rVal;
827 if ((lVal < 0 != rVal < 0) && (remV != 0))
828 rslt--;
829 return rslt;
832 // Known in ILASM as [RTS]CP_rts::CpModL
833 public static long CpModL(long lVal, long rVal)
835 // A correction is required if the signs of
836 // the two operands are different, but the
837 // remainder is non-zero. Inc rem by rVal.
838 long rslt = lVal % rVal;
839 if ((lVal < 0 != rVal < 0) && (rslt != 0))
840 rslt += rVal;
841 return rslt;
844 // Known in ILASM as [RTS]CP_rts::CpDivL
845 public static long CpDivL(long lVal, long rVal)
847 // A correction is required if the signs of
848 // the two operands are different, but the
849 // remainder is non-zero. Dec quo by 1.
850 long rslt = lVal / rVal;
851 long remV = lVal % rVal;
852 if ((lVal < 0 != rVal < 0) && (remV != 0))
853 rslt--;
854 return rslt;
857 /* ==================================================================== *
858 * Various string and char-array helpers *
859 * ==================================================================== */
861 // Known in ILASM as [RTS]CP_rts::mkStr
862 public static System.String mkStr(char[] arr) {
863 int len = chrArrLength(arr);
864 return new System.String(arr,0,len);
867 /* -------------------------------------------------------------------- */
869 // Known in ILASM as [RTS]CP_rts::caseMesg
870 public static System.String caseMesg(int i)
872 System.String s = "CASE-trap: selector = " + i;
873 return s;
876 /* -------------------------------------------------------------------- */
878 // Known in ILASM as [RTS]CP_rts::withMesg
879 public static System.String withMesg(System.Object o)
881 // System.String c = o.getClass().getName();
882 // c = c.substring(c.lastIndexOf('.') + 1);
883 // c = "WITH else-trap: type = " + c;
884 // NEEDS TO USE LIGHTNING REFLECTION SERVICES HERE
885 string c = "WITH else-trap: type = " + o.ToString();
886 return c;
889 /* -------------------------------------------------------------------- */
891 // Known in ILASM as [RTS]CP_rts::chrArrLength
892 public static int chrArrLength(char[] src)
894 int ix = 0;
895 char ch;
896 do {
897 ch = src[ix];
898 ix++;
899 } while (ch != '\0');
900 // System.Console.Write(ix-1);
901 // System.Console.Write(' ');
902 // System.Console.WriteLine(src);
903 return ix-1;
906 /* -------------------------------------------------------------------- */
908 // Known in ILASM as [RTS]CP_rts::chrArrLplus1
909 public static int chrArrLplus1(char[] src)
911 int ix = 0;
912 char ch;
913 do {
914 ch = src[ix];
915 ix++;
916 } while (ch != '\0');
917 return ix;
920 /* -------------------------------------------------------------------- */
922 // Known in ILASM as [RTS]CP_rts::strToChO
923 public static char[] strToChO(System.String input)
925 if (input == null) return null;
927 int len = input.Length;
928 char[] arr = new char[len+1];
929 input.CopyTo(0, arr, 0, len);
930 arr[len] = '\0';
931 return arr;
932 // return input.ToCharArray();
935 /* -------------------------------------------------------------------- */
937 // Known in ILASM as [RTS]CP_rts::StrToChF
938 public static void StrToChF(char[] res, System.String inp)
940 if (inp == null) {
941 res[0] = '\0'; return;
943 int len = inp.Length;
944 inp.CopyTo(0, res, 0, len);
945 res[len] = '\0';
948 /* -------------------------------------------------------------------- */
950 // Known in ILASM as [RTS]CP_rts::Stringify
951 public static void Stringify(char[] dst, char[] src)
953 int ix = 0;
954 char ch;
955 do {
956 ch = src[ix];
957 dst[ix] = ch;
958 ix++;
959 } while (ch != '\0');
962 /* -------------------------------------------------------------------- */
964 // Known in ILASM as [RTS]CP_rts::ChrArrCheck
965 public static void ChrArrCheck(char[] src)
967 int ix = 0;
968 char ch;
969 do {
970 ch = src[ix];
971 if (ch > 0xFF) throw new
972 System.Exception("error applying SHORT to array");
973 ix++;
974 } while (ch != '\0');
977 /* -------------------------------------------------------------------- */
979 // Known in ILASM as [RTS]CP_rts::strCmp
980 public static int strCmp(char[] l, char[] r)
982 int minL;
983 int lLen = chrArrLength(l);
984 int rLen = chrArrLength(r);
985 if (lLen < rLen) minL = lLen; else minL = rLen;
986 // System.Console.WriteLine();
987 for (int ix = 0; ix < minL; ix++) {
988 char lCh = l[ix];
989 char rCh = r[ix];
990 if (lCh < rCh) return -1;
991 else if (lCh > rCh) return 1;
993 if (lLen < rLen) return -1;
994 else if (lLen > rLen) return 1;
995 else return 0;
998 /* ==================================================================== *
999 * String concatenation helper methods *
1000 * ==================================================================== */
1002 // Known in ILASM as [RTS]CP_rts::aaToStr
1003 public static System.String aaToStr(char[] l, char[] r)
1005 int llen = chrArrLength(l);
1006 int rlen = chrArrLength(r);
1007 System.Text.StringBuilder buff =
1008 new System.Text.StringBuilder(llen + rlen);
1009 return buff.Append(l,0,llen).Append(r,0,rlen).ToString();
1012 // Known in ILASM as [RTS]CP_rts::asToStr
1013 public static System.String asToStr(char[] l, System.String r)
1015 int llen = chrArrLength(l);
1016 System.Text.StringBuilder buff =
1017 new System.Text.StringBuilder(3 * llen);
1018 return buff.Append(l,0,llen).Append(r).ToString();
1021 // Known in ILASM as [RTS]CP_rts::saToStr
1022 public static System.String saToStr(System.String l, char[] r)
1024 int rlen = chrArrLength(r);
1025 System.Text.StringBuilder buff =
1026 new System.Text.StringBuilder(3 * rlen);
1027 return buff.Append(l).Append(r,0,rlen).ToString();
1030 // Known in ILASM as [RTS]CP_rts::ssToStr
1031 public static System.String ssToStr(System.String l, System.String r)
1033 System.Text.StringBuilder buff =
1034 new System.Text.StringBuilder(l);
1035 return buff.Append(r).ToString();
1039 /* ==================================================================== */
1040 /* ==================================================================== */
1041 /* ==================================================================== */
1042 /* ==================================================================== */
1044 public class NativeStrings
1045 // Known in ILASM as [RTS]NativeStrings
1047 /* -------------------------------------------------------------------- */
1048 //
1049 // PROCEDURE mkStr*(IN s : ARRAY OF CHAR) : String; BEGIN RETURN NIL END mkStr;
1050 //
1051 /* -------------------------------------------------------------------- */
1052 // Known in ILASM as [RTS]NativeStrings::mkStr
1053 public static System.String mkStr(char[] arr) {
1054 int len = CP_rts.chrArrLength(arr);
1055 return new System.String(arr,0,len);
1058 /* -------------------------------------------------------------------- */
1059 //
1060 // PROCEDURE mkArr*(s : String) : RTS.CharOpen); END mkArr;
1061 //
1062 /* -------------------------------------------------------------------- */
1063 // Known in ILASM as [RTS]NativeStrings::mkArr
1064 public static char[] mkArr(System.String inp)
1066 if (inp == null) return null;
1068 int len = inp.Length;
1069 char[] res = new char[len+1];
1070 inp.CopyTo(0, res, 0, len);
1071 res[len] = '\0';
1072 return res;
1075 /* ==================================================================== */
1076 /* ==================================================================== */
1077 //
1078 // Body of Console interface.
1079 // This file implements the code of the Console.cp file.
1080 // kjg May 2000.
1081 //
1082 public class Console
1083 // Known in ILASM as [RTS]Console
1085 public static void WriteLn()
1087 System.Console.WriteLine();
1090 public static void Write(char ch)
1092 System.Console.Write(ch);
1095 private static char[] strRep(int val)
1097 if (val < 0) { // ==> must be minInt
1098 char[] min = {' ',' ','2','1','4','7','4','8','3','6','4','8'};
1099 return min;
1102 char[] str = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
1103 str[11] = (char) (val % 10 + (int) '0'); val = val / 10;
1104 for (int i = 10; val != 0; i--) {
1105 str[i] = (char) (val % 10 + (int) '0'); val = val / 10;
1107 return str;
1110 public static void WriteInt(int val, int fwd)
1112 char[] str = (val >= 0 ? strRep(val) : strRep(-val));
1114 int blank;
1115 for (blank = 0; str[blank] == ' '; blank++)
1117 if (val < 0) {
1118 str[blank-1] = '-'; blank--;
1120 // format ...
1121 // 01...............901
1122 // _________xxxxxxxxxxx
1123 // <-blank->< 12-blank>
1124 // <-----fwd------>
1125 if (fwd == 0) // magic case, put out exactly one blank
1126 System.Console.Write(str, blank-1, 13-blank);
1127 else if (fwd < (12-blank))
1128 System.Console.Write(str, blank, 12-blank);
1129 else if (fwd <= 12)
1130 System.Console.Write(str, 12-fwd, fwd);
1131 else { // fwd > 12
1132 for (int i = fwd-12; i > 0; i--)
1133 System.Console.Write(" ");
1134 System.Console.Write(str);
1138 public static void WriteHex(int val, int wid)
1140 char[] str = new char[9];
1141 int j; // index of last blank
1142 int i = 8;
1143 do {
1144 int dig = val & 0xF;
1145 val = (int)((uint) val >> 4);
1146 if (dig >= 10)
1147 str[i] = (char) (dig + ((int) 'A' - 10));
1148 else
1149 str[i] = (char) (dig + (int) '0');
1150 i--;
1151 } while (val != 0);
1152 j = i;
1153 while (i >= 0) {
1154 str[i] = ' '; i--;
1156 if (wid == 0) // special case, exactly one blank
1157 System.Console.Write(str, j, 9-j);
1158 else if (wid < (8-j))
1159 System.Console.Write(str, j+1, 8-j);
1160 else if (wid <= 9)
1161 System.Console.Write(str, 9-wid, wid);
1162 else {
1163 for (i = wid-9; i > 0; i--)
1164 System.Console.Write(" ");
1165 System.Console.Write(str);
1170 public static void WriteString(char[] str)
1172 int len = str.Length;
1173 for (int i = 0; i < len && str[i] != '\0'; i++)
1174 System.Console.Write(str[i]);
1176 } // end of public class Console
1177 /* ==================================================================== */
1178 /* ==================================================================== */
1179 //
1180 // Body of StdIn module.
1181 // This file implements the code of the StdIn.cp file.
1182 // kjg Sep 2004.
1183 //
1184 // Known in ILASM as [RTS]StdIn
1185 //
1186 public class StdIn
1188 public static void Read(ref char c) {
1189 int chr = System.Console.Read();
1190 if (chr == -1) {
1191 c = '\0';
1192 } else {
1193 c = (char) chr;
1197 public static void ReadLn(char[] arr) {
1198 string str = System.Console.ReadLine();
1199 if (str == null) {
1200 arr[0] = '\0'; return;
1202 int dLen = arr.Length;
1203 int sLen = str.Length;
1204 int cLen = (sLen < dLen ? sLen : dLen-1);
1205 str.CopyTo(0, arr, 0, cLen);
1206 arr[cLen] = '\0';
1209 public static void SkipLn() {
1210 string str = System.Console.ReadLine();
1213 public static bool More() {
1214 return true; // temporary, until we figure out how to get the same
1215 } // semantics on .NET and the JVM (kjg Sep. 2004
1217 } // end of public class StdIn
1219 /* ==================================================================== */
1220 /* ==================================================================== */
1221 //
1222 // Body of Error interface.
1223 // This file implements the code of the Error.cp file.
1224 // kjg May 2000.
1225 //
1226 public class Error
1227 // Known in ILASM as [RTS]Error
1229 public static void WriteLn()
1231 System.Console.Error.Write(System.Environment.NewLine);
1234 public static void Write(char ch)
1236 System.Console.Error.Write(ch);
1239 private static char[] strRep(int val)
1241 if (val < 0) { // ==> must be minInt
1242 char[] min = {' ',' ','2','1','4','7','4','8','3','6','4','8'};
1243 return min;
1246 char[] str = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
1247 str[11] = (char) (val % 10 + (int) '0'); val = val / 10;
1248 for (int i = 10; val != 0; i--) {
1249 str[i] = (char) (val % 10 + (int) '0'); val = val / 10;
1251 return str;
1254 public static void WriteInt(int val, int fwd)
1256 char[] str = (val >= 0 ? strRep(val) : strRep(-val));
1258 int blank;
1259 for (blank = 0; str[blank] == ' '; blank++)
1261 if (val < 0) {
1262 str[blank-1] = '-'; blank--;
1264 // format ...
1265 // 01...............901
1266 // _________xxxxxxxxxxx
1267 // <-blank->< 12-blank>
1268 // <-----fwd------>
1269 if (fwd == 0) // magic case, put out exactly one blank
1270 System.Console.Error.Write(str, blank-1, 13-blank);
1271 else if (fwd < (12-blank))
1272 System.Console.Error.Write(str, blank, 12-blank);
1273 else if (fwd <= 12)
1274 System.Console.Error.Write(str, 12-fwd, fwd);
1275 else { // fwd > 12
1276 for (int i = fwd-12; i > 0; i--)
1277 System.Console.Error.Write(" ");
1278 System.Console.Error.Write(str);
1282 public static void WriteHex(int val, int wid)
1284 char[] str = new char[9];
1285 int j; // index of last blank
1286 int i = 8;
1287 do {
1288 int dig = val & 0xF;
1289 val = (int)((uint) val >> 4);
1290 if (dig >= 10)
1291 str[i] = (char) (dig + ((int) 'A' - 10));
1292 else
1293 str[i] = (char) (dig + (int) '0');
1294 i--;
1295 } while (val != 0);
1296 j = i;
1297 while (i >= 0) {
1298 str[i] = ' '; i--;
1300 if (wid == 0) // special case, exactly one blank
1301 System.Console.Error.Write(str, j, 9-j);
1302 else if (wid < (8-j))
1303 System.Console.Error.Write(str, j+1, 8-j);
1304 else if (wid <= 9)
1305 System.Console.Error.Write(str, 9-wid, wid);
1306 else {
1307 for (i = wid-9; i > 0; i--)
1308 System.Console.Error.Write(" ");
1309 System.Console.Error.Write(str);
1314 public static void WriteString(char[] str)
1316 int len = str.Length;
1317 for (int i = 0; i < len && str[i] != '\0'; i++)
1318 System.Console.Error.Write(str[i]);
1320 } // end of public class Error
1322 /* ==================================================================== */
1324 abstract public class XHR
1325 // Known in ILASM as [RTS]XHR
1327 /* ==================================================================== *
1328 * eXplicit Heap-allocated activation Record *
1329 * ==================================================================== */
1330 public XHR prev;
1332 /* ==================================================================== */
1334 namespace Vectors {
1335 public abstract class VecBase {
1336 public int tide;
1338 public abstract void expand();
1341 public class VecChr : VecBase {
1342 public char[] elms;
1344 public override void expand() {
1345 char[] tmp = new char[this.elms.Length * 2];
1346 for (int i = 0; i < this.tide; i++) {
1347 tmp[i] = this.elms[i];
1349 this.elms = tmp;
1353 public class VecI32 : VecBase {
1354 public int[] elms;
1356 public override void expand() {
1357 int[] tmp = new int[this.elms.Length * 2];
1358 for (int i = 0; i < this.tide; i++) {
1359 tmp[i] = this.elms[i];
1361 this.elms = tmp;
1365 public class VecI64 : VecBase {
1366 public long[] elms;
1368 public override void expand() {
1369 long[] tmp = new long[this.elms.Length * 2];
1370 for (int i = 0; i < this.tide; i++) {
1371 tmp[i] = this.elms[i];
1373 this.elms = tmp;
1377 public class VecR32 : VecBase {
1378 public float[] elms;
1380 public override void expand() {
1381 float[] tmp = new float[this.elms.Length * 2];
1382 for (int i = 0; i < this.tide; i++) {
1383 tmp[i] = this.elms[i];
1385 this.elms = tmp;
1389 public class VecR64 : VecBase {
1390 public double[] elms;
1392 public override void expand() {
1393 double[] tmp = new double[this.elms.Length * 2];
1394 for (int i = 0; i < this.tide; i++) {
1395 tmp[i] = this.elms[i];
1397 this.elms = tmp;
1401 public class VecRef : VecBase {
1402 public object[] elms;
1404 public override void expand() {
1405 object[] tmp = new object[this.elms.Length * 2];
1406 for (int i = 0; i < this.tide; i++) {
1407 tmp[i] = this.elms[i];
1409 this.elms = tmp;
1414 /* ==================================================================== */
1419 /* ==================================================================== */