From: yu.dongliang <18588496441@163.com> Date: Thu, 26 Sep 2024 07:07:21 +0000 (+0800) Subject: support simple macro define, rename some long struct name X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=a3e04f3ba8aed6b9338f41caa981bcbee73713ed;p=scf.git support simple macro define, rename some long struct name --- diff --git a/core/scf_core_types.h b/core/scf_core_types.h index ba80cbd..f742960 100644 --- a/core/scf_core_types.h +++ b/core/scf_core_types.h @@ -322,4 +322,3 @@ static int scf_type_is_setcc(int type) } #endif - diff --git a/core/scf_lex_word.c b/core/scf_lex_word.c index 3802900..e320aef 100644 --- a/core/scf_lex_word.c +++ b/core/scf_lex_word.c @@ -2,34 +2,48 @@ scf_lex_word_t* scf_lex_word_alloc(scf_string_t* file, int line, int pos, int type) { - assert(file); + if (!file) + return NULL; scf_lex_word_t* w = calloc(1, sizeof(scf_lex_word_t)); - assert(w); + if (!w) + return NULL; w->file = scf_string_clone(file); - assert(w->file); + if (!w->file) { + free(w); + return NULL; + } w->type = type; w->line = line; - w->pos = pos; + w->pos = pos; return w; } scf_lex_word_t* scf_lex_word_clone(scf_lex_word_t* w) { + if (!w) + return NULL; + scf_lex_word_t* w1 = calloc(1, sizeof(scf_lex_word_t)); - assert(w1); + if (!w1) + return NULL; - scf_list_init(&w1->list); w1->type = w->type; switch (w->type) { case SCF_LEX_WORD_CONST_CHAR: w1->data.u32 = w->data.u32; break; + case SCF_LEX_WORD_CONST_STRING: + w1->data.s = scf_string_clone(w->data.s); + if (!w1->data.s) { + free(w1); + return NULL; + } break; case SCF_LEX_WORD_CONST_INT: @@ -55,27 +69,76 @@ scf_lex_word_t* scf_lex_word_clone(scf_lex_word_t* w) break; }; - w1->text = scf_string_clone(w->text); - w1->file = scf_string_clone(w->file); - w1->line = w->line; - w1->pos = w->pos; + if (w->text) { + w1->text = scf_string_clone(w->text); + + if (!w1->text) { + scf_lex_word_free(w1); + return NULL; + } + } + if (w->file) { + w1->file = scf_string_clone(w->file); + + if (!w1->file) { + scf_lex_word_free(w1); + return NULL; + } + } + + w1->line = w->line; + w1->pos = w->pos; return w1; } void scf_lex_word_free(scf_lex_word_t* w) { - assert(w); + if (w) { + if (SCF_LEX_WORD_CONST_STRING == w->type) + scf_string_free(w->data.s); + + if (w->text) + scf_string_free(w->text); + + if (w->file) + scf_string_free(w->file); - if (SCF_LEX_WORD_CONST_STRING == w->type) - scf_string_free(w->data.s); + free(w); + } +} + +scf_macro_t* scf_macro_alloc(scf_lex_word_t* w) +{ + if (!w) + return NULL; - if (w->text) - scf_string_free(w->text); - if (w->file) - scf_string_free(w->file); + scf_macro_t* m = calloc(1, sizeof(scf_macro_t)); + if (!m) + return NULL; - free(w); - w = NULL; + m->w = w; + m->refs = 1; + return m; } +void scf_macro_free(scf_macro_t* m) +{ + if (m) { + if (--m->refs > 0) + return; + + assert(0 == m->refs); + + scf_lex_word_free(m->w); + + if (m->argv) { + scf_vector_clear(m->argv, ( void (*)(void*) ) scf_lex_word_free); + scf_vector_free (m->argv); + } + + scf_slist_clear(m->text_list, scf_lex_word_t, next, scf_lex_word_free); + + free(m); + } +} diff --git a/core/scf_lex_word.h b/core/scf_lex_word.h index 24a92ed..85ddb3c 100644 --- a/core/scf_lex_word.h +++ b/core/scf_lex_word.h @@ -4,6 +4,10 @@ #include"scf_string.h" #include"scf_list.h" +typedef struct scf_lex_word_s scf_lex_word_t; +typedef struct scf_complex_s scf_complex_t; +typedef struct scf_macro_s scf_macro_t; + enum scf_lex_words { SCF_LEX_WORD_PLUS = 0, // + @@ -66,11 +70,15 @@ enum scf_lex_words SCF_LEX_WORD_RANGE, // .. range SCF_LEX_WORD_VAR_ARGS, // ... variable args - SCF_LEX_WORD_LB, // { left brace - SCF_LEX_WORD_RB, // } right brace + SCF_LEX_WORD_LB, // { left brace + SCF_LEX_WORD_RB, // } right brace + + SCF_LEX_WORD_LF, // '\n', line feed, LF + SCF_LEX_WORD_HASH, // # hash + SCF_LEX_WORD_HASH2, // ## hash2 - SCF_LEX_WORD_COMMA, // , comma - SCF_LEX_WORD_SEMICOLON, // ; + SCF_LEX_WORD_COMMA, // , comma + SCF_LEX_WORD_SEMICOLON, // ; SCF_LEX_WORD_COLON, // : colon SCF_LEX_WORD_SPACE, // ' ' space @@ -160,19 +168,30 @@ enum scf_lex_words SCF_LEX_WORD_CONST_I64, SCF_LEX_WORD_CONST_U64, - // identity SCF_LEX_WORD_ID, // identity, start of _, a-z, A-Z, may include 0-9 }; -typedef struct { +struct scf_complex_s +{ float real; float imag; -} scf_complex_t; +}; -typedef struct { - scf_list_t list; // manage list, all words in this list, FIFO +struct scf_macro_s +{ + int refs; + + scf_lex_word_t* w; + scf_vector_t* argv; + + scf_lex_word_t* text_list; +}; + +struct scf_lex_word_s +{ + scf_lex_word_t* next; + int type; - int type; union { int32_t i; // value for <= int32_t uint32_t u32; // value for <= uint32_t @@ -189,8 +208,7 @@ typedef struct { scf_string_t* file; // original code file name int line; // line in the code file above int pos; // position in the line above - -} scf_lex_word_t; +}; static inline int scf_lex_is_identity(scf_lex_word_t* w) { @@ -212,7 +230,10 @@ static inline int scf_lex_is_base_type(scf_lex_word_t* w) return SCF_LEX_WORD_KEY_CHAR <= w->type && SCF_LEX_WORD_KEY_VOID >= w->type; } -scf_lex_word_t* scf_lex_word_alloc(scf_string_t* file, int line, int pos, int type); +scf_macro_t* scf_macro_alloc(scf_lex_word_t* w); +void scf_macro_free (scf_macro_t* m); + +scf_lex_word_t* scf_lex_word_alloc(scf_string_t* file, int line, int pos, int type); scf_lex_word_t* scf_lex_word_clone(scf_lex_word_t* w); void scf_lex_word_free (scf_lex_word_t* w); diff --git a/elf/scf_dwarf.c b/elf/scf_dwarf.c index de4179e..9768f61 100644 --- a/elf/scf_dwarf.c +++ b/elf/scf_dwarf.c @@ -1,4 +1,4 @@ -#include"scf_dwarf_def.h" +#include"scf_dwarf.h" #include"scf_leb128.h" static scf_dwarf_abbrev_attribute_t abbrev_cu[] = @@ -224,7 +224,7 @@ int scf_dwarf_abbrev_add_cu(scf_vector_t* abbrevs) return scf_dwarf_abbrev_add_subprogram(abbrevs); } -int scf_dwarf_debug_encode(scf_dwarf_debug_t* debug) +int scf_dwarf_debug_encode(scf_dwarf_t* debug) { if (0 == debug->infos->size) return 0; @@ -335,9 +335,9 @@ int scf_dwarf_debug_encode(scf_dwarf_debug_t* debug) return 0; } -scf_dwarf_debug_t* scf_dwarf_debug_alloc() +scf_dwarf_t* scf_dwarf_debug_alloc() { - scf_dwarf_debug_t* debug = calloc(1, sizeof(scf_dwarf_debug_t)); + scf_dwarf_t* debug = calloc(1, sizeof(scf_dwarf_t)); if (!debug) return NULL; @@ -423,7 +423,7 @@ scf_dwarf_debug_t* scf_dwarf_debug_alloc() return debug; } -void scf_dwarf_debug_free (scf_dwarf_debug_t* debug) +void scf_dwarf_debug_free (scf_dwarf_t* debug) { if (debug) { if (debug->base_types) @@ -465,4 +465,3 @@ void scf_dwarf_debug_free (scf_dwarf_debug_t* debug) free(debug); } } - diff --git a/elf/scf_dwarf.h b/elf/scf_dwarf.h new file mode 100644 index 0000000..08675bc --- /dev/null +++ b/elf/scf_dwarf.h @@ -0,0 +1,444 @@ +#ifndef SCF_DWARF_H +#define SCF_DWARF_H + +#include"scf_list.h" +#include"scf_vector.h" +#include"scf_string.h" + +typedef int8_t scf_dwarf_sbyte_t; +typedef uint8_t scf_dwarf_ubyte_t; +typedef uint16_t scf_dwarf_uhalf_t; +typedef int32_t scf_dwarf_sword_t; +typedef uint32_t scf_dwarf_uword_t; + +typedef struct scf_dwarf_line_machine_s scf_dwarf_line_machine_t; +typedef struct scf_dwarf_line_prologue_s scf_dwarf_line_prologue_t; +typedef struct scf_dwarf_line_filename_s scf_dwarf_line_filename_t; +typedef struct scf_dwarf_line_result_s scf_dwarf_line_result_t; + +typedef struct scf_dwarf_abbrev_declaration_s scf_dwarf_abbrev_declaration_t; +typedef struct scf_dwarf_abbrev_attribute_s scf_dwarf_abbrev_attribute_t; + +typedef struct scf_dwarf_info_header_s scf_dwarf_info_header_t; +typedef struct scf_dwarf_info_entry_s scf_dwarf_info_entry_t; +typedef struct scf_dwarf_info_attr_s scf_dwarf_info_attr_t; +typedef struct scf_dwarf_attr_block_s scf_dwarf_attr_block_t; +typedef struct scf_dwarf_attr_ref_s scf_dwarf_attr_ref_t; + +typedef struct scf_dwarf_s scf_dwarf_t; + +// dwarf line standard opcodes +#define DW_LNS_copy 1 +#define DW_LNS_advance_pc 2 +#define DW_LNS_advance_line 3 +#define DW_LNS_set_file 4 +#define DW_LNS_set_column 5 +#define DW_LNS_negate_stmt 6 +#define DW_LNS_set_basic_block 7 +#define DW_LNS_const_add_pc 8 +#define DW_LNS_fixed_advance_pc 9 + +// dwarf line extended opcodes +#define DW_LNE_end_sequence 1 +#define DW_LNE_set_address 2 +#define DW_LNE_define_file 3 + +// dwarf tags +#define DW_TAG_array_type 0x01 +#define DW_TAG_class_type 0x02 +#define DW_TAG_entry_point 0x03 +#define DW_TAG_enumeration_type 0x04 +#define DW_TAG_formal_parameter 0x05 + +#define DW_TAG_imported_declaration 0x08 + +#define DW_TAG_label 0x0a +#define DW_TAG_lexical_block 0x0b + +#define DW_TAG_member 0x0d + +#define DW_TAG_pointer_type 0x0f + +#define DW_TAG_reference_type 0x10 +#define DW_TAG_compile_unit 0x11 +#define DW_TAG_string_type 0x12 +#define DW_TAG_structure_type 0x13 + +#define DW_TAG_subroutine_type 0x15 +#define DW_TAG_typedef 0x16 +#define DW_TAG_union_type 0x17 +#define DW_TAG_unspecified_parameters 0x18 +#define DW_TAG_variant 0x19 +#define DW_TAG_common_block 0x1a +#define DW_TAG_common_inclusion 0x1b +#define DW_TAG_inheritance 0x1c +#define DW_TAG_inlined_subroutine 0x1d +#define DW_TAG_module 0x1e +#define DW_TAG_ptr_to_member_type 0x1f + +#define DW_TAG_set_type 0x20 +#define DW_TAG_subrange_type 0x21 +#define DW_TAG_with_stmt 0x22 +#define DW_TAG_access_declaration 0x23 +#define DW_TAG_base_type 0x24 +#define DW_TAG_catch_block 0x25 +#define DW_TAG_const_type 0x26 +#define DW_TAG_constant 0x27 +#define DW_TAG_enumerator 0x28 +#define DW_TAG_file_type 0x29 +#define DW_TAG_friend 0x2a +#define DW_TAG_namelist 0x2b +#define DW_TAG_namelist_item 0x2c +#define DW_TAG_packed_type 0x2d +#define DW_TAG_subprogram 0x2e +#define DW_TAG_template_type_param 0x2f + +#define DW_TAG_template_value_param 0x30 +#define DW_TAG_thrown_type 0x31 +#define DW_TAG_try_block 0x32 +#define DW_TAG_variant_part 0x33 +#define DW_TAG_variable 0x34 +#define DW_TAG_volatile_type 0x35 +#define DW_TAG_dwarf_procedure 0x36 +#define DW_TAG_restrict_type 0x37 +#define DW_TAG_interface_type 0x38 +#define DW_TAG_namespace 0x39 +#define DW_TAG_imported_module 0x3a +#define DW_TAG_unspecified_type 0x3b +#define DW_TAG_partial_unit 0x3c +#define DW_TAG_imported_unit 0x3d + +#define DW_TAG_condition 0x3f +#define DW_TAG_shared_type 0x40 +#define DW_TAG_type_unit 0x41 +#define DW_TAG_rvalue_reference_type 0x42 +#define DW_TAG_template_alias 0x43 +#define DW_TAG_lo_user 0x4080 +#define DW_TAG_hi_user 0xffff + +#define DW_CHILDREN_no 0 +#define DW_CHILDREN_yes 1 + +// dwarf attributes +#define DW_AT_sibling 0x01 +#define DW_AT_location 0x02 +#define DW_AT_name 0x03 + +#define DW_AT_ordering 0x09 + +#define DW_AT_byte_size 0x0b +#define DW_AT_bit_offset 0x0c +#define DW_AT_bit_size 0x0d + +#define DW_AT_stmt_list 0x10 +#define DW_AT_low_pc 0x11 +#define DW_AT_high_pc 0x12 +#define DW_AT_language 0x13 + +#define DW_AT_discr 0x15 +#define DW_AT_discr_value 0x16 +#define DW_AT_visibility 0x17 +#define DW_AT_import 0x18 +#define DW_AT_string_length 0x19 +#define DW_AT_common_reference 0x1a +#define DW_AT_comp_dir 0x1b +#define DW_AT_const_value 0x1c +#define DW_AT_containing_type 0x1d +#define DW_AT_default_value 0x1e + +#define DW_AT_inline 0x20 +#define DW_AT_is_optional 0x21 +#define DW_AT_lower_bound 0x22 + +#define DW_AT_producer 0x25 + +#define DW_AT_prototyped 0x27 + +#define DW_AT_return_addr 0x2a +#define DW_AT_start_scope 0x2c +#define DW_AT_stride_size 0x2e +#define DW_AT_upper_bound 0x2f + +#define DW_AT_abstract_origin 0x31 +#define DW_AT_accessibility 0x32 +#define DW_AT_address_class 0x33 +#define DW_AT_artificial 0x34 +#define DW_AT_base_types 0x35 +#define DW_AT_calling_convention 0x36 +#define DW_AT_count 0x37 +#define DW_AT_data_member_location 0x38 +#define DW_AT_decl_column 0x39 +#define DW_AT_decl_file 0x3a +#define DW_AT_decl_line 0x3b +#define DW_AT_declaration 0x3c +#define DW_AT_discr_list 0x3d +#define DW_AT_encoding 0x3e +#define DW_AT_external 0x3f +#define DW_AT_frame_base 0x40 +#define DW_AT_friend 0x41 +#define DW_AT_identifier_case 0x42 +#define DW_AT_macro_info 0x43 +#define DW_AT_namelist_item 0x44 +#define DW_AT_priority 0x45 +#define DW_AT_segment 0x46 +#define DW_AT_specification 0x47 +#define DW_AT_static_link 0x48 +#define DW_AT_type 0x49 +#define DW_AT_use_location 0x4a +#define DW_AT_variable_parameter 0x4b +#define DW_AT_virtuality 0x4c +#define DW_AT_vtable_elem_location 0x4d + +#define DW_AT_lo_user 0x2000 +#define DW_AT_GNU_all_call_sites 0x2117 +#define DW_AT_hi_user 0x3fff + +#define DW_ATE_address 0x1 +#define DW_ATE_boolean 0x2 +#define DW_ATE_complex_float 0x3 +#define DW_ATE_float 0x4 +#define DW_ATE_signed 0x5 +#define DW_ATE_signed_char 0x6 +#define DW_ATE_unsigned 0x7 +#define DW_ATE_unsigned_char 0x8 +#define DW_ATE_lo_user 0x80 +#define DW_ATE_hi_user 0xff + +// dwarf forms +#define DW_FORM_addr 0x01 +#define DW_FORM_block2 0x03 +#define DW_FORM_block4 0x04 +#define DW_FORM_data2 0x05 +#define DW_FORM_data4 0x06 +#define DW_FORM_data8 0x07 +#define DW_FORM_string 0x08 +#define DW_FORM_block 0x09 +#define DW_FORM_block1 0x0a +#define DW_FORM_data1 0x0b +#define DW_FORM_flag 0x0c +#define DW_FORM_sdata 0x0d +#define DW_FORM_strp 0x0e +#define DW_FORM_udata 0x0f +#define DW_FORM_ref_addr 0x10 +#define DW_FORM_ref1 0x11 +#define DW_FORM_ref2 0x12 +#define DW_FORM_ref4 0x13 +#define DW_FORM_ref8 0x14 +#define DW_FORM_ref_udata 0x15 +#define DW_FORM_indirect 0x16 +#define DW_FORM_sec_offset 0x17 +#define DW_FORM_exprloc 0x18 +#define DW_FORM_flag_present 0x19 +#define DW_FORM_ref_sig8 0x20 + +#define DW_OP_fbreg 0x91 +#define DW_OP_call_frame_cfa 0x9c + +struct scf_dwarf_info_header_s +{ + scf_dwarf_uword_t length; + scf_dwarf_uhalf_t version; + scf_dwarf_uword_t offset; + scf_dwarf_ubyte_t address_size; +}; + +struct scf_dwarf_info_entry_s +{ + scf_dwarf_uword_t code; + + uint64_t cu_byte_offset; + + scf_vector_t* attributes; + + int type; + int nb_pointers; +}; + +struct scf_dwarf_attr_ref_s +{ + scf_dwarf_info_attr_t* iattr; + + scf_dwarf_info_entry_t* ie; + scf_dwarf_info_entry_t* ref_entry; + + uint64_t offset; + size_t size; +}; + +struct scf_dwarf_info_attr_s +{ + scf_dwarf_uword_t name; + scf_dwarf_uword_t form; + + scf_dwarf_info_entry_t* ref_entry; + + uintptr_t block_ref; + uint64_t block_ref8; + + union { + scf_dwarf_uword_t block_length; + + uintptr_t address; + uint8_t const1; + uint16_t const2; + uint32_t const4; + uint64_t const8; + scf_dwarf_sword_t sdata; + scf_dwarf_uword_t udata; + + scf_dwarf_ubyte_t flag; + + scf_dwarf_uword_t lineptr; + scf_dwarf_uword_t exprloc; + + uintptr_t ref; + uint64_t ref8; + + scf_dwarf_uword_t str_offset; + }; + + scf_string_t* data; +}; + +struct scf_dwarf_abbrev_attribute_s +{ + scf_dwarf_uword_t name; + scf_dwarf_uword_t form; +}; + +struct scf_dwarf_abbrev_declaration_s +{ + scf_dwarf_uword_t code; + scf_dwarf_uword_t tag; + scf_dwarf_ubyte_t has_children; + + scf_vector_t* attributes; + + scf_vector_t* childs; + + scf_dwarf_abbrev_declaration_t* parent; + + uint32_t visited_flag:1; +}; + +struct scf_dwarf_line_result_s +{ + uintptr_t address; + + scf_string_t* file_name; + scf_dwarf_uword_t line; + scf_dwarf_uword_t column; + + scf_dwarf_ubyte_t is_stmt :1; + scf_dwarf_ubyte_t basic_block :1; + scf_dwarf_ubyte_t end_sequence:1; +}; + +struct scf_dwarf_line_filename_s +{ + scf_dwarf_ubyte_t* name; + scf_dwarf_uword_t dir_index; + scf_dwarf_uword_t time_modified; + scf_dwarf_uword_t length; +}; + +struct scf_dwarf_line_prologue_s +{ + scf_dwarf_uword_t total_length; + scf_dwarf_uhalf_t version; + scf_dwarf_uword_t prologue_length; + + scf_dwarf_ubyte_t minimum_instruction_length; + + scf_dwarf_ubyte_t default_is_stmt; + + scf_dwarf_sbyte_t line_base; + scf_dwarf_ubyte_t line_range; + + scf_dwarf_ubyte_t opcode_base; + scf_dwarf_ubyte_t* standard_opcode_lengths; + scf_dwarf_ubyte_t* include_directories; + scf_vector_t* file_names; +}; + +struct scf_dwarf_line_machine_s +{ + // registers + uintptr_t address; + + scf_dwarf_uword_t file; + scf_dwarf_uword_t line; + scf_dwarf_uword_t column; + + scf_dwarf_ubyte_t is_stmt :1; + scf_dwarf_ubyte_t basic_block :1; + scf_dwarf_ubyte_t end_sequence:1; + + scf_dwarf_line_prologue_t* prologue; +}; + +struct scf_dwarf_s +{ + char* arch; + + scf_vector_t* base_types; + scf_vector_t* struct_types; + + scf_vector_t* lines; + scf_vector_t* infos; + scf_vector_t* abbrevs; + scf_string_t* str; + + scf_string_t* debug_line; + scf_string_t* debug_info; + scf_string_t* debug_abbrev; + + scf_vector_t* line_relas; + scf_vector_t* info_relas; + scf_vector_t* file_names; +}; + +scf_dwarf_t* scf_dwarf_debug_alloc(); +void scf_dwarf_debug_free (scf_dwarf_t* debug); +int scf_dwarf_debug_encode(scf_dwarf_t* debug); + +int scf_dwarf_abbrev_add_cu (scf_vector_t* abbrevs); +int scf_dwarf_abbrev_add_var(scf_vector_t* abbrevs); + +int scf_dwarf_abbrev_add_subprogram (scf_vector_t* abbrevs); +int scf_dwarf_abbrev_add_struct_type (scf_vector_t* abbrevs); +int scf_dwarf_abbrev_add_member_var (scf_vector_t* abbrevs); +int scf_dwarf_abbrev_add_base_type (scf_vector_t* abbrevs); +int scf_dwarf_abbrev_add_pointer_type(scf_vector_t* abbrevs); + +scf_dwarf_info_entry_t* scf_dwarf_info_entry_alloc(); +void scf_dwarf_info_entry_free(scf_dwarf_info_entry_t* ie); +void scf_dwarf_info_attr_free (scf_dwarf_info_attr_t* attr); +int scf_dwarf_info_fill_attr (scf_dwarf_info_attr_t* iattr, uint8_t* data, size_t len); + +scf_dwarf_line_machine_t* scf_dwarf_line_machine_alloc(); +void scf_dwarf_line_machine_print(scf_dwarf_line_machine_t* lm); +int scf_dwarf_line_machine_fill (scf_dwarf_line_machine_t* lm, scf_vector_t* file_names); +void scf_dwarf_line_machine_free (scf_dwarf_line_machine_t* lm); +void scf_dwarf_line_filename_free(scf_dwarf_line_filename_t* f); + +scf_dwarf_abbrev_declaration_t* scf_dwarf_abbrev_declaration_alloc(); +void scf_dwarf_abbrev_declaration_free(scf_dwarf_abbrev_declaration_t* d); + +int scf_dwarf_line_decode( scf_dwarf_line_machine_t* lm, scf_vector_t* line_results, const char* debug_line, size_t debug_line_size); +int scf_dwarf_line_encode(scf_dwarf_t* debug, scf_dwarf_line_machine_t* lm, scf_vector_t* line_results, scf_string_t* debug_line); + +int scf_dwarf_abbrev_decode(scf_vector_t* abbrev_results, const char* debug_abbrev, size_t debug_abbrev_size); +int scf_dwarf_abbrev_encode(scf_vector_t* abbrev_results, scf_string_t* debug_abbrev); + +int scf_dwarf_info_decode(scf_vector_t* infos, scf_vector_t* abbrevs, scf_string_t* debug_str, const char* debug_info, size_t debug_info_size, scf_dwarf_info_header_t* header); +int scf_dwarf_info_encode(scf_dwarf_t* debug, scf_dwarf_info_header_t* header); + +void scf_dwarf_abbrev_print(scf_vector_t* abbrev_results); +void scf_dwarf_info_print (scf_vector_t* infos); + +const char* scf_dwarf_find_tag (const uint32_t type); +const char* scf_dwarf_find_form(const uint32_t type); +const char* scf_dwarf_find_attribute(const uint32_t type); + +#endif diff --git a/elf/scf_dwarf_abbrev.c b/elf/scf_dwarf_abbrev.c index 5a44e5c..93b26b0 100644 --- a/elf/scf_dwarf_abbrev.c +++ b/elf/scf_dwarf_abbrev.c @@ -1,4 +1,4 @@ -#include"scf_dwarf_def.h" +#include"scf_dwarf.h" #include"scf_leb128.h" typedef struct { diff --git a/elf/scf_dwarf_info.c b/elf/scf_dwarf_info.c index b270d16..76bf924 100644 --- a/elf/scf_dwarf_info.c +++ b/elf/scf_dwarf_info.c @@ -1,4 +1,4 @@ -#include"scf_dwarf_def.h" +#include"scf_dwarf.h" #include"scf_leb128.h" #include"scf_native.h" #include"scf_elf.h" @@ -291,7 +291,7 @@ int scf_dwarf_info_decode(scf_vector_t* infos, scf_vector_t* abbrevs, scf_string return 0; } -static int _add_rela_common(scf_dwarf_debug_t* debug, const char* sym, uint64_t offset, int type, int addend) +static int _add_rela_common(scf_dwarf_t* debug, const char* sym, uint64_t offset, int type, int addend) { scf_rela_t* rela = calloc(1, sizeof(scf_rela_t)); if (!rela) @@ -350,7 +350,7 @@ static int _add_rela_common(scf_dwarf_debug_t* debug, const char* sym, uint64_t return 0; } -static int _add_rela_subprogram(scf_dwarf_debug_t* debug, scf_dwarf_info_entry_t* ie, uint64_t offset, int type) +static int _add_rela_subprogram(scf_dwarf_t* debug, scf_dwarf_info_entry_t* ie, uint64_t offset, int type) { scf_dwarf_info_attr_t* iattr = NULL; @@ -368,7 +368,7 @@ static int _add_rela_subprogram(scf_dwarf_debug_t* debug, scf_dwarf_info_entry_t return _add_rela_common(debug, iattr->data->data, offset, type, 0); } -int scf_dwarf_info_encode(scf_dwarf_debug_t* debug, scf_dwarf_info_header_t* header) +int scf_dwarf_info_encode(scf_dwarf_t* debug, scf_dwarf_info_header_t* header) { if (!debug || !header) { scf_loge("\n"); @@ -845,4 +845,3 @@ void scf_dwarf_info_print(scf_vector_t* infos) } } } - diff --git a/elf/scf_dwarf_line.c b/elf/scf_dwarf_line.c index a59ec10..15a5aa8 100644 --- a/elf/scf_dwarf_line.c +++ b/elf/scf_dwarf_line.c @@ -1,4 +1,4 @@ -#include"scf_dwarf_def.h" +#include"scf_dwarf.h" #include"scf_leb128.h" #include"scf_native.h" #include"scf_elf.h" @@ -542,7 +542,7 @@ int scf_dwarf_line_decode(scf_dwarf_line_machine_t* lm, scf_vector_t* line_resul return 0; } -int scf_dwarf_line_encode(scf_dwarf_debug_t* debug, scf_dwarf_line_machine_t* lm, scf_vector_t* line_results, scf_string_t* debug_line) +int scf_dwarf_line_encode(scf_dwarf_t* debug, scf_dwarf_line_machine_t* lm, scf_vector_t* line_results, scf_string_t* debug_line) { if (!debug || !lm || !line_results || !debug_line) return -EINVAL; diff --git a/examples/auto_gc_0.c b/examples/auto_gc_0.c index 96a6ffc..76c0e9b 100644 --- a/examples/auto_gc_0.c +++ b/examples/auto_gc_0.c @@ -1,4 +1,4 @@ -include "../lib/scf_capi.c"; +#include "../lib/scf_capi.c" int* f() { diff --git a/examples/complex.c b/examples/complex.c index 611e02b..0eb8776 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -1,5 +1,5 @@ -include "../lib/scf_capi.c"; -include "../lib/scf_complex.c"; +#include "../lib/scf_capi.c" +#include "../lib/scf_complex.c" int main() { diff --git a/examples/list_test.c b/examples/list_test.c index f5d179c..7d5985b 100644 --- a/examples/list_test.c +++ b/examples/list_test.c @@ -1,5 +1,5 @@ -include "../lib/scf_list.c"; -include "../lib/scf_capi.c"; +#include "../lib/scf_list.c" +#include "../lib/scf_capi.c" struct Data { diff --git a/examples/macro.c b/examples/macro.c new file mode 100644 index 0000000..a931386 --- /dev/null +++ b/examples/macro.c @@ -0,0 +1,10 @@ + +int printf(const char* fmt, ...); + +#define ABC 1111 + +int main() +{ + printf("ABC: %d\n", ABC); + return 0; +} diff --git a/examples/mat.c b/examples/mat.c index 4c5241c..fb99a1c 100644 --- a/examples/mat.c +++ b/examples/mat.c @@ -1,5 +1,5 @@ -include "../lib/scf_capi.c"; +#include"../lib/scf_capi.c" const int MAT_TYPE_NONE = 0; const int MAT_TYPE_U8 = 1; diff --git a/examples/recursive_pointer.c b/examples/recursive_pointer.c index 2434b41..83ae6bc 100644 --- a/examples/recursive_pointer.c +++ b/examples/recursive_pointer.c @@ -1,4 +1,4 @@ -include "../lib/scf_capi.c"; +#include "../lib/scf_capi.c" int f(int** t0, int **t1); diff --git a/examples/ret2_err.c b/examples/ret2_err.c index ee00f0d..cdc12ce 100644 --- a/examples/ret2_err.c +++ b/examples/ret2_err.c @@ -1,5 +1,5 @@ -include "../lib/scf_capi.c"; +#include "../lib/scf_capi.c" struct str { diff --git a/examples/str.c b/examples/str.c index f204d72..335b19b 100644 --- a/examples/str.c +++ b/examples/str.c @@ -1,5 +1,5 @@ -include "../lib/scf_capi.c"; +#include "../lib/scf_capi.c" struct str { @@ -39,4 +39,3 @@ int main() printf("%s\n", p0->data); return 0; } - diff --git a/examples/string.c b/examples/string.c index 81b522b..081733e 100644 --- a/examples/string.c +++ b/examples/string.c @@ -1,4 +1,4 @@ -include "../lib/scf_capi.c"; +#include "../lib/scf_capi.c" struct string { diff --git a/lex/scf_lex.c b/lex/scf_lex.c index adda700..42fff04 100644 --- a/lex/scf_lex.c +++ b/lex/scf_lex.c @@ -66,6 +66,7 @@ static scf_key_word_t key_words[] = {"async", SCF_LEX_WORD_KEY_ASYNC}, {"include", SCF_LEX_WORD_KEY_INCLUDE}, + {"define", SCF_LEX_WORD_KEY_DEFINE}, {"enum", SCF_LEX_WORD_KEY_ENUM}, {"union", SCF_LEX_WORD_KEY_UNION}, @@ -114,10 +115,6 @@ int scf_lex_open(scf_lex_t** plex, const char* path) if (!lex) return -ENOMEM; - scf_list_init(&lex->word_list_head); - - lex->char_list_head = NULL; - lex->fp = fopen(path, "r"); if (!lex->fp) { char cwd[4096]; @@ -144,18 +141,28 @@ int scf_lex_open(scf_lex_t** plex, const char* path) int scf_lex_close(scf_lex_t* lex) { if (lex) { - scf_list_clear(&lex->word_list_head, scf_lex_word_t, list, scf_lex_word_free); + scf_slist_clear(lex->char_list, scf_char_t, next, free); + scf_slist_clear(lex->word_list, scf_lex_word_t, next, scf_lex_word_free); + + if (lex->macros) { + scf_vector_clear(lex->macros, ( void (*)(void*) )scf_macro_free); + scf_vector_free (lex->macros); + } + scf_string_free(lex->file); + + fclose(lex->fp); free(lex); } return 0; } -int scf_lex_push_word(scf_lex_t* lex, scf_lex_word_t* word) +void scf_lex_push_word(scf_lex_t* lex, scf_lex_word_t* w) { - if (lex && word) - scf_list_add_front(&lex->word_list_head, &word->list); - return 0; + if (lex && w) { + w->next = lex->word_list; + lex->word_list = w; + } } static int _lex_plus(scf_lex_t* lex, scf_lex_word_t** pword, scf_char_t* c0) @@ -613,28 +620,87 @@ static int _lex_string(scf_lex_t* lex, scf_lex_word_t** pword, scf_char_t* c0) } } -int scf_lex_pop_word(scf_lex_t* lex, scf_lex_word_t** pword) +static int _lex_macro(scf_lex_t* lex) { - if (!lex || !lex->fp || !pword) - return -EINVAL; + scf_char_t* h = NULL; + scf_char_t** pp = &h; + scf_char_t* c; + scf_char_t* c2; + + while (1) { + c = _lex_pop_char(lex); + if (!c) { + scf_slist_clear(h, scf_char_t, next, free); + return -1; + } + + *pp = c; + pp = &c->next; + + if (EOF == c->c) + break; + + if ('\n' == c->c) { + c->flag = SCF_UTF8_LF; + break; + } + + if ('\\' == c->c) { + + c2 = _lex_pop_char(lex); + if (!c2) { + scf_slist_clear(h, scf_char_t, next, free); + return -1; + } + *pp = c2; + pp = &c2->next; + + if (EOF == c2->c) + break; + + if ('\n' == c2->c) + c->c = ' '; + } + } + + *pp = lex->char_list; + lex->char_list = h; + return 0; +} + +static int __lex_pop_word(scf_lex_t* lex, scf_lex_word_t** pword) +{ scf_list_t* l = NULL; scf_char_t* c = NULL; scf_lex_word_t* w = NULL; - if (!scf_list_empty(&lex->word_list_head)) { - l = scf_list_head(&lex->word_list_head); - w = scf_list_data(l, scf_lex_word_t, list); + if (lex->word_list) { + w = lex->word_list; + lex->word_list = w->next; - scf_list_del(&w->list); *pword = w; return 0; } c = _lex_pop_char(lex); - while ('\n' == c->c || '\r' == c->c || '\t' == c->c || ' ' == c->c) { + while ('\n' == c->c || '\r' == c->c || '\t' == c->c || ' ' == c->c) { + if ('\n' == c->c) { + if (SCF_UTF8_LF == c->flag) { + w = scf_lex_word_alloc(lex->file, lex->nb_lines, lex->pos, SCF_LEX_WORD_LF); + w->text = scf_string_cstr("LF"); + *pword = w; + + lex->nb_lines++; + lex->pos = 0; + + free(c); + c = NULL; + return 0; + } + lex->nb_lines++; lex->pos = 0; } else @@ -696,7 +762,7 @@ int scf_lex_pop_word(scf_lex_t* lex, scf_lex_word_t** pword) } } - return scf_lex_pop_word(lex, pword); + return __lex_pop_word(lex, pword); } else if ('*' == c2->c) { free(c); @@ -736,7 +802,7 @@ int scf_lex_pop_word(scf_lex_t* lex, scf_lex_word_t** pword) } } - return scf_lex_pop_word(lex, pword); + return __lex_pop_word(lex, pword); } else { _lex_push_char(lex, c2); @@ -838,6 +904,7 @@ int scf_lex_pop_word(scf_lex_t* lex, scf_lex_word_t** pword) if ('\"' == c->c) { scf_lex_word_t* w0 = NULL; + scf_lex_word_t* w1 = NULL; int ret = _lex_string(lex, &w0, c); if (ret < 0) { @@ -846,9 +913,7 @@ int scf_lex_pop_word(scf_lex_t* lex, scf_lex_word_t** pword) } while (1) { - scf_lex_word_t* w1 = NULL; - - ret = scf_lex_pop_word(lex, &w1); + ret = __lex_pop_word(lex, &w1); if (ret < 0) { scf_lex_word_free(w0); *pword = NULL; @@ -888,6 +953,312 @@ int scf_lex_pop_word(scf_lex_t* lex, scf_lex_word_t** pword) return _lex_identity(lex, pword, c); } + if ('#' == c->c) { + w = scf_lex_word_alloc(lex->file, lex->nb_lines, lex->pos, SCF_LEX_WORD_HASH); + + free(c); + c = _lex_pop_char(lex); + + if ('#' == c->c) { + w->type = SCF_LEX_WORD_HASH2; + w->text = scf_string_cstr("##"); + + lex->pos += 2; + free(c); + } else { + w->text = scf_string_cstr("#"); + + lex->pos++; + _lex_push_char(lex, c); + } + + c = NULL; + *pword = w; + return _lex_macro(lex); + } + scf_loge("unknown char: %c, utf: %#x, in file: %s, line: %d\n", c->c, c->c, lex->file->data, lex->nb_lines); return -1; } + +static int __parse_macro_argv(scf_lex_t* lex, scf_macro_t* m) +{ + scf_lex_word_t* w = NULL; + scf_lex_word_t* w2; + + int comma = 0; + int id = 0; + + while (1) { + int ret = __lex_pop_word(lex, &w); + if (ret < 0) + return ret; + + if (!comma) { + if (SCF_LEX_WORD_RP == w->type) { + scf_lex_word_free(w); + break; + } + + if (SCF_LEX_WORD_COMMA == w->type) { + scf_lex_word_free(w); + w = NULL; + + if (!id) { + scf_loge("an identity is needed before ',' in file: %s, line: %d\n", w->file->data, w->line); + return -1; + } + + id = 0; + comma = 1; + continue; + } + } + + if (!scf_lex_is_identity(w)) { + scf_loge("macro arg '%s' should be an identity, file: %s, line: %d\n", w->text->data, w->file->data, w->line); + scf_lex_word_free(w); + return -1; + } + + if (id) { + scf_loge("',' is needed before macro arg '%s', file: %s, line: %d\n", w->text->data, w->file->data, w->line); + scf_lex_word_free(w); + return -1; + } + + int i; + for (i = 0; i < m->argv->size; i++) { + w2 = m->argv->data[i]; + + if (!scf_string_cmp(w2->text, w->text)) { + scf_loge("macro has same args '%s', file: %s, line: %d\n", w->text->data, w->file->data, w->line); + scf_lex_word_free(w); + return -1; + } + } + + ret = scf_vector_add(m->argv, w); + if (ret < 0) { + scf_lex_word_free(w); + return ret; + } + w = NULL; + + id = 1; + comma = 0; + } + + return 0; +} + +static int __parse_macro_define(scf_lex_t* lex, scf_lex_word_t* w0, scf_lex_word_t* w1) +{ + scf_lex_word_t** pp; + scf_lex_word_t* w = NULL; + scf_macro_t* m; + scf_macro_t* m0; + + scf_lex_word_free(w0); + scf_lex_word_free(w1); + w0 = NULL; + w1 = NULL; + + int ret = __lex_pop_word(lex, &w); + if (ret < 0) + return ret; + + if (!scf_lex_is_identity(w)) { + scf_loge("macro '%s' should be an identity, file: %s, line: %d\n", w->text->data, w->file->data, w->line); + scf_lex_word_free(w); + return -1; + } + + m = scf_macro_alloc(w); + if (!m) { + scf_lex_word_free(w); + return -ENOMEM; + } + + w = NULL; + ret = __lex_pop_word(lex, &w); + if (ret < 0) { + scf_macro_free(m); + return ret; + } + + pp = &m->text_list; + + if (SCF_LEX_WORD_LP == w->type) { + scf_lex_word_free(w); + w = NULL; + + m->argv = scf_vector_alloc(); + if (!m->argv) { + scf_macro_free(m); + return -ENOMEM; + } + + ret = __parse_macro_argv(lex, m); + if (ret < 0) { + scf_macro_free(m); + return ret; + } + } else { + *pp = w; + pp = &w->next; + w = NULL; + } + + while (1) { + ret = __lex_pop_word(lex, &w); + if (ret < 0) { + scf_macro_free(m); + return ret; + } + + if (SCF_LEX_WORD_LF == w->type) { + scf_lex_word_free(w); + w = NULL; + break; + } + + *pp = w; + pp = &w->next; + w = NULL; + } + + if (!lex->macros) { + lex->macros = scf_vector_alloc(); + if (!lex->macros) + return -ENOMEM; + + } else { + int i; + for (i = lex->macros->size - 1; i >= 0; i--) { + m0 = lex->macros->data[i]; + + if (!scf_string_cmp(m->w->text, m0->w->text)) { + scf_logw("macro '%s' defined before in file: %s, line: %d\n", + m0->w->text->data, m0->w->file->data, m0->w->line); + break; + } + } + } + + ret = scf_vector_add(lex->macros, m); + if (ret < 0) { + scf_macro_free(m); + return ret; + } + + return 0; +} + +static int __parse_macro(scf_lex_t* lex, scf_lex_word_t** pword, scf_lex_word_t* w0) +{ + scf_lex_word_t* w1 = NULL; + + int ret = __lex_pop_word(lex, &w1); + if (ret < 0) { + scf_lex_word_free(w0); + return ret; + } + + switch (w1->type) { + + case SCF_LEX_WORD_KEY_INCLUDE: + + scf_lex_push_word(lex, w1); + *pword = w0; + return 0; + + break; + case SCF_LEX_WORD_KEY_DEFINE: + + ret = __parse_macro_define(lex, w0, w1); + if (ret < 0) + return ret; + break; + + default: + scf_loge("unknown macro '%s', file: %s, line: %d\n", w1->text->data, w1->file->data, w1->line); + + scf_lex_word_free(w0); + scf_lex_word_free(w1); + return -1; + break; + }; + + return scf_lex_pop_word(lex, pword); +} + +static int __use_macro(scf_lex_t* lex, scf_lex_word_t** pword, scf_lex_word_t* w) +{ + scf_lex_word_t** pp; + scf_lex_word_t* p; + scf_lex_word_t* h; + scf_macro_t* m; + + if (!lex->macros) { + *pword = w; + return 0; + } + + int i; + for (i = lex->macros->size - 1; i >= 0; i--) { + m = lex->macros->data[i]; + + if (!scf_string_cmp(m->w->text, w->text)) + break; + } + + if (i < 0) { + *pword = w; + return 0; + } + + scf_lex_word_free(w); + w = NULL; + + h = NULL; + pp = &h; + for (p = m->text_list; p; p = p->next) { + + w = scf_lex_word_clone(p); + if (!w) { + scf_slist_clear(h, scf_lex_word_t, next, scf_lex_word_free); + return -ENOMEM; + } + + *pp = w; + pp = &w->next; + } + + *pp = lex->word_list; + lex->word_list = h; + + return scf_lex_pop_word(lex, pword); +} + +int scf_lex_pop_word(scf_lex_t* lex, scf_lex_word_t** pword) +{ + if (!lex || !lex->fp || !pword) + return -EINVAL; + + scf_lex_word_t* w = NULL; + + int ret = __lex_pop_word(lex, &w); + if (ret < 0) + return ret; + + if (SCF_LEX_WORD_HASH == w->type) + return __parse_macro(lex, pword, w); + + //use macro to identity + if (scf_lex_is_identity(w)) + return __use_macro(lex, pword, w); + + *pword = w; + return 0; +} diff --git a/lex/scf_lex.h b/lex/scf_lex.h index 9d42928..bdcf539 100644 --- a/lex/scf_lex.h +++ b/lex/scf_lex.h @@ -4,6 +4,7 @@ #include"scf_lex_word.h" typedef struct scf_char_s scf_char_t; +typedef struct scf_lex_s scf_lex_t; typedef struct { char* text; @@ -16,6 +17,7 @@ typedef struct { } scf_escape_char_t; #define SCF_UTF8_MAX 6 +#define SCF_UTF8_LF 1 struct scf_char_s { scf_char_t* next; @@ -23,29 +25,35 @@ struct scf_char_s int len; uint8_t utf8[SCF_UTF8_MAX]; + uint8_t flag; }; -typedef struct { - scf_list_t word_list_head; // word list head - scf_char_t* char_list_head; // temp char list head +struct scf_lex_s +{ + scf_lex_t* next; + + scf_lex_word_t* word_list; + scf_char_t* char_list; + + scf_vector_t* macros; FILE* fp; // file pointer to the code scf_string_t* file; // original code file name int nb_lines; int pos; -} scf_lex_t; +}; scf_char_t* _lex_pop_char (scf_lex_t* lex); void _lex_push_char(scf_lex_t* lex, scf_char_t* c); -int scf_lex_open (scf_lex_t** plex, const char* path); -int scf_lex_close(scf_lex_t* lex); +int scf_lex_open (scf_lex_t** plex, const char* path); +int scf_lex_close(scf_lex_t* lex); -int scf_lex_push_word(scf_lex_t* lex, scf_lex_word_t* word); -int scf_lex_pop_word (scf_lex_t* lex, scf_lex_word_t** pword); +void scf_lex_push_word(scf_lex_t* lex, scf_lex_word_t* word); +int scf_lex_pop_word (scf_lex_t* lex, scf_lex_word_t** pword); int _lex_number_base_16(scf_lex_t* lex, scf_lex_word_t** pword, scf_string_t* s); diff --git a/lex/scf_lex_util.c b/lex/scf_lex_util.c index 14ff78d..5a6e589 100644 --- a/lex/scf_lex_util.c +++ b/lex/scf_lex_util.c @@ -7,15 +7,16 @@ scf_char_t* _lex_pop_char(scf_lex_t* lex) scf_char_t* c; - if (lex->char_list_head) { - c = lex->char_list_head; - lex->char_list_head = c->next; + if (lex->char_list) { + c = lex->char_list; + lex->char_list = c->next; return c; } c = malloc(sizeof(scf_char_t)); if (!c) return NULL; + c->next = NULL; int ret = fgetc(lex->fp); if (EOF == ret) { @@ -82,8 +83,8 @@ void _lex_push_char(scf_lex_t* lex, scf_char_t* c) assert(lex); assert(c); - c->next = lex->char_list_head; - lex->char_list_head = c; + c->next = lex->char_list; + lex->char_list = c; } int _lex_op1_ll1(scf_lex_t* lex, scf_lex_word_t** pword, scf_char_t* c0, int type0) diff --git a/lib/scf_complex.c b/lib/scf_complex.c index 430b600..86ac0e6 100644 --- a/lib/scf_complex.c +++ b/lib/scf_complex.c @@ -1,5 +1,4 @@ - -include "../lib/scf_capi.c"; +#include "../lib/scf_capi.c" struct complex { diff --git a/parse/Makefile b/parse/Makefile index b3c380f..d793978 100644 --- a/parse/Makefile +++ b/parse/Makefile @@ -118,13 +118,14 @@ CFILES += ../core/scf_optimizer_group.c CFILES += ../core/scf_optimizer_generate_loads_saves.c CFILES += ../core/scf_ast.c - CFILES += ../core/scf_calculate.c CFILES += scf_dfa.c CFILES += scf_dfa_parse.c +CFILES += scf_dfa_macro.c CFILES += scf_dfa_include.c + CFILES += scf_dfa_call.c CFILES += scf_dfa_create.c CFILES += scf_dfa_sizeof.c diff --git a/parse/main.c b/parse/main.c index cd3a7d1..c96968e 100644 --- a/parse/main.c +++ b/parse/main.c @@ -211,7 +211,7 @@ int main(int argc, char* argv[]) } } - char* obj = "1.elf"; + char* obj = "1.o"; char* exec = "1.out"; if (out) { diff --git a/parse/scf_dfa.c b/parse/scf_dfa.c index 96b773c..2c14d16 100644 --- a/parse/scf_dfa.c +++ b/parse/scf_dfa.c @@ -299,7 +299,7 @@ static int _scf_dfa_node_parse_word(scf_dfa_t* dfa, scf_dfa_node_t* node, scf_ve int ret = SCF_DFA_NEXT_WORD; scf_lex_word_t* w = words->data[words->size - 1]; - scf_logi("\033[33m%s->action(), w: %s, position: %d,%d\033[0m\n", node->name, w->text->data, w->line, w->pos); + scf_logi("\033[32m%s->action(), w: %s, position: %d,%d\033[0m\n", node->name, w->text->data, w->line, w->pos); if (node->action) { @@ -320,13 +320,13 @@ static int _scf_dfa_node_parse_word(scf_dfa_t* dfa, scf_dfa_node_t* node, scf_ve #if 1 scf_dfa_hook_t* h = dfa->hooks[SCF_DFA_HOOK_POST]; while (h) { - scf_logd("\033[32m post hook: %s\033[0m\n", h->node->name); + scf_logd("\033[34m post hook: %s\033[0m\n", h->node->name); h = h->next; } h = dfa->hooks[SCF_DFA_HOOK_END]; while (h) { - scf_logd("\033[32m end hook: %s\033[0m\n", h->node->name); + scf_logd("\033[36m end hook: %s\033[0m\n", h->node->name); h = h->next; } printf("\n"); @@ -339,14 +339,14 @@ static int _scf_dfa_node_parse_word(scf_dfa_t* dfa, scf_dfa_node_t* node, scf_ve scf_dfa_clear_hooks(&(dfa->hooks[SCF_DFA_HOOK_POST]), hook->next); hook = NULL; - scf_logi("\033[32m post hook: %s->action()\033[0m\n", hook_node->name); + scf_logi("\033[36m post hook: %s->action()\033[0m\n", hook_node->name); if (hook_node != node && hook_node->action) { ret = hook_node->action(dfa, words, data); if (SCF_DFA_SWITCH_TO == ret) { - scf_logi("\033[31m post hook: switch to %s->%s\033[0m\n", node->name, hook_node->name); + scf_logi("\033[36m post hook: switch to %s->%s\033[0m\n", node->name, hook_node->name); node = hook_node; ret = SCF_DFA_NEXT_WORD; @@ -366,7 +366,7 @@ static int _scf_dfa_node_parse_word(scf_dfa_t* dfa, scf_dfa_node_t* node, scf_ve free(hook); hook = NULL; - scf_logi("\033[32m end hook: %s->action()\033[0m\n", hook_node->name); + scf_logi("\033[34m end hook: %s->action()\033[0m\n", hook_node->name); if (!hook_node->action) continue; @@ -377,7 +377,7 @@ static int _scf_dfa_node_parse_word(scf_dfa_t* dfa, scf_dfa_node_t* node, scf_ve continue; if (SCF_DFA_SWITCH_TO == ret) { - scf_logi("\033[31m end hook: switch to %s->%s\033[0m\n\n", node->name, hook_node->name); + scf_logi("\033[34m end hook: switch to %s->%s\033[0m\n\n", node->name, hook_node->name); node = hook_node; ret = SCF_DFA_NEXT_WORD; diff --git a/parse/scf_dfa_async.c b/parse/scf_dfa_async.c index 703a026..d6a826d 100644 --- a/parse/scf_dfa_async.c +++ b/parse/scf_dfa_async.c @@ -14,7 +14,7 @@ static int _async_is_async(scf_dfa_t* dfa, void* word) static int _async_action_async(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; if (d->expr) { @@ -33,8 +33,8 @@ static int _async_action_async(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _async_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; if (!d->expr) { scf_loge("\n"); @@ -122,7 +122,6 @@ static int _dfa_init_syntax_async(scf_dfa_t* dfa) scf_dfa_node_add_child(async, expr); scf_dfa_node_add_child(expr, semicolon); - scf_logi("\n"); return 0; } @@ -132,4 +131,3 @@ scf_dfa_module_t dfa_module_async = .init_module = _dfa_init_module_async, .init_syntax = _dfa_init_syntax_async, }; - diff --git a/parse/scf_dfa_block.c b/parse/scf_dfa_block.c index 2ed9bc1..50b6432 100644 --- a/parse/scf_dfa_block.c +++ b/parse/scf_dfa_block.c @@ -19,7 +19,7 @@ typedef struct { static int _block_action_entry(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_stack_t* s = d->module_datas[dfa_module_block.index]; dfa_block_data_t* bd = scf_stack_top(s); @@ -34,10 +34,9 @@ static int _block_action_entry(scf_dfa_t* dfa, scf_vector_t* words, void* data) scf_stack_push(s, bd); - scf_logi("\033[31m new bd: %p, s->size: %d\033[0m\n", bd, s->size); - } else { - scf_logi("\033[31m new bd: %p, s->size: %d\033[0m\n", bd, s->size); - } + scf_logi("new bd: %p, s->size: %d\n", bd, s->size); + } else + scf_logi("new bd: %p, s->size: %d\n", bd, s->size); return words->size > 0 ? SCF_DFA_CONTINUE : SCF_DFA_NEXT_WORD; } @@ -45,12 +44,12 @@ static int _block_action_entry(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _block_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_stack_t* s = d->module_datas[dfa_module_block.index]; dfa_block_data_t* bd = scf_stack_top(s); if (bd->nb_rbs < bd->nb_lbs) { - scf_logi("\033[31m end bd: %p, bd->nb_lbs: %d, bd->nb_rbs: %d, s->size: %d\033[0m\n", + scf_logi("end bd: %p, bd->nb_lbs: %d, bd->nb_rbs: %d, s->size: %d\n", bd, bd->nb_lbs, bd->nb_rbs, s->size); SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "block_end"), SCF_DFA_HOOK_END); @@ -64,7 +63,7 @@ static int _block_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) scf_stack_pop(s); - scf_logi("\033[31m end bd: %p, bd->nb_lbs: %d, bd->nb_rbs: %d, s->size: %d\033[0m\n", + scf_logi("end bd: %p, bd->nb_lbs: %d, bd->nb_rbs: %d, s->size: %d\n", bd, bd->nb_lbs, bd->nb_rbs, s->size); free(bd); @@ -76,7 +75,7 @@ static int _block_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _block_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_block.index]; @@ -102,7 +101,7 @@ static int _block_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) bd->nb_lbs++; scf_stack_push(s, bd); - scf_logi("\033[31m new bd: %p, s->size: %d\033[0m\n", bd, s->size); + scf_logi("new bd: %p, s->size: %d\n", bd, s->size); return SCF_DFA_NEXT_WORD; } @@ -110,14 +109,14 @@ static int _block_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _block_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_block.index]; dfa_block_data_t* bd = scf_stack_top(s); bd->nb_rbs++; - scf_logi("\033[31m bd: %p, bd->nb_lbs: %d, bd->nb_rbs: %d, s->size: %d\033[0m\n", + scf_logi("bd: %p, bd->nb_lbs: %d, bd->nb_rbs: %d, s->size: %d\n", bd, bd->nb_lbs, bd->nb_rbs, s->size); assert(bd->nb_lbs == bd->nb_rbs); @@ -143,6 +142,8 @@ static int _dfa_init_module_block(scf_dfa_t* dfa) SCF_DFA_GET_MODULE_NODE(dfa, expr, entry, expr); SCF_DFA_GET_MODULE_NODE(dfa, type, entry, type); + SCF_DFA_GET_MODULE_NODE(dfa, macro, hash, macro); + SCF_DFA_GET_MODULE_NODE(dfa, if, _if, _if); SCF_DFA_GET_MODULE_NODE(dfa, while, _while, _while); SCF_DFA_GET_MODULE_NODE(dfa, do, _do, _do); @@ -171,6 +172,8 @@ static int _dfa_init_module_block(scf_dfa_t* dfa) scf_dfa_node_add_child(entry, expr); scf_dfa_node_add_child(entry, type); + scf_dfa_node_add_child(entry, macro); + scf_dfa_node_add_child(entry, _if); scf_dfa_node_add_child(entry, _while); scf_dfa_node_add_child(entry, _do); @@ -188,7 +191,7 @@ static int _dfa_init_module_block(scf_dfa_t* dfa) scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; scf_stack_t* s = d->module_datas[dfa_module_block.index]; assert(!s); @@ -206,7 +209,7 @@ static int _dfa_init_module_block(scf_dfa_t* dfa) static int _dfa_fini_module_block(scf_dfa_t* dfa) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; scf_stack_t* s = d->module_datas[dfa_module_block.index]; if (s) { diff --git a/parse/scf_dfa_break.c b/parse/scf_dfa_break.c index 8ad7970..1bab393 100644 --- a/parse/scf_dfa_break.c +++ b/parse/scf_dfa_break.c @@ -13,9 +13,9 @@ static int _break_is_break(scf_dfa_t* dfa, void* word) static int _break_action_break(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; scf_node_t* _break = scf_node_alloc(w, SCF_OP_BREAK, NULL); if (!_break) { @@ -50,8 +50,6 @@ static int _dfa_init_syntax_break(scf_dfa_t* dfa) SCF_DFA_GET_MODULE_NODE(dfa, break, _break, _break); scf_dfa_node_add_child(_break, semicolon); - - printf("%s(),%d\n\n", __func__, __LINE__); return 0; } @@ -61,4 +59,3 @@ scf_dfa_module_t dfa_module_break = .init_module = _dfa_init_module_break, .init_syntax = _dfa_init_syntax_break, }; - diff --git a/parse/scf_dfa_call.c b/parse/scf_dfa_call.c index d356579..ad27a5d 100644 --- a/parse/scf_dfa_call.c +++ b/parse/scf_dfa_call.c @@ -20,9 +20,9 @@ typedef struct { static int _call_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_call.index]; - dfa_call_data_t* cd = scf_stack_top(s); + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_call.index]; + dfa_call_data_t* cd = scf_stack_top(s); if (!cd) { scf_loge("\n"); @@ -39,19 +39,19 @@ static int _call_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _call_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w1 = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_call.index]; - scf_function_t* f = NULL; - dfa_call_data_t* cd = NULL; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w1 = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_call.index]; + scf_function_t* f = NULL; + dfa_call_data_t* cd = NULL; - scf_variable_t* var_pf = NULL; - scf_node_t* node_pf = NULL; - scf_type_t* pt = NULL; + scf_variable_t* var_pf = NULL; + scf_node_t* node_pf = NULL; + scf_type_t* pt = NULL; - scf_node_t* node_call = NULL; - scf_operator_t* op = scf_find_base_operator_by_type(SCF_OP_CALL); + scf_node_t* node_call = NULL; + scf_operator_t* op = scf_find_base_operator_by_type(SCF_OP_CALL); if (scf_ast_find_type_type(&pt, parse->ast, SCF_FUNCTION_PTR) < 0) return SCF_DFA_ERROR; @@ -146,12 +146,12 @@ static int _call_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) return SCF_DFA_ERROR; } - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_call.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_call.index]; + dfa_call_data_t* cd = scf_stack_top(s); - dfa_call_data_t* cd = scf_stack_top(s); if (!cd) { scf_loge("\n"); return SCF_DFA_ERROR; @@ -219,7 +219,7 @@ static int _call_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) } scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_call.index]; @@ -255,9 +255,9 @@ static int _dfa_init_module_call(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, call, comma, scf_dfa_is_comma, _call_action_comma); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_call.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_call.index]; assert(!s); @@ -274,9 +274,9 @@ static int _dfa_init_module_call(scf_dfa_t* dfa) static int _dfa_fini_module_call(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_call.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_call.index]; if (s) { scf_stack_free(s); @@ -289,15 +289,15 @@ static int _dfa_fini_module_call(scf_dfa_t* dfa) static int _dfa_init_syntax_call(scf_dfa_t* dfa) { - SCF_DFA_GET_MODULE_NODE(dfa, call, lp, lp); - SCF_DFA_GET_MODULE_NODE(dfa, call, rp, rp); - SCF_DFA_GET_MODULE_NODE(dfa, call, comma, comma); + SCF_DFA_GET_MODULE_NODE(dfa, call, lp, lp); + SCF_DFA_GET_MODULE_NODE(dfa, call, rp, rp); + SCF_DFA_GET_MODULE_NODE(dfa, call, comma, comma); - SCF_DFA_GET_MODULE_NODE(dfa, expr, entry, expr); + SCF_DFA_GET_MODULE_NODE(dfa, expr, entry, expr); - SCF_DFA_GET_MODULE_NODE(dfa, create, create, create); - SCF_DFA_GET_MODULE_NODE(dfa, create, identity, create_id); - SCF_DFA_GET_MODULE_NODE(dfa, create, rp, create_rp); + SCF_DFA_GET_MODULE_NODE(dfa, create, create, create); + SCF_DFA_GET_MODULE_NODE(dfa, create, identity, create_id); + SCF_DFA_GET_MODULE_NODE(dfa, create, rp, create_rp); // no args scf_dfa_node_add_child(lp, rp); @@ -317,7 +317,6 @@ static int _dfa_init_syntax_call(scf_dfa_t* dfa) scf_dfa_node_add_child(comma, expr); scf_dfa_node_add_child(expr, rp); - scf_logi("\n"); return 0; } @@ -329,4 +328,3 @@ scf_dfa_module_t dfa_module_call = .fini_module = _dfa_fini_module_call, }; - diff --git a/parse/scf_dfa_class.c b/parse/scf_dfa_class.c index 21b0f07..9531048 100644 --- a/parse/scf_dfa_class.c +++ b/parse/scf_dfa_class.c @@ -27,7 +27,7 @@ static int _class_is_class(scf_dfa_t* dfa, void* word) static int _class_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; class_module_data_t* md = d->module_datas[dfa_module_class.index]; scf_lex_word_t* w = words->data[words->size - 1]; @@ -54,7 +54,7 @@ static int _class_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* dat md->parent_block = parse->ast->current_block; SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "class_end"), SCF_DFA_HOOK_END); - printf("\033[31m%s(), %d, t: %p, t->type: %d\033[0m\n", __func__, __LINE__, t, t->type); + scf_logi("\033[31m t: %p, t->type: %d\033[0m\n", t, t->type); return SCF_DFA_NEXT_WORD; } @@ -62,7 +62,7 @@ static int _class_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _class_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; class_module_data_t* md = d->module_datas[dfa_module_class.index]; scf_lex_word_t* w = words->data[words->size - 1]; @@ -122,8 +122,7 @@ static int _class_calculate_size(scf_dfa_t* dfa, scf_type_t* s) for (j = 0; j < v->nb_dimentions; j++) { if (v->dimentions[j] < 0) { - - printf("%s(), %d, error: v: '%s'\n", __func__, __LINE__, v->w->text->data); + scf_loge("v: '%s'\n", v->w->text->data); return SCF_DFA_ERROR; } @@ -136,20 +135,20 @@ static int _class_calculate_size(scf_dfa_t* dfa, scf_type_t* s) size = v->offset + v->size; } - printf("%s(), %d, class '%s', member: '%s', offset: %d, size: %d, v->dim: %d, v->capacity: %d\n", __func__, __LINE__, + scf_logi("class '%s', member: '%s', offset: %d, size: %d, v->dim: %d, v->capacity: %d\n", s->name->data, v->w->text->data, v->offset, v->size, v->nb_dimentions, v->capacity); } s->size = size; s->node.define_flag = 1; - printf("%s(), %d, class '%s', size: %d\n", __func__, __LINE__, s->name->data, s->size); + scf_logi("class '%s', size: %d\n", s->name->data, s->size); return 0; } static int _class_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; class_module_data_t* md = d->module_datas[dfa_module_class.index]; if (_class_calculate_size(dfa, md->current_class) < 0) { @@ -170,7 +169,7 @@ static int _class_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* da static int _class_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; class_module_data_t* md = d->module_datas[dfa_module_class.index]; if (md->nb_rbs == md->nb_lbs) { @@ -204,16 +203,14 @@ static int _dfa_init_module_class(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, class, end, scf_dfa_is_entry, _class_action_end); scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; class_module_data_t* md = d->module_datas[dfa_module_class.index]; assert(!md); md = calloc(1, sizeof(class_module_data_t)); - if (!md) { - printf("%s(),%d, error: \n", __func__, __LINE__); + if (!md) return SCF_DFA_ERROR; - } d->module_datas[dfa_module_class.index] = md; @@ -223,7 +220,7 @@ static int _dfa_init_module_class(scf_dfa_t* dfa) static int _dfa_fini_module_class(scf_dfa_t* dfa) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; class_module_data_t* md = d->module_datas[dfa_module_class.index]; if (md) { @@ -266,7 +263,6 @@ static int _dfa_init_syntax_class(scf_dfa_t* dfa) scf_dfa_node_add_child(end, member); scf_dfa_node_add_child(end, rb); - printf("%s(),%d\n\n", __func__, __LINE__); return 0; } @@ -278,4 +274,3 @@ scf_dfa_module_t dfa_module_class = .fini_module = _dfa_fini_module_class, }; - diff --git a/parse/scf_dfa_container.c b/parse/scf_dfa_container.c index d7583fb..5a2ac2b 100644 --- a/parse/scf_dfa_container.c +++ b/parse/scf_dfa_container.c @@ -18,7 +18,7 @@ typedef struct { static int _container_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_stack_t* s = d->module_datas[dfa_module_container.index]; dfa_container_data_t* cd = scf_stack_top(s); @@ -37,7 +37,7 @@ static int _container_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* static int _container_action_container(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_container.index]; @@ -78,7 +78,7 @@ static int _container_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _container_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_container.index]; dfa_container_data_t* cd = scf_stack_top(s); @@ -161,7 +161,7 @@ static int _container_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* da static int _container_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_container.index]; dfa_container_data_t* cd = scf_stack_top(s); @@ -245,9 +245,9 @@ static int _dfa_init_module_container(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, container, lp_stat, scf_dfa_is_lp, _container_action_lp_stat); SCF_DFA_MODULE_NODE(dfa, container, comma, scf_dfa_is_comma, _container_action_comma); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_container.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_container.index]; assert(!s); @@ -264,9 +264,9 @@ static int _dfa_init_module_container(scf_dfa_t* dfa) static int _dfa_fini_module_container(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_container.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_container.index]; if (s) { scf_stack_free(s); @@ -301,7 +301,6 @@ static int _dfa_init_syntax_container(scf_dfa_t* dfa) scf_dfa_node_add_child(comma, identity); scf_dfa_node_add_child(identity, rp); - scf_logi("\n"); return 0; } diff --git a/parse/scf_dfa_continue.c b/parse/scf_dfa_continue.c index 1a87c00..28c47b9 100644 --- a/parse/scf_dfa_continue.c +++ b/parse/scf_dfa_continue.c @@ -13,9 +13,9 @@ static int _continue_is_continue(scf_dfa_t* dfa, void* word) static int _continue_action_continue(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; scf_node_t* _continue = scf_node_alloc(w, SCF_OP_CONTINUE, NULL); if (!_continue) { @@ -50,8 +50,6 @@ static int _dfa_init_syntax_continue(scf_dfa_t* dfa) SCF_DFA_GET_MODULE_NODE(dfa, continue, _continue, _continue); scf_dfa_node_add_child(_continue, semicolon); - - scf_logi("\n"); return 0; } @@ -61,4 +59,3 @@ scf_dfa_module_t dfa_module_continue = .init_module = _dfa_init_module_continue, .init_syntax = _dfa_init_syntax_continue, }; - diff --git a/parse/scf_dfa_create.c b/parse/scf_dfa_create.c index cf71e8a..6496de4 100644 --- a/parse/scf_dfa_create.c +++ b/parse/scf_dfa_create.c @@ -18,16 +18,16 @@ typedef struct { static int _create_is_create(scf_dfa_t* dfa, void* word) { - scf_lex_word_t* w = word; + scf_lex_word_t* w = word; return SCF_LEX_WORD_KEY_CREATE == w->type; } static int _create_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_create.index]; - create_module_data_t* md = d->module_datas[dfa_module_create.index]; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_create.index]; + create_module_data_t* md = d->module_datas[dfa_module_create.index]; md->nb_lps++; @@ -39,7 +39,7 @@ static int _create_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _create_action_create(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; create_module_data_t* md = d->module_datas[dfa_module_create.index]; @@ -59,7 +59,7 @@ static int _create_action_create(scf_dfa_t* dfa, scf_vector_t* words, void* data static int _create_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; create_module_data_t* md = d->module_datas[dfa_module_create.index]; @@ -107,7 +107,7 @@ static int _create_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* da static int _create_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; create_module_data_t* md = d->module_datas[dfa_module_create.index]; @@ -126,7 +126,7 @@ static int _create_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _create_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; create_module_data_t* md = d->module_datas[dfa_module_create.index]; @@ -169,7 +169,7 @@ static int _create_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _create_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; create_module_data_t* md = d->module_datas[dfa_module_create.index]; @@ -199,7 +199,7 @@ static int _dfa_init_module_create(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, create, comma, scf_dfa_is_comma, _create_action_comma); scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; create_module_data_t* md = d->module_datas[dfa_module_create.index]; assert(!md); @@ -217,8 +217,8 @@ static int _dfa_init_module_create(scf_dfa_t* dfa) static int _dfa_fini_module_create(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; create_module_data_t* md = d->module_datas[dfa_module_create.index]; if (md) { @@ -250,7 +250,6 @@ static int _dfa_init_syntax_create(scf_dfa_t* dfa) scf_dfa_node_add_child(comma, expr); scf_dfa_node_add_child(expr, rp); - scf_logi("\n"); return 0; } diff --git a/parse/scf_dfa_do.c b/parse/scf_dfa_do.c index 13d3e92..172c9d9 100644 --- a/parse/scf_dfa_do.c +++ b/parse/scf_dfa_do.c @@ -19,13 +19,13 @@ typedef struct { static int _do_action_do(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_do.index]; - scf_block_t* b = NULL; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_do.index]; + scf_block_t* b = NULL; + scf_node_t* _do = scf_node_alloc(w, SCF_OP_DO, NULL); - scf_node_t* _do = scf_node_alloc(w, SCF_OP_DO, NULL); if (!_do) { scf_loge("node alloc failed\n"); return SCF_DFA_ERROR; @@ -66,8 +66,8 @@ static int _do_action_while(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _do_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; assert(!d->expr); d->expr_local_flag = 1; @@ -80,9 +80,9 @@ static int _do_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _do_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_do.index]; - dfa_do_data_t* dd = scf_stack_top(s); + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_do.index]; + dfa_do_data_t* dd = scf_stack_top(s); SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "do_lp_stat"), SCF_DFA_HOOK_POST); @@ -93,11 +93,11 @@ static int _do_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _do_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_do.index]; - dfa_do_data_t* dd = scf_stack_top(s); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_do.index]; + dfa_do_data_t* dd = scf_stack_top(s); if (!d->expr) { scf_loge("\n"); @@ -126,10 +126,10 @@ static int _do_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _do_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_do.index]; - dfa_do_data_t* dd = scf_stack_pop(s); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_do.index]; + dfa_do_data_t* dd = scf_stack_pop(s); assert(parse->ast->current_block == dd->parent_block); @@ -156,9 +156,9 @@ static int _dfa_init_module_do(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, do, _do, scf_dfa_is_do, _do_action_do); SCF_DFA_MODULE_NODE(dfa, do, _while, scf_dfa_is_while, _do_action_while); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_do.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_do.index]; assert(!s); @@ -175,9 +175,9 @@ static int _dfa_init_module_do(scf_dfa_t* dfa) static int _dfa_fini_module_do(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_do.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_do.index]; if (s) { scf_stack_free(s); @@ -211,7 +211,6 @@ static int _dfa_init_syntax_do(scf_dfa_t* dfa) scf_dfa_node_add_child(expr, rp); scf_dfa_node_add_child(rp, semicolon); - scf_logi("\n"); return 0; } diff --git a/parse/scf_dfa_enum.c b/parse/scf_dfa_enum.c index a8fd9f8..7ab4727 100644 --- a/parse/scf_dfa_enum.c +++ b/parse/scf_dfa_enum.c @@ -20,7 +20,7 @@ typedef struct { static int _enum_action_type(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; scf_lex_word_t* w = words->data[words->size - 1]; @@ -50,7 +50,7 @@ static int _enum_action_type(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _enum_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; scf_lex_word_t* w = words->data[words->size - 1]; scf_type_t* t = NULL; @@ -89,7 +89,7 @@ static int _enum_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _enum_action_var(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; scf_lex_word_t* w = words->data[words->size - 1]; @@ -144,7 +144,7 @@ static int _enum_action_var(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _enum_action_assign(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; scf_lex_word_t* w = words->data[words->size - 1]; @@ -164,7 +164,7 @@ static int _enum_action_assign(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _enum_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; scf_lex_word_t* w = words->data[words->size - 1]; scf_variable_t* r = NULL; @@ -200,8 +200,8 @@ static int _enum_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _enum_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; scf_lex_word_t* w = words->data[words->size - 1]; scf_variable_t* r = NULL; @@ -253,7 +253,7 @@ static int _enum_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _enum_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; scf_lex_word_t* w = words->data[words->size - 1]; @@ -293,7 +293,7 @@ static int _dfa_init_module_enum(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, enum, comma, scf_dfa_is_comma, _enum_action_comma); scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; assert(!md); @@ -318,8 +318,8 @@ static int _dfa_init_module_enum(scf_dfa_t* dfa) static int _dfa_fini_module_enum(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; enum_module_data_t* md = d->module_datas[dfa_module_enum.index]; if (md) { @@ -371,7 +371,6 @@ static int _dfa_init_syntax_enum(scf_dfa_t* dfa) // end scf_dfa_node_add_child(rb, semicolon); - scf_logi("\n"); return 0; } diff --git a/parse/scf_dfa_expr.c b/parse/scf_dfa_expr.c index 412bd07..6bfff2e 100644 --- a/parse/scf_dfa_expr.c +++ b/parse/scf_dfa_expr.c @@ -18,8 +18,7 @@ int _type_find_type(scf_dfa_t* dfa, dfa_identity_t* id); static int _expr_is_expr(scf_dfa_t* dfa, void* word) { - scf_parse_t* parse = dfa->priv; - scf_lex_word_t* w = word; + scf_lex_word_t* w = word; if (SCF_LEX_WORD_SEMICOLON == w->type || scf_lex_is_operator(w) @@ -31,15 +30,14 @@ static int _expr_is_expr(scf_dfa_t* dfa, void* word) static int _expr_is_number(scf_dfa_t* dfa, void* word) { - scf_lex_word_t* w = word; + scf_lex_word_t* w = word; return scf_lex_is_const(w); } static int _expr_is_unary_op(scf_dfa_t* dfa, void* word) { - scf_parse_t* parse = dfa->priv; - scf_lex_word_t* w = word; + scf_lex_word_t* w = word; if (SCF_LEX_WORD_LS == w->type || SCF_LEX_WORD_RS == w->type @@ -64,8 +62,7 @@ static int _expr_is_unary_post(scf_dfa_t* dfa, void* word) static int _expr_is_binary_op(scf_dfa_t* dfa, void* word) { - scf_parse_t* parse = dfa->priv; - scf_lex_word_t* w = word; + scf_lex_word_t* w = word; if (SCF_LEX_WORD_LS == w->type || SCF_LEX_WORD_RS == w->type @@ -79,7 +76,7 @@ static int _expr_is_binary_op(scf_dfa_t* dfa, void* word) return 0; } -int _expr_add_var(scf_parse_t* parse, dfa_parse_data_t* d) +int _expr_add_var(scf_parse_t* parse, dfa_data_t* d) { expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; scf_variable_t* var = NULL; @@ -155,8 +152,8 @@ int _expr_add_var(scf_parse_t* parse, dfa_parse_data_t* d) static int _expr_action_expr(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; if (!d->expr) { d->expr = scf_expr_alloc(); @@ -173,9 +170,9 @@ static int _expr_action_expr(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _expr_action_number(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; scf_logd("w: %s\n", w->text->data); @@ -279,12 +276,12 @@ static int _expr_action_number(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _expr_action_op(scf_dfa_t* dfa, scf_vector_t* words, void* data, int nb_operands) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; - scf_operator_t* op; - scf_node_t* node; + scf_operator_t* op; + scf_node_t* node; op = scf_find_base_operator(w->text->data, nb_operands); if (!op) { @@ -317,20 +314,18 @@ static int _expr_action_binary_op(scf_dfa_t* dfa, scf_vector_t* words, void* dat scf_logd("\n"); scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; - dfa_identity_t* id = scf_stack_top(d->current_identities); + scf_variable_t* v; if (SCF_LEX_WORD_STAR == w->type) { if (id && id->identity) { - scf_variable_t* var = scf_block_find_variable(parse->ast->current_block, - id->identity->text->data); - - if (!var) { + v = scf_block_find_variable(parse->ast->current_block, id->identity->text->data); + if (!v) { scf_logw("'%s' not var\n", id->identity->text->data); if (d->expr) { @@ -366,7 +361,7 @@ static int _expr_action_binary_op(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _expr_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; scf_expr_t* e = scf_expr_alloc(); @@ -391,9 +386,8 @@ static int _expr_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _expr_action_rp_cast(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; - dfa_identity_t* id = scf_stack_top(d->current_identities); if (!id) { @@ -480,11 +474,10 @@ static int _expr_action_rp_cast(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _expr_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; - - dfa_identity_t* id = scf_stack_top(d->current_identities); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; + dfa_identity_t* id = scf_stack_top(d->current_identities); if (id && id->identity) { @@ -531,12 +524,11 @@ static int _expr_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _expr_action_unary_post(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - scf_lex_word_t* w = words->data[words->size - 1]; - dfa_parse_data_t* d = data; - scf_node_t* n = NULL; - - dfa_identity_t* id = scf_stack_top(d->current_identities); + scf_parse_t* parse = dfa->priv; + scf_lex_word_t* w = words->data[words->size - 1]; + dfa_data_t* d = data; + scf_node_t* n = NULL; + dfa_identity_t* id = scf_stack_top(d->current_identities); if (id && id->identity) { if (_expr_add_var(parse, d) < 0) { @@ -569,11 +561,11 @@ static int _expr_action_unary_post(scf_dfa_t* dfa, scf_vector_t* words, void* da static int _expr_action_ls(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - scf_lex_word_t* w = words->data[words->size - 1]; - dfa_parse_data_t* d = data; - expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; - dfa_identity_t* id = scf_stack_top(d->current_identities); + scf_parse_t* parse = dfa->priv; + scf_lex_word_t* w = words->data[words->size - 1]; + dfa_data_t* d = data; + expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; + dfa_identity_t* id = scf_stack_top(d->current_identities); if (id && id->identity) { if (_expr_add_var(parse, d) < 0) { @@ -614,11 +606,10 @@ static int _expr_action_ls(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _expr_action_rs(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; - - dfa_identity_t* id = scf_stack_top(d->current_identities); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; + dfa_identity_t* id = scf_stack_top(d->current_identities); if (id && id->identity) { if (_expr_add_var(parse, d) < 0) { @@ -739,10 +730,9 @@ int _expr_multi_rets(scf_expr_t* e) return 0; } -static int _expr_fini_expr(scf_parse_t* parse, dfa_parse_data_t* d, int semi_flag) +static int _expr_fini_expr(scf_parse_t* parse, dfa_data_t* d, int semi_flag) { expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; - dfa_identity_t* id = scf_stack_top(d->current_identities); if (id && id->identity) { @@ -796,8 +786,8 @@ static int _expr_fini_expr(scf_parse_t* parse, dfa_parse_data_t* d, int semi_fla static int _expr_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; if (_expr_fini_expr(parse, d, 0) < 0) return SCF_DFA_ERROR; @@ -807,9 +797,9 @@ static int _expr_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _expr_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; if (_expr_fini_expr(parse, d, 1) < 0) return SCF_DFA_ERROR; @@ -835,9 +825,9 @@ static int _dfa_init_module_expr(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, expr, comma, scf_dfa_is_comma, _expr_action_comma); SCF_DFA_MODULE_NODE(dfa, expr, semicolon, scf_dfa_is_semicolon, _expr_action_semicolon); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; assert(!md); @@ -871,9 +861,9 @@ _ls_exprs: static int _dfa_fini_module_expr(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + expr_module_data_t* md = d->module_datas[dfa_module_expr.index]; if (md) { if (md->ls_exprs) @@ -1047,7 +1037,6 @@ static int _dfa_init_syntax_expr(scf_dfa_t* dfa) scf_dfa_node_add_child(identity, semicolon); scf_dfa_node_add_child(rs, semicolon); - scf_logi("\n\n"); return 0; } diff --git a/parse/scf_dfa_for.c b/parse/scf_dfa_for.c index 3970233..0258ced 100644 --- a/parse/scf_dfa_for.c +++ b/parse/scf_dfa_for.c @@ -29,12 +29,12 @@ static int _for_is_end(scf_dfa_t* dfa, void* word) static int _for_action_for(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_for.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_for.index]; + scf_node_t* _for = scf_node_alloc(w, SCF_OP_FOR, NULL); - scf_node_t* _for = scf_node_alloc(w, SCF_OP_FOR, NULL); if (!_for) { scf_loge("node alloc failed\n"); return SCF_DFA_ERROR; @@ -66,7 +66,7 @@ static int _for_action_for(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _for_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_for.index]; dfa_for_data_t* fd = scf_stack_top(s); @@ -84,9 +84,9 @@ static int _for_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _for_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_for.index]; - dfa_for_data_t* fd = scf_stack_top(s); + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_for.index]; + dfa_for_data_t* fd = scf_stack_top(s); SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "for_lp_stat"), SCF_DFA_HOOK_POST); @@ -97,11 +97,11 @@ static int _for_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _for_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_for.index]; - dfa_for_data_t* fd = scf_stack_top(s); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_for.index]; + dfa_for_data_t* fd = scf_stack_top(s); if (!d->expr) { scf_loge("need expr before ','\n"); @@ -138,17 +138,16 @@ static int _for_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _for_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - printf("%s(),%d\n", __func__, __LINE__); if (!data) { - printf("%s(), %d, error: \n", __func__, __LINE__); + scf_loge("\n"); return SCF_DFA_ERROR; } - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_for.index]; - dfa_for_data_t* fd = scf_stack_top(s); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_for.index]; + dfa_for_data_t* fd = scf_stack_top(s); if (0 == fd->nb_semicolons) { if (d->expr) { @@ -230,11 +229,11 @@ static int _for_add_exprs(dfa_for_data_t* fd) static int _for_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_for.index]; - dfa_for_data_t* fd = scf_stack_top(s); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_for.index]; + dfa_for_data_t* fd = scf_stack_top(s); fd->nb_rps++; @@ -269,12 +268,10 @@ static int _for_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _for_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_for.index]; - dfa_for_data_t* fd = scf_stack_pop(s); - - scf_loge("current_block: %p, parent_block: %p\n", parse->ast->current_block, fd->parent_block); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_for.index]; + dfa_for_data_t* fd = scf_stack_pop(s); if (3 == fd->_for->nb_nodes) scf_node_add_child(fd->_for, NULL); @@ -305,9 +302,9 @@ static int _dfa_init_module_for(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, for, _for, scf_dfa_is_for, _for_action_for); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_for.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_for.index]; assert(!s); @@ -324,9 +321,9 @@ static int _dfa_init_module_for(scf_dfa_t* dfa) static int _dfa_fini_module_for(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_for.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_for.index]; if (s) { scf_stack_free(s); @@ -367,7 +364,6 @@ static int _dfa_init_syntax_for(scf_dfa_t* dfa) // for body block scf_dfa_node_add_child(rp, block); - scf_logi("\n"); return 0; } @@ -380,4 +376,3 @@ scf_dfa_module_t dfa_module_for = .fini_module = _dfa_fini_module_for, }; - diff --git a/parse/scf_dfa_function.c b/parse/scf_dfa_function.c index 30ffda1..9bbe184 100644 --- a/parse/scf_dfa_function.c +++ b/parse/scf_dfa_function.c @@ -10,7 +10,7 @@ typedef struct { } dfa_fun_data_t; -int _function_add_function(scf_dfa_t* dfa, dfa_parse_data_t* d) +int _function_add_function(scf_dfa_t* dfa, dfa_data_t* d) { if (d->current_identities->size < 2) { scf_loge("d->current_identities->size: %d\n", d->current_identities->size); @@ -119,7 +119,7 @@ int _function_add_function(scf_dfa_t* dfa, dfa_parse_data_t* d) return SCF_DFA_NEXT_WORD; } -int _function_add_arg(scf_dfa_t* dfa, dfa_parse_data_t* d) +int _function_add_arg(scf_dfa_t* dfa, dfa_data_t* d) { dfa_identity_t* t = NULL; dfa_identity_t* v = NULL; @@ -200,7 +200,7 @@ int _function_add_arg(scf_dfa_t* dfa, dfa_parse_data_t* d) static int _function_action_vargs(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; d->current_function->vargs_flag = 1; @@ -209,7 +209,7 @@ static int _function_action_vargs(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _function_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; if (_function_add_arg(dfa, d) < 0) { scf_loge("function add arg failed\n"); @@ -223,9 +223,9 @@ static int _function_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _function_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; assert(!d->current_node); @@ -245,11 +245,11 @@ static int _function_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _function_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; - scf_function_t* f = d->current_function; - scf_function_t* fprev = NULL; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; + scf_function_t* f = d->current_function; + scf_function_t* fprev = NULL; d->nb_rps++; @@ -366,12 +366,12 @@ static int _function_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _function_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; - parse->ast->current_block = (scf_block_t*)(fd->parent_block); + parse->ast->current_block = (scf_block_t*)(fd->parent_block); if (d->current_function->node.nb_nodes > 0) d->current_function->node.define_flag = 1; @@ -395,9 +395,9 @@ static int _dfa_init_module_function(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, function, lp, scf_dfa_is_lp, _function_action_lp); SCF_DFA_MODULE_NODE(dfa, function, rp, scf_dfa_is_rp, _function_action_rp); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; assert(!fd); @@ -414,9 +414,9 @@ static int _dfa_init_module_function(scf_dfa_t* dfa) static int _dfa_fini_module_function(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + dfa_fun_data_t* fd = d->module_datas[dfa_module_function.index]; if (fd) { free(fd); @@ -471,7 +471,6 @@ static int _dfa_init_syntax_function(scf_dfa_t* dfa) // function body scf_dfa_node_add_child(rp, block); - scf_logi("\n"); return 0; } diff --git a/parse/scf_dfa_goto.c b/parse/scf_dfa_goto.c index 176b067..b3ea503 100644 --- a/parse/scf_dfa_goto.c +++ b/parse/scf_dfa_goto.c @@ -7,7 +7,7 @@ extern scf_dfa_module_t dfa_module_goto; static int _goto_action_goto(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_node_t* _goto = scf_node_alloc(w, SCF_OP_GOTO, NULL); @@ -29,7 +29,7 @@ static int _goto_action_goto(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _goto_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_label_t* l = scf_label_alloc(w); @@ -42,7 +42,7 @@ static int _goto_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data static int _goto_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; d->current_goto = NULL; @@ -67,7 +67,6 @@ static int _dfa_init_syntax_goto(scf_dfa_t* dfa) scf_dfa_node_add_child(_goto, identity); scf_dfa_node_add_child(identity, semicolon); - scf_logi("\n"); return 0; } @@ -77,4 +76,3 @@ scf_dfa_module_t dfa_module_goto = .init_module = _dfa_init_module_goto, .init_syntax = _dfa_init_syntax_goto, }; - diff --git a/parse/scf_dfa_identity.c b/parse/scf_dfa_identity.c index 5861ae9..01d08bf 100644 --- a/parse/scf_dfa_identity.c +++ b/parse/scf_dfa_identity.c @@ -6,9 +6,9 @@ extern scf_dfa_module_t dfa_module_identity; static int _identity_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_lex_word_t* w = words->data[words->size - 1]; - dfa_parse_data_t* d = data; - scf_stack_t* s = d->current_identities; + scf_lex_word_t* w = words->data[words->size - 1]; + dfa_data_t* d = data; + scf_stack_t* s = d->current_identities; scf_logd("w: '%s'\n", w->text->data); @@ -37,8 +37,6 @@ static int _dfa_init_module_identity(scf_dfa_t* dfa) static int _dfa_init_syntax_identity(scf_dfa_t* dfa) { - scf_logi("\n"); - SCF_DFA_GET_MODULE_NODE(dfa, identity, identity, identity); scf_vector_add(dfa->syntaxes, identity); diff --git a/parse/scf_dfa_if.c b/parse/scf_dfa_if.c index dc50c61..cd2f75b 100644 --- a/parse/scf_dfa_if.c +++ b/parse/scf_dfa_if.c @@ -29,13 +29,13 @@ static int _if_is_end(scf_dfa_t* dfa, void* word) static int _if_action_if(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_if.index]; - dfa_if_data_t* ifd = scf_stack_top(s); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_if.index]; + dfa_if_data_t* ifd = scf_stack_top(s); + scf_node_t* _if = scf_node_alloc(w, SCF_OP_IF, NULL); - scf_node_t* _if = scf_node_alloc(w, SCF_OP_IF, NULL); if (!_if) { scf_loge("\n"); return SCF_DFA_ERROR; @@ -68,11 +68,11 @@ static int _if_action_if(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _if_action_else(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_if.index]; - dfa_if_data_t* ifd = scf_stack_top(s); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_if.index]; + dfa_if_data_t* ifd = scf_stack_top(s); if (!ifd) { scf_loge("no 'if' before 'else' in line: %d\n", w->line); @@ -97,9 +97,9 @@ static int _if_action_else(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _if_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_if.index]; - dfa_if_data_t* ifd = scf_stack_top(s); + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_if.index]; + dfa_if_data_t* ifd = scf_stack_top(s); if (d->expr) { scf_expr_free(d->expr); @@ -117,9 +117,9 @@ static int _if_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _if_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_if.index]; - dfa_if_data_t* ifd = scf_stack_top(s); + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_if.index]; + dfa_if_data_t* ifd = scf_stack_top(s); ifd->nb_lps++; @@ -130,9 +130,9 @@ static int _if_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _if_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_if.index]; - dfa_if_data_t* ifd = scf_stack_top(s); + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_if.index]; + dfa_if_data_t* ifd = scf_stack_top(s); if (!d->expr) { scf_loge("\n"); @@ -172,11 +172,11 @@ static int _is_end(scf_dfa_t* dfa) static int _if_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_if.index]; - dfa_if_data_t* ifd = scf_stack_top(s); - scf_lex_word_t* prev_else = ifd->prev_else; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_if.index]; + dfa_if_data_t* ifd = scf_stack_top(s); + scf_lex_word_t* prev_else = ifd->prev_else; if (!_is_end(dfa)) { @@ -216,9 +216,9 @@ static int _dfa_init_module_if(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, if, _if, scf_dfa_is_if, _if_action_if); SCF_DFA_MODULE_NODE(dfa, if, _else, scf_dfa_is_else, _if_action_else); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_if.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_if.index]; assert(!s); @@ -235,9 +235,9 @@ static int _dfa_init_module_if(scf_dfa_t* dfa) static int _dfa_fini_module_if(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_if.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_if.index]; if (s) { scf_stack_free(s); @@ -278,7 +278,6 @@ static int _dfa_init_syntax_if(scf_dfa_t* dfa) // last else block scf_dfa_node_add_child(_else, block); - scf_logi("\n"); return 0; } @@ -291,4 +290,3 @@ scf_dfa_module_t dfa_module_if = .fini_module = _dfa_fini_module_if, }; - diff --git a/parse/scf_dfa_include.c b/parse/scf_dfa_include.c index 20e5fe8..141bf42 100644 --- a/parse/scf_dfa_include.c +++ b/parse/scf_dfa_include.c @@ -6,9 +6,9 @@ extern scf_dfa_module_t dfa_module_include; static int _include_action_include(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; scf_loge("include '%s', line %d\n", w->text->data, w->line); return SCF_DFA_NEXT_WORD; @@ -16,11 +16,11 @@ static int _include_action_include(scf_dfa_t* dfa, scf_vector_t* words, void* da static int _include_action_path(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_lex_t* lex = parse->lex; - scf_block_t* cur = parse->ast->current_block; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_lex_t* lex = parse->lex; + scf_block_t* cur = parse->ast->current_block; assert(w->data.s); scf_loge("include '%s', line %d\n", w->data.s->data, w->line); @@ -34,8 +34,25 @@ static int _include_action_path(scf_dfa_t* dfa, scf_vector_t* words, void* data) return SCF_DFA_ERROR; } - if (parse->lex) - scf_lex_close(parse->lex); + if (parse->lex != lex && parse->lex->macros) { // copy macros + + if (!lex->macros) { + lex->macros = scf_vector_clone(parse->lex->macros); + if (!lex->macros) + return -ENOMEM; + } else { + ret = scf_vector_cat(lex->macros, parse->lex->macros); + if (ret < 0) + return ret; + } + + scf_macro_t* m; + int i; + for (i = 0; i < parse->lex->macros->size; i++) { + m = parse->lex->macros->data[i]; + m->refs++; + } + } parse->lex = lex; parse->ast->current_block = cur; @@ -43,7 +60,7 @@ static int _include_action_path(scf_dfa_t* dfa, scf_vector_t* words, void* data) return SCF_DFA_NEXT_WORD; } -static int _include_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) +static int _include_action_LF(scf_dfa_t* dfa, scf_vector_t* words, void* data) { return SCF_DFA_OK; } @@ -52,23 +69,23 @@ static int _dfa_init_module_include(scf_dfa_t* dfa) { SCF_DFA_MODULE_NODE(dfa, include, include, scf_dfa_is_include, _include_action_include); SCF_DFA_MODULE_NODE(dfa, include, path, scf_dfa_is_const_string, _include_action_path); - SCF_DFA_MODULE_NODE(dfa, include, semicolon, scf_dfa_is_semicolon, _include_action_semicolon); + SCF_DFA_MODULE_NODE(dfa, include, LF, scf_dfa_is_LF, _include_action_LF); return SCF_DFA_OK; } static int _dfa_init_syntax_include(scf_dfa_t* dfa) { - SCF_DFA_GET_MODULE_NODE(dfa, include, include, include); - SCF_DFA_GET_MODULE_NODE(dfa, include, path, path); - SCF_DFA_GET_MODULE_NODE(dfa, include, semicolon, semicolon); + SCF_DFA_GET_MODULE_NODE(dfa, include, include, include); + SCF_DFA_GET_MODULE_NODE(dfa, include, path, path); + SCF_DFA_GET_MODULE_NODE(dfa, include, LF, LF); - scf_vector_add(dfa->syntaxes, include); + SCF_DFA_GET_MODULE_NODE(dfa, macro, hash, hash); + scf_dfa_node_add_child(hash, include); scf_dfa_node_add_child(include, path); - scf_dfa_node_add_child(path, semicolon); + scf_dfa_node_add_child(path, LF); - scf_logi("\n"); return 0; } @@ -78,4 +95,3 @@ scf_dfa_module_t dfa_module_include = .init_module = _dfa_init_module_include, .init_syntax = _dfa_init_syntax_include, }; - diff --git a/parse/scf_dfa_init_data.c b/parse/scf_dfa_init_data.c index 62fc35a..d9fce44 100644 --- a/parse/scf_dfa_init_data.c +++ b/parse/scf_dfa_init_data.c @@ -2,12 +2,12 @@ #include"scf_dfa_util.h" #include"scf_parse.h" -extern scf_dfa_module_t dfa_module_init_data; +extern scf_dfa_module_t dfa_module_init_data; -int scf_array_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_vector_t* init_exprs); +int scf_array_init (scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_vector_t* init_exprs); int scf_struct_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_vector_t* init_exprs); -int _expr_add_var(scf_parse_t* parse, dfa_parse_data_t* d); +int _expr_add_var(scf_parse_t* parse, dfa_data_t* d); typedef struct { int nb_lbs; @@ -15,7 +15,7 @@ typedef struct { } data_module_data_t; -static int _do_data_init(scf_dfa_t* dfa, scf_vector_t* words, dfa_parse_data_t* d) +static int _do_data_init(scf_dfa_t* dfa, scf_vector_t* words, dfa_data_t* d) { scf_parse_t* parse = dfa->priv; scf_variable_t* var = d->current_var; @@ -89,7 +89,7 @@ error: return ret; } -static int _add_data_init_expr(scf_dfa_t* dfa, scf_vector_t* words, dfa_parse_data_t* d) +static int _add_data_init_expr(scf_dfa_t* dfa, scf_vector_t* words, dfa_data_t* d) { assert(!d->expr->parent); assert(d->current_dim >= 0); @@ -113,7 +113,7 @@ static int _add_data_init_expr(scf_dfa_t* dfa, scf_vector_t* words, dfa_parse_da static int _data_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; if (d->current_dim < 0) { @@ -145,7 +145,7 @@ static int _data_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) } scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 2]; data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; @@ -183,7 +183,7 @@ static int _data_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _data_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; if (d->expr) { @@ -224,12 +224,11 @@ static int _data_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _dfa_init_module_init_data(scf_dfa_t* dfa) { SCF_DFA_MODULE_NODE(dfa, init_data, comma, scf_dfa_is_comma, _data_action_comma); - SCF_DFA_MODULE_NODE(dfa, init_data, lb, scf_dfa_is_lb, _data_action_lb); SCF_DFA_MODULE_NODE(dfa, init_data, rb, scf_dfa_is_rb, _data_action_rb); scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; assert(!md); @@ -247,12 +246,10 @@ static int _dfa_init_module_init_data(scf_dfa_t* dfa) static int _dfa_init_syntax_init_data(scf_dfa_t* dfa) { - SCF_DFA_GET_MODULE_NODE(dfa, init_data, comma, comma); - - SCF_DFA_GET_MODULE_NODE(dfa, init_data, lb, lb); - SCF_DFA_GET_MODULE_NODE(dfa, init_data, rb, rb); - - SCF_DFA_GET_MODULE_NODE(dfa, expr, entry, expr); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, comma, comma); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, lb, lb); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, rb, rb); + SCF_DFA_GET_MODULE_NODE(dfa, expr, entry, expr); // empty init, use 0 to fill the data scf_dfa_node_add_child(lb, rb); @@ -269,7 +266,6 @@ static int _dfa_init_syntax_init_data(scf_dfa_t* dfa) scf_dfa_node_add_child(comma, expr); scf_dfa_node_add_child(expr, rb); - scf_logi("\n"); return 0; } diff --git a/parse/scf_dfa_label.c b/parse/scf_dfa_label.c index 22f50c6..a017cf7 100644 --- a/parse/scf_dfa_label.c +++ b/parse/scf_dfa_label.c @@ -7,7 +7,7 @@ extern scf_dfa_module_t dfa_module_label; static int _label_action_colon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; dfa_identity_t* id = scf_stack_top(d->current_identities); if (!id || !id->identity) { diff --git a/parse/scf_dfa_macro.c b/parse/scf_dfa_macro.c new file mode 100644 index 0000000..68d4280 --- /dev/null +++ b/parse/scf_dfa_macro.c @@ -0,0 +1,27 @@ +#include"scf_dfa.h" +#include"scf_dfa_util.h" +#include"scf_parse.h" + +extern scf_dfa_module_t dfa_module_macro; + +static int _dfa_init_module_macro(scf_dfa_t* dfa) +{ + SCF_DFA_MODULE_NODE(dfa, macro, hash, scf_dfa_is_hash, scf_dfa_action_next); + + return SCF_DFA_OK; +} + +static int _dfa_init_syntax_macro(scf_dfa_t* dfa) +{ + SCF_DFA_GET_MODULE_NODE(dfa, macro, hash, hash); + + scf_vector_add(dfa->syntaxes, hash); + return 0; +} + +scf_dfa_module_t dfa_module_macro = +{ + .name = "macro", + .init_module = _dfa_init_module_macro, + .init_syntax = _dfa_init_syntax_macro, +}; diff --git a/parse/scf_dfa_operator.c b/parse/scf_dfa_operator.c index cbd2698..3e54678 100644 --- a/parse/scf_dfa_operator.c +++ b/parse/scf_dfa_operator.c @@ -26,7 +26,7 @@ static int _operator_is_op(scf_dfa_t* dfa, void* word) return (SCF_LEX_WORD_PLUS <= w->type && SCF_LEX_WORD_GE >= w->type); } -int _operator_add_operator(scf_dfa_t* dfa, dfa_parse_data_t* d) +int _operator_add_operator(scf_dfa_t* dfa, dfa_data_t* d) { scf_parse_t* parse = dfa->priv; dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; @@ -45,8 +45,7 @@ int _operator_add_operator(scf_dfa_t* dfa, dfa_parse_data_t* d) return SCF_DFA_ERROR; } - scf_logi("operator: %s,line:%d,pos:%d\n", - f->node.w->text->data, f->node.w->line, f->node.w->pos); + scf_logi("operator: %s,line:%d,pos:%d\n", f->node.w->text->data, f->node.w->line, f->node.w->pos); while (d->current_identities->size > 0) { @@ -91,7 +90,7 @@ int _operator_add_operator(scf_dfa_t* dfa, dfa_parse_data_t* d) return SCF_DFA_NEXT_WORD; } -int _operator_add_arg(scf_dfa_t* dfa, dfa_parse_data_t* d) +int _operator_add_arg(scf_dfa_t* dfa, dfa_data_t* d) { scf_variable_t* arg = NULL; @@ -127,8 +126,7 @@ int _operator_add_arg(scf_dfa_t* dfa, dfa_parse_data_t* d) arg->arg_flag = 1; arg->local_flag = 1; - scf_logi("d->current_function->argv->size: %d, %p\n", - d->current_function->argv->size, d->current_function); + scf_logi("d->current_function->argv->size: %d, %p\n", d->current_function->argv->size, d->current_function); free(id0); free(id1); @@ -141,7 +139,7 @@ int _operator_add_arg(scf_dfa_t* dfa, dfa_parse_data_t* d) static int _operator_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; if (_operator_add_arg(dfa, d) < 0) { scf_loge("add arg failed\n"); @@ -155,9 +153,9 @@ static int _operator_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _operator_action_op(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; opd->word_op = words->data[words->size - 1]; @@ -166,9 +164,9 @@ static int _operator_action_op(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _operator_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; assert(!d->current_node); @@ -188,10 +186,10 @@ static int _operator_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _operator_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; - scf_function_t* f = NULL; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; + scf_function_t* f = NULL; d->nb_rps++; @@ -278,10 +276,10 @@ static int _operator_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _operator_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; parse->ast->current_block = (scf_block_t*)(opd->parent_block); @@ -313,9 +311,9 @@ static int _dfa_init_module_operator(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, operator, key, _operator_is_key, NULL); SCF_DFA_MODULE_NODE(dfa, operator, op, _operator_is_op, _operator_action_op); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; assert(!opd); @@ -332,9 +330,9 @@ static int _dfa_init_module_operator(scf_dfa_t* dfa) static int _dfa_fini_module_operator(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + dfa_op_data_t* opd = d->module_datas[dfa_module_operator.index]; if (opd) { free(opd); @@ -400,7 +398,6 @@ static int _dfa_init_syntax_operator(scf_dfa_t* dfa) // operator body scf_dfa_node_add_child(rp, block); - scf_logi("\n"); return 0; } @@ -413,4 +410,3 @@ scf_dfa_module_t dfa_module_operator = .fini_module = _dfa_fini_module_operator, }; - diff --git a/parse/scf_dfa_parse.c b/parse/scf_dfa_parse.c index b6805a5..495c411 100644 --- a/parse/scf_dfa_parse.c +++ b/parse/scf_dfa_parse.c @@ -1,6 +1,7 @@ #include"scf_dfa.h" #include"scf_parse.h" +extern scf_dfa_module_t dfa_module_macro; extern scf_dfa_module_t dfa_module_include; extern scf_dfa_module_t dfa_module_identity; @@ -42,6 +43,7 @@ extern scf_dfa_module_t dfa_module_block; scf_dfa_module_t* dfa_modules[] = { + &dfa_module_macro, &dfa_module_include, &dfa_module_identity, @@ -91,7 +93,7 @@ int scf_parse_dfa_init(scf_parse_t* parse) int nb_modules = sizeof(dfa_modules) / sizeof(dfa_modules[0]); - parse->dfa_data = calloc(1, sizeof(dfa_parse_data_t)); + parse->dfa_data = calloc(1, sizeof(dfa_data_t)); if (!parse->dfa_data) { scf_loge("\n"); return -1; diff --git a/parse/scf_dfa_return.c b/parse/scf_dfa_return.c index a3915f3..73228c7 100644 --- a/parse/scf_dfa_return.c +++ b/parse/scf_dfa_return.c @@ -2,14 +2,14 @@ #include"scf_dfa_util.h" #include"scf_parse.h" -extern scf_dfa_module_t dfa_module_return; +extern scf_dfa_module_t dfa_module_return; static int _return_action_return(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_node_t* _return = NULL; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_node_t* _return = NULL; if (d->expr) { scf_loge("\n"); @@ -38,8 +38,8 @@ static int _return_action_return(scf_dfa_t* dfa, scf_vector_t* words, void* data static int _return_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; if (d->expr) { scf_node_add_child(d->current_return, d->expr); @@ -53,8 +53,8 @@ static int _return_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _return_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; if (d->expr) { scf_node_add_child(d->current_return, d->expr); @@ -95,7 +95,6 @@ static int _dfa_init_syntax_return(scf_dfa_t* dfa) scf_dfa_node_add_child(comma, expr); scf_dfa_node_add_child(expr, semicolon); - scf_logi("\n"); return 0; } @@ -105,4 +104,3 @@ scf_dfa_module_t dfa_module_return = .init_module = _dfa_init_module_return, .init_syntax = _dfa_init_syntax_return, }; - diff --git a/parse/scf_dfa_sizeof.c b/parse/scf_dfa_sizeof.c index 3b2bdb2..df6366f 100644 --- a/parse/scf_dfa_sizeof.c +++ b/parse/scf_dfa_sizeof.c @@ -18,9 +18,9 @@ typedef struct { static int _sizeof_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; - dfa_sizeof_data_t* sd = scf_stack_top(s); + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; + dfa_sizeof_data_t* sd = scf_stack_top(s); if (!sd) { scf_loge("\n"); @@ -36,10 +36,10 @@ static int _sizeof_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _sizeof_action_sizeof(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; dfa_sizeof_data_t* sd = calloc(1, sizeof(dfa_sizeof_data_t)); if (!sd) { @@ -77,7 +77,7 @@ static int _sizeof_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _sizeof_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; dfa_sizeof_data_t* sd = scf_stack_top(s); @@ -196,9 +196,9 @@ static int _dfa_init_module_sizeof(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, sizeof, rp, scf_dfa_is_rp, _sizeof_action_rp); SCF_DFA_MODULE_NODE(dfa, sizeof, lp_stat, scf_dfa_is_lp, _sizeof_action_lp_stat); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; assert(!s); @@ -215,9 +215,9 @@ static int _dfa_init_module_sizeof(scf_dfa_t* dfa) static int _dfa_fini_module_sizeof(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_sizeof.index]; if (s) { scf_stack_free(s); @@ -250,7 +250,6 @@ static int _dfa_init_syntax_sizeof(scf_dfa_t* dfa) scf_dfa_node_add_child(identity, rp); scf_dfa_node_add_child(star, rp); - scf_logi("\n"); return 0; } @@ -262,4 +261,3 @@ scf_dfa_module_t dfa_module_sizeof = .fini_module = _dfa_fini_module_sizeof, }; - diff --git a/parse/scf_dfa_switch.c b/parse/scf_dfa_switch.c index 589279a..e95de06 100644 --- a/parse/scf_dfa_switch.c +++ b/parse/scf_dfa_switch.c @@ -24,12 +24,12 @@ static int _switch_is_end(scf_dfa_t* dfa, void* word) static int _switch_action_switch(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_stack_t* s = d->module_datas[dfa_module_switch.index]; - + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_stack_t* s = d->module_datas[dfa_module_switch.index]; scf_node_t* _switch = scf_node_alloc(w, SCF_OP_SWITCH, NULL); + if (!_switch) { scf_loge("node alloc failed\n"); return SCF_DFA_ERROR; @@ -58,8 +58,8 @@ static int _switch_action_switch(scf_dfa_t* dfa, scf_vector_t* words, void* data static int _switch_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; assert(!d->expr); d->expr_local_flag = 1; @@ -72,7 +72,7 @@ static int _switch_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _switch_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_stack_t* s = d->module_datas[dfa_module_switch.index]; dfa_switch_data_t* sd = scf_stack_top(s); @@ -86,7 +86,7 @@ static int _switch_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _switch_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_stack_t* s = d->module_datas[dfa_module_switch.index]; dfa_switch_data_t* sd = scf_stack_top(s); @@ -119,7 +119,7 @@ static int _switch_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _switch_action_case(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_switch.index]; dfa_switch_data_t* sd = scf_stack_top(s); @@ -139,7 +139,7 @@ static int _switch_action_case(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _switch_action_default(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_switch.index]; dfa_switch_data_t* sd = scf_stack_top(s); @@ -158,7 +158,7 @@ static int _switch_action_default(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _switch_action_colon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_stack_t* s = d->module_datas[dfa_module_switch.index]; dfa_switch_data_t* sd = scf_stack_top(s); @@ -184,7 +184,7 @@ static int _switch_action_colon(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _switch_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_stack_t* s = d->module_datas[dfa_module_switch.index]; dfa_switch_data_t* sd = scf_stack_pop(s); @@ -215,7 +215,7 @@ static int _dfa_init_module_switch(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, switch, end, _switch_is_end, _switch_action_end); scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; scf_stack_t* s = d->module_datas[dfa_module_switch.index]; assert(!s); @@ -233,9 +233,9 @@ static int _dfa_init_module_switch(scf_dfa_t* dfa) static int _dfa_fini_module_switch(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_switch.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_switch.index]; if (s) { scf_stack_free(s); @@ -270,7 +270,6 @@ static int _dfa_init_syntax_switch(scf_dfa_t* dfa) scf_dfa_node_add_child(expr, colon); scf_dfa_node_add_child(_default, colon); - scf_logi("\n"); return 0; } diff --git a/parse/scf_dfa_type.c b/parse/scf_dfa_type.c index 4b2ef7d..5ef9055 100644 --- a/parse/scf_dfa_type.c +++ b/parse/scf_dfa_type.c @@ -14,7 +14,7 @@ static int _type_is__struct(scf_dfa_t* dfa, void* word) static int _type_action_const(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; d->const_flag = 1; @@ -23,7 +23,7 @@ static int _type_action_const(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _type_action_extern(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; d->extern_flag = 1; @@ -32,7 +32,7 @@ static int _type_action_extern(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _type_action_static(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; d->static_flag = 1; @@ -41,7 +41,7 @@ static int _type_action_static(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _type_action_inline(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; d->inline_flag = 1; @@ -50,12 +50,12 @@ static int _type_action_inline(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _type_action_base_type(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - scf_lex_word_t* w = words->data[words->size - 1]; - dfa_parse_data_t* d = data; - scf_stack_t* s = d->current_identities; + scf_parse_t* parse = dfa->priv; + scf_lex_word_t* w = words->data[words->size - 1]; + dfa_data_t* d = data; + scf_stack_t* s = d->current_identities; + dfa_identity_t* id = calloc(1, sizeof(dfa_identity_t)); - dfa_identity_t* id = calloc(1, sizeof(dfa_identity_t)); if (!id) return SCF_DFA_ERROR; @@ -150,11 +150,11 @@ int _type_find_type(scf_dfa_t* dfa, dfa_identity_t* id) static int _type_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - scf_lex_word_t* w = words->data[words->size - 1]; - dfa_parse_data_t* d = data; - scf_stack_t* s = d->current_identities; - dfa_identity_t* id = NULL; + scf_parse_t* parse = dfa->priv; + scf_lex_word_t* w = words->data[words->size - 1]; + dfa_data_t* d = data; + scf_stack_t* s = d->current_identities; + dfa_identity_t* id = NULL; if (s->size > 0) { id = scf_stack_top(s); @@ -181,7 +181,7 @@ static int _type_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data static int _type_action_star(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; + dfa_data_t* d = data; dfa_identity_t* id = scf_stack_top(d->current_identities); assert(id); @@ -201,8 +201,8 @@ static int _type_action_star(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _type_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - dfa_identity_t* id = scf_stack_top(d->current_identities); + dfa_data_t* d = data; + dfa_identity_t* id = scf_stack_top(d->current_identities); assert(id); @@ -312,7 +312,6 @@ static int _dfa_init_syntax_type(scf_dfa_t* dfa) scf_logd("n->name: %s\n", n->name); } - scf_logi("\n"); return SCF_DFA_OK; } diff --git a/parse/scf_dfa_union.c b/parse/scf_dfa_union.c index eb6754c..6957bb7 100644 --- a/parse/scf_dfa_union.c +++ b/parse/scf_dfa_union.c @@ -21,7 +21,7 @@ typedef struct { static int _union_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; union_module_data_t* md = d->module_datas[dfa_module_union.index]; scf_lex_word_t* w = words->data[words->size - 1]; @@ -53,7 +53,7 @@ static int _union_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* dat static int _union_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; union_module_data_t* md = d->module_datas[dfa_module_union.index]; scf_lex_word_t* w = words->data[words->size - 1]; scf_type_t* t = NULL; @@ -144,7 +144,7 @@ static int _union_calculate_size(scf_dfa_t* dfa, scf_type_t* s) static int _union_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; union_module_data_t* md = d->module_datas[dfa_module_union.index]; if (_union_calculate_size(dfa, md->current_union) < 0) { @@ -165,7 +165,7 @@ static int _union_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _union_action_var(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; union_module_data_t* md = d->module_datas[dfa_module_union.index]; scf_lex_word_t* w = words->data[words->size - 1]; @@ -190,7 +190,7 @@ static int _union_action_var(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _union_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; union_module_data_t* md = d->module_datas[dfa_module_union.index]; if (md->nb_rbs == md->nb_lbs) { @@ -229,7 +229,7 @@ static int _dfa_init_module_union(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, union, var, scf_dfa_is_identity, _union_action_var); scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; union_module_data_t* md = d->module_datas[dfa_module_union.index]; assert(!md); @@ -248,7 +248,7 @@ static int _dfa_init_module_union(scf_dfa_t* dfa) static int _dfa_fini_module_union(scf_dfa_t* dfa) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; union_module_data_t* md = d->module_datas[dfa_module_union.index]; if (md) { @@ -296,7 +296,6 @@ static int _dfa_init_syntax_union(scf_dfa_t* dfa) scf_dfa_node_add_child(var, semicolon); scf_dfa_node_add_child(rb, semicolon); - scf_logi("\n"); return 0; } diff --git a/parse/scf_dfa_util.h b/parse/scf_dfa_util.h index d8f75fd..693b221 100644 --- a/parse/scf_dfa_util.h +++ b/parse/scf_dfa_util.h @@ -4,6 +4,20 @@ #include"scf_lex_word.h" #include"scf_dfa.h" +static inline int scf_dfa_is_hash(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_HASH == w->type; +} + +static inline int scf_dfa_is_LF(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_LF == w->type; +} + static inline int scf_dfa_is_lp(scf_dfa_t* dfa, void* word) { scf_lex_word_t* w = word; @@ -130,6 +144,13 @@ static inline int scf_dfa_is_include(scf_dfa_t* dfa, void* word) return SCF_LEX_WORD_KEY_INCLUDE == w->type; } +static inline int scf_dfa_is_define(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_KEY_DEFINE == w->type; +} + static inline int scf_dfa_is_vargs(scf_dfa_t* dfa, void* word) { scf_lex_word_t* w = word; diff --git a/parse/scf_dfa_va_arg.c b/parse/scf_dfa_va_arg.c index 752f1c5..0a4dc98 100644 --- a/parse/scf_dfa_va_arg.c +++ b/parse/scf_dfa_va_arg.c @@ -9,9 +9,9 @@ int _type_find_type(scf_dfa_t* dfa, dfa_identity_t* id); static int _va_arg_action_start(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; if (d->current_va_start || d->current_va_arg @@ -33,9 +33,9 @@ static int _va_arg_action_start(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _va_arg_action_arg(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; if (d->current_va_start || d->current_va_arg @@ -60,9 +60,9 @@ static int _va_arg_action_arg(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _va_arg_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; if (d->current_va_start || d->current_va_arg @@ -84,9 +84,9 @@ static int _va_arg_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _va_arg_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; assert(d->current_va_start || d->current_va_arg @@ -100,13 +100,13 @@ static int _va_arg_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _va_arg_action_ap(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; - scf_variable_t* ap; - scf_type_t* t; - scf_node_t* node; + scf_variable_t* ap; + scf_type_t* t; + scf_node_t* node; ap = scf_block_find_variable(parse->ast->current_block, w->text->data); if (!ap) { @@ -141,12 +141,7 @@ static int _va_arg_action_ap(scf_dfa_t* dfa, scf_vector_t* words, void* data) scf_loge("\n"); return SCF_DFA_ERROR; } -#if 0 - if (ap->arg_flag) { - ap->nb_pointers = 1; - ap->size = sizeof(void*); - } -#endif + return SCF_DFA_NEXT_WORD; } @@ -159,14 +154,14 @@ static int _va_arg_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _va_arg_action_fmt(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; - scf_function_t* f; - scf_variable_t* fmt; - scf_variable_t* arg; - scf_node_t* node; + scf_function_t* f; + scf_variable_t* fmt; + scf_variable_t* arg; + scf_node_t* node; fmt = scf_block_find_variable(parse->ast->current_block, w->text->data); if (!fmt) { @@ -230,12 +225,11 @@ static int _va_arg_action_fmt(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _va_arg_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; if (d->current_va_arg) { - if (d->current_va_arg->nb_nodes != 1) { scf_loge("\n"); return SCF_DFA_ERROR; @@ -302,8 +296,8 @@ static int _dfa_init_module_va_arg(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, va_arg, comma, scf_dfa_is_comma, _va_arg_action_comma); SCF_DFA_MODULE_NODE(dfa, va_arg, semicolon, scf_dfa_is_semicolon, _va_arg_action_semicolon); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; d->current_va_start = NULL; d->current_va_arg = NULL; @@ -349,7 +343,6 @@ static int _dfa_init_syntax_va_arg(scf_dfa_t* dfa) scf_dfa_node_add_child(identity, rp); scf_dfa_node_add_child(star, rp); - scf_logi("\n"); return 0; } @@ -359,4 +352,3 @@ scf_dfa_module_t dfa_module_va_arg = .init_module = _dfa_init_module_va_arg, .init_syntax = _dfa_init_syntax_va_arg, }; - diff --git a/parse/scf_dfa_var.c b/parse/scf_dfa_var.c index 00d0282..b261507 100644 --- a/parse/scf_dfa_var.c +++ b/parse/scf_dfa_var.c @@ -40,18 +40,17 @@ int _check_recursive(scf_type_t* parent, scf_type_t* child, scf_lex_word_t* w) return SCF_DFA_OK; } -static int _var_add_var(scf_dfa_t* dfa, dfa_parse_data_t* d) +static int _var_add_var(scf_dfa_t* dfa, dfa_data_t* d) { - scf_parse_t* parse = dfa->priv; - - dfa_identity_t* id = scf_stack_top(d->current_identities); + scf_parse_t* parse = dfa->priv; + dfa_identity_t* id = scf_stack_top(d->current_identities); if (id && id->identity) { scf_logd("d->current_identity: %p\n", id->identity); - scf_variable_t* var = scf_scope_find_variable(parse->ast->current_block->scope, id->identity->text->data); - if (var) { + scf_variable_t* v = scf_scope_find_variable(parse->ast->current_block->scope, id->identity->text->data); + if (v) { scf_loge("repeated declare var '%s', line: %d\n", id->identity->text->data, id->identity->line); return SCF_DFA_ERROR; } @@ -123,27 +122,27 @@ static int _var_add_var(scf_dfa_t* dfa, dfa_parse_data_t* d) return SCF_DFA_ERROR; } - var = SCF_VAR_ALLOC_BY_TYPE(id->identity, id0->type, id0->const_flag, id0->nb_pointers, id0->func_ptr); - if (!var) { + v = SCF_VAR_ALLOC_BY_TYPE(id->identity, id0->type, id0->const_flag, id0->nb_pointers, id0->func_ptr); + if (!v) { scf_loge("alloc var failed\n"); return SCF_DFA_ERROR; } - var->local_flag = local_flag; - var->global_flag = global_flag; - var->member_flag = member_flag; + v->local_flag = local_flag; + v->global_flag = global_flag; + v->member_flag = member_flag; - var->static_flag = id0->static_flag; - var->extern_flag = id0->extern_flag; + v->static_flag = id0->static_flag; + v->extern_flag = id0->extern_flag; scf_logi("type: %d, nb_pointers: %d,nb_dimentions: %d, var: %s,line:%d,pos:%d, local: %d, global: %d, member: %d, extern: %d, static: %d\n\n", - var->type, var->nb_pointers, var->nb_dimentions, - var->w->text->data, var->w->line, var->w->pos, - var->local_flag, var->global_flag, var->member_flag, - var->extern_flag, var->static_flag); + v->type, v->nb_pointers, v->nb_dimentions, + v->w->text->data, v->w->line, v->w->pos, + v->local_flag, v->global_flag, v->member_flag, + v->extern_flag, v->static_flag); - scf_scope_push_var(parse->ast->current_block->scope, var); + scf_scope_push_var(parse->ast->current_block->scope, v); - d->current_var = var; + d->current_var = v; d->current_var_w = id->identity; id0->nb_pointers = 0; id0->const_flag = 0; @@ -158,7 +157,7 @@ static int _var_add_var(scf_dfa_t* dfa, dfa_parse_data_t* d) return 0; } -static int _var_init_expr(scf_dfa_t* dfa, dfa_parse_data_t* d, int semi_flag) +static int _var_init_expr(scf_dfa_t* dfa, dfa_data_t* d, int semi_flag) { scf_parse_t* parse = dfa->priv; @@ -205,8 +204,8 @@ static int _var_init_expr(scf_dfa_t* dfa, dfa_parse_data_t* d, int semi_flag) static int _var_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; if (_var_add_var(dfa, d) < 0) { scf_loge("add var error\n"); @@ -227,9 +226,9 @@ static int _var_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _var_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - dfa_identity_t* id = NULL; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + dfa_identity_t* id = NULL; d->var_semicolon_flag = 0; @@ -238,8 +237,8 @@ static int _var_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data return SCF_DFA_ERROR; } - d->nb_lss = 0; - d->nb_rss = 0; + d->nb_lss = 0; + d->nb_rss = 0; id = scf_stack_pop(d->current_identities); assert(id && id->type); @@ -270,8 +269,8 @@ static int _var_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data static int _var_action_assign(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; if (_var_add_var(dfa, d) < 0) { scf_loge("add var error\n"); @@ -281,7 +280,7 @@ static int _var_action_assign(scf_dfa_t* dfa, scf_vector_t* words, void* data) d->nb_lss = 0; d->nb_rss = 0; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_lex_word_t* w = words->data[words->size - 1]; if (!d->current_var) { scf_loge("\n"); @@ -327,8 +326,8 @@ static int _var_action_assign(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _var_action_ls(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; if (_var_add_var(dfa, d) < 0) { scf_loge("add var error\n"); @@ -356,8 +355,8 @@ static int _var_action_ls(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _var_action_rs(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; d->nb_rss++; @@ -450,8 +449,6 @@ static int _dfa_init_syntax_var(scf_dfa_t* dfa) scf_dfa_node_add_child(init_data_rb, semicolon); scf_dfa_node_add_child(identity, semicolon); - - printf("%s(),%d\n\n", __func__, __LINE__); return 0; } diff --git a/parse/scf_dfa_while.c b/parse/scf_dfa_while.c index 086b652..77aa343 100644 --- a/parse/scf_dfa_while.c +++ b/parse/scf_dfa_while.c @@ -25,11 +25,11 @@ static int _while_is_end(scf_dfa_t* dfa, void* word) static int _while_action_while(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_while.index]; + scf_node_t* _while = scf_node_alloc(w, SCF_OP_WHILE, NULL); - scf_node_t* _while = scf_node_alloc(w, SCF_OP_WHILE, NULL); if (!_while) { scf_loge("node alloc failed\n"); return SCF_DFA_ERROR; @@ -59,7 +59,7 @@ static int _while_action_while(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _while_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_while.index]; dfa_while_data_t* wd = scf_stack_top(s); @@ -75,9 +75,9 @@ static int _while_action_lp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _while_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_while.index]; - dfa_while_data_t* wd = scf_stack_top(s); + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_while.index]; + dfa_while_data_t* wd = scf_stack_top(s); SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "while_lp_stat"), SCF_DFA_HOOK_POST); @@ -89,7 +89,7 @@ static int _while_action_lp_stat(scf_dfa_t* dfa, scf_vector_t* words, void* data static int _while_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; + dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; scf_stack_t* s = d->module_datas[dfa_module_while.index]; dfa_while_data_t* wd = scf_stack_top(s); @@ -123,10 +123,10 @@ static int _while_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _while_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = data; - scf_stack_t* s = d->module_datas[dfa_module_while.index]; - dfa_while_data_t* wd = scf_stack_pop(s); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_while.index]; + dfa_while_data_t* wd = scf_stack_pop(s); assert(parse->ast->current_block == wd->parent_block); @@ -152,9 +152,9 @@ static int _dfa_init_module_while(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, while, _while, scf_dfa_is_while, _while_action_while); - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_while.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_while.index]; assert(!s); @@ -171,9 +171,9 @@ static int _dfa_init_module_while(scf_dfa_t* dfa) static int _dfa_fini_module_while(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_parse_data_t* d = parse->dfa_data; - scf_stack_t* s = d->module_datas[dfa_module_while.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_while.index]; if (s) { scf_stack_free(s); @@ -206,7 +206,6 @@ static int _dfa_init_syntax_while(scf_dfa_t* dfa) // while body scf_dfa_node_add_child(rp, block); - scf_logi("\n"); return 0; } @@ -219,4 +218,3 @@ scf_dfa_module_t dfa_module_while = .fini_module = _dfa_fini_module_while, }; - diff --git a/parse/scf_parse.c b/parse/scf_parse.c index a191965..e7059b6 100644 --- a/parse/scf_parse.c +++ b/parse/scf_parse.c @@ -157,24 +157,22 @@ static int _scf_parse_add_sym(scf_parse_t* parse, const char* name, int scf_parse_file(scf_parse_t* parse, const char* path) { - assert(parse); - - scf_block_t* root = parse->ast->root_block; - scf_block_t* b = NULL; - - int i; - for (i = 0; i < root->node.nb_nodes; i++) { - b = (scf_block_t*)root->node.nodes[i]; + if (!parse || !path) + return -EINVAL; - if (SCF_OP_BLOCK != b->node.type) - continue; + scf_lex_t* lex = parse->lex_list; - if (!strcmp(b->name->data, path)) + while (lex) { + if (!strcmp(lex->file->data, path)) break; + + lex = lex->next; } - if (i < root->node.nb_nodes) + if (lex) { + parse->lex = lex; return 0; + } if (scf_lex_open(&parse->lex, path) < 0) { scf_loge("\n"); @@ -182,16 +180,19 @@ int scf_parse_file(scf_parse_t* parse, const char* path) } scf_ast_add_file_block(parse->ast, path); + parse->lex->next = parse->lex_list; + parse->lex_list = parse->lex; - dfa_parse_data_t* d = parse->dfa_data; + dfa_data_t* d = parse->dfa_data; + scf_lex_word_t* w = NULL; - while (1) { - scf_lex_word_t* w = NULL; + int ret = 0; - int ret = scf_lex_pop_word(parse->lex, &w); + while (1) { + ret = scf_lex_pop_word(parse->lex, &w); if (ret < 0) { scf_loge("lex pop word failed\n"); - return ret; + break; } if (SCF_LEX_WORD_EOF == w->type) { @@ -215,13 +216,16 @@ int scf_parse_file(scf_parse_t* parse, const char* path) assert(!d->expr); - if (scf_dfa_parse_word(parse->dfa, w, d) < 0) { + ret = scf_dfa_parse_word(parse->dfa, w, d); + if (ret < 0) { scf_loge("dfa parse failed\n"); - return -1; + break; } } - return 0; + fclose(parse->lex->fp); + parse->lex->fp = NULL; + return ret; } static int _debug_abbrev_find_by_tag(const void* v0, const void* v1) diff --git a/parse/scf_parse.h b/parse/scf_parse.h index 8ef3d05..c04ad9c 100644 --- a/parse/scf_parse.h +++ b/parse/scf_parse.h @@ -5,10 +5,10 @@ #include"scf_ast.h" #include"scf_dfa.h" #include"scf_stack.h" -#include"scf_dwarf_def.h" +#include"scf_dwarf.h" -typedef struct scf_parse_s scf_parse_t; -typedef struct dfa_parse_data_s dfa_parse_data_t; +typedef struct scf_parse_s scf_parse_t; +typedef struct dfa_data_s dfa_data_t; #define SCF_SHNDX_TEXT 1 #define SCF_SHNDX_RODATA 2 @@ -21,17 +21,18 @@ typedef struct dfa_parse_data_s dfa_parse_data_t; struct scf_parse_s { - scf_lex_t* lex; + scf_lex_t* lex_list; + scf_lex_t* lex; - scf_ast_t* ast; + scf_ast_t* ast; - scf_dfa_t* dfa; - dfa_parse_data_t* dfa_data; + scf_dfa_t* dfa; + dfa_data_t* dfa_data; scf_vector_t* symtab; scf_vector_t* global_consts; - scf_dwarf_debug_t* debug; + scf_dwarf_t* debug; }; typedef struct { @@ -56,7 +57,7 @@ typedef struct { } dfa_identity_t; -struct dfa_parse_data_s { +struct dfa_data_s { void** module_datas; scf_expr_t* expr; @@ -114,8 +115,8 @@ struct dfa_parse_data_s { int scf_parse_dfa_init(scf_parse_t* parse); -int scf_parse_open(scf_parse_t** pparse); -int scf_parse_close(scf_parse_t* parse); +int scf_parse_open (scf_parse_t** pparse); +int scf_parse_close(scf_parse_t* parse); int scf_parse_file(scf_parse_t* parse, const char* path); @@ -125,4 +126,3 @@ int _find_global_var(scf_node_t* node, void* arg, scf_vector_t* vec); int _find_function (scf_node_t* node, void* arg, scf_vector_t* vec); #endif - diff --git a/util/scf_list.h b/util/scf_list.h index 4b5812c..3462669 100644 --- a/util/scf_list.h +++ b/util/scf_list.h @@ -65,6 +65,16 @@ static inline void scf_list_add_front(scf_list_t* h, scf_list_t* n) }\ } while(0) +#define scf_slist_clear(h, type, next, type_free) \ + do { \ + while (h) { \ + type* p = h; \ + h = p->next; \ + type_free(p); \ + p = NULL; \ + } \ + } while (0) + #define scf_list_mov(dst, src, type, member) \ do {\ scf_list_t* l;\ @@ -78,4 +88,3 @@ static inline void scf_list_add_front(scf_list_t* h, scf_list_t* n) } while(0) #endif - diff --git a/util/scf_rbtree.h b/util/scf_rbtree.h index c8df171..72324a4 100644 --- a/util/scf_rbtree.h +++ b/util/scf_rbtree.h @@ -59,6 +59,4 @@ int scf_rbtree_foreach(scf_rbtree_t* tree, scf_rbtree_node_t* roo int scf_rbtree_foreach_reverse(scf_rbtree_t* tree, scf_rbtree_node_t* root, void* data, scf_rbtree_node_do_pt done); - #endif - diff --git a/util/scf_string.c b/util/scf_string.c index a0b4587..d0ee3d0 100644 --- a/util/scf_string.c +++ b/util/scf_string.c @@ -103,9 +103,6 @@ void scf_string_print_bin(scf_string_t* s) int scf_string_cmp(const scf_string_t* s0, const scf_string_t* s1) { - if (!s0 || !s1 || !s0->data || !s1->data) - return -EINVAL; - if (s0->len < s1->len) return -1; @@ -116,17 +113,11 @@ int scf_string_cmp(const scf_string_t* s0, const scf_string_t* s1) int scf_string_cmp_cstr(const scf_string_t* s0, const char* str) { - if (!s0 || !s0->data || !str) - return -EINVAL; - return scf_string_cmp_cstr_len(s0, str, strlen(str)); } int scf_string_cmp_cstr_len(const scf_string_t* s0, const char* str, size_t len) { - if (!s0 || !s0->data || !str) - return -EINVAL; - scf_string_t s1; s1.capacity = -1; s1.len = len; diff --git a/util/scf_vector.h b/util/scf_vector.h index d8abe21..95592f0 100644 --- a/util/scf_vector.h +++ b/util/scf_vector.h @@ -29,23 +29,23 @@ static inline scf_vector_t* scf_vector_alloc() return v; } -static inline scf_vector_t* scf_vector_clone(scf_vector_t* origin) +static inline scf_vector_t* scf_vector_clone(scf_vector_t* src) { - scf_vector_t* clone = calloc(1, sizeof(scf_vector_t)); - if (!clone) + scf_vector_t* dst = calloc(1, sizeof(scf_vector_t)); + if (!dst) return NULL; - clone->data = calloc(origin->capacity, sizeof(void*)); - if (!clone->data) { - free(clone); - clone = NULL; + dst->data = calloc(src->capacity, sizeof(void*)); + if (!dst->data) { + free(dst); return NULL; } - clone->capacity = origin->capacity; - clone->size = origin->size; - memcpy(clone->data, origin->data, origin->size * sizeof(void*)); - return clone; + dst->capacity = src->capacity; + dst->size = src->size; + + memcpy(dst->data, src->data, src->size * sizeof(void*)); + return dst; } static inline int scf_vector_cat(scf_vector_t* dst, scf_vector_t* src)