void testSymbol() { #if 0 // Operator int i = 0; for(; i < KeywordsCount; ++i) { printf("%s\n", Operators[i]); } #endif // test: isKeyword isLiteral isVar isSemicolon symbol_construct symbol_deconstruct #if 0 // not ok char *symbol[] = {"int", "i", "=", "12", ";"}; for(int i = 0; i < sizeof(symbol) / sizeof(symbol[0]); ++i) { // ok printf("symbol %s : isKeyword:%s isLiteral:%s isVar:%s isSemicolon:%s\n", symbol[i], TO_BOOL_STR(isKeyword(symbol[i])), TO_BOOL_STR(isLiteral(symbol[i])), TO_BOOL_STR(isVar(symbol[i])), TO_BOOL_STR(isSemicolon(symbol[i]))); Symbol *sb = symbol_construct(symbol[i]); if(sb) { // not ok printf("%x %x %x %x \n", IS_KEYWORD(sb->type), IS_LITERAL(sb->type), IS_VAR(sb->type), IS_SEMICOLON(sb->type)); symbol_deconstruct(sb); } } #endif // test: isCharLiteral isStringLiteral isDecNumber isOctNumber isHexNumber isFloatNumer // tes: isDoubleNumber #if 0 // ok int i = 0; const char *strArr[] = {"\'c\'", "\"abc\"", "453", "0453", "781", "a90", "0x34", "0X56", "9.34", "9.4e2", "9.5E5", "9e+2", "9e-3", "9.34f", "9.34F" }; for(; i < sizeof(strArr) / sizeof(strArr[0]); ++i) { printf("%s: isCharLiteral(%s)\n\t", strArr[i], TO_BOOL_STR(isCharLiteral(strArr[i]))); printf("isStringLiteral(%s)\n\t", TO_BOOL_STR(isStringLiteral(strArr[i]))); printf("isDecNumber(%s)\n\t", TO_BOOL_STR(isDecNumber(strArr[i]))); printf("isOctNumber(%s)\n\t", TO_BOOL_STR(isOctNumber(strArr[i]))); printf("isHexNumber(%s)\n\t", TO_BOOL_STR(isHexNumber(strArr[i]))); printf("isFloatNumber(%s)\n\t", TO_BOOL_STR(isFloatNumber(strArr[i]))); printf("isDoubleNumber(%s)\n", TO_BOOL_STR(isDoubleNumber(strArr[i]))); } /* // I don't know why, but it can't output all strings for(; i < sizeof(strArr) / sizeof(strArr[0]); ++i) { printf("%s: isCharLiteral(%s)\n\t isStringLiteral(%s)\n\t isDecNumber(%s)\n\t isOctNumber(%s)\n\t isHexNumber(%s)\n\t isFloatNumber(%s)\n\t isDoubleNumber(%s)\n", strArr[i], TO_BOOL_STR(isCharLiteral(strArr[i])), TO_BOOL_STR(isStringLiteral(strArr[i])), TO_BOOL_STR(isDecNumber(strArr[i])), TO_BOOL_STR(isOctNumber(strArr[i])), TO_BOOL_STR(isHexNumber(strArr[i])), TO_BOOL_STR(isFloatNumber(strArr[i])), TO_BOOL_STR(isDoubleNumber(strArr[i]))); } */ #endif }
void l_scan (SOURCE_FILE * sf) { int r = 1,in_sub = FALSE; int block_stack[STACK_SIZE],block_size = 0; list_init(&list_sub); list_init(&list_var); list_init(&list_array); while(r) { r = l_get_line(sf); pline = line; l_get_token (); // skip empty line if (token_type==TT_LINE_END) continue; // check command if (IS_KEYWORD(token_type)) { if (token_type==KEY_SUB) { USER_SUB * sub; if(in_sub) { merror_msg("sub procedure can not be nested"); } in_sub = TRUE; match_type(TT_ID); sub = (USER_SUB*)calloc(sizeof(USER_SUB),1); sub->name = s_strdup(token); sub->pos = sf->pos; list_push(&list_sub,sub); match_type(TT_LINE_END); push_block(B_SUB); continue; } else if (token_type==KEY_END) { if (block_size<=0 || block_top==B_REPEAT) merror_illegal_token(); match_type(TT_LINE_END); if(pop_block==B_SUB) { in_sub = FALSE; } continue; } else if(token_type==KEY_VAR) { while(1) { match_type(TT_ID); l_get_token(); if (token_type==TT_END) break; else if (token_type==TT_COM) continue; else merror_illegal_token(); } } if (!in_sub) merror_illegal_token(); if(token_type==KEY_IF) { match_exp(pline); match_type(TT_LINE_END); push_block(B_IF); } else if (token_type==KEY_ELSEIF) { if (block_size<=0 || block_top!=B_IF) merror_illegal_token(); match_exp(pline); match_type(TT_LINE_END); } else if (token_type==KEY_ELSE) { if (block_size<=0 || !(block_top==B_IF || block_top==B_CASE)) merror_illegal_token(); match_type(TT_LINE_END); } else if (token_type==KEY_WHILE) { match_exp(pline); match_type(TT_LINE_END); push_block(B_WHILE); } else if (token_type==KEY_FOR) { // 'for' ID '=' EXP 'to' EXP match_type(TT_ID); match_type(OPR_EQ); match_exp(pline); match_str("to"); match_exp(pline); l_get_token(); if (token_type!=TT_LINE_END) { if (!str_eq(token,"step")) merror_expect("step"); match_exp(pline); match_type(TT_LINE_END); } push_block(B_FOR); } else if (token_type==KEY_CASE) { match_type(TT_ID); match_type(TT_LINE_END); push_block(B_CASE); } else if (token_type==KEY_REPEAT) { match_type(TT_LINE_END); push_block(B_REPEAT); } else if (token_type==KEY_UNTIL) { if (block_size<=0 || block_top!=B_REPEAT) merror_illegal_token(); match_exp(pline); match_type(TT_LINE_END); pop_block; } else if (token_type==KEY_WHEN) { if (block_size<=0 || block_top!=B_CASE) merror_illegal_token(); match_exp(pline); match_type(TT_LINE_END); } else if (token_type==KEY_GOSUB) { match_type(TT_ID); match_type(TT_LINE_END); } else if (token_type==KEY_EXIT) { match_type(TT_LINE_END); } else if (token_type==KEY_BREAK) { match_type(TT_LINE_END); } else if (token_type==KEY_RETURN) { match_type(TT_LINE_END); } else if (token_type==KEY_DIM) { while(1) { match_type (TT_ID); match_type (TT_LBK); match_exp (pline); match_type (TT_RBK); l_get_token(); if (token_type==TT_COM) continue; else if (token_type==TT_LINE_END) break; else merror_illegal_token(); } } } else if (token_type==TT_ID) { if (!in_sub) merror_illegal_token(); if (str_eq(token,"print")) { while(1) { l_get_token(); if(token_type!=TT_STRING) { l_put_back(); match_exp(pline); } l_get_token(); if(token_type==TT_LINE_END) break; else if (token_type==TT_COM) continue; else merror_expect(","); } } else if (str_eq(token,"input")) { while(1) { match_type(TT_ID); l_get_token(); if(token_type==TT_LINE_END) break; else if (token_type==TT_COM) continue; else merror_expect(","); } } else { // match: [var][=][exp] l_get_token(); if (token_type==OPR_EQ) { match_exp(pline); match_type(TT_LINE_END); } else { if (token_type!=TT_LBK)merror_expect("("); match_exp(pline); match_type(TT_RBK); match_type(OPR_EQ); match_exp(pline); match_type(TT_LINE_END); } } } else merror_illegal_token (); } if (block_size>0) merror_msg("incompleted '%s' block!",BLOCK_NAME[block_top]); }
int i_execute_line (SOURCE_FILE * sf,const int todo) { l_get_line(sf);pline = line; l_get_token(); if (token_type==TT_LINE_END) { return EXE_DO;// skip empty line } else if (IS_KEYWORD(token_type)) { if (token_type==KEY_END) { return EXE_END; } else if (token_type==KEY_IF) { return i_execute_if (sf,todo); } else if (token_type==KEY_WHILE) { return i_execute_while(sf,todo); } else if (token_type==KEY_REPEAT) { return i_execute_repeat(sf,todo); } if (token_type==KEY_UNTIL) { return EXE_UNTIL; } else if (token_type==KEY_FOR) { return i_execute_for(sf,todo); } else if (token_type==KEY_CASE) { return i_execute_case(sf,todo); } else if (token_type==KEY_ELSEIF) { return EXE_ELSEIF; } else if (token_type==KEY_WHEN) { return EXE_WHEN; } else if (token_type==KEY_ELSE) { return EXE_ELSE; } if (!todo) return EXE_DO; if (token_type==KEY_BREAK) { return EXE_BREAK; } else if (token_type==KEY_RETURN) { return EXE_RETURN; } else if (token_type==KEY_GOSUB) { extern int i_call_sub (SOURCE_FILE *,const char *); l_get_token(); return i_call_sub(sf,token); } else if (token_type==KEY_DIM) { ARRAY * array; long size; while(1) { l_get_token(); // new array array = (ARRAY*)calloc(sizeof(ARRAY),1); array->name = s_strdup(token); list_push(&list_array,array); // l_get_token();// skip '(' size = (long)calc_check(FALSE,pline); if (size<=0) { merror_msg("illegal array size %d!",size); } array->size = size; array->array = calloc(sizeof(real),size); { int i; for (i=0;i<size;++i) array->array[i] = 0.0; } l_get_token();// skip ')' l_get_token(); if (token_type==TT_COM) continue; else if (token_type==TT_LINE_END) break; } } else if (token_type==KEY_EXIT) { d_exit(); } } else if(token_type==TT_ID) { if (str_eq(token,"print")) { while(1) { l_get_token(); if (token_type==TT_STRING) { printf("%s",token); } else { real result; char buf[128]; l_put_back(); result = calc_check(FALSE,pline); d_ftoa(result,buf); printf(buf); } l_get_token(); if (token_type==TT_LINE_END)break; }//while }//print else if (str_eq(token,"input")) { VAR * v;char buf[64]; while(1) { l_get_token(); v = get_var(token); gets(64,buf);puts(buf); v->value = atof(buf); l_get_token(); if (token_type==TT_LINE_END)break; }//while }//input else { ARRAY * a; VAR * var; var = find_var(token); if (var!=NULL) { l_get_token(); //skip '=' var->value = calc_check(FALSE,pline); return EXE_DO; } a = find_array(token); if(a!=NULL) { int index; l_get_token();//skip ( index = (int)calc_check(FALSE,pline); // delete this line,calc_check call skip ( automatically //l_get_token();//skip ) l_get_token();// skip = assign_element(a,index,calc_check(FALSE,pline)); return EXE_DO; } var = create_var(token); l_get_token(); //skip '=' var->value = calc_check(FALSE,pline); return EXE_DO; }//assign }// TT_ID else { merror_illegal_token(); } return EXE_DO; }