From 3689331b9a2eada98d0e12cf658fadcce936c19a Mon Sep 17 00:00:00 2001 From: Sayon Date: Sun, 3 Aug 2014 20:41:08 +0400 Subject: [PATCH] refactoring, bugfixes, error handling in parser --- dwarf/dwarf-compiler/AST.c | 215 ------------------ dwarf/dwarf-compiler/AST.h | 2 - dwarf/dwarf-compiler/bytecode.h | 7 +- dwarf/dwarf-compiler/dwarf-compiler.vcxproj | 15 +- .../dwarf-compiler.vcxproj.filters | 37 ++- dwarf/dwarf-compiler/lang_types.h | 10 - dwarf/dwarf-compiler/lexer.c | 43 ++-- dwarf/dwarf-compiler/lexer.h | 5 +- dwarf/dwarf-compiler/main.c | 13 +- dwarf/dwarf-compiler/parser.c | 131 ++++++----- dwarf/dwarf-compiler/parser.h | 5 +- dwarf/dwarf-compiler/token_list.c | 30 +++ dwarf/dwarf-compiler/token_list.h | 16 ++ dwarf/dwarf-compiler/tokens.c | 50 +--- dwarf/dwarf-compiler/tokens.h | 22 +- dwarf/dwarf.sln | 2 + 16 files changed, 204 insertions(+), 399 deletions(-) delete mode 100755 dwarf/dwarf-compiler/AST.c delete mode 100755 dwarf/dwarf-compiler/lang_types.h create mode 100755 dwarf/dwarf-compiler/token_list.c create mode 100755 dwarf/dwarf-compiler/token_list.h diff --git a/dwarf/dwarf-compiler/AST.c b/dwarf/dwarf-compiler/AST.c deleted file mode 100755 index 37b5bf5..0000000 --- a/dwarf/dwarf-compiler/AST.c +++ /dev/null @@ -1,215 +0,0 @@ -#include - - -#include "AST.h" -#include - -void ast_visit( ast_t* node, void (before)( ast_t* node ), void (after)( ast_t* node )) { - if ( node == NULL ) return; - - if ( before != NULL ) before( node ); - switch ( node->type ) - { - case AST_IF: - ast_visit( node->attributes.as_if.cond, before, after ); - ast_visit( node->attributes.as_if.yes, before, after ); - ast_visit( node->attributes.as_if.no, before, after ); - break; - case AST_WHILE: - ast_visit( node->attributes.as_while.cond, before, after ); - ast_visit( node->attributes.as_while.body, before, after ); - break; - case AST_ASSIGN: - ast_visit( node->attributes.as_assign.value, before, after ); - break; - - case AST_PLUS: - case AST_MINUS: - case AST_MULT: - case AST_DIVIDE: - ast_visit( node->attributes.as_binop.left, before, after ); - ast_visit( node->attributes.as_binop.right, before, after ); - break; - case AST_SEQ: - ast_visit( node->attributes.as_seq.first, before, after ); - ast_visit( node->attributes.as_seq.second, before, after ); - break; - case AST_PRINT: - ast_visit( node->attributes.as_unop.expr, before, after ); - break; - case AST_IDENTIFIER: - case AST_NUMBER: - case AST_SKIP: - break; - default: - fprintf( stderr, "Can't deallocate memory for such type of AST nodes!" ); - } - - after( node ); -} - - -static void ast_node_free( ast_t* node ) { - free( node ); -} - -void ast_print( ast_t* node, FILE* out ) { - if ( node == NULL ) { fprintf( stderr, "Pointer to AST is null!" ); return; } - switch ( node-> type ) - { - case AST_IF: - fprintf( out, "if (" ); - ast_print( node->attributes.as_if.cond, out ); - fprintf( out, ") then {" ); - ast_print( node->attributes.as_if.yes, out ); - fprintf( out, "} else {" ); - ast_print( node->attributes.as_if.no, out ); - fprintf( out, "}" ); - break; - case AST_WHILE: - fprintf( out, "while (" ); - ast_print( node->attributes.as_while.cond, out ); - fprintf( out, ") {" ); - ast_print( node->attributes.as_while.body, out ); - fprintf( out, "}" ); - break; - case AST_ASSIGN: - ast_print( node->attributes.as_assign.identifier, out ); - fprintf( out, " := " ); - ast_print( node->attributes.as_assign.value, out ); - break; - case AST_PLUS: - fprintf( out, "( " ); - ast_print( node->attributes.as_binop.left, out ); - fprintf( out, " + " ); - ast_print( node->attributes.as_binop.right, out ); - fprintf( out, " )" ); - break; - case AST_MINUS: - fprintf( out, "( " ); - ast_print( node->attributes.as_binop.left, out ); - fprintf( out, " - " ); - ast_print( node->attributes.as_binop.right, out ); - fprintf( out, " )" ); - break; - case AST_MULT: - fprintf( out, "( " ); - ast_print( node->attributes.as_binop.left, out ); - fprintf( out, " * " ); - ast_print( node->attributes.as_binop.right, out ); - fprintf( out, " )" ); - break; - case AST_DIVIDE: - fprintf( out, "( " ); - ast_print( node->attributes.as_binop.left, out ); - fprintf( out, " / " ); - ast_print( node->attributes.as_binop.right, out ); - fprintf( out, " )" ); - break; - case AST_SEQ: - ast_print( node->attributes.as_seq.first, out ); - fprintf( out, " ; " ); - ast_print( node->attributes.as_seq.second, out ); - break; - case AST_PRINT: - fprintf( out, "print( " ); - ast_print( node->attributes.as_unop.expr, out ); - fprintf( out, " )" ); - break; - case AST_NUMBER: - fprintf( out, LANG_INT_PRINTF_SPECIFICATOR, node->attributes.as_number ); - break; - case AST_IDENTIFIER: - fprintf( out, "%s", node->attributes.as_identifier.name ); - break; - case AST_SKIP: - fprintf( out, "skip" ); - break; - default: - break; - } -} - -void ast_free( ast_t* node ) { - if ( node != NULL ) ast_visit( node, NULL, ast_node_free ); -} - - -//helper method, inaccessible from outside. -static ast_t* ast_binop( ast_t* left, ast_t* right, ast_node_type_t operation_type ) -{ - ast_t* node = (ast_t*) malloc( sizeof( ast_t* ) ); - node->type = operation_type; - node->attributes.as_binop.left = left; - node->attributes.as_binop.right = right; - return node; -} - - -ast_t* ast_number( lang_int_t num ) { - ast_t* node = (ast_t*) malloc( sizeof( ast_t* ) ); - node->type = AST_NUMBER; - node->attributes.as_number = num; - return node; -} - - -ast_t* ast_plus( ast_t* left, ast_t* right ) { - return ast_binop( left, right, AST_PLUS ); -} - -ast_t* ast_minus( ast_t* left, ast_t* right ) { - return ast_binop( left, right, AST_MINUS ); -} - -ast_t* ast_mult( ast_t* left, ast_t* right ) { - return ast_binop( left, right, AST_MULT ); -} - -ast_t* ast_div( ast_t* left, ast_t* right ) { - return ast_binop( left, right, AST_DIVIDE ); -} - - -ast_t* ast_seq( ast_t* left, ast_t* right) -{ - ast_t* node = (ast_t*) malloc( sizeof( ast_t* ) ); - node->type = AST_SEQ; - node->attributes.as_binop.left = left; - node->attributes.as_binop.right = right; - return node; -} -ast_t* ast_identifier( char* name ) { - ast_t* node; - node = (ast_t*) malloc( sizeof( ast_t ) ); - node->type = AST_IDENTIFIER; - strncpy( node->attributes.as_identifier.name, name, IDENTIFIER_MAX_LENGTH ); - return node; -} - -ast_t* ast_if_then_else( ast_t* cond, ast_t* yes, ast_t* no ) { - ast_t* node; - node = (ast_t*) malloc( sizeof( ast_t ) ); - node->type = AST_IF; - node->attributes.as_if.cond = cond; - node->attributes.as_if.yes = yes; - node->attributes.as_if.no = no; - return node; -} - -ast_t* ast_while( ast_t* cond, ast_t* body ) { - ast_t* node; - node = (ast_t*) malloc( sizeof( ast_t ) ); - node->type = AST_WHILE; - node->attributes.as_while.cond = cond; - node->attributes.as_while.body = body; - return node; -} -ast_t* ast_assignment( ast_t* ident, ast_t* expr ) { - ast_t* node; - node = (ast_t*) malloc( sizeof( ast_t ) ); - node->type = AST_ASSIGN; - node->attributes.as_assign.identifier = ident; - node->attributes.as_assign.value = expr; - return node; -} \ No newline at end of file diff --git a/dwarf/dwarf-compiler/AST.h b/dwarf/dwarf-compiler/AST.h index 98f805b..3c26e43 100755 --- a/dwarf/dwarf-compiler/AST.h +++ b/dwarf/dwarf-compiler/AST.h @@ -19,8 +19,6 @@ typedef enum { AST_PRINT } ast_node_type_t; - - typedef struct { struct ast_t* first; struct ast_t* second; diff --git a/dwarf/dwarf-compiler/bytecode.h b/dwarf/dwarf-compiler/bytecode.h index 93f0fdc..8971e38 100755 --- a/dwarf/dwarf-compiler/bytecode.h +++ b/dwarf/dwarf-compiler/bytecode.h @@ -42,9 +42,7 @@ typedef signed long addr_offset_t; typedef short reg_num_t; extern size_t bytecode_size[]; - - - + typedef struct bytecode_t { opcode_t opcode; @@ -68,8 +66,7 @@ typedef struct { -bytecode_t* emit_bytecode( bytecode_builder_t* builder, opcode_t opcode ); - +bytecode_t* emit_bytecode( bytecode_builder_t* builder, opcode_t opcode ); /* Helpers to emit specific bytecodes with parameters */ diff --git a/dwarf/dwarf-compiler/dwarf-compiler.vcxproj b/dwarf/dwarf-compiler/dwarf-compiler.vcxproj index e40224e..e2d6154 100755 --- a/dwarf/dwarf-compiler/dwarf-compiler.vcxproj +++ b/dwarf/dwarf-compiler/dwarf-compiler.vcxproj @@ -78,17 +78,26 @@ - + + + - + + + - + + + + + + diff --git a/dwarf/dwarf-compiler/dwarf-compiler.vcxproj.filters b/dwarf/dwarf-compiler/dwarf-compiler.vcxproj.filters index 2b9acc5..2c1a250 100755 --- a/dwarf/dwarf-compiler/dwarf-compiler.vcxproj.filters +++ b/dwarf/dwarf-compiler/dwarf-compiler.vcxproj.filters @@ -18,16 +18,28 @@ Header Files - + Header Files - + Header Files - + Header Files - + + Header Files + + + Header Files + + + Header Files + + + Header Files + + Header Files @@ -41,7 +53,22 @@ Source Files - + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + Source Files diff --git a/dwarf/dwarf-compiler/lang_types.h b/dwarf/dwarf-compiler/lang_types.h deleted file mode 100755 index 6d62b1b..0000000 --- a/dwarf/dwarf-compiler/lang_types.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef LANG_TYPES -#define LANG_TYPES -#include - -typedef int64_t lang_int_t; -#define LANG_INT_PRINTF_SPECIFICATOR "%ld" - - - -#endif \ No newline at end of file diff --git a/dwarf/dwarf-compiler/lexer.c b/dwarf/dwarf-compiler/lexer.c index ec534e7..c8a51cd 100755 --- a/dwarf/dwarf-compiler/lexer.c +++ b/dwarf/dwarf-compiler/lexer.c @@ -1,10 +1,24 @@ #include #include #include -#include "tokens.h" #include #include "common.h" +#include "token_list.h" + + + +static void token_list_quick_append( token_t* token, token_list_t** first, token_list_t** last ) { + token_list_t* item; + + item = token_list_create( token ); + + if ( *first == NULL ) *first = *last = item; + else { + (**last).next = item; + (*last) = item; + } +} static bool_t starts_with( const char *str, const char *pattern ) { @@ -76,11 +90,11 @@ size_t get_token( char* str, token_t* token ) { return 0; } -token_list_t tokenize(char* str) { +token_list_t* tokenize( char* const str ) { char* start = str; - token_list_t tokens = token_list_create(); - - if ( str == NULL ) return tokens; + token_list_t* tokens = NULL; + token_list_t* last_token = NULL; + if ( str == NULL ) return NULL; for( ;; ) { @@ -89,20 +103,11 @@ token_list_t tokenize(char* str) { start = skip_whitespaces( start ); token_size = get_token( start, &token ); if ( token_size == 0 ) break; - token_list_add( &tokens, token ); + token.start_position = start - str; + token_list_quick_append( &token, &tokens, &last_token ); start += token_size; } - token_list_add( &tokens, token_EOF ); + token_list_quick_append( &token_EOF, &tokens, &last_token ); + return tokens; -} -// -//int main( int argc, char** argv ) { -// token_list_t list; -// -// list = tokenize( "4 + 5 ; if (3) then 4;" ); -// -// token_list_foreach( &list, token_print ); -// system("PAUSE"); -// return 0; -// -//} \ No newline at end of file +} \ No newline at end of file diff --git a/dwarf/dwarf-compiler/lexer.h b/dwarf/dwarf-compiler/lexer.h index b025669..fc68438 100755 --- a/dwarf/dwarf-compiler/lexer.h +++ b/dwarf/dwarf-compiler/lexer.h @@ -1,9 +1,8 @@ #ifndef LEXER_H #define LEXER_H -#include "tokens.h" - -token_list_t tokenize(char* str) ; +#include "token_list.h" +token_list_t* tokenize(char* str) ; #endif \ No newline at end of file diff --git a/dwarf/dwarf-compiler/main.c b/dwarf/dwarf-compiler/main.c index 0fdd7c6..ac89b84 100755 --- a/dwarf/dwarf-compiler/main.c +++ b/dwarf/dwarf-compiler/main.c @@ -8,19 +8,18 @@ int main( int argc, char** argv ) { - token_list_t list; + token_list_t* list; bytecode_builder_t bc; ast_t* tree; - - list = tokenize( "x := 4; if (x) then y:=2 else z := 5" ); - tree = parse( list ); + char* program = "x := 4; if (x) then y:=2 5 else z := 5" ; + list = tokenize( program ); + tree = parse( list, program ); bc = generate_from_ast( tree ); puts(""); bytecode_prettyprint( bc.first ); - //token_list_destroy( &list ); - ast_free( tree ); - getchar(); + token_list_free( list ); + ast_free( tree ); } diff --git a/dwarf/dwarf-compiler/parser.c b/dwarf/dwarf-compiler/parser.c index 4217121..afb9f0b 100755 --- a/dwarf/dwarf-compiler/parser.c +++ b/dwarf/dwarf-compiler/parser.c @@ -1,3 +1,5 @@ +#include +#include "common.h" #include "lexer.h" #include "AST.h" #include "parser.h" @@ -22,28 +24,37 @@ statement ; statement */ -static ast_t* statements( token_list_item_t** stream ); -static ast_t* statement( token_list_item_t** stream ); -static ast_t* assignment( token_list_item_t** stream ); -static ast_t* if_then_else( token_list_item_t** stream ); -static ast_t* while_cond( token_list_item_t** stream ); -static ast_t* expression( token_list_item_t** stream ); -static ast_t* expression1( token_list_item_t** stream ); -static ast_t* atom( token_list_item_t** stream ); + +static void error_if_null( ast_t* node, token_list_t** stream, const char* const program_code ) { + size_t pos; + if ( node != NULL ) return; + pos = (**stream).token.start_position ; + fprintf( stderr, "Error while parsing program at position %d : \n\n%.20s\n", pos, program_code + pos); + exit( EXIT_FAILURE ); +} + +static ast_t* statements( token_list_t** stream, const char* const code ); +static ast_t* statement( token_list_t** stream, const char* const code ); +static ast_t* assignment( token_list_t** stream, const char* const code ); +static ast_t* if_then_else( token_list_t** stream, const char* const code ); +static ast_t* while_cond( token_list_t** stream, const char* const code ); +static ast_t* expression( token_list_t** stream, const char* const code ); +static ast_t* expression1( token_list_t** stream, const char* const code ); +static ast_t* atom( token_list_t** stream, const char* const code ); /* Helper methods */ -static bool_t is_EOF( token_list_item_t** stream ) { +static bool_t is_EOF( token_list_t** stream ) { return (**stream).token.type == TOK_EOF; } -static bool_t advance( token_list_item_t** stream ) { +static bool_t advance( token_list_t** stream ) { if ( ! is_EOF( stream ) ) { (*stream) = (**stream).next; return TRUE; } else return FALSE; } -static token_t* accept( token_list_item_t** stream, token_type_t token_type ) +static token_t* accept( token_list_t** stream, token_type_t token_type ) { token_t* token; if ( (**stream).token.type == token_type) @@ -57,105 +68,104 @@ static token_t* accept( token_list_item_t** stream, token_type_t token_type ) /* Recursive-descent parser */ -static ast_t* expression( token_list_item_t** stream ) { +static ast_t* expression( token_list_t** stream, const char* const code ) { ast_t* lhs, *rhs; - lhs = expression1( stream ); + lhs = expression1( stream, code ); + error_if_null( lhs, stream, code ); if ( accept( stream, TOK_PLUS ) ) { - rhs = expression( stream ); + rhs = expression( stream, code ); error_if_null( rhs, stream, code ); return ast_plus( lhs, rhs ); } else if ( accept( stream, TOK_MINUS ) ) { - rhs = expression( stream ); + rhs = expression( stream, code ); error_if_null( rhs, stream, code ); return ast_plus( lhs, rhs ); } else return lhs; } -static ast_t* expression1( token_list_item_t** stream ) { +static ast_t* expression1( token_list_t** stream, const char* const code ) { ast_t* lhs, *rhs; - lhs = atom( stream ); + lhs = atom( stream, code ); + error_if_null( lhs, stream, code ); if ( accept( stream, TOK_MULT ) ) { - rhs = expression1( stream ); + rhs = expression1( stream, code ); error_if_null( rhs, stream, code ); return ast_mult( lhs, rhs ); } else if ( accept( stream, TOK_DIVIDE ) ) { - rhs = expression1( stream ); + rhs = expression1( stream, code ); error_if_null( rhs, stream, code ); return ast_div( lhs, rhs ); } else return lhs; } -static ast_t* atom( token_list_item_t** stream ) { - token_t* token; +static ast_t* atom( token_list_t** stream, const char* const code ) { + token_t* token = NULL; + ast_t* expr = NULL; if( token = accept( stream, TOK_NUM )) return ast_number( token->data.integer ); else if ( token = accept( stream, TOK_IDENT ) ) return ast_identifier( token->data.ident ) ; - else if ( accept( stream, TOK_LPAR ) ) - { - ast_t* expr = expression( stream ); - if ( accept( stream, TOK_RPAR ) ) - return expr; - else ast_free( expr ); - } - fprintf( stderr, "Expecting a number or an expression between parentheses"); + else if ( accept( stream, TOK_LPAR ) + && ( expr = expression( stream, code ) ) + && accept( stream, TOK_RPAR ) ) + return expr; + + error_if_null( expr, stream, code ); return NULL; } -static ast_t* statements( token_list_item_t** stream ){ +static ast_t* statements( token_list_t** stream, const char* const code ){ ast_t* lhs, *rhs; - lhs = statement( stream ); + lhs = statement( stream, code ); if ( accept( stream, TOK_SEMICOLON ) ) { - rhs = statements( stream ); - return ast_seq( lhs, rhs ); + rhs = statements( stream, code ); + if ( rhs == NULL ) return lhs; else return ast_seq( lhs, rhs ); } else return lhs; } -static ast_t* statement( token_list_item_t** stream ) +static ast_t* statement( token_list_t** stream, const char* const code ) { ast_t* result; - if ( result = assignment( stream ) ) return result; - else if ( accept( stream, TOK_LBRACE ) ) { - result = statements( stream ); - accept( stream, TOK_RBRACE ); - return result; - } - else if (result = if_then_else( stream ) ) return result; - else if (result = while_cond( stream ) ) return result; - else return NULL; + if ( result = assignment( stream, code ) ) return result; + else if ( accept( stream, TOK_LBRACE ) + && ( result = statements( stream, code ) ) + && accept( stream, TOK_RBRACE ) ) return result; + else if ( result = if_then_else( stream, code ) ) return result; + else if ( result = while_cond( stream, code ) ) return result; + error_if_null( result, stream, code ); + return NULL; } -static ast_t* assignment( token_list_item_t** stream ) { +static ast_t* assignment( token_list_t** stream, const char* const code ) { ast_t* expr; token_t* ident; - if ( ident = accept( stream, TOK_IDENT )) { - if ( accept( stream, TOK_ASSIGN ) && ( expr = expression( stream ) ) ) - return ast_assignment( ast_identifier( ident->data.ident ), expr ); - else return NULL; - } + if ( ( ident = accept( stream, TOK_IDENT ) ) + && accept( stream, TOK_ASSIGN ) + && ( expr = expression( stream, code ) ) ) + return ast_assignment( ast_identifier( ident->data.ident ), expr ); else return NULL; -} +} -static ast_t* if_then_else( token_list_item_t** stream ) { +static ast_t* if_then_else( token_list_t** stream, const char* const code ) { ast_t* cond = NULL; ast_t* yes = NULL; ast_t* no = NULL; if ( accept( stream, TOK_IF ) && accept( stream, TOK_LPAR ) && - (cond = expression( stream )) && + (cond = expression( stream, code )) && accept( stream, TOK_RPAR ) && accept( stream, TOK_THEN ) && - (yes = statement( stream )) && + (yes = statement( stream, code )) && accept( stream, TOK_ELSE ) && - (no = statement( stream )) ) + (no = statement( stream, code )) ) return ast_if_then_else( cond, yes, no ); ast_free( cond ); @@ -164,14 +174,14 @@ static ast_t* if_then_else( token_list_item_t** stream ) { return NULL; } -static ast_t* while_cond( token_list_item_t** stream ) { +static ast_t* while_cond( token_list_t** stream, const char* const code ) { ast_t* cond = NULL; ast_t* body= NULL; if ( accept( stream, TOK_WHILE ) && accept( stream, TOK_LPAR ) && - (cond = expression( stream )) && + (cond = expression( stream, code )) && accept( stream, TOK_RPAR ) && - (body = statement( stream )) ) + (body = statement( stream, code )) ) return ast_while( cond, body); ast_free( cond ); @@ -180,10 +190,9 @@ static ast_t* while_cond( token_list_item_t** stream ) { } /* the main parsing method */ -ast_t* parse( token_list_t list ) { +ast_t* parse( token_list_t* list, const char* const code ) { ast_t* tree; - token_list_item_t** stream = &( list.first ); - tree = statements( stream ); - ast_print( tree, stdout ); + token_list_t* stream = list; + tree = statements( &stream, code ); return tree; } diff --git a/dwarf/dwarf-compiler/parser.h b/dwarf/dwarf-compiler/parser.h index 8268e27..0d8d258 100755 --- a/dwarf/dwarf-compiler/parser.h +++ b/dwarf/dwarf-compiler/parser.h @@ -1,6 +1,9 @@ #ifndef PARSER_H #define PARSER_H -ast_t* parse( token_list_t list ) ; +#include "token_list.h" +#include "ast.h" + +ast_t* parse( token_list_t* list, const char* const code ); #endif \ No newline at end of file diff --git a/dwarf/dwarf-compiler/token_list.c b/dwarf/dwarf-compiler/token_list.c new file mode 100755 index 0000000..94631e9 --- /dev/null +++ b/dwarf/dwarf-compiler/token_list.c @@ -0,0 +1,30 @@ +#include +#include "token_list.h" + +token_list_t* token_list_create( token_t* token ) { + token_list_t* item = (token_list_t*) malloc( sizeof( token_list_t ) ); + item->next = NULL; + item->token = *token; + return item; +} + + +void token_list_foreach( token_list_t* list, void (action)(token_list_t*) ) { + token_list_t* current = list; + token_list_t* next; + + if ( list == NULL ) { fprintf( stderr, "token list is null!" ); abort(); return; } + + while ( current != NULL ) + { + next = current-> next; + action( current ); + current = next; + } +} + + +static void token_list_item_destroy( token_list_t* item ) { free( item ); } +void token_list_free( token_list_t* list ) { + token_list_foreach( list, token_list_item_destroy ); +} \ No newline at end of file diff --git a/dwarf/dwarf-compiler/token_list.h b/dwarf/dwarf-compiler/token_list.h new file mode 100755 index 0000000..09cdbe6 --- /dev/null +++ b/dwarf/dwarf-compiler/token_list.h @@ -0,0 +1,16 @@ +#ifndef TOKEN_LIST +#define TOKEN_LIST + +#include "tokens.h" + +typedef struct token_list_t { + token_t token; + struct token_list_t* next; +} token_list_t; + + +token_list_t* token_list_create( token_t* token ); +void token_list_free( token_list_t* list ); +void token_list_foreach( token_list_t* list, void (action)(token_list_t*) ); + +#endif \ No newline at end of file diff --git a/dwarf/dwarf-compiler/tokens.c b/dwarf/dwarf-compiler/tokens.c index 520476e..cc3d6b0 100755 --- a/dwarf/dwarf-compiler/tokens.c +++ b/dwarf/dwarf-compiler/tokens.c @@ -1,54 +1,8 @@ #include "tokens.h" #include + +token_t token_EOF = { TOK_EOF, 0 }; -#define CHECK_LIST_NOT_NULL( lst ) if ( lst == NULL ) { fprintf( stderr, "token list is null!" ); abort(); return; } - -token_t token_EOF = { TOK_EOF, 0 }; - - -token_list_item_t* token_list_item_create( token_t token ) { - token_list_item_t* item = (token_list_item_t*) malloc( sizeof( token_list_item_t ) ); - item->next = NULL; - item->token = token; - return item; - -} -void token_list_add( token_list_t* list, token_t token ) { - token_list_item_t* item; - - CHECK_LIST_NOT_NULL( list ); - - item = token_list_item_create( token ); - - if ( list-> first == NULL ) - list-> first = list-> last = item; - else - { - list-> last-> next = item; - list->last = item; - } -} - -token_list_t token_list_create(void) { - token_list_t list; - list.first = list.last = NULL; - return list; -} - -void token_list_foreach( token_list_t* list, void (action)(token_t*) ) { - token_list_item_t* current, *next; - - CHECK_LIST_NOT_NULL( list ); - - current = list-> first; - - while ( current != NULL ) - { - next = current-> next; - action( &(current-> token) ); - current = next; - } -} void token_print( token_t* token ) { printf(" "); diff --git a/dwarf/dwarf-compiler/tokens.h b/dwarf/dwarf-compiler/tokens.h index 9ed8e6c..3eadff1 100755 --- a/dwarf/dwarf-compiler/tokens.h +++ b/dwarf/dwarf-compiler/tokens.h @@ -56,6 +56,7 @@ static char* token_strings[] = { typedef struct { token_type_t type; + size_t start_position; union { lang_int_t integer; char ident[IDENTIFIER_MAX_LENGTH]; @@ -64,25 +65,6 @@ typedef struct { extern token_t token_EOF; -typedef struct token_list_item_t { - token_t token; - struct token_list_item_t* next; -} token_list_item_t; - -typedef struct { - struct token_list_item_t* first; - struct token_list_item_t* last; -} token_list_t; - - -token_list_item_t* token_list_item_create( token_t token ); - -void token_list_add( token_list_t* list, token_t token ); - -token_list_t token_list_create( void ); -void token_list_destroy( token_list_t* list ); -void token_list_foreach( token_list_t* list, void (action)(token_t*) ); - -void token_print( token_t* token ) ; +void token_print( token_t* token ); #endif \ No newline at end of file diff --git a/dwarf/dwarf.sln b/dwarf/dwarf.sln index b17d8f4..2c64317 100755 --- a/dwarf/dwarf.sln +++ b/dwarf/dwarf.sln @@ -5,6 +5,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dwarf-vm", "dwarf\dwarf.vcx EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dwarf-compiler", "dwarf-compiler\dwarf-compiler.vcxproj", "{5A1C9D4C-8821-4339-944A-6722A5E1E992}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "common", "common", "{D81DE875-F986-4425-B006-DC2540A81CF0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32