Annotation of capa/capa51/pProj/capaFormulaLexer.c, revision 1.1
1.1 ! albertel 1:
! 2: /* =========================================================== */
! 3: /* capaFormulaLexer.c created by Isaac Tsai @ Feb 1999 */
! 4: /* copyrighted by Isaac Tsai 1999 */
! 5: /* =========================================================== */
! 6:
! 7: #include <ctype.h> /* isspace() */
! 8: #include <stdio.h> /* sscanf() */
! 9: #include "capaParser.h"
! 10: #include "capaFormula.h"
! 11: #include "capaToken.h"
! 12:
! 13: /* --------------- Global variables ------------------------- */
! 14: extern int Func_idx;
! 15: extern Symbol FuncStack[MAX_FUNC_NEST];
! 16:
! 17:
! 18: extern char Fbuf[ONE_K]; /* lexer input buffer */
! 19: extern int Fidx; /* lexer input char index */
! 20:
! 21: extern Symbol_p fml_lval; /* lexical variable used in parser */
! 22:
! 23: /* ---- Token value returned from this lexer ---------------- */
! 24:
! 25: /* --------------- Global variables ------------------------ */
! 26:
! 27:
! 28: /* scan a F_NUMBER token */
! 29: double
! 30: f_get_float()
! 31: {
! 32: double num;
! 33: int ii=0, len;
! 34: char num_str[QUARTER_K];
! 35:
! 36: num_str[ii]=0;
! 37: while( isspace(Fbuf[Fidx]) ) { Fidx++; }
! 38: if( Fbuf[Fidx] == '+' || Fbuf[Fidx] == '-' ) {
! 39: num_str[ii++] = Fbuf[Fidx++];
! 40: }
! 41: while( isdigit(Fbuf[Fidx]) || Fbuf[Fidx] == '.' ) {
! 42: num_str[ii++] = Fbuf[Fidx++];
! 43: }
! 44: if( Fbuf[Fidx] == 'E' || Fbuf[Fidx] == 'e' ) { /* a number followed immediately by e or E */
! 45: if( Fbuf[Fidx+1] == '+' || Fbuf[Fidx+1] == '-' || isdigit(Fbuf[Fidx+1]) ) {
! 46: /* e or E followed immediately by a digit */
! 47: num_str[ii++] = Fbuf[Fidx++];
! 48: num_str[ii++] = Fbuf[Fidx++];
! 49: while( isdigit(Fbuf[Fidx]) ) {
! 50: num_str[ii++] = Fbuf[Fidx++];
! 51: }
! 52: }
! 53: }
! 54: num_str[ii] = 0; /* terminate the str */
! 55: len = strlen(num_str);
! 56: if(len > 0 ) {
! 57: sscanf(num_str,"%lg", &num);
! 58: } else {
! 59: num = 1.0;
! 60: }
! 61: return (num);
! 62: }
! 63:
! 64: char *
! 65: f_get_id()
! 66: {
! 67: char *var_p;
! 68: int ii=0, len;
! 69: char id_str[QUARTER_K];
! 70:
! 71: id_str[ii]=0;
! 72: while( isspace(Fbuf[Fidx]) ) { Fidx++; }
! 73: if( isalpha( Fbuf[Fidx] ) ) {
! 74: id_str[ii++] = Fbuf[Fidx++];
! 75: }
! 76: while( isalnum(Fbuf[Fidx]) || Fbuf[Fidx] == '_' ) {
! 77: id_str[ii++] = Fbuf[Fidx++];
! 78: }
! 79: id_str[ii] = 0; /* terminate the str */
! 80: len = strlen(id_str);
! 81: var_p = (char *)capa_malloc( (len+1), sizeof(char));
! 82: strcpy(var_p,id_str);
! 83: return (var_p);
! 84: }
! 85:
! 86: int
! 87: f_peek_next_token()
! 88: {
! 89: int idx;
! 90:
! 91: idx = Fidx;
! 92: while( isspace(Fbuf[idx]) ) { idx++; }
! 93: return (Fbuf[idx]);
! 94: }
! 95:
! 96:
! 97: /* ======================================================= */
! 98:
! 99: int
! 100: fml_lex()
! 101: {
! 102: char *id_p;
! 103: int c;
! 104:
! 105: if( Fbuf[Fidx] == 0 ) { /* printf("EoI\n"); */ return (EOF); }
! 106: while( isspace(Fbuf[Fidx]) ) { Fidx++; }
! 107: if( isalpha(Fbuf[Fidx]) ) {
! 108: id_p = f_get_id();
! 109: c = f_peek_next_token();
! 110: if( c == '(' ) {
! 111: (FuncStack[Func_idx]).s_type = FUNCTION_ID;
! 112: (FuncStack[Func_idx]).s_name = id_p;
! 113: Func_idx++;
! 114:
! 115: return (F_ID);
! 116: } else {
! 117: fml_lval = find_formula_id(id_p); capa_mfree((char *)id_p);
! 118: if( fml_lval == NULL) {
! 119: return (F_ERROR);
! 120: } else {
! 121: return (V_ID);
! 122: }
! 123: }
! 124: }
! 125: if( isdigit(Fbuf[Fidx]) || Fbuf[Fidx] == '.' ) {
! 126:
! 127: fml_lval = (Symbol *) capa_malloc(1, sizeof(Symbol)); /* *** */
! 128: fml_lval->s_real = f_get_float();
! 129: fml_lval->s_type = R_CONSTANT;
! 130:
! 131: return (F_NUMBER);
! 132: }
! 133: if( Fbuf[Fidx] == '(' ) {
! 134: Fidx++;
! 135: return (F_LPAR);
! 136: }
! 137: if( Fbuf[Fidx] == ')' ) {
! 138: Fidx++;
! 139: return (F_RPAR);
! 140: }
! 141: if( Fbuf[Fidx] == '+' ) {
! 142: Fidx++;
! 143: return (F_PLUS);
! 144: }
! 145: if( Fbuf[Fidx] == '-' ) {
! 146: Fidx++;
! 147: return (F_MINUS);
! 148: }
! 149: if( Fbuf[Fidx] == '*' ) {
! 150: Fidx++;
! 151: return (F_MULT);
! 152: }
! 153: if( Fbuf[Fidx] == '/' ) {
! 154: Fidx++;
! 155: return (F_DIV);
! 156: }
! 157: if( Fbuf[Fidx] == '%' ) {
! 158: Fidx++;
! 159: return (F_MOD);
! 160: }
! 161: if( Fbuf[Fidx] == '^' ) {
! 162: Fidx++;
! 163: return (F_POW);
! 164: }
! 165: if( Fbuf[Fidx] == ',' ) {
! 166: Fidx++;
! 167: return (F_COMMA);
! 168: }
! 169: return (F_ERROR);
! 170:
! 171: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>