// Returns true if str is a keyword. bool is_keyword(String str) { if (lookup_keyword(str) != Identifier_tok) return true; else return false; }
/*********************************************************** * Function: // main * Description: // Ö÷º¯Êý ***********************************************************/ int main( void ) { int ret = -1; ret = lookup_keyword( "do", keyword, sizeof( keyword ) / sizeof( keyword[0] ) ); printf( "lookup do ,ret=%d \n", ret ); return 0; }
int main() { const char * const txt = "for"; int ret = lookup_keyword(txt,keyword,N_KEYWORD); printf("ret: %d\n",ret); //int int_array[3][5] = { // {1,2,3,4,5}, // {21,22,23,24,25}, // {31,32,33,34,35} //}; //int *pi = int_array[0]; //int i; //for(i=0;i<10;i++){ // printf("%d: -> val: %d\n",i,pi[i]); //} // //int (*p)[5] = int_array; //for(i=0;i<3;i++){ // printf("%d: -> val: %lu\n",i,sizeof(p[i])/sizeof(p[i][0])); //} return 0; }
static void parse_config(const char *fn, char *s) { struct parse_state state; next_token(&state); int kw = lookup_keyword(args[0]); parse_new_section(&state, kw, nargs, args); { // case K_service: set service handle action // add service into list state->context = parse_service(state, nargs, args); { struct service *svc; list_init(&svc->onrestart.commands); list_add_tail(&service_list, &svc->slist); } // case K_on: // add action into list state->context = parse_action(state, nargs, args); { list_add_tail(&action_list, &act->alist); } } }
// Save an identifier. inline void save_identifier(Lexer& lex, std::size_t n) { String str(String(&*lex.head, n)); Token_type t = lookup_keyword(str); lex.tokens.emplace_back(t, str, lex.loc); consume(lex, n); }
int main(void) { char word[] = "shile"; int index = lookup_keyword(word,keyword); if(index == -1) printf("There are no keywords in desired_word\n"); else printf("%s is keyword,and it's index in keyword_table is %d",word,index); return 0; }
//============================================================================= void CubitInstrumentation::write_keywords(std::vector<CubitString> keywords) { if (check_token_log() && !keywords.empty()) { if (outputState != Command) { outputState = open_section(Command); } else { *tokenUsageStream << ","; } *tokenUsageStream << lookup_keyword(keywords[0].c_str()); for (size_t i = 1; i < keywords.size(); i++) { int debug = lookup_keyword(keywords[i].c_str()); *tokenUsageStream << ":" << debug; } tokenUsageStream->flush(); } }
int main(void) { char test[][9] = { "do", "for", "if", "register", "return", "switch", "while" }; int i; for(i = 0; i < 7; i++) { printf("offset in keyword table (-1 no match): %d\n", lookup_keyword(&test[i][0], keyword)); } return 0; }
int ws_yy_lex(YYSTYPE *yylval, YYLTYPE *yylloc, void *context) { WsCompiler *compiler = (WsCompiler *) context; WsUInt32 ch, ch2; WsBuffer buffer; unsigned char *p; WsBool success; /* Just check that we get the correct amount of arguments. */ gw_assert(compiler->magic == COMPILER_MAGIC); while (ws_stream_getc(compiler->input, &ch)) { /* Save the token's line number. */ yylloc->first_line = compiler->linenum; switch (ch) { case '\t': /* Whitespace characters. */ case '\v': case '\f': case ' ': continue; case '\n': /* Line terminators. */ case '\r': if (ch == '\r' && ws_stream_getc(compiler->input, &ch2)) { if (ch2 != '\n') ws_stream_ungetc(compiler->input, ch2); } compiler->linenum++; continue; case '!': /* !, != */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '=') return tNE; ws_stream_ungetc(compiler->input, ch2); } return '!'; case '%': /* %, %= */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '=') return tREMA; ws_stream_ungetc(compiler->input, ch2); } return '%'; case '&': /* &, &&, &= */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '&') return tAND; if (ch2 == '=') return tANDA; ws_stream_ungetc(compiler->input, ch2); } return '&'; case '*': /* *, *= */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '=') return tMULA; ws_stream_ungetc(compiler->input, ch2); } return '*'; case '+': /* +, ++, += */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '+') return tPLUSPLUS; if (ch2 == '=') return tADDA; ws_stream_ungetc(compiler->input, ch2); } return '+'; case '-': /* -, --, -= */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '-') return tMINUSMINUS; if (ch2 == '=') return tSUBA; ws_stream_ungetc(compiler->input, ch2); } return '-'; case '.': if (ws_stream_getc(compiler->input, &ch2)) { if (WS_IS_DECIMAL_DIGIT(ch2)) { /* DecimalFloatLiteral. */ ws_buffer_init(&buffer); if (!ws_buffer_append_space(&buffer, &p, 2)) { ws_error_memory(compiler); ws_buffer_uninit(&buffer); return EOF; } p[0] = '.'; p[1] = (unsigned char) ch2; success = read_float_from_point(compiler, &buffer, &yylval->vfloat); ws_buffer_uninit(&buffer); if (!success) return EOF; return tFLOAT; } ws_stream_ungetc(compiler->input, ch2); } return '.'; case '/': /* /, /=, block or a single line comment */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '*') { /* Block comment. */ while (1) { if (!ws_stream_getc(compiler->input, &ch)) { ws_src_error(compiler, 0, "EOF in comment"); return EOF; } if (ch == '\n' || ch == '\r') { /* Line terminators. */ if (ch == '\r' && ws_stream_getc(compiler->input, &ch2)) { if (ch2 != '\n') ws_stream_ungetc(compiler->input, ch2); } compiler->linenum++; /* Continue reading the block comment. */ continue; } if (ch == '*' && ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '/') /* The end of the comment found. */ break; ws_stream_ungetc(compiler->input, ch2); } } /* Continue after the comment. */ continue; } if (ch2 == '/') { /* Single line comment. */ while (1) { if (!ws_stream_getc(compiler->input, &ch)) /* The end of input stream reached. We accept this as a valid comment terminator. */ break; if (ch == '\n' || ch == '\r') { /* Line terminators. */ if (ch == '\r' && ws_stream_getc(compiler->input, &ch2)) { if (ch2 != '\n') ws_stream_ungetc(compiler->input, ch2); } /* The end of the line (and the comment) reached. */ compiler->linenum++; break; } } /* Continue after the comment. */ continue; } if (ch2 == '=') return tDIVA; ws_stream_ungetc(compiler->input, ch2); } return '/'; case '<': /* <, <<, <<=, <= */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '<') { if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '=') return tLSHIFTA; ws_stream_ungetc(compiler->input, ch2); } return tLSHIFT; } if (ch2 == '=') return tLE; ws_stream_ungetc(compiler->input, ch2); } return '<'; case '=': /* =, == */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '=') return tEQ; ws_stream_ungetc(compiler->input, ch2); } return '='; case '>': /* >, >=, >>, >>=, >>>, >>>= */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '>') { if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '>') { if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '=') return tRSZSHIFTA; ws_stream_ungetc(compiler->input, ch2); } return tRSZSHIFT; } if (ch2 == '=') return tRSSHIFTA; ws_stream_ungetc(compiler->input, ch2); } return tRSSHIFT; } if (ch2 == '=') return tGE; ws_stream_ungetc(compiler->input, ch2); } return '>'; case '^': /* ^, ^= */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '=') return tXORA; ws_stream_ungetc(compiler->input, ch2); } return '^'; case '|': /* |, |=, || */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == '=') return tORA; if (ch2 == '|') return tOR; ws_stream_ungetc(compiler->input, ch2); } return '|'; case '#': /* The simple cases. */ case '(': case ')': case ',': case ':': case ';': case '?': case '{': case '}': case '~': return (int) ch; case '\'': /* String literals. */ case '"': { WsUInt32 string_end_ch = ch; WsUtf8String *str = ws_utf8_alloc(); if (str == NULL) { ws_error_memory(compiler); return EOF; } while (1) { if (!ws_stream_getc(compiler->input, &ch)) { eof_in_string_literal: ws_src_error(compiler, 0, "EOF in string literal"); ws_utf8_free(str); return EOF; } if (ch == string_end_ch) /* The end of string reached. */ break; if (ch == '\\') { /* An escape sequence. */ if (!ws_stream_getc(compiler->input, &ch)) goto eof_in_string_literal; switch (ch) { case '\'': case '"': case '\\': case '/': /* The character as-is. */ break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case 'x': case 'u': { int i, len; int type = ch; if (ch == 'x') len = 2; else len = 4; ch = 0; for (i = 0; i < len; i++) { if (!ws_stream_getc(compiler->input, &ch2)) goto eof_in_string_literal; if (!WS_IS_HEX_DIGIT(ch2)) { ws_src_error(compiler, 0, "malformed `\\%c' escape in " "string literal", (char) type); ch = 0; break; } ch *= 16; ch += WS_HEX_TO_INT(ch2); } } break; default: if (WS_IS_OCTAL_DIGIT(ch)) { int i; int limit = 3; ch = WS_OCTAL_TO_INT(ch); if (ch > 3) limit = 2; for (i = 1; i < limit; i++) { if (!ws_stream_getc(compiler->input, &ch2)) goto eof_in_string_literal; if (!WS_IS_OCTAL_DIGIT(ch2)) { ws_stream_ungetc(compiler->input, ch2); break; } ch *= 8; ch += WS_OCTAL_TO_INT(ch2); } } else { ws_src_error(compiler, 0, "unknown escape sequence `\\%c' in " "string literal", (char) ch); ch = 0; } break; } /* FALLTHROUGH */ } if (!ws_utf8_append_char(str, ch)) { ws_error_memory(compiler); ws_utf8_free(str); return EOF; } } if (!ws_lexer_register_utf8(compiler, str)) { ws_error_memory(compiler); ws_utf8_free(str); return EOF; } gw_assert(str != NULL); yylval->string = str; return tSTRING; } break; default: /* Identifiers, keywords and number constants. */ if (WS_IS_IDENTIFIER_LETTER(ch)) { WsBool got; int token; unsigned char *p; unsigned char *np; size_t len = 0; /* An identifier or a keyword. We start with a 256 * bytes long buffer but it is expanded dynamically if * needed. However, 256 should be enought for most * cases since the byte-code format limits the function * names to 255 characters. */ p = ws_malloc(256); if (p == NULL) { ws_error_memory(compiler); return EOF; } do { /* Add one extra for the possible terminator character. */ np = ws_realloc(p, len + 2); if (np == NULL) { ws_error_memory(compiler); ws_free(p); return EOF; } p = np; /* This is ok since the only valid identifier names * can be written in 7 bit ASCII. */ p[len++] = (unsigned char) ch; } while ((got = ws_stream_getc(compiler->input, &ch)) && (WS_IS_IDENTIFIER_LETTER(ch) || WS_IS_DECIMAL_DIGIT(ch))); if (got) /* Put back the terminator character. */ ws_stream_ungetc(compiler->input, ch); /* Is it a keyword? */ if (lookup_keyword((char *) p, len, &token)) { /* Yes it is... */ ws_free(p); /* ...except one case: `div='. */ if (token == tIDIV) { if (ws_stream_getc(compiler->input, &ch)) { if (ch == '=') return tIDIVA; ws_stream_ungetc(compiler->input, ch); } } /* Return the token value. */ return token; } /* It is a normal identifier. Let's pad the name with a null-character. We have already allocated space for it. */ p[len] = '\0'; if (!ws_lexer_register_block(compiler, p)) { ws_error_memory(compiler); ws_free(p); return EOF; } gw_assert(p != NULL); yylval->identifier = (char *) p; return tIDENTIFIER; } if (WS_IS_NON_ZERO_DIGIT(ch)) { /* A decimal integer literal or a decimal float literal. */ ws_buffer_init(&buffer); if (!ws_buffer_append_space(&buffer, &p, 1)) { number_error_memory: ws_error_memory(compiler); ws_buffer_uninit(&buffer); return EOF; } p[0] = ch; while (ws_stream_getc(compiler->input, &ch)) { if (WS_IS_DECIMAL_DIGIT(ch)) { if (!ws_buffer_append_space(&buffer, &p, 1)) goto number_error_memory; p[0] = ch; } else if (ch == '.' || ch == 'e' || ch == 'E') { /* DecimalFloatLiteral. */ if (ch == '.') { if (!ws_buffer_append_space(&buffer, &p, 1)) goto number_error_memory; p[0] = '.'; success = read_float_from_point(compiler, &buffer, &yylval->vfloat); } else { ws_stream_ungetc(compiler->input, ch); success = read_float_from_exp(compiler, &buffer, &yylval->vfloat); } ws_buffer_uninit(&buffer); if (!success) return EOF; return tFLOAT; } else { ws_stream_ungetc(compiler->input, ch); break; } } /* Now the buffer contains an integer number as a string. Let's convert it to an integer number. */ yylval->integer = buffer_to_int(compiler, &buffer); ws_buffer_uninit(&buffer); /* Read a DecimalIntegerLiteral. */ return tINTEGER; } if (ch == '0') { /* The integer constant 0, an octal number or a HexIntegerLiteral. */ if (ws_stream_getc(compiler->input, &ch2)) { if (ch2 == 'x' || ch2 == 'X') { /* HexIntegerLiteral. */ ws_buffer_init(&buffer); if (!ws_buffer_append_space(&buffer, &p, 2)) goto number_error_memory; p[0] = '0'; p[1] = 'x'; while (ws_stream_getc(compiler->input, &ch)) { if (WS_IS_HEX_DIGIT(ch)) { if (!ws_buffer_append_space(&buffer, &p, 1)) goto number_error_memory; p[0] = ch; } else { ws_stream_ungetc(compiler->input, ch); break; } } if (ws_buffer_len(&buffer) == 2) { ws_buffer_uninit(&buffer); ws_src_error(compiler, 0, "numeric constant with no digits"); yylval->integer = 0; return tINTEGER; } /* Now the buffer contains an integer number as * a string. Let's convert it to an integer * number. */ yylval->integer = buffer_to_int(compiler, &buffer); ws_buffer_uninit(&buffer); /* Read a HexIntegerLiteral. */ return tINTEGER; } if (WS_IS_OCTAL_DIGIT(ch2)) { /* OctalIntegerLiteral. */ ws_buffer_init(&buffer); if (!ws_buffer_append_space(&buffer, &p, 2)) goto number_error_memory; p[0] = '0'; p[1] = ch2; while (ws_stream_getc(compiler->input, &ch)) { if (WS_IS_OCTAL_DIGIT(ch)) { if (!ws_buffer_append_space(&buffer, &p, 1)) goto number_error_memory; p[0] = ch; } else { ws_stream_ungetc(compiler->input, ch); break; } } /* Convert the buffer into an intger number. */ yylval->integer = buffer_to_int(compiler, &buffer); ws_buffer_uninit(&buffer); /* Read an OctalIntegerLiteral. */ return tINTEGER; } if (ch2 == '.' || ch2 == 'e' || ch2 == 'E') { /* DecimalFloatLiteral. */ ws_buffer_init(&buffer); if (ch2 == '.') { if (!ws_buffer_append_space(&buffer, &p, 1)) goto number_error_memory; p[0] = '.'; success = read_float_from_point(compiler, &buffer, &yylval->vfloat); } else { ws_stream_ungetc(compiler->input, ch); success = read_float_from_exp(compiler, &buffer, &yylval->vfloat); } ws_buffer_uninit(&buffer); if (!success) return EOF; return tFLOAT; } ws_stream_ungetc(compiler->input, ch2); } /* Integer literal 0. */ yylval->integer = 0; return tINTEGER; } /* Garbage found from the input stream. */ ws_src_error(compiler, 0, "garbage found from the input stream: character=0x%x", ch); return EOF; break; } } return EOF; }
int parse_directive(struct prog_info *pi) { int directive, pragma; int ok = True; int i; char *next, *data, buf[140]; struct file_info *fi_bak; struct def *def; struct data_list *incpath, *dl; next = get_next_token(pi->fi->scratch, TERM_SPACE); my_strupr(pi->fi->scratch); directive = lookup_keyword(directive_list, pi->fi->scratch + 1, True); if(directive == -1) { print_msg(pi, MSGTYPE_ERROR, "Unknown directive: %s", pi->fi->scratch); return(True); } switch(directive) { case DIRECTIVE_BYTE: if (!next) { print_msg(pi, MSGTYPE_ERROR, ".BYTE needs a size operand"); return(True); } if (pi->segment == pi->cseg) print_msg(pi, MSGTYPE_ERROR, ".BYTE directive cannot be used within the code segment (.CSEG)"); get_next_token(next, TERM_END); if (!get_expr(pi, next, &i)) return(False); if ((pi->pass == PASS_2) && pi->list_line && pi->list_on) { fprintf(pi->list_file, "%c:%06x %s\n", pi->segment->ident, pi->segment->addr, pi->list_line); pi->list_line = NULL; } if (i > 0) { fix_orglist(pi->segment); advance_ip(pi->segment, i); def_orglist(pi->segment); } break; case DIRECTIVE_CSEG: fix_orglist(pi->segment); def_orglist(pi->cseg); break; case DIRECTIVE_CSEGSIZE: break; case DIRECTIVE_DB: if((pi->pass == PASS_2) && pi->list_line && pi->list_on) { fprintf(pi->list_file, " %s\n", pi->list_line); pi->list_line = NULL; } return(parse_db(pi, next)); // break; /* Directive .def */ case DIRECTIVE_DEF: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".DEF needs an operand"); return(True); } data = get_next_token(next, TERM_EQUAL); if(!(data && (tolower(data[0]) == 'r') && isdigit(data[1]))) { print_msg(pi, MSGTYPE_ERROR, "%s needs a register (e.g. .def BZZZT = r16)", next); return(True); } i = atoi(&data[1]); /* check range of given register */ if(i > 31) print_msg(pi, MSGTYPE_ERROR, "R%d is not a valid register", i); /* check if this reg is already assigned */ for(def = pi->first_def; def; def = def->next) { if(def->reg == i && pi->pass == PASS_1 && !pi->NoRegDef) { print_msg(pi, MSGTYPE_WARNING, "r%d is already assigned to '%s'!", i, def->name); return(True); } } /* check if this regname is already defined */ for(def = pi->first_def; def; def = def->next) { if(!nocase_strcmp(def->name, next)) { if(pi->pass == PASS_1 && !pi->NoRegDef) { print_msg(pi, MSGTYPE_WARNING, "'%s' is already assigned as r%d but will now be set to r%i!", next, def->reg, i); } def->reg = i; return(True); } } /* B.A.: Check, if symbol is already defined as a label or constant */ if(pi->pass == PASS_2) { if(get_label(pi,next,NULL)) print_msg(pi, MSGTYPE_WARNING, "Name '%s' is used for a register and a label", next); if(get_constant(pi,next,NULL)) print_msg(pi, MSGTYPE_WARNING, "Name '%s' is used for a register and a constant", next); } def = malloc(sizeof(struct def)); if(!def) { print_msg(pi, MSGTYPE_OUT_OF_MEM, NULL); return(False); } def->next = NULL; if(pi->last_def) pi->last_def->next = def; else pi->first_def = def; pi->last_def = def; def->name = malloc(strlen(next) + 1); if(!def->name) { print_msg(pi, MSGTYPE_OUT_OF_MEM, NULL); return(False); } strcpy(def->name, next); def->reg = i; break; case DIRECTIVE_DEVICE: if(pi->pass == PASS_2) return(True); if(!next) { print_msg(pi, MSGTYPE_ERROR, ".DEVICE needs an operand"); return(True); } if (pi->device->name != NULL) { /* B.A.: Check for multiple device definitions */ print_msg(pi, MSGTYPE_ERROR, "More than one .DEVICE definition"); } if (pi->cseg->count || pi->dseg->count || pi->eseg->count) { /* B.A.: Check if something was already assembled */ print_msg(pi, MSGTYPE_ERROR, ".DEVICE definition must be before any code lines"); } else { if ((pi->cseg->addr != pi->cseg->lo_addr ) || (pi->dseg->addr != pi->dseg->lo_addr ) || (pi->eseg->addr != pi->eseg->lo_addr )) { /* B.A.: Check if something was already assembled XXX probably redundant */ print_msg(pi, MSGTYPE_ERROR, ".DEVICE definition must be before any .ORG directive"); } } get_next_token(next, TERM_END); pi->device = get_device(pi,next); if (!pi->device) { print_msg(pi, MSGTYPE_ERROR, "Unknown device: %s", next); pi->device = get_device(pi,NULL); /* B.A.: Fix segmentation fault if device is unknown */ } /* Now that we know the device type, we can * start memory allocation from the correct offsets. */ fix_orglist(pi->segment); rewind_segments(pi); def_orglist(pi->segment); break; case DIRECTIVE_DSEG: fix_orglist(pi->segment); def_orglist(pi->dseg); if (pi->dseg->hi_addr == 0) { /* XXX move to emit */ print_msg(pi, MSGTYPE_ERROR, "Can't use .DSEG directive because device has no RAM"); } break; case DIRECTIVE_DW: if (pi->segment->flags & SEG_BSS_DATA) { print_msg(pi, MSGTYPE_ERROR, "Can't use .DW directive in data segment (.DSEG)"); return(True); } while (next) { data = get_next_token(next, TERM_COMMA); if(pi->pass == PASS_2) { if(!get_expr(pi, next, &i)) return(False); if((i < -32768) || (i > 65535)) print_msg(pi, MSGTYPE_WARNING, "Value %d is out of range (-32768 <= k <= 65535). Will be masked", i); } if (pi->pass == PASS_2) { if (pi->list_line && pi->list_on) { fprintf(pi->list_file, " %s\n", pi->list_line); pi->list_line = NULL; fprintf(pi->list_file, "%c:%06x %04x\n", pi->segment->ident, pi->segment->addr, i); } if (pi->segment == pi->eseg) { write_ee_byte(pi, pi->eseg->addr, (unsigned char)i); write_ee_byte(pi, pi->eseg->addr + 1, (unsigned char)(i >> 8)); } if (pi->segment == pi->cseg) { write_prog_word(pi, pi->cseg->addr, i); } } if (pi->segment == pi->eseg) advance_ip(pi->eseg, 2); if (pi->segment == pi->cseg) advance_ip(pi->cseg, 1); next = data; } break; case DIRECTIVE_ENDM: case DIRECTIVE_ENDMACRO: print_msg(pi, MSGTYPE_ERROR, "No .MACRO found before .ENDMACRO"); break; case DIRECTIVE_EQU: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".EQU needs an operand"); return(True); } data = get_next_token(next, TERM_EQUAL); if(!data) { print_msg(pi, MSGTYPE_ERROR, "%s needs an expression (e.g. .EQU BZZZT = 0x2a)", next); return(True); } get_next_token(data, TERM_END); if(!get_expr(pi, data, &i)) return(False); if(test_label(pi,next,"%s have already been defined as a label")!=NULL) return(True); if(test_variable(pi,next,"%s have already been defined as a .SET variable")!=NULL) return(True); /* B.A. : New. Forward references allowed. But check, if everything is ok ... */ if(pi->pass==PASS_1) { /* Pass 1 */ if(test_constant(pi,next,"Can't redefine constant %s, use .SET instead")!=NULL) return(True); if(def_const(pi, next, i)==False) return(False); } else { /* Pass 2 */ int j; if(get_constant(pi, next, &j)==False) { /* Defined in Pass 1 and now missing ? */ print_msg(pi, MSGTYPE_ERROR, "Constant %s is missing in pass 2", next); return(False); } if(i != j) { print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %d in pass1 to %d in pass 2", next,j,i); return(False); } /* OK. Definition is unchanged */ } if((pi->pass == PASS_2) && pi->list_line && pi->list_on) { fprintf(pi->list_file, " %s\n", pi->list_line); pi->list_line = NULL; } break; case DIRECTIVE_ESEG: fix_orglist(pi->segment); def_orglist(pi->eseg); if(pi->device->eeprom_size == 0) { /* XXX */ print_msg(pi, MSGTYPE_ERROR, "Can't use .ESEG directive because device has no EEPROM"); } break; case DIRECTIVE_EXIT: pi->fi->exit_file = True; break; /*** .include ***/ case DIRECTIVE_INCLUDE: if(!next) { print_msg(pi, MSGTYPE_ERROR, "Nothing to include"); return(True); } next = term_string(pi, next); if((pi->pass == PASS_2) && pi->list_line && pi->list_on) { fprintf(pi->list_file, " %s\n", pi->list_line); pi->list_line = NULL; } // Test if include is in local directory ok = test_include(next); data = NULL; if(!ok) for(incpath = GET_ARG_LIST(pi->args, ARG_INCLUDEPATH); incpath && !ok; incpath = incpath->next) { i = strlen(incpath->data); if(data) free(data); data = malloc(i + strlen(next) + 2); if(!data) { print_msg(pi, MSGTYPE_OUT_OF_MEM, NULL); return(False); } strcpy(data, incpath->data); if((data[i - 1] != '\\') && (data[i - 1] != '/')) data[i++] = '/'; strcpy(&data[i], next); //printf("testing: %s\n", data); ok = test_include(data); } if(ok) { fi_bak = pi->fi; ok = parse_file(pi, data ? data : next); pi->fi = fi_bak; } else print_msg(pi, MSGTYPE_ERROR, "Cannot find include file: %s", next); if(data) free(data); break; /*** .includepath ***/ case DIRECTIVE_INCLUDEPATH: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".INCLUDEPATH needs an operand"); return(True); } data = get_next_token(next, TERM_SPACE); if(data) { print_msg(pi, MSGTYPE_ERROR, ".INCLUDEPATH needs an operand!!!"); get_next_token(data, TERM_END); if(!get_expr(pi, data, &i)) return(False); } next = term_string(pi, next); /* get arg list start pointer */ incpath = GET_ARG_LIST(pi->args, ARG_INCLUDEPATH); /* search for last element */ if(incpath == NULL) { dl = malloc(sizeof(struct data_list)); data = malloc(strlen(next)+1); if(dl && data) { dl->next = NULL; strcpy(data, next); dl->data = data; SET_ARG_LIST(pi->args, ARG_INCLUDEPATH, dl); } else { printf("Error: Unable to allocate memory\n"); return(False); } } else add_arg(&incpath, next); break; case DIRECTIVE_LIST: if(pi->pass == PASS_2) if(pi->list_file) pi->list_on = True; break; case DIRECTIVE_LISTMAC: if(pi->pass == PASS_2) SET_ARG_I(pi->args, ARG_LISTMAC, True); break; case DIRECTIVE_MACRO: return(read_macro(pi, next)); // break; case DIRECTIVE_NOLIST: if(pi->pass == PASS_2) pi->list_on = False; break; case DIRECTIVE_ORG: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".ORG needs an operand"); return(True); } get_next_token(next, TERM_END); if(!get_expr(pi, next, &i)) return(False); fix_orglist(pi->segment); pi->segment->addr = i; /* XXX advance */ def_orglist(pi->segment); if(pi->fi->label) pi->fi->label->value = i; if((pi->pass == PASS_2) && pi->list_line && pi->list_on) { fprintf(pi->list_file, " %s\n", pi->list_line); pi->list_line = NULL; } break; case DIRECTIVE_SET: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".SET needs an operand"); return(True); } data = get_next_token(next, TERM_EQUAL); if(!data) { print_msg(pi, MSGTYPE_ERROR, "%s needs an expression (e.g. .SET BZZZT = 0x2a)", next); return(True); } get_next_token(data, TERM_END); if(!get_expr(pi, data, &i)) return(False); if(test_label(pi,next,"%s have already been defined as a label")!=NULL) return(True); if(test_constant(pi,next,"%s have already been defined as a .EQU constant")!=NULL) return(True); return(def_var(pi, next, i)); // break; case DIRECTIVE_DEFINE: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".DEFINE needs an operand"); return(True); } data = get_next_token(next, TERM_SPACE); if(data) { get_next_token(data, TERM_END); if(!get_expr(pi, data, &i)) return(False); } else i = 1; if(test_label(pi,next,"%s have already been defined as a label")!=NULL) return(True); if(test_variable(pi,next,"%s have already been defined as a .SET variable")!=NULL) return(True); /* B.A. : New. Forward references allowed. But check, if everything is ok ... */ if(pi->pass==PASS_1) { /* Pass 1 */ if(test_constant(pi,next,"Can't redefine constant %s, use .SET instead")!=NULL) return(True); if(def_const(pi, next, i)==False) return(False); } else { /* Pass 2 */ int j; if(get_constant(pi, next, &j)==False) { /* Defined in Pass 1 and now missing ? */ print_msg(pi, MSGTYPE_ERROR, "Constant %s is missing in pass 2", next); return(False); } if(i != j) { print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %d in pass1 to %d in pass 2", next,j,i); return(False); } /* OK. Definition is unchanged */ } if((pi->pass == PASS_2) && pi->list_line && pi->list_on) { fprintf(pi->list_file, " %s\n", pi->list_line); pi->list_line = NULL; } break; case DIRECTIVE_NOOVERLAP: if (pi->pass == PASS_1) { fix_orglist(pi->segment); pi->segment_overlap = SEG_DONT_OVERLAP; def_orglist(pi->segment); } break; case DIRECTIVE_OVERLAP: if (pi->pass == PASS_1) { fix_orglist(pi->segment); pi->segment_overlap = SEG_ALLOW_OVERLAP; def_orglist(pi->segment); } break; case DIRECTIVE_PRAGMA: if (!next) { print_msg(pi, MSGTYPE_ERROR, "PRAGMA needs an operand, %s should be specified", snprint_list(buf, sizeof(buf), pragma_list)); return(True); } my_strupr(next); data = get_next_token(next, TERM_SPACE); pragma = lookup_keyword(pragma_list, next, False); switch (pragma) { case PRAGMA_OVERLAP: if (pi->pass == PASS_1) { int overlap_setting = OVERLAP_UNDEFINED; if (data) { my_strupr(data); overlap_setting = lookup_keyword(overlap_value, data, False); }; switch (overlap_setting) { case OVERLAP_DEFAULT: pi->effective_overlap = GET_ARG_I(pi->args, ARG_OVERLAP); break; case OVERLAP_IGNORE: case OVERLAP_WARNING: case OVERLAP_ERROR: pi->effective_overlap = overlap_setting; break; default: print_msg(pi, MSGTYPE_ERROR, "For PRAGMA %s directive" " %s should be specified as the parameter", next, snprint_list(buf, sizeof(buf), overlap_value)); return(False); } } return(True); break; default: if(pi->pass == PASS_2) print_msg(pi, MSGTYPE_MESSAGE, "PRAGMA %s directive currently ignored", next); return(True); } break; case DIRECTIVE_UNDEF: // TODO break; case DIRECTIVE_IFDEF: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".IFDEF needs an operand"); return(True); } get_next_token(next, TERM_END); /* B.A. : Forward referenc is not allowed for ifdef and ifndef */ /* Store undefined symbols in blacklist in pass1 and check, if they are still undefined in pass2 */ if(get_symbol(pi, next, NULL)) { #if 0 // If it's not defined in the first pass, but was defined later // then it should be considered OK with regards to ifdef..endif and // ifndef..endif code sections. Removed this code. if(pi->pass==PASS_2) { /* B.A. : 'Still undefined'-test in pass 2 */ if(test_blacklist(pi,next,"Forward reference (%s) not allowed in .ifdef directive")!=NULL) return(False); } #else pi->conditional_depth++; #endif } else { if(pi->pass==PASS_1) { /* B.A. : Store undefined symbols in pass 1 */ if(def_blacklist(pi, next)==False) return(False); } if(!spool_conditional(pi, False)) return(False); } break; case DIRECTIVE_IFNDEF: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".IFNDEF needs an operand"); return(True); } get_next_token(next, TERM_END); /* B.A. : Forward referenc is not allowed for ifdef and ifndef */ /* Store undefined symbols in blacklist in pass1 and check, if they are still undefined in pass2 */ if(!get_symbol(pi, next, NULL)) { #if 0 if(pi->pass==PASS_2) { /* B.A. : 'Still undefined'-test in pass 2 */ // If it's not defined in the first pass, but was defined later // then it should be considered OK with regards to ifdef..endif and // ifndef..endif code sections. Removed this code. if(test_blacklist(pi,next,"Forward reference (%s) not allowed in .ifndef directive")!=NULL) return(False); } if(!spool_conditional(pi, False)) return(False); #else pi->conditional_depth++; #endif } else { if(pi->pass==PASS_1) { /* B.A. : Store undefined symbols in pass 1 */ if(def_blacklist(pi, next)==False) return(False); } if(!spool_conditional(pi, False)) return(False); } break; case DIRECTIVE_IF: if(!next) { print_msg(pi, MSGTYPE_ERROR, ".IF needs an expression"); return(True); } get_next_token(next, TERM_END); if(!get_expr(pi, next, &i)) return(False); if(i) pi->conditional_depth++; else { if(!spool_conditional(pi, False)) return(False); } break; case DIRECTIVE_ELSE: case DIRECTIVE_ELIF: case DIRECTIVE_ELSEIF: if(!spool_conditional(pi, True)) return(False); break; case DIRECTIVE_ENDIF: if(pi->conditional_depth == 0) print_msg(pi, MSGTYPE_ERROR, "Too many .ENDIF"); else pi->conditional_depth--; break; case DIRECTIVE_MESSAGE: if(pi->pass == PASS_1) return(True); if(!next) { print_msg(pi, MSGTYPE_ERROR, "No message parameter supplied"); return(True); } /* B.A : Extended .MESSAGE. Now a comma separated list like in .db is possible and not only a string */ print_msg(pi, MSGTYPE_MESSAGE_NO_LF, NULL); /* Prints Line Header (filename, linenumber) without trailing /n */ while(next) { /* Modified code from parse_db(). Thank you :-) */ data = get_next_token(next, TERM_COMMA); if(next[0] == '\"') { /* string parsing */ next = term_string(pi, next); print_msg(pi, MSGTYPE_APPEND,"%s",next); while(*next != '\0') { next++; } } else { if(!get_expr(pi, next, &i)) { print_msg(pi, MSGTYPE_APPEND,"\n"); /* Add newline */ return(False); } print_msg(pi, MSGTYPE_APPEND,"0x%02X",i); } next = data; } print_msg(pi, MSGTYPE_APPEND,"\n"); /* Add newline */ break; case DIRECTIVE_WARNING: if(pi->pass == PASS_1) return(True); if(!next) { print_msg(pi, MSGTYPE_ERROR, "No warning string supplied"); return(True); } next = term_string(pi, next); print_msg(pi, MSGTYPE_WARNING, next); break; case DIRECTIVE_ERROR: if(!next) { /* B.A : Fix segfault bug if .error without parameter was used */ print_msg(pi, MSGTYPE_ERROR, "No error string supplied"); return(True); } next = term_string(pi, next); print_msg(pi, MSGTYPE_ERROR, "%s", next); pi->error_count = pi->max_errors; if(pi->pass == PASS_1) return(True); break; }
int get_token(int lookup_flag) { context_buffer.clear(); for (;;) { int n = 0; int bol = input_stack::bol(); int c = input_stack::get_char(); if (bol && c == command_char) { token_buffer.clear(); token_buffer += c; // the newline is not part of the token for (;;) { c = input_stack::peek_char(); if (c == EOF || c == '\n') break; input_stack::get_char(); token_buffer += char(c); } context_buffer = token_buffer; return COMMAND_LINE; } switch (c) { case EOF: return EOF; case ' ': case '\t': break; case '\\': { int d = input_stack::peek_char(); if (d != '\n') { context_buffer = '\\'; return '\\'; } input_stack::get_char(); break; } case '#': do { c = input_stack::get_char(); } while (c != '\n' && c != EOF); if (c == '\n') context_buffer = '\n'; return c; case '"': context_buffer = '"'; token_buffer.clear(); for (;;) { c = input_stack::get_char(); if (c == '\\') { context_buffer += '\\'; c = input_stack::peek_char(); if (c == '"') { input_stack::get_char(); token_buffer += '"'; context_buffer += '"'; } else token_buffer += '\\'; } else if (c == '\n') { error("newline in string"); break; } else if (c == EOF) { error("missing `\"'"); break; } else if (c == '"') { context_buffer += '"'; break; } else { context_buffer += char(c); token_buffer += char(c); } } return TEXT; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { int overflow = 0; n = 0; for (;;) { if (n > (INT_MAX - 9)/10) { overflow = 1; break; } n *= 10; n += c - '0'; context_buffer += char(c); c = input_stack::peek_char(); if (c == EOF || !csdigit(c)) break; c = input_stack::get_char(); } token_double = n; if (overflow) { for (;;) { token_double *= 10.0; token_double += c - '0'; context_buffer += char(c); c = input_stack::peek_char(); if (c == EOF || !csdigit(c)) break; c = input_stack::get_char(); } // if somebody asks for 1000000000000th, we will silently // give them INT_MAXth double temp = token_double; // work around gas 1.34/sparc bug if (token_double > INT_MAX) n = INT_MAX; else n = int(temp); } } switch (c) { case 'i': case 'I': context_buffer += char(c); input_stack::get_char(); return NUMBER; case '.': { context_buffer += '.'; input_stack::get_char(); got_dot: double factor = 1.0; for (;;) { c = input_stack::peek_char(); if (c == EOF || !csdigit(c)) break; input_stack::get_char(); context_buffer += char(c); factor /= 10.0; if (c != '0') token_double += factor*(c - '0'); } if (c != 'e' && c != 'E') { if (c == 'i' || c == 'I') { context_buffer += char(c); input_stack::get_char(); } return NUMBER; } } // fall through case 'e': case 'E': { int echar = c; input_stack::get_char(); c = input_stack::peek_char(); int sign = '+'; if (c == '+' || c == '-') { sign = c; input_stack::get_char(); c = input_stack::peek_char(); if (c == EOF || !csdigit(c)) { input_stack::push_back(sign); input_stack::push_back(echar); return NUMBER; } context_buffer += char(echar); context_buffer += char(sign); } else { if (c == EOF || !csdigit(c)) { input_stack::push_back(echar); return NUMBER; } context_buffer += char(echar); } input_stack::get_char(); context_buffer += char(c); n = c - '0'; for (;;) { c = input_stack::peek_char(); if (c == EOF || !csdigit(c)) break; input_stack::get_char(); context_buffer += char(c); n = n*10 + (c - '0'); } if (sign == '-') n = -n; if (c == 'i' || c == 'I') { context_buffer += char(c); input_stack::get_char(); } token_double *= pow(10.0, n); return NUMBER; } case 'n': input_stack::get_char(); c = input_stack::peek_char(); if (c == 'd') { input_stack::get_char(); token_int = n; context_buffer += "nd"; return ORDINAL; } input_stack::push_back('n'); return NUMBER; case 'r': input_stack::get_char(); c = input_stack::peek_char(); if (c == 'd') { input_stack::get_char(); token_int = n; context_buffer += "rd"; return ORDINAL; } input_stack::push_back('r'); return NUMBER; case 't': input_stack::get_char(); c = input_stack::peek_char(); if (c == 'h') { input_stack::get_char(); token_int = n; context_buffer += "th"; return ORDINAL; } input_stack::push_back('t'); return NUMBER; case 's': input_stack::get_char(); c = input_stack::peek_char(); if (c == 't') { input_stack::get_char(); token_int = n; context_buffer += "st"; return ORDINAL; } input_stack::push_back('s'); return NUMBER; default: return NUMBER; } break; case '\'': { c = input_stack::peek_char(); if (c == 't') { input_stack::get_char(); c = input_stack::peek_char(); if (c == 'h') { input_stack::get_char(); context_buffer = "'th"; return TH; } else input_stack::push_back('t'); } context_buffer = "'"; return '\''; } case '.': { c = input_stack::peek_char(); if (c != EOF && csdigit(c)) { n = 0; token_double = 0.0; context_buffer = '.'; goto got_dot; } return get_token_after_dot(c); } case '<': c = input_stack::peek_char(); if (c == '-') { input_stack::get_char(); c = input_stack::peek_char(); if (c == '>') { input_stack::get_char(); context_buffer = "<->"; return DOUBLE_ARROW_HEAD; } context_buffer = "<-"; return LEFT_ARROW_HEAD; } else if (c == '=') { input_stack::get_char(); context_buffer = "<="; return LESSEQUAL; } context_buffer = "<"; return '<'; case '-': c = input_stack::peek_char(); if (c == '>') { input_stack::get_char(); context_buffer = "->"; return RIGHT_ARROW_HEAD; } context_buffer = "-"; return '-'; case '!': c = input_stack::peek_char(); if (c == '=') { input_stack::get_char(); context_buffer = "!="; return NOTEQUAL; } context_buffer = "!"; return '!'; case '>': c = input_stack::peek_char(); if (c == '=') { input_stack::get_char(); context_buffer = ">="; return GREATEREQUAL; } context_buffer = ">"; return '>'; case '=': c = input_stack::peek_char(); if (c == '=') { input_stack::get_char(); context_buffer = "=="; return EQUALEQUAL; } context_buffer = "="; return '='; case '&': c = input_stack::peek_char(); if (c == '&') { input_stack::get_char(); context_buffer = "&&"; return ANDAND; } context_buffer = "&"; return '&'; case '|': c = input_stack::peek_char(); if (c == '|') { input_stack::get_char(); context_buffer = "||"; return OROR; } context_buffer = "|"; return '|'; default: if (c != EOF && csalpha(c)) { token_buffer.clear(); token_buffer = c; for (;;) { c = input_stack::peek_char(); if (c == EOF || (!csalnum(c) && c != '_')) break; input_stack::get_char(); token_buffer += char(c); } int tok = lookup_keyword(token_buffer.contents(), token_buffer.length()); if (tok != 0) { context_buffer = token_buffer; return tok; } char *def = 0; if (lookup_flag) { token_buffer += '\0'; def = macro_table.lookup(token_buffer.contents()); token_buffer.set_length(token_buffer.length() - 1); if (def) { if (c == '(') { input_stack::get_char(); interpolate_macro_with_args(def); } else input_stack::push(new macro_input(def)); } } if (!def) { context_buffer = token_buffer; if (csupper(token_buffer[0])) return LABEL; else return VARIABLE; } } else { context_buffer = char(c); return (unsigned char)c; } break; } } }
int yylex(YYSTYPE *yylval, YYLTYPE *loc, PARSER *parser) { char *q; int len; int kw_id; int in_comment = 0; do { if (!(*parser->p)) { if (!parser->input(parser->buffer, parser->file)) return 0; parser->p = parser->buffer; } if (!in_comment) { if (*parser->p == '/' && *(parser->p+1) == '*') { parser->p += 2; in_comment = 1; while (*parser->p) { if (*parser->p == '*' && *(parser->p+1) == '/') { parser->p += 2; in_comment = 0; break; } parser->p++; } } else if (*parser->p == '/' && *(parser->p+1) == '/') { *parser->p = 0; } else if (isspace(*parser->p)) { if (*parser->p == '\n') loc->first_line++; parser->p++; } else { break; } } else { while (*parser->p) { if (*parser->p == '*' && *(parser->p+1) == '/') { parser->p += 2; in_comment = 0; break; } parser->p++; } loc->first_line++; } } while (1); q = parser->p; if (*q == '\"') { parser->p++; while (*parser->p && *parser->p != '\"') { parser->p++; } parser->p++; } else { parser->p++; while (*parser->p && same_token(*(parser->p-1), *parser->p)) { parser->p++; } } if (q > parser->p) q = parser->buffer; len = (*q != '\"') ? parser->p - q : parser->p - q - 2; char *str = add_string(parser->module, (*q != '\"') ? q : q + 1, len); yylval->name = str; loc->first_column = q - parser->buffer + 1; //printf("[%s", yylval->name); if (*q == '\"') return STRING_CONSTANT; if (issymbol(yylval->name[0]) && yylval->name[1] == 0) { //printf(" -> %d]\n", yylval->name[0]); return yylval->name[0]; } kw_id = lookup_keyword(yylval->name); if (kw_id) { //printf(" -> %d]\n", k->id); return kw_id; } if (isdigit(yylval->name[0]) || yylval->name[0] == '-') { //printf(" -> %d]\n", yylval->ch); return INT_CONSTANT; } //printf(" -> NAME]\n"); return NAME; }