/** * Parses the source string for the next valid token * * @return the next valid token in the source string */ Token Scanner::next() { Token t; if (position < source_length) { // bypass whitespace while (is_whitespace(source[position])) { position++; col_number++; if (position == source_length) { t.kind = END_OF_FILE; t.line_position = line_number; t.col_position = col_number; return t; } } // create digit if (is_digit(source[position])) { content_flag = true; return handle_integer(); // create identifier } else if (is_letter(source[position])) { content_flag = true; return handle_identifier(); // create symbols } else if (is_symbol(source[position])) { // single line comments if (position < source_length - 1 && source[position] == '/' && (source[position + 1] == '/')) { // move past comment position += 2; col_number += 2; handle_single_comment(); // handle comments and stop before newline so it's handled // out here return next(); // block comments } else if (position < source_length - 1 && source[position] == '/' && (source[position + 1] == '*')) { // move past comment position += 2; col_number += 2; // handle comments handle_block_comments(); // return next token after comments return next(); } else { // don't set content flag for new_line if (source[position] != '\n') { content_flag = true; } return handle_symbol(); } } else { ErrorHandler::illegal_character(true, source[position], line_number, col_number); } } // return EOF if we've finished the source t.kind = END_OF_FILE; t.line_position = line_number; t.col_position = col_number; return t; }
void handle(node *n) { if (n->type == type_id) { vlog("[code_gen] handle: identifier\n"); handle_identifier(n); } else if (n->type == type_con) { vlog("[code_gen] handle: constant\n"); handle_constant(n); } else if (n->type == type_string) { vlog("[code_gen] handle: string constant\n"); handle_string(n); } else { if (strcmp(n->id,"postfix_expr") == 0) { vlog("[code_gen] handle: postfix_expr \n"); handle_postfix_expr(n); } else if (strcmp(n->id,"argument_expr_list") == 0) { vlog("[code_gen] handle: argument_expr_list \n"); handle_argument_expr_list(n); } else if (strcmp(n->id,"unary_expr") == 0) { vlog("[code_gen] handle: unary_expr \n"); handle_unary_expr(n); } else if (strcmp(n->id,"sizeof") == 0) { vlog("[code_gen] handle: sizeof \n"); handle_sizeof(n); } else if (strcmp(n->id,"cast_expr") == 0) { vlog("[code_gen] handle: cast_expr \n"); handle_cast_expr(n); } else if (strcmp(n->id,"multiply") == 0) { vlog("[code_gen] handle: multiply \n"); handle_multiply(n); } else if (strcmp(n->id,"divide") == 0) { vlog("[code_gen] handle: divide \n"); handle_divide(n); } else if (strcmp(n->id,"mod") == 0) { vlog("[code_gen] handle: mod \n"); handle_mod(n); } else if (strcmp(n->id,"add") == 0) { vlog("[code_gen] handle: add \n"); handle_add(n); } else if (strcmp(n->id,"sub") == 0) { vlog("[code_gen] handle: sub \n"); handle_sub(n); } else if (strcmp(n->id,"shift_left") == 0) { vlog("[code_gen] handle: shift_left \n"); handle_shift_left(n); } else if (strcmp(n->id,"shift_right") == 0) { vlog("[code_gen] handle: shift_right \n"); handle_shift_right(n); } else if (strcmp(n->id,"less_than") == 0) { vlog("[code_gen] handle: less_than \n"); handle_less_than(n); } else if (strcmp(n->id,"greater_than") == 0) { vlog("[code_gen] handle: greater_than \n"); handle_greater_than(n); } else if (strcmp(n->id,"less_equal_than") == 0) { vlog("[code_gen] handle: less_equal_than \n"); handle_less_equal_than(n); } else if (strcmp(n->id,"greater_equal_than") == 0) { vlog("[code_gen] handle: greater_equal_than \n"); handle_greater_equal_than(n); } else if (strcmp(n->id,"equality") == 0) { vlog("[code_gen] handle: equality \n"); handle_equality(n); } else if (strcmp(n->id,"equality_not") == 0) { vlog("[code_gen] handle: equality_not \n"); handle_equality_not(n); } else if (strcmp(n->id,"bitwise_and") == 0) { vlog("[code_gen] handle: bitwise_and \n"); handle_bitwise_and(n); } else if (strcmp(n->id,"bitwise_xor") == 0) { vlog("[code_gen] handle: bitwise_xor \n"); handle_bitwise_xor(n); } else if (strcmp(n->id,"bitwise_or") == 0) { vlog("[code_gen] handle: bitwise_or \n"); handle_bitwise_or(n); } else if (strcmp(n->id,"logical_and") == 0) { vlog("[code_gen] handle: logical_and \n"); handle_logical_and(n); } else if (strcmp(n->id,"logical_or") == 0) { vlog("[code_gen] handle: logical_or \n"); handle_logical_or(n); } else if (strcmp(n->id,"conditional") == 0) { vlog("[code_gen] handle: conditional \n"); handle_conditional(n); } else if (strcmp(n->id,"assignment") == 0) { vlog("[code_gen] handle: assignment \n"); handle_assignment(n); } else if (strcmp(n->id,"expression") == 0) { vlog("[code_gen] handle: expression \n"); handle_expression(n); } else if (strcmp(n->id,"declaration") == 0) { vlog("[code_gen] handle: declaration \n"); handle_declaration(n); } else if (strcmp(n->id,"declaration_specifier") == 0) { vlog("[code_gen] handle: declaration_specifier \n"); handle_declaration_specifier(n); } else if (strcmp(n->id,"init_declarator_list") == 0) { vlog("[code_gen] handle: init_declarator_list \n"); handle_init_declarator_list(n); } else if (strcmp(n->id,"init_declarator") == 0) { vlog("[code_gen] handle: init_declarator \n"); handle_init_declarator(n); } else if (strcmp(n->id,"struct_union") == 0) { vlog("[code_gen] handle: struct_union \n"); handle_struct_union(n); } else if (strcmp(n->id,"struct_declaration_list") == 0) { vlog("[code_gen] handle: struct_declaration_list \n"); handle_struct_declaration_list(n); } else if (strcmp(n->id,"struct_declaration") == 0) { vlog("[code_gen] handle: struct_declaration \n"); handle_struct_declaration(n); } else if (strcmp(n->id,"specifier_qualifier_list") == 0) { vlog("[code_gen] handle: specifier_qualifier_list \n"); handle_specifier_qualifier_list(n); } else if (strcmp(n->id,"struct_declarator_list") == 0) { vlog("[code_gen] handle: struct_declarator_list \n"); handle_struct_declarator_list(n); } else if (strcmp(n->id,"struct_declarator") == 0) { vlog("[code_gen] handle: struct_declarator \n"); handle_struct_declarator(n); } else if (strcmp(n->id,"enum_specifier") == 0) { vlog("[code_gen] handle: enum_specifier \n"); handle_enum_specifier(n); } else if (strcmp(n->id,"enum_list") == 0) { vlog("[code_gen] handle: enum_list \n"); handle_enum_list(n); } else if (strcmp(n->id,"enumerator") == 0) { vlog("[code_gen] handle: enumerator \n"); handle_enumerator(n); } else if (strcmp(n->id,"declarator") == 0) { vlog("[code_gen] handle: declarator \n"); handle_declarator(n); } else if (strcmp(n->id,"direct_declarator") == 0) { vlog("[code_gen] handle: direct_declarator \n"); handle_direct_declarator(n); } else if (strcmp(n->id,"pointer") == 0) { vlog("[code_gen] handle: pointer \n"); handle_pointer(n); } else if (strcmp(n->id,"type_qualifier_list") == 0) { vlog("[code_gen] handle: type_qualifier_list \n"); handle_type_qualifier_list(n); } else if (strcmp(n->id,"parameter_type_list") == 0) { vlog("[code_gen] handle: parameter_type_list \n"); handle_parameter_type_list(n); } else if (strcmp(n->id,"parameter_list") == 0) { vlog("[code_gen] handle: parameter_list \n"); handle_parameter_list(n); } else if (strcmp(n->id,"parameter_declaration") == 0) { vlog("[code_gen] handle: parameter_declaration \n"); handle_parameter_declaration(n); } else if (strcmp(n->id,"identifier_list") == 0) { vlog("[code_gen] handle: identifier_list \n"); handle_identifier_list(n); } else if (strcmp(n->id,"type_name") == 0) { vlog("[code_gen] handle: type_name \n"); handle_type_name(n); } else if (strcmp(n->id,"abstract_declarator") == 0) { vlog("[code_gen] handle: abstract_declarator \n"); handle_abstract_declarator(n); } else if (strcmp(n->id,"direct_abstract_declarator") == 0) { vlog("[code_gen] handle: direct_abstract_declarator \n"); handle_direct_abstract_declarator(n); } else if (strcmp(n->id,"initializer") == 0) { vlog("[code_gen] handle: initializer \n"); handle_initializer(n); } else if (strcmp(n->id,"initializer_list") == 0) { vlog("[code_gen] handle: initializer_list \n"); handle_initializer_list(n); } else if (strcmp(n->id,"labeled_statement") == 0) { vlog("[code_gen] handle: labeled_statement \n"); handle_labeled_statement(n); } else if (strcmp(n->id,"compound_statement") == 0) { vlog("[code_gen] handle: compound_statement \n"); handle_compound_statement(n); } else if (strcmp(n->id,"declaration_list") == 0) { vlog("[code_gen] handle: declaration_list \n"); handle_declaration_list(n); } else if (strcmp(n->id,"statement_list") == 0) { vlog("[code_gen] handle: statement_list \n"); handle_statement_list(n); } else if (strcmp(n->id,"expression_statement") == 0) { vlog("[code_gen] handle: expression_statement \n"); handle_expression_statement(n); } else if (strcmp(n->id,"selection_statement") == 0) { vlog("[code_gen] handle: selection_statement \n"); handle_selection_statement(n); } else if (strcmp(n->id,"iteration_statement") == 0) { vlog("[code_gen] handle: iteration_statement \n"); handle_iteration_statement(n); } else if (strcmp(n->id,"jump_statement") == 0) { vlog("[code_gen] handle: jump_statement \n"); handle_jump_statement(n); } else if (strcmp(n->id,"translation_unit") == 0) { vlog("[code_gen] handle: translation_unit \n"); handle_translation_unit(n); } else if (strcmp(n->id,"function_definition") == 0) { vlog("[code_gen] handle: function_definition \n"); handle_function_definition(n); } else if (strcmp(n->id,"asm") == 0) { vlog("[code_gen] handle: inline_assembly \n"); handle_asm(n); } } }