Run lang/basic/lib through clang-format.

This commit is contained in:
David Given 2016-12-12 21:16:32 +01:00
parent a53b51001b
commit 04e54d6cb1
26 changed files with 534 additions and 438 deletions

View file

@ -3,7 +3,8 @@
void asrt(int b) void asrt(int b)
{ {
if(!b){ if (!b)
{
printf("ASSERTION ERROR\n"); printf("ASSERTION ERROR\n");
abort(); abort();
} }

View file

@ -10,4 +10,3 @@
#include <math.h> #include <math.h>
double _atn(double x) { return atan(x); } double _atn(double x) { return atan(x); }

View file

@ -3,10 +3,12 @@
int _cint(double f) int _cint(double f)
{ {
int r; int r;
if( f<-32768 || f>32767) error(4); if (f < -32768 || f > 32767)
error(4);
if (f < 0) if (f < 0)
r = f - 0.5; r = f - 0.5;
else r= f+0.5; else
r = f + 0.5;
return (r); return (r);
} }
@ -21,10 +23,13 @@ double _trunc(double f)
double _fcint(double f) double _fcint(double f)
{ {
long r; long r;
if(f<0){ if (f < 0)
{
r = -f; r = -f;
r = -r - 1; r = -r - 1;
}else r= f; }
else
r = f;
f = r; f = r;
return (f); return (f);
} }
@ -32,7 +37,8 @@ int _fix(double f)
{ {
int r; int r;
if( f<-32768.0 || f>32767.0) error(4); if (f < -32768.0 || f > 32767.0)
error(4);
r = _sgn(f) * _fcint((f > 0.0 ? f : -f)); r = _sgn(f) * _fcint((f > 0.0 ? f : -f));
return (r); return (r);
} }

View file

@ -57,7 +57,8 @@ void error(int index)
_setline(); _setline();
if (index < 0 || index > 40) if (index < 0 || index > 40)
printf("LINE %d:ERROR %d: Unprintable error\n", _erlsym, index); printf("LINE %d:ERROR %d: Unprintable error\n", _erlsym, index);
else printf("LINE %d:ERROR %d: %s\n",_erlsym,index,errortable[index]); else
printf("LINE %d:ERROR %d: %s\n", _erlsym, index, errortable[index]);
_errsym = index; _errsym = index;
_trap(); _trap();
} }

View file

@ -12,8 +12,6 @@ Filedesc _fdtable[16];
1-15 user files 1-15 user files
*/ */
int _chann = -1; int _chann = -1;
FILE* _chanrd = stdin; FILE* _chanrd = stdin;
FILE* _chanwr = stdout; FILE* _chanwr = stdout;
@ -42,7 +40,8 @@ void _asschn(void)
#ifdef DEBUG #ifdef DEBUG
printf("_asschn %d\n", _chann); printf("_asschn %d\n", _chann);
#endif #endif
if( _chann == -1) return; if (_chann == -1)
return;
#ifdef DEBUG #ifdef DEBUG
printf(" file %d\n", _fdtable[_chann].fd); printf(" file %d\n", _fdtable[_chann].fd);
#endif #endif
@ -56,9 +55,11 @@ void _asschn(void)
void _clochn(int nr) void _clochn(int nr)
{ {
if( nr<1 || nr >15 || _fdtable[nr].fd==0) error(3); if (nr < 1 || nr > 15 || _fdtable[nr].fd == 0)
error(3);
fclose(_fdtable[nr].fd); fclose(_fdtable[nr].fd);
_fdtable[nr].fd=0; _fdtable[nr].fname=0; _fdtable[nr].fd = 0;
_fdtable[nr].fname = 0;
} }
void _opnchn(int reclen, String* fname, String* mode) void _opnchn(int reclen, String* fname, String* mode)
@ -110,12 +111,14 @@ int _ioeof(int channel)
{ {
FILE* fd; FILE* fd;
char c; char c;
if( channel<0 || channel >15) error(3); if (channel < 0 || channel > 15)
error(3);
fd = _fdtable[channel].fd; fd = _fdtable[channel].fd;
if (fd == 0) if (fd == 0)
error(3); error(3);
c = fgetc(fd); c = fgetc(fd);
if( feof(_fdtable[channel].fd) ) return(-1); if (feof(_fdtable[channel].fd))
return (-1);
ungetc(c, fd); ungetc(c, fd);
return (0); return (0);
} }

View file

@ -14,16 +14,24 @@ void _out(char* str)
{ {
int pos; int pos;
if( _chann== -1) pos= _pos; if (_chann == -1)
else pos= _fdtable[_chann].pos; pos = _pos;
else
pos = _fdtable[_chann].pos;
while (*str) while (*str)
{ {
if( pos>= _width){ _outnl(); pos=0;} if (pos >= _width)
{
_outnl();
pos = 0;
}
fputc(*str++, _chanwr); fputc(*str++, _chanwr);
pos++; pos++;
} }
if( _chann== -1) _pos=pos; if (_chann == -1)
else _fdtable[_chann].pos= pos; _pos = pos;
else
_fdtable[_chann].pos = pos;
} }
void _outnl(void) void _outnl(void)
@ -40,8 +48,10 @@ void _zone(void)
int pos; int pos;
if (_chann == -1) if (_chann == -1)
pos = _pos; pos = _pos;
else pos= _fdtable[_chann].pos; else
do{ pos = _fdtable[_chann].pos;
do
{
fputc(' ', _chanwr); fputc(' ', _chanwr);
pos++; pos++;
if (pos == _width) if (pos == _width)
@ -51,8 +61,10 @@ void _zone(void)
break; break;
} }
} while (pos % _zonewidth != 0); } while (pos % _zonewidth != 0);
if( _chann== -1) _pos=pos; if (_chann == -1)
else _fdtable[_chann].pos= pos; _pos = pos;
else
_fdtable[_chann].pos = pos;
} }
void _in(char* buf) void _in(char* buf)
{ {
@ -62,26 +74,36 @@ void _in(char* buf)
if (_chann == -1) if (_chann == -1)
{ {
pos = _pos; pos = _pos;
}else pos= _fdtable[_chann].pos; }
else
pos = _fdtable[_chann].pos;
c = buf; c = buf;
while( (holder = fgetc(_chanrd)) != EOF && holder != '\n'){ while ((holder = fgetc(_chanrd)) != EOF && holder != '\n')
{
*c = holder; *c = holder;
if( _chann == -1) putchar(holder); if (_chann == -1)
c++; pos++; putchar(holder);
c++;
pos++;
} }
*c = 0; *c = 0;
if (_chann == -1) if (_chann == -1)
{ {
_pos = pos; _pos = pos;
} else _fdtable[_chann].pos= pos; }
else
_fdtable[_chann].pos = pos;
} }
void _tab(int x) void _tab(int x)
{ {
if( x> _width) error(3); if (x > _width)
if( x< _pos) _outnl(); error(3);
if (x < _pos)
_outnl();
_spc(x - _pos); _spc(x - _pos);
} }
void _spc(int x) void _spc(int x)
{ {
while(x-->0) _out(" "); while (x-- > 0)
_out(" ");
} }

View file

@ -1,4 +1,3 @@
#include <math.h> #include <math.h>
double _power(double x, double y) { return pow(x, y); } double _power(double x, double y) { return pow(x, y); }

View file

@ -25,35 +25,47 @@ void _prinum(int i)
_asschn(); _asschn();
if (i >= 0) if (i >= 0)
sprintf(buffer, " %d ", i); sprintf(buffer, " %d ", i);
else sprintf(buffer,"-%d ",-i); else
sprintf(buffer, "-%d ", -i);
_out(buffer); _out(buffer);
} }
void _str(double f, char* buffer) void _str(double f, char* buffer)
{ {
register char* c = buffer; register char* c = buffer;
int eformat = 0; int eformat = 0;
if( f>=0){ if (f >= 0)
if( f> 1.0e8) { {
if (f > 1.0e8)
{
eformat = 1; eformat = 1;
sprintf(buffer, " %e", f); sprintf(buffer, " %e", f);
} }
else sprintf(buffer," %f",f); else
sprintf(buffer, " %f", f);
c++; c++;
}else { }
if(-f> 1.0e8) { else
{
if (-f > 1.0e8)
{
eformat = 1; eformat = 1;
sprintf(buffer, "-%e", -f); sprintf(buffer, "-%e", -f);
} }
else sprintf(buffer,"-%f",-f); else
sprintf(buffer, "-%f", -f);
} }
if (! eformat) { if (!eformat)
for( ; *c && *c!= ' ';c++) ; {
for (; *c && *c != ' '; c++)
;
c--; c--;
while (c > buffer && *c == '0') while (c > buffer && *c == '0')
{ {
*c= 0;c--; *c = 0;
c--;
} }
if( *c=='.') *c=0; if (*c == '.')
*c = 0;
} }
} }
void _prfnum(double f) void _prfnum(double f)
@ -71,6 +83,8 @@ void _prfnum(double f)
void _prstr(String* str) void _prstr(String* str)
{ {
_asschn(); _asschn();
if( str==0) _out("<null>"); if (str == 0)
else _out(str->strval); _out("<null>");
else
_out(str->strval);
} }

View file

@ -20,7 +20,8 @@ void _setrand(int i)
} }
double _rnd(double d) double _rnd(double d)
{ {
double f; f= (int) rand(); double f;
f = (int)rand();
return (f / return (f /
#if EM_WSIZE == 4 #if EM_WSIZE == 4
2147483647.0 2147483647.0

View file

@ -31,8 +31,10 @@ void _readint(int* addr)
_asschn(); _asschn();
if (fscanf(_chanrd, "%d", &i) != 1) if (fscanf(_chanrd, "%d", &i) != 1)
{ {
if( ferror(_chanrd)) error(29); if (ferror(_chanrd))
if( feof(_chanrd)) error(2); error(29);
if (feof(_chanrd))
error(2);
if (_chann == -1) if (_chann == -1)
{ {
_asschn(); /* may be closed by now */ _asschn(); /* may be closed by now */
@ -42,7 +44,12 @@ void _readint(int* addr)
return; return;
} }
error(40); error(40);
}else { readskip(); *addr=i;} }
else
{
readskip();
*addr = i;
}
} }
void _readflt(double* addr) void _readflt(double* addr)
{ {
@ -55,8 +62,10 @@ void _readflt(double* addr)
_asschn(); _asschn();
if (fscanf(_chanrd, "%lf", &f) != 1) if (fscanf(_chanrd, "%lf", &f) != 1)
{ {
if( ferror(_chanrd)) error(29); if (ferror(_chanrd))
if( feof(_chanrd)) error(2); error(29);
if (feof(_chanrd))
error(2);
if (_chann == -1) if (_chann == -1)
{ {
fgets(buf, 1024, _chanrd); fgets(buf, 1024, _chanrd);
@ -65,7 +74,12 @@ void _readflt(double* addr)
return; return;
} }
error(40); error(40);
}else { readskip(); *addr=f;} }
else
{
readskip();
*addr = f;
}
} }
void _readstr(String** s) void _readstr(String** s)
{ {
@ -88,25 +102,29 @@ void _readstr(String** s)
#ifdef DEBUG #ifdef DEBUG
printf("qouted string\n"); printf("qouted string\n");
#endif #endif
while ( (kar= fgetc(_chanrd)) != EOF && kar!='"' ) *c++ = kar ; while ((kar = fgetc(_chanrd)) != EOF && kar != '"')
*c++ = kar;
ungetc(kar, _chanrd); ungetc(kar, _chanrd);
*c = 0; *c = 0;
}else }
if( isalpha(*c)) else if (isalpha(*c))
{ {
/* read normal string */ /* read normal string */
c++; c++;
#ifdef DEBUG #ifdef DEBUG
printf("non-qouted string\n"); printf("non-qouted string\n");
#endif #endif
while( (kar= fgetc(_chanrd)) != ',' && kar!= EOF && while ((kar = fgetc(_chanrd)) != ',' && kar != EOF && !isspace(kar) && kar != '\n')
!isspace(kar) && kar!='\n')
*c++ = kar; *c++ = kar;
ungetc(kar, _chanrd); ungetc(kar, _chanrd);
*c = 0; *c = 0;
}else{ }
if( ferror(_chanrd)) error(29); else
if( feof(_chanrd)) error(2); {
if (ferror(_chanrd))
error(29);
if (feof(_chanrd))
error(2);
if (_chann == -1) if (_chann == -1)
{ {
fgets(buffer, 1024, _chanrd); fgets(buffer, 1024, _chanrd);
@ -148,7 +166,8 @@ void _restore(int line)
#ifdef DEBUG #ifdef DEBUG
printf(" %d lines to skip\n", nr); printf(" %d lines to skip\n", nr);
#endif #endif
while(nr-- >0 ) fgets(buffer,1024,_chanrd); while (nr-- > 0)
fgets(buffer, 1024, _chanrd);
} }
} }
void _rdline(String** s) void _rdline(String** s)

View file

@ -11,7 +11,8 @@ void _gosub(int x)
#ifdef DEBUG #ifdef DEBUG
printf("store %d in %d\n", x, topstk); printf("store %d in %d\n", x, topstk);
#endif #endif
if( topstk== MAXNESTING) error(26); if (topstk == MAXNESTING)
error(26);
_gotable[topstk] = x; _gotable[topstk] = x;
topstk++; topstk++;
} }

View file

@ -4,13 +4,16 @@ char * salloc(unsigned length)
{ {
char* c, *s; char* c, *s;
c = malloc(length); c = malloc(length);
if( !c ) error(5); if (!c)
for(s=c;s<c+length;s++) *s = 0; error(5);
for (s = c; s < c + length; s++)
*s = 0;
return (c); return (c);
} }
void sfree(char* c) void sfree(char* c)
{ {
if( !c ) return; if (!c)
return;
free(c); free(c);
} }

View file

@ -2,13 +2,16 @@
int _sgn(double v) int _sgn(double v)
{ {
if( v>0) return(1); if (v > 0)
if( v<0) return(-1); return (1);
if (v < 0)
return (-1);
return (0); return (0);
} }
int _forsgn(double v) int _forsgn(double v)
{ {
if (v >= 0) return 1; if (v >= 0)
return 1;
return -1; return -1;
} }

View file

@ -13,4 +13,3 @@
double _sin(double x) { return sin(x); } double _sin(double x) { return sin(x); }
double _cos(double x) { return cos(x); } double _cos(double x) { return cos(x); }
double _tan(double x) { return tan(x); } double _tan(double x) { return tan(x); }

View file

@ -11,4 +11,3 @@
#include <math.h> #include <math.h>
double _sqt(double x) { return sqrt(x); } double _sqt(double x) { return sqrt(x); }

View file

@ -4,8 +4,12 @@
/* $Id$ */ /* $Id$ */
#define ok(X) if( X ==0) return; #define ok(X) \
#define okr(X) if( X ==0) return(0); if (X == 0) \
return;
#define okr(X) \
if (X == 0) \
return (0);
extern char* salloc(); extern char* salloc();
@ -35,9 +39,11 @@ void _decstr(String* str)
{ {
ok(str); ok(str);
/* Strings in ROM are initialized with this count */ /* Strings in ROM are initialized with this count */
if ( str->strcount==9999 ) return ; if (str->strcount == 9999)
return;
str->strcount--; str->strcount--;
if(str->strcount<=0) _delstr(str); if (str->strcount <= 0)
_delstr(str);
} }
void _strcpy(String* dst, String* src) void _strcpy(String* dst, String* src)
{ {
@ -57,7 +63,8 @@ String *_concat(String* s1,String* s2)
{ {
String* s; String* s;
int length; int length;
okr(s1); okr(s2); okr(s1);
okr(s2);
s = (String*)salloc(sizeof(String)); s = (String*)salloc(sizeof(String));
s->strlength = _length(s1) + _length(s2); s->strlength = _length(s1) + _length(s2);
s->strval = salloc(s->strlength + 1); s->strval = salloc(s->strlength + 1);
@ -68,7 +75,8 @@ String *_concat(String* s1,String* s2)
} }
int _strcomp(String* s1, String* s2) int _strcomp(String* s1, String* s2)
{ {
okr(s1);okr(s2); okr(s1);
okr(s2);
return (strcmp(s2->strval, s1->strval)); return (strcmp(s2->strval, s1->strval));
} }
@ -78,7 +86,8 @@ String *_left(int size, String* s)
int i; int i;
okr(s); okr(s);
if( size <0 || size >s->strlength) error(3); if (size < 0 || size > s->strlength)
error(3);
ns = (String*)salloc(sizeof(String)); ns = (String*)salloc(sizeof(String));
ns->strval = salloc(size + 1); ns->strval = salloc(size + 1);
ns->strcount = 1; ns->strcount = 1;
@ -113,8 +122,10 @@ String *_string(double f, double d)
int i, j; int i, j;
String* s; String* s;
i=d;j=f; i = d;
if( i<0 || i>MAXSTRING) error(3); j = f;
if (i < 0 || i > MAXSTRING)
error(3);
s = (String*)salloc(sizeof(String)); s = (String*)salloc(sizeof(String));
s->strlength = i; s->strlength = i;
s->strcount = 1; s->strcount = 1;
@ -129,11 +140,15 @@ void _midstmt(String* s2, int i1, int i2, String* s)
int l; int l;
/*printf("mid called %d %d %s %s\n",i1,i2,s->strval, s2->strval);*/ /*printf("mid called %d %d %s %s\n",i1,i2,s->strval, s2->strval);*/
if (i2 < 0 || i1 < -1) error(3); if (i2 < 0 || i1 < -1)
if( s->strlength<i2 || s2->strlength < i1) error(3); /* source string too short */ error(3);
if( i1== -1) i1= s2->strlength; if (s->strlength < i2 || s2->strlength < i1)
error(3); /* source string too short */
if (i1 == -1)
i1 = s2->strlength;
l = s->strlength - i2 + 1; l = s->strlength - i2 + 1;
if( i1>l ) i1=l; if (i1 > l)
i1 = l;
strncpy(s->strval + i2 - 1, s2->strval, i1); strncpy(s->strval + i2 - 1, s2->strval, i1);
} }
String* _mid(int i1, int i2, String* s) String* _mid(int i1, int i2, String* s)
@ -142,13 +157,17 @@ String *_mid(int i1, int i2, String* s)
String* s2; String* s2;
/* printf("mid fcn called %d %d %s\n",i1,i2,s->strval);*/ /* printf("mid fcn called %d %d %s\n",i1,i2,s->strval);*/
if (i2 < 0 || i1 < -1) return(s2); /* or error? */ if (i2 < 0 || i1 < -1)
if( i1 == -1) i1= s->strlength; return (s2); /* or error? */
if (i1 == -1)
i1 = s->strlength;
s2 = _newstr(s->strval); s2 = _newstr(s->strval);
s2->strval[0] = 0; s2->strval[0] = 0;
if( s->strlength<i2) return(s2); /* source string too short */ if (s->strlength < i2)
return (s2); /* source string too short */
l = s->strlength - i2 + 1; l = s->strlength - i2 + 1;
if( i1>l ) i1=l; if (i1 > l)
i1 = l;
strncpy(s2->strval, s->strval + i2 - 1, i1); strncpy(s2->strval, s->strval + i2 - 1, i1);
s2->strval[i1] = 0; s2->strval[i1] = 0;
return (s2); return (s2);
@ -160,7 +179,8 @@ String *_right(int length, String* str)
int i; int i;
i = _length(str) - length; i = _length(str) - length;
if(i<0) i=0; if (i < 0)
i = 0;
s = _newstr(str->strval + i); s = _newstr(str->strval + i);
return (s); return (s);
} }

View file

@ -41,7 +41,6 @@ void _ini_trp(void)
signal(i, _trpfatal); signal(i, _trpfatal);
} }
void _settrap(int nr) void _settrap(int nr)
{ {
_trpline = nr; _trpline = nr;
@ -50,7 +49,8 @@ void _trap(void)
{ {
int line; int line;
if( _trpline==0) exit(-1); if (_trpline == 0)
exit(-1);
line = _trpline; line = _trpline;
_trpline = 0; /* should be reset by user */ _trpline = 0; /* should be reset by user */
_ini_trp(); _ini_trp();

View file

@ -7,26 +7,32 @@
void _wrnl(void) void _wrnl(void)
{ {
if( fputc('\n',_chanwr) == EOF) error(29); if (fputc('\n', _chanwr) == EOF)
error(29);
} }
void _wrcomma(void) void _wrcomma(void)
{ {
if( fputc(',',_chanwr) == EOF) error(29); if (fputc(',', _chanwr) == EOF)
error(29);
} }
void _wrint(int i) void _wrint(int i)
{ {
if (i > 0) if (i > 0)
if( fputc(' ',_chanwr)==EOF) error(29); if (fputc(' ', _chanwr) == EOF)
error(29);
fprintf(_chanwr, "%d", i); fprintf(_chanwr, "%d", i);
if( ferror(_chanwr) ) error(29); if (ferror(_chanwr))
error(29);
} }
void _wrflt(double f) void _wrflt(double f)
{ {
fprintf(_chanwr, "%f", f); fprintf(_chanwr, "%f", f);
if( ferror(_chanwr) ) error(29); if (ferror(_chanwr))
error(29);
} }
void _wrstr(String* s) void _wrstr(String* s)
{ {
fprintf(_chanwr, "\"%s\"", s->strval); fprintf(_chanwr, "\"%s\"", s->strval);
if( ferror(_chanwr) ) error(29); if (ferror(_chanwr))
error(29);
} }