remove String module; use std::string

This commit is contained in:
Josh Holtrop 2018-05-08 20:13:47 -04:00
parent accaed47bc
commit 24f18c5233
6 changed files with 33 additions and 127 deletions

View File

@ -1,9 +0,0 @@
#include "Node.h"
#include <stdlib.h>
Node * Node_new(int type)
{
Node * node = (Node *)malloc(sizeof(Node));
node->type = type;
return node;
}

2
src/Node.cc Normal file
View File

@ -0,0 +1,2 @@
#include "Node.h"
#include <stdlib.h>

View File

@ -1,35 +1,31 @@
#ifndef NODE_H
#define NODE_H
#include "String.h"
#include <string>
#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

View File

@ -1,53 +0,0 @@
#include "String.h"
#include <string.h>
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);
}

View File

@ -1,28 +0,0 @@
#ifndef STRING_H
#define STRING_H
#include <stdlib.h>
#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

View File

@ -4,23 +4,23 @@
%{
#include "parser.h"
#include "String.h"
#include <stdlib.h>
#include "Node.h"
#include <string>
#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);
}
<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);
}