home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
pot
/
potsrc
/
lib
/
Core
/
c
/
pOtRTL
< prev
next >
Wrap
Text File
|
1995-02-21
|
19KB
|
733 lines
/* pOt RTL implementation file, DT Wed Jan 26 1994 */
#include <pOtRTL.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#ifdef __sun__
#define memmove(d,s,size) bcopy(s,d,size)
#endif
#define PtrSize 4
#define MinChar 0x0
#define MaxChar 0x0FF
#define MinBool 0
#define MaxBool 1
#define MinSInt -128
#define MaxSInt 127
#define MinInt -32768
#define MaxInt 32767
#define MinLInt ((pOt_LONGINT)0x80000000)
#define MaxLInt 0x7FFFFFFF
#define MinReal -3.40282347E+38
#define MaxReal 3.40282347E+38
#define MinLReal -1.7976931348623157E+308
#define MaxLReal 1.7976931348623157E+308
#define MinSet 0
#define MaxSet 31
typedef struct pOt__tag_gc_node {
struct pOt__tag_gc_node *next;
void *pvar[1];
} pOt__gc_node;
int pOt__gc_enabled = 1;
pOt__gc_node *pOt__gc_root = pOt_NIL;
char *pOt__parfilename = NULL;
extern void pOt__gc pOt__ARGS((void));
static void pOt__gc_register pOt__ARGS((void *p, pOt_LONGINT size));
void pOt__init_var(rec,td)
pOt__TypDsc **rec; pOt__TypDsc *td;
{
*rec = td;
switch(td->mode) {
case 0: /* rec */ {
pOt_LONGINT i, stop;
pOt__RecTypDsc *rtd = (pOt__RecTypDsc *)td;
for(;;) {
stop = rtd->nstr; i = 0;
while(i != stop) {
pOt__init_var((pOt__TypDsc**)((char *)rec + rtd->tab[i].poffs), rtd->tab[i].fld_td);
i++;
}
stop += rtd->nptr + rtd->npro;
while(i != stop) {
*(void **)((char *)rec + rtd->tab[i++].poffs) = pOt_NIL;
}
if(!(i = rtd->extlev)) break;
rtd = rtd->base_td[i-1];
}
}
break;
case 1: /* basic arr */
/* no initialization required */
break;
case 2: /* ptr arr */
case 3: /* proc arr */
{
pOt_LONGINT i;
pOt__PtrArrTypDsc *atd = (pOt__PtrArrTypDsc *)td;
i = 0; rec = (pOt__TypDsc**)((char*)rec + sizeof(pOt__PtrArrTypDsc *));
while(i++ != atd->nofel) {
*rec = pOt_NIL;
rec = (pOt__TypDsc**)((char*)rec + atd->elsize);
}
}
break;
case 4: /* rec arr */ {
pOt_LONGINT i;
pOt__StrArrTypDsc *atd = (pOt__StrArrTypDsc *)td;
i = 0; rec = (pOt__TypDsc**)((char*)rec + sizeof(pOt__StrArrTypDsc *));
while(i++ != atd->nofel) {
pOt__init_var(rec, atd->base_td);
rec = (pOt__TypDsc**)((char*)rec + atd->elsize);
}
}
break;
}
}
/* halt */
void pOt__halt(
char *filename, unsigned long line, pOt_SHORTINT trapnum)
{
printf("\n%s(%lu):trap %i\n", filename, line, trapnum);
exit(trapnum);
}
/* checks */
pOt_LONGINT pOt__inxchk(
char *filename, unsigned long line, pOt_LONGINT len, pOt_LONGINT li)
{
if((0 > li) || (li >= len)) {
pOt__halt(filename,line,3);
}
return li;
}
void *pOt__nilchk(filename,line,ptr)
char *filename; unsigned long line; void *ptr;
{
if(ptr == NULL) pOt__halt(filename,line,5);
return ptr;
}
pOt_REAL pOt__rngchk_r(filename,line,lr)
char *filename; unsigned long line; pOt_LONGREAL lr;
{
if((lr < MinReal) || (MaxReal < lr)) pOt__halt(filename,line,4);
return (pOt_REAL)lr;
}
pOt_LONGINT pOt__rngchk_li(filename,line,lr)
char *filename; unsigned long line; pOt_LONGREAL lr;
{
pOt_LONGREAL flr;
flr = floor(lr);
if((flr < (pOt_LONGREAL)MinLInt) || ((pOt_LONGREAL)MaxLInt < flr)) pOt__halt(filename,line,4);
return (pOt_LONGINT)flr;
}
pOt_INTEGER pOt__rngchk_i(filename,line,li)
char *filename; unsigned long line; pOt_LONGINT li;
{
if((li < MinInt) || (MaxInt < li)) pOt__halt(filename,line,4);
return (pOt_INTEGER)li;
}
pOt_SHORTINT pOt__rngchk_si(
char *filename, unsigned long line, pOt_INTEGER i)
{
if((i < MinSInt) || (MaxSInt < i)) pOt__halt(filename,line,4);
return (pOt_SHORTINT)i;
}
pOt_SHORTINT pOt__rngchk_se(filename,line,i)
char *filename; unsigned long line; pOt_LONGINT i;
{
if((i < MinSet) || (MaxSet < i)) pOt__halt(filename,line,4);
return (pOt_SHORTINT)i;
}
unsigned char pOt__rngchk_cn(filename,line,li)
char *filename; unsigned long line; pOt_LONGINT li;
{
if((li < MinChar) || (MaxChar < li)) pOt__halt(filename,line,4);
return (unsigned char)li;
}
pOt__RecTypDsc **pOt__typchk(filename,line,rec,td,extlev)
char *filename; unsigned long line; pOt__RecTypDsc**rec; pOt__RecTypDsc *td; pOt_LONGINT extlev;
{
if((((*rec)->extlev > extlev) && ((*rec)->base_td[extlev] != td)) || ((*rec) != td)) pOt__halt(filename,line,18);
return rec;
}
/* operations */
pOt_LONGINT pOt__div(x,y)
pOt_LONGINT x; pOt_LONGINT y;
{
if(x >= 0) return x/y; return -((-x - 1)/y + 1);
}
pOt_LONGINT pOt__addchk(
char *filename, unsigned long line, pOt_LONGINT x, pOt_LONGINT y, pOt_SHORTINT typ)
{
x += y;
switch(typ) {
case 4: if((MinSInt > x) || (x > MaxSInt)) pOt__halt(filename,line,4); break;
case 5: if((MinInt > x) || (x > MaxInt)) pOt__halt(filename,line,4); break;
case 6: break;
default: pOt__halt(filename,line,16); break;
}
return x;
}
pOt_LONGINT pOt__subchk(
char *filename, unsigned long line, pOt_LONGINT x, pOt_LONGINT y, pOt_SHORTINT typ)
{
x -= y;
switch(typ) {
case 4: if((MinSInt > x) || (x > MaxSInt)) pOt__halt(filename,line,4); break;
case 5: if((MinInt > x) || (x > MaxInt)) pOt__halt(filename,line,4); break;
case 6: break;
default: pOt__halt(filename,line,16); break;
}
return x;
}
pOt_LONGINT pOt__mulchk(
char *filename, unsigned long line, pOt_LONGINT x, pOt_LONGINT y, pOt_SHORTINT typ)
{
x *= y;
switch(typ) {
case 4: if((MinSInt > x) || (x > MaxSInt)) pOt__halt(filename,line,4); break;
case 5: if((MinInt > x) || (x > MaxInt)) pOt__halt(filename,line,4); break;
case 6: break;
default: pOt__halt(filename,line,16); break;
}
return x;
}
pOt_LONGINT pOt__divchk(
char *filename, unsigned long line, pOt_LONGINT x, pOt_LONGINT y, pOt_SHORTINT typ)
{
if(y == 0) pOt__halt(filename,line,6);
if(y < 0) pOt__halt(filename,line,7);
if(x >= 0) return x/y; return -((-x - 1)/y + 1);
}
pOt_LONGINT pOt__mod(x,y)
pOt_LONGINT x; pOt_LONGINT y;
{
if(x >= 0) return x%y; return y - 1 - (-x-1)%y;
}
pOt_LONGINT pOt__modchk(
char *filename, unsigned long line, pOt_LONGINT x, pOt_LONGINT y, pOt_SHORTINT typ)
{
if(y == 0) pOt__halt(filename,line,6);
if(y < 0) pOt__halt(filename,line,7);
if(x >= 0) return x%y; return y - 1 - (-x-1)%y;
}
pOt_BOOLEAN pOt__typtest(rec,td,extlev)
pOt__RecTypDsc **rec; pOt__RecTypDsc *td; pOt_LONGINT extlev;
{
if((*rec)->extlev > extlev) return (*rec)->base_td[extlev] == td;
return (*rec) == td;
}
/* strings relations */
pOt_BOOLEAN pOt__cmpss(
pOt_CHAR *s1, pOt_CHAR *s2, pOt_INTEGER op)
{
pOt_LONGINT i;
s1 += sizeof(pOt__ArrTypDsc*); s2 += sizeof(pOt__ArrTypDsc*);
i = 0; while((s1[i] != '\0') && (s1[i] == s2[i])) i++;
if(s1[i] == s2[i]) {
switch(op) {
case 9: case 12: case 14: return pOt_TRUE;
case 10: case 11: case 13: return pOt_FALSE;
}
}
else {
switch(op) {
case 9: return pOt_FALSE;
case 10: return pOt_TRUE;
case 11: case 12: return s1[i] < s2[i];
case 13: case 14: return s1[i] > s2[i];
}
}
}
pOt_BOOLEAN pOt__cmpsc(
pOt_CHAR *s1, pOt_CHAR c2, pOt_INTEGER op)
{
s1 += sizeof(pOt__ArrTypDsc*);
if(s1[0] == c2) {
switch(op) {
case 9: return s1[1] == '\0';
case 10: return s1[1] != '\0';
case 11: return pOt_FALSE;
case 12: return s1[1] == '\0';
case 13: return s1[1] > '\0';
case 14: return s1[1] >= '\0';
}
}
else {
switch(op) {
case 9: return pOt_FALSE;
case 10: return pOt_TRUE;
case 11: case 12: return s1[0] < c2;
case 13: case 14: return s1[0] > c2;
}
}
}
pOt_BOOLEAN pOt__cmpcs(
pOt_CHAR c1, pOt_CHAR *s2, pOt_INTEGER op)
{
s2 += sizeof(pOt__ArrTypDsc*);
if(c1 == s2[0]) {
switch(op) {
case 9: return s2[1] == '\0';
case 10: return s2[1] != '\0';
case 11: return s2[1] > '\0';
case 12: return s2[1] >= '\0';
case 13: return pOt_FALSE;
case 14: return s2[1] == '\0';
}
}
else {
switch(op) {
case 9: return pOt_FALSE;
case 10: return pOt_TRUE;
case 11: case 12: return c1 < s2[0];
case 13: case 14: return c1 > s2[0];
}
}
}
/* built-in functions */
void pOt__new(filename,lin