version 1.5, 2004/09/10 13:20:23 |
version 1.11, 2020/10/06 11:33:47 |
|
|
/*$OpenXM: OpenXM/src/kan96xx/Kan/scanner.c,v 1.4 2001/05/04 01:06:25 takayama Exp $*/ |
/*$OpenXM: OpenXM/src/kan96xx/Kan/scanner.c,v 1.10 2018/09/07 00:15:44 takayama Exp $*/ |
/* scanner.c (SM StackMachine) */ |
/* scanner.c (SM StackMachine) */ |
/* export: struct tokens getokenSM(actionType kind,char *str); |
/* export: struct tokens getokenSM(actionType kind,char *str); |
scanner.c is used to get tokens from streams. |
scanner.c is used to get tokens from streams. |
files: none |
files: none |
*/ |
*/ |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
#include "datatype.h" |
#include "datatype.h" |
#include "stackm.h" |
#include "stackm.h" |
struct tokens lookupTokens(struct tokens t); |
struct tokens lookupTokens(struct tokens t); |
Line 60 static int FileStackP = 0; |
|
Line 62 static int FileStackP = 0; |
|
/**************** end of declaration part of lexical analizer ******/ |
/**************** end of declaration part of lexical analizer ******/ |
|
|
static int getSM(); |
static int getSM(); |
static putSM(); |
static void putSM(); |
static struct tokens flushSM(); |
static struct tokens flushSM(); |
static isSpaceSM(); |
static int isSpaceSM(); |
static isDollarSM(); |
static int isDollarSM(); |
static isBraceSM(); |
static int isBraceSM(); |
static isKakkoSM(); |
static int isKakkoSM(); |
static isSymbolSM(); |
static int isSymbolSM(); |
static mygetchar(); |
static int mygetchar(); |
static myungetchar(); |
static int myungetchar(); |
|
|
|
int ScannerWhich = 0; |
|
unsigned char ScannerBuf[SCANNERBUF_SIZE]; |
|
int ScannerPt = 0; |
|
|
/* |
/* |
static mygetchar() |
static mygetchar() |
{ |
{ |
|
|
return( getc(Cfp) ); |
return( getc(Cfp) ); |
} |
} |
*/ |
*/ |
|
|
static mygetchar() |
static int mygetchar() |
{ int c; |
{ int c; |
|
c = getc(Cfp); |
|
if (c > 0) { /* ungetchar is ignored */ |
|
ScannerPt++; if (ScannerPt >= SCANNERBUF_SIZE) ScannerPt = 0; |
|
ScannerBuf[ScannerPt] = c; |
|
} |
if (EchoInScanner) { |
if (EchoInScanner) { |
c = getc(Cfp); |
|
if (c==EOF) { |
if (c==EOF) { |
printf("\n%% EOF of file %x\n",(int) Cfp); |
printf("\n%% EOF of file %p\n",Cfp); |
}else{ |
}else{ |
printf("%c",c); |
printf("%c",c); |
} |
} |
return( c ); |
return( c ); |
}else{ |
}else{ |
return( getc(Cfp) ); |
return( c ); |
} |
} |
} |
} |
|
|
|
|
static myungetchar(c) |
static int myungetchar(c) |
int c; |
int c; |
{ |
{ |
return( ungetc(c,Cfp) ); |
return( ungetc(c,Cfp) ); |
Line 105 static int getSM() |
|
Line 115 static int getSM() |
|
/* get a letter from StringSM */ |
/* get a letter from StringSM */ |
{ |
{ |
int c; |
int c; |
|
|
|
if ((StrpSM > 0) && (StringSM[StrpSM] == ',') && (StringSM[StrpSM-1] == ',')) { int i; |
|
fprintf(stderr,"Warning: ,, is found."); |
|
for (i=(StrpSM-30>0?StrpSM-30:0); i<=StrpSM; i++) { |
|
fprintf(stderr,"%c",StringSM[i]); |
|
} |
|
fprintf(stderr,"\n"); |
|
} |
|
|
c = StringSM[StrpSM++]; |
c = StringSM[StrpSM++]; |
if (c == '\0') { |
if (c == '\0') { |
StrpSM--;return(EOF); |
StrpSM--;return(EOF); |
} else return(c); |
} else return(c); |
} |
} |
|
|
static putSM(c) |
static void putSM(c) |
int c; |
int c; |
/* put a letter on BufSM */ |
/* put a letter on BufSM */ |
{ |
{ |
Line 146 static struct tokens flushSM() |
|
Line 165 static struct tokens flushSM() |
|
strcpy(token,BufSM); |
strcpy(token,BufSM); |
r.token = token; |
r.token = token; |
r.kind = TypeSM; |
r.kind = TypeSM; |
|
r.tflag = 0; |
if (r.kind == ID) { |
if (r.kind == ID) { |
if (isLiteral(r.token)) { |
if (isLiteral(r.token)) { |
r.object = lookupLiteralString(r.token); |
r.object = lookupLiteralString(r.token); |
Line 156 static struct tokens flushSM() |
|
Line 176 static struct tokens flushSM() |
|
return(r); |
return(r); |
} |
} |
|
|
static isSpaceSM(c) |
static int isSpaceSM(c) |
int c; |
int c; |
{ |
{ |
static int prev=0; |
|
if ((c == ',') && (prev == ',')) fprintf(stderr,"Warning! ,, is found.\n"); |
|
prev = c; |
|
|
|
if (((c <= ' ') || c == ',') && (c!= EOF)) return(1); |
if (((c <= ' ') || c == ',') && (c!= EOF)) return(1); |
else return(0); |
else return(0); |
} |
} |
|
|
static isDollarSM(c) |
static int isDollarSM(c) |
int c; |
int c; |
{ |
{ |
if (c == '$') return(1); |
if (c == '$') return(1); |
else return(0); |
else return(0); |
} |
} |
|
|
static isBraceSM(c) |
static int isBraceSM(c) |
int c; |
int c; |
{ |
{ |
if (c == '{') return(1); |
if (c == '{') return(1); |
else return(0); |
else return(0); |
} |
} |
|
|
static isKakkoSM(c) |
static int isKakkoSM(c) |
int c; |
int c; |
{ |
{ |
if (c == '(') return(1); |
if (c == '(') return(1); |
else return(0); |
else return(0); |
} |
} |
|
|
static isSymbolSM(c) |
static int isSymbolSM(c) |
int c; |
int c; |
{ |
{ |
if ((c == '{') || |
if ((c == '{') || |
Line 211 struct tokens getokenSM(kind,str) |
|
Line 227 struct tokens getokenSM(kind,str) |
|
char fname[1024]; |
char fname[1024]; |
|
|
if (kind == INIT) { |
if (kind == INIT) { |
|
ScannerWhich = 2; |
|
ScannerPt = 0; |
|
ScannerBuf[0] = 0; |
|
|
StrpSM = 0; |
StrpSM = 0; |
ExistSM = 0; |
ExistSM = 0; |
|
|
|
|
char *getLOAD_SM1_PATH() { |
char *getLOAD_SM1_PATH() { |
char *p; |
char *p; |
char *p2; |
char *p2; |
char *getenv(char *s); |
char *getenv(const char *s); |
p = getenv("LOAD_SM1_PATH"); |
p = getenv("LOAD_SM1_PATH"); |
if (p == NULL) { |
if (p == NULL) { |
p = getenv("OpenXM_HOME"); |
p = getenv("OpenXM_HOME"); |
Line 422 char *getLOAD_SM1_PATH() { |
|
Line 442 char *getLOAD_SM1_PATH() { |
|
strcpy(p2,p); strcat(p2,"/"); |
strcpy(p2,p); strcat(p2,"/"); |
return(p2); |
return(p2); |
} |
} |
|
} |
|
|
|
char *traceShowScannerBuf() { |
|
char *s; |
|
int i,k; |
|
s = NULL; |
|
/* |
|
printf("ScannerPt=%d\n",ScannerPt); |
|
for (i=0; i<SCANNERBUF_SIZE; i++) { |
|
printf("%x ",(int) (ScannerBuf[i])); |
|
} |
|
printf("\n"); fflush(NULL); sleep(10); |
|
*/ |
|
if ((ScannerPt == 0) && (ScannerBuf[0] == 0)) { |
|
s = sGC_malloc(1); s[0] = 0; |
|
return s; |
|
} |
|
if ((ScannerPt > 0) && (ScannerBuf[0] == 0)) { |
|
s = sGC_malloc(ScannerPt+1); |
|
for (i=1; i<=ScannerPt; i++) { |
|
s[i-1] = ScannerBuf[i]; s[i] = 0; |
|
} |
|
return s; |
|
} |
|
if (ScannerBuf[0] != 0) { |
|
s = sGC_malloc(SCANNERBUF_SIZE+1); |
|
k = ScannerPt+1; |
|
if (k >= SCANNERBUF_SIZE) k = 0; |
|
for (i=0; i<SCANNERBUF_SIZE; i++) { |
|
s[i] = ScannerBuf[k]; s[i+1] = 0; |
|
k++; if (k >= SCANNERBUF_SIZE) k = 0; |
|
} |
|
return s; |
|
} |
|
return s; |
} |
} |