/* * m: matrix calculator with C syntax. * * (c) copyright 1991, Dr. Rick Perry, perry@{ucis,vu-vlsi}.vill.edu * Department of Electrical Engineering, Villanova University, * Villanova, PA 19085, 215-519-4969, -4970, hm: 259-8734 * * The implementation is based on the calculator program in Chapter 8 of * "The Unix Programming Environment" by Brian W. Kernighan and Rob Pike, * Prentice-Hall, 1984. The program was inspired by previous experience with * the original Matlab by Cleve Moler, now marketed by The Mathworks, Inc., * and by experience with APL, A Programming Language, from IBM. */ #include "m.h" #include #include #include /* for access, F_OK */ #define MRC ".mrc" #define MRCTAIL "/.mrc" FILE *fin; FILE *fdiary = NULL; int diary = 0, batch = 0; jmp_buf begin; void prompt( void) { (void) printf("m:%d> ", line_number); } #ifndef ANSI_TEST void yyerror( char *s) { dprint( "m: %s\n", s); derror(); } #endif void error( char *s, char *t) { dprint( "m: %s%s\n", s, t); if( fin != stdin) { fclose(fin); fin = nextfile(); } longjmp( begin, 0); } void fpecatch( int s) { error("floating point exception",""); } void stop( int s) { error("interrupt",""); } #ifdef DEBUG int stack_debug = 0; int prog_debug = 0; int trace = 0; int stack_dump = 0; int exe = 1; #endif int main( int argc, char *argv[]) { extern int yyparse( void); #if YYDEBUG yydebug = 0; #endif if( access( MRC, F_OK) == 0) addfile( MRC); else { char *p, buf[BUFSIZ]; if( (p = getenv( "HOME")) != NULL) { (void) strcat( strcpy( buf, p), MRCTAIL); if( access( buf, F_OK) == 0) addfile( buf); } } while( --argc) if( **++argv == '-') { while( *++*argv) switch( **argv) { case 'b': batch = 1; break; case 'd': while(*++*argv) switch(**argv) { #if YYDEBUG case '1': ++yydebug; break; #endif #ifdef DEBUG case '2': ++stack_debug; break; case '3': ++prog_debug; break; case '4': ++stack_dump; break; case '5': exe = 0; ++prog_debug; break; case '6': ++trace; break; #endif default: (void) fprintf( stderr, "m: unrecognized option -d%c\n", **argv); break; } --*argv; break; default: (void) fprintf( stderr, "m: unrecognized option -%c\n", **argv); break; } } else addfile( *argv); init(); if( !batch) version(); fin = nextfile(); setjmp(begin); signal( SIGFPE, fpecatch); signal( SIGINT, stop); for( cleanup(1), initcode(); yyparse(); cleanup(1), initcode()) { sp = stack; #ifdef DEBUG if( prog_debug) { dprint("\n"); disasm(prog,pp); } if( exe) execute(prog); #else execute(prog); #endif } exit(SUCCESS); }