diff --git a/src/Node.c b/src/Node.c deleted file mode 100644 index 82b1acb..0000000 --- a/src/Node.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "Node.h" -#include - -Node * Node_new(int type) -{ - Node * node = (Node *)malloc(sizeof(Node)); - node->type = type; - return node; -} diff --git a/src/Node.cc b/src/Node.cc new file mode 100644 index 0000000..f8e9571 --- /dev/null +++ b/src/Node.cc @@ -0,0 +1,2 @@ +#include "Node.h" +#include diff --git a/src/Node.h b/src/Node.h index 515787c..e3beaa9 100644 --- a/src/Node.h +++ b/src/Node.h @@ -1,35 +1,31 @@ #ifndef NODE_H #define NODE_H -#include "String.h" +#include -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct +class Node { +public: + Node(int _type) + { + type = _type; + } + int type; union { struct { - String * fname; + std::string * fname; size_t line; - String * text; + std::string * text; } token; }; -} Node; +}; enum { NODE_TYPE_TOKEN, }; -Node * Node_new(int type); - -#ifdef __cplusplus -} -#endif - #endif diff --git a/src/String.c b/src/String.c deleted file mode 100644 index 2c53d62..0000000 --- a/src/String.c +++ /dev/null @@ -1,53 +0,0 @@ -#include "String.h" -#include - -String * String_new(const char * s) -{ - size_t size = strlen(s); - return String_new_size(s, size); -} - -String * String_new_size(const char * s, size_t size) -{ - String * new_st = (String *)malloc(sizeof(String)); - char * smem = (char *)malloc(size + 1u); - memcpy(smem, s, size); - smem[size] = '\0'; - new_st->value = smem; - new_st->size = size; - return new_st; -} - -String * String_plus(const String * st, const char * s) -{ - String * new_st = (String *)malloc(sizeof(String)); - size_t size1 = st->size; - size_t size2 = strlen(s); - size_t size = size1 + size2; - char * smem = (char *)malloc(size + 1u); - memcpy(smem, st->value, size1); - memcpy(&smem[size1], s, size2); - smem[size] = '\0'; - new_st->value = smem; - new_st->size = size; - return new_st; -} - -String * String_concat(String * st, const char * s) -{ - size_t size2 = strlen(s); - size_t size = st->size + size2; - char * smem = (char *)malloc(size + 1u); - memcpy(smem, st->value, st->size); - memcpy(&smem[st->size], s, size2); - smem[size] = '\0'; - st->value = smem; - st->size = size; - return st; -} - -void String_free(String * st) -{ - free(st->value); - free(st); -} diff --git a/src/String.h b/src/String.h deleted file mode 100644 index 63d2c73..0000000 --- a/src/String.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef STRING_H -#define STRING_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct -{ - char * value; - size_t size; -} String; - -String * String_new(const char * s); -String * String_new_size(const char * s, size_t size); -String * String_plus(const String * st, const char * s); -String * String_concat(String * st, const char * s); -static inline char * String_cstr(const String * st) { return st->value; } -static inline size_t String_size(const String * st) { return st->size; } -void String_free(String * st); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/parser/parser.l b/src/parser/parser.l index 2587fe8..70b8c6f 100644 --- a/src/parser/parser.l +++ b/src/parser/parser.l @@ -4,23 +4,23 @@ %{ #include "parser.h" -#include "String.h" #include #include "Node.h" +#include #define YY_USER_ACTION \ do { \ yylloc->first_column += yyleng; \ yylloc->last_column += yyleng; \ - *yylval = Node_new(NODE_TYPE_TOKEN); \ + *yylval = new Node(NODE_TYPE_TOKEN); \ (*yylval)->token.fname = current_file; \ (*yylval)->token.line = current_line; \ - (*yylval)->token.text = String_new(yytext); \ + (*yylval)->token.text = new std::string(yytext); \ } while(0); -static String * build_string = NULL; +static std::string * build_string = nullptr; static size_t current_line = 1u; -static String * current_file = NULL; +static std::string * current_file = nullptr; void handle_loc(const char * input); %} @@ -138,11 +138,11 @@ L?'\\[0-7]{1,3}' return CHAR_CONST; ([0-9]+\.[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?[fFlL]? return FLOAT_CONST; L?\" { - if (build_string != NULL) + if (build_string != nullptr) { - String_free(build_string); + delete build_string; } - build_string = String_new(""); + build_string = new std::string(); BEGIN(str); } { @@ -154,25 +154,23 @@ L?\" { /* hexadecimal escape code */ unsigned int val; (void)sscanf(yytext + 1, "%x", &val); - char v[2] = {(char)val, '\0'}; - String_concat(build_string, v); + *build_string += (char)val; } \\[0-7]{1,3} { /* octal escape code */ unsigned int val; (void)sscanf(yytext + 1, "%o", &val); - char v[2] = {(char)val, '\0'}; - String_concat(build_string, v); + *build_string += (char)val; } -\\a String_concat(build_string, "\a"); -\\b String_concat(build_string, "\b"); -\\f String_concat(build_string, "\f"); -\\n String_concat(build_string, "\n"); -\\r String_concat(build_string, "\r"); -\\t String_concat(build_string, "\t"); -\\v String_concat(build_string, "\v"); -\\. String_concat(build_string, &yytext[1]); -[^\\\"]+ String_concat(build_string, yytext); +\\a *build_string += '\a'; +\\b *build_string += '\b'; +\\f *build_string += '\f'; +\\n *build_string += '\n'; +\\r *build_string += '\r'; +\\t *build_string += '\t'; +\\v *build_string += '\v'; +\\. *build_string += (char)yytext[1]; +[^\\\"]+ *build_string += yytext; } [a-zA-Z_][a-zA-Z_0-9]* return IDENTIFIER; @@ -209,5 +207,5 @@ void handle_loc(const char * input) input++; } size_t fname_len = (input - fname_start); - current_file = String_new_size(fname_start, fname_len); + current_file = new std::string(fname_start, fname_len); }