⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 token.c

📁 CC386 is a general-purpose 32-bit C compiler. It is not an optimizing compiler but given that the co
💻 C
📖 第 1 页 / 共 2 页
字号:
 copy_string(token, next_token);
 Affected_thing.set = True;
 If token_type IsNot switch_end_token_type
  Then
   Affected_thing.val = Affected_thing.def;
   return;
  EndIf;
 scan_out_token();
 copy_string(token, next_token);
  If (Not token_is_number)                                OrIf
     (token_numeric_value LessThan    Affected_thing.min) OrIf
     (token_numeric_value GreaterThan Affected_thing.max)
   Then
   linker_error(8,"Syntax error:  Switch \"%s\" requires a numeric value\n"
                  "               between %u and %u\n",
                  Current_switch.full_name,
                  Affected_thing.min, Affected_thing.max);
   Else
    Affected_thing.val = token_numeric_value;
   EndIf;
 scan_out_token();
 copy_string(token, next_token);
 return;
EndCode
#undef Current_switch
#undef Affected_thing

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                         scan_out_token                                  |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
void scan_out_token()
BeginDeclarations
bit_16                                 paren_count;
EndDeclarations
BeginCode
 eat_white_space();
 copy_string(next_token, null_string);
 token_is_hex_number =
 token_is_number     = False;
 Using token_break_char
  BeginCase
   When '\n':
    prompt_next_stdin = True;
    concat_char_to_string(next_token, token_break_char);
    If token_stack.first Is token_stack.last
     Then
      token_type       = end_of_command_line_token_type;
     Else
      token_type       = line_end_token_type;
     EndIf;
    token_break_char = ' ';  /* Make it look like we advanced a character */
    break;
   When ',':
    concat_char_to_string(next_token, token_break_char);
    token_type       = separator_token_type;
    token_break_char = ' ';  /* Make it look like we advanced a character */
    break;
   When ';':
    concat_char_to_string(next_token, token_break_char);
    token_type       = terminator_token_type;
    break;
   When '+':
    concat_char_to_string(next_token, token_break_char);
    token_type       = continuation_token_type;
    token_break_char = ' ';  /* Make it look like we advanced a character */
    break;
   When '/':
   When '-':
    concat_char_to_string(next_token, token_break_char);
    token_type       = switch_token_type;
    token_break_char = ' ';  /* Make it look like we advanced a character */
    break;
   When ':':
    concat_char_to_string(next_token, token_break_char);
    token_type       = switch_end_token_type;
    token_break_char = ' ';  /* Make it look like we advanced a character */
    break;
   When '@':
    concat_char_to_string(next_token, token_break_char);
    token_type       = indirect_file_token_type;
    token_break_char = ' ';  /* Make it look like we advanced a character */
    break;
   When '(':
    paren_count = 1;
    token_type  = text_token_type;
    concat_char_to_string(next_token, token_break_char);
    While paren_count IsNotZero
     BeginWhile
      token_get_char();
      If token_break_char IsNot '\n'
       Then
        concat_char_to_string(next_token, token_break_char);
       Else
        If (*token_stack.first).source_file Is stdin
         Then
          linker_message("continue parenthesized text:  ");
         EndIf;
       EndIf;
      If token_break_char Is '('
       Then
        paren_count++;
       EndIf;
      If token_break_char Is ')'
       Then
        paren_count--;
       EndIf;
     EndWhile;
    token_break_char = ' ';  /* Make it look like we advanced a character */
    break;
   Otherwise:
    token_is_number     = True;
    token_numeric_value = 0;
    If token_break_char Is '"'
     Then
      quoted = '"' ;
      token_get_char() ;
     EndIf;
    While (token_break_char IsNot ',')  AndIf
          (token_break_char IsNot ';')  AndIf
          (token_break_char IsNot '+')  AndIf
          (token_break_char IsNot '/')  AndIf
          (token_break_char IsNot '@')  AndIf
          (token_break_char IsNot ':')  AndIf
          (token_break_char IsNot quoted)  AndIf
          (token_break_char IsNot '\n')
     BeginWhile
      concat_char_to_string(next_token, token_break_char);
      If (Length(next_token) Is 2) AndIf (String(next_token)[0] Is '0') AndIf
         ((String(next_token)[1] Is 'x') OrIf (String(next_token)[1] Is 'X'))
       Then
        token_is_hex_number = True;
       Else
        If token_is_hex_number IsFalse
         Then
          token_is_number = token_is_number AndIf
                            isdigit(token_break_char);
          If token_is_number
           Then
            token_numeric_value = (token_numeric_value * 10) +
                                  Bit_32(token_break_char - '0');
           EndIf;
         Else
          token_is_hex_number =
          token_is_number     = token_is_number AndIf
                                isxdigit(token_break_char);
          If token_is_number
           Then
            If isdigit(token_break_char)
             Then
              token_numeric_value = (token_numeric_value * 16) +
                                    Bit_32(token_break_char - '0');
             Else
              token_numeric_value = (token_numeric_value * 16) +
                                 Bit_32(toupper(token_break_char) - 'A' + 10);
             EndIf;
           EndIf;
         EndIf;
       EndIf;
      token_get_char();
     EndWhile;
    If token_break_char Is '"' And quoted Is '"'
     Then
      quoted = ' ' ;
      token_get_char() ;
     EndIf;
    token_type = filename_token_type;
    break;
  EndCase;
 return;
EndCode

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                           scan_help_switch                              |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
void scan_help_switch(switch_table_ptr current_switch)
BeginDeclarations
#define Affected_thing (*((boolean_switch_ptr)(*current_switch).affected_thing))
FILE                                  *help_file;
EndDeclarations
BeginCode
 Affected_thing.val = True;
 help_file = fopen(CharPtr(near_string(help_filename)), "r");
 If help_file IsNull
  Then
   printf("Could not open help file \"%s\".\n", String(help_filename));
  Else
   While fgets(CharPtr(object_file_element), MAX_ELEMENT_SIZE, help_file) 
         IsNotNull
    BeginWhile
     fputs(CharPtr(object_file_element), stdout);
     If strcmp(CharPtr(object_file_element), 
               "Press [RETURN] to continue.\n") IsZero
      Then
       gets(CharPtr(object_file_element));
      EndIf;
    EndWhile;
   fclose(help_file);
  EndIf;
 exit(0);
 return;
EndCode
#undef Affected_thing

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                           scan_reset_bit_16                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
void scan_reset_bit_16(switch_table_ptr current_switch)
BeginDeclarations
#define Affected_thing (*((bit_32_switch_ptr)(*current_switch).affected_thing))
EndDeclarations
BeginCode
 scan_out_token();
 copy_string(token, next_token);
 Affected_thing.set = False;
 Affected_thing.val = Affected_thing.def;
 return;
EndCode
#undef Affected_thing

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                           scan_reset_switch                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
void scan_reset_switch(switch_table_ptr current_switch)
BeginDeclarations
#define Current_switch                 (*current_switch)
#define Affected_thing (*((boolean_switch_ptr)(*current_switch).affected_thing))
EndDeclarations
BeginCode
 Affected_thing.val = False;
 scan_out_token();
 copy_string(token, next_token);
 return;
EndCode
#undef Current_switch
#undef Affected_thing

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                            scan_set_switch                              |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
void scan_set_switch(switch_table_ptr current_switch)
BeginDeclarations
#define Current_switch                 (*current_switch)
#define Affected_thing (*((boolean_switch_ptr)(*current_switch).affected_thing))
EndDeclarations
BeginCode
 Affected_thing.val = True;
 scan_out_token();
 copy_string(token, next_token);
 return;
EndCode
#undef Current_switch
#undef Affected_thing

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                            scan_string_switch                           |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
void scan_string_switch(switch_table_ptr current_switch)
BeginDeclarations
#define Current_switch                 (*current_switch)
#define Affected_thing (*((text_switch_ptr)(*current_switch).affected_thing))
EndDeclarations
BeginCode
 scan_out_token();
 copy_string(token, next_token);
 If token_type IsNot switch_end_token_type
  Then
   linker_error(8,"Syntax error:  \":\" did not follow switch \"%s\"\n",
                  Current_switch.full_name);
  EndIf;
 scan_out_token();
 copy_string(token, next_token);
 If token_type IsNot text_token_type
  Then
   linker_error(8, "Syntax error:  Parenthesized text did not follow\n"
                   "\t\"%s\" switch.  Instead found \"%s\".\n",
                  Current_switch.full_name, String(token));
  EndIf;
 memcpy(String(next_token),String(next_token)+1,Length(next_token)-2) ;
 Length(next_token) -= 2 ;
 String(next_token)[Length(next_token)] = 0 ;
 Affected_thing.val = duplicate_string(next_token);
 scan_out_token();
 copy_string(token, next_token);
 return;
EndCode
#undef Affected_thing
#undef Current_switch
/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                            scan_text_switch                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
void scan_text_switch(switch_table_ptr current_switch)
BeginDeclarations
#define Current_switch                 (*current_switch)
#define Affected_thing (*((text_switch_ptr)(*current_switch).affected_thing))
EndDeclarations
BeginCode
 scan_out_token();
 copy_string(token, next_token);
 If token_type IsNot switch_end_token_type
  Then
   linker_error(8,"Syntax error:  \":\" did not follow switch \"%s\"\n",
                  Current_switch.full_name);
  EndIf;
 scan_out_token();
 copy_string(token, next_token);
 If token_type IsNot text_token_type
  Then
   linker_error(8, "Syntax error:  Parenthesized text did not follow\n"
                   "\t\"%s\" switch.  Instead found \"%s\".\n",
                  Current_switch.full_name, String(token));
  EndIf;
 Affected_thing.val = duplicate_string(next_token);
 scan_out_token();
 copy_string(token, next_token);
 return;
EndCode
#undef Affected_thing
#undef Current_switch

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                           token_get_char                                |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
void token_get_char()
BeginDeclarations
int_16                                 c;
token_stack_ptr                        tos;
#define Tos                            (*tos)
#define Tos_string                     (*tos).token_string
EndDeclarations
BeginCode
 Loop
  BeginLoop
   If ((*token_stack.first).source_file Is stdin) AndIf
      (prompt_next_stdin)
    Then
     linker_message("continue:  ");
     prompt_next_stdin = False;
    EndIf;
   tos = token_stack.first;
   If tos IsNull
    Then
     token_break_char = ';';
     return;
    EndIf;
   If Tos.source_file IsNull
    Then /* Input is from a string */
     If Tos.token_string_index LessThan Length(Tos_string)
      Then
       token_break_char = String(Tos_string)[Tos.token_string_index++];
       ExitLoop;
      Else
       If token_stack.first Is token_stack.last
        Then
         token_break_char = '\n';
         return;
        Else
         Pop token_stack InTo tos EndPop;
         Push tos OnTo token_stack_free_list EndPush;
         ContinueLoop;
        EndIf;
      EndIf;
    Else /* Input is from a file */
     c = fgetc(Tos.source_file);
     If c Is EOF
      Then
       If Tos.source_file IsNot stdin
        Then
         fclose(Tos.source_file);
        EndIf;
       Pop token_stack InTo tos EndPop;
       token_break_char = Tos.break_char;
       Push tos OnTo token_stack_free_list EndPush;
       ExitLoop;
      Else
       token_break_char = Byte(c);
       ExitLoop;
      EndIf;
    EndIf;
  EndLoop;
 If token_break_char Is '\r'
  Then
   token_break_char = '\n';
  EndIf;
 If token_break_char Is '\t'
  Then
   token_break_char = ' ';
  EndIf;
 return;
EndCode
#undef Tos
#undef Tos_string


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -