add identifier to parser
This commit is contained in:
@@ -1,30 +1,27 @@
|
||||
#include "lex.yy.h"
|
||||
#include "lexer.h"
|
||||
#include "lex.yy.h"
|
||||
|
||||
void lexer(LexerState state) {
|
||||
yyscan_t scanner;
|
||||
yyscan_t scanner;
|
||||
|
||||
yylex_init(&scanner);
|
||||
yylex_init(&scanner);
|
||||
|
||||
yyset_extra(&state, scanner);
|
||||
yyset_extra(&state, scanner);
|
||||
|
||||
yyset_in(state.file, scanner);
|
||||
yyset_in(state.file, scanner);
|
||||
|
||||
int token;
|
||||
while ((token = yylex(scanner)) != 0) {
|
||||
Token * token_struct = create_token(
|
||||
token,
|
||||
yyget_lineno(scanner),
|
||||
state.current_column+1,
|
||||
yyget_text(scanner)
|
||||
);
|
||||
darray_push(state.tokens, token_struct);
|
||||
free(token_struct);
|
||||
if (token == TOKEN_NEW_LINE) {
|
||||
state.current_column = 0;
|
||||
} else {
|
||||
state.current_column += yyget_leng(scanner);
|
||||
}
|
||||
int token;
|
||||
while ((token = yylex(scanner)) != 0) {
|
||||
Token *token_struct =
|
||||
create_token(token, yyget_lineno(scanner), state.current_column + 1,
|
||||
yyget_text(scanner));
|
||||
darray_push(state.tokens, token_struct);
|
||||
free(token_struct);
|
||||
if (token == TOKEN_NEW_LINE) {
|
||||
state.current_column = 0;
|
||||
} else {
|
||||
state.current_column += yyget_leng(scanner);
|
||||
}
|
||||
yylex_destroy(scanner);
|
||||
}
|
||||
yylex_destroy(scanner);
|
||||
}
|
||||
@@ -1,19 +1,18 @@
|
||||
#ifndef LEXER_H
|
||||
#define LEXER_H
|
||||
|
||||
#include "token.h"
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include "token.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
const char *path;
|
||||
FILE *file;
|
||||
int current_column;
|
||||
DArray* tokens;
|
||||
// add more fields as needed
|
||||
const char *path;
|
||||
FILE *file;
|
||||
int current_column;
|
||||
DArray *tokens;
|
||||
// add more fields as needed
|
||||
} LexerState;
|
||||
|
||||
void lexer(LexerState state);
|
||||
|
||||
|
||||
#endif // LEXER_H
|
||||
@@ -1,17 +1,17 @@
|
||||
#include "token.h"
|
||||
#include <stdlib.h>
|
||||
#include "../string/string.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
Token *create_token(TokenType type, int line, int column, char *value) {
|
||||
Token * token = malloc(sizeof(Token));
|
||||
token->type = type;
|
||||
token->line=line;
|
||||
token->column=column;
|
||||
token->value=cloneString(value);
|
||||
return token;
|
||||
Token *token = malloc(sizeof(Token));
|
||||
token->type = type;
|
||||
token->line = line;
|
||||
token->column = column;
|
||||
token->value = cloneString(value);
|
||||
return token;
|
||||
}
|
||||
|
||||
void free_token(void * ptr) {
|
||||
Token* token = ptr;
|
||||
free(token->value);
|
||||
void free_token(void *ptr) {
|
||||
Token *token = ptr;
|
||||
free(token->value);
|
||||
}
|
||||
@@ -20,15 +20,15 @@ typedef enum {
|
||||
TOKEN_ASSIGN_CARET,
|
||||
|
||||
// Operators
|
||||
TOKEN_AND, // &&
|
||||
TOKEN_OR, // ||
|
||||
TOKEN_NOT_IN, // not in
|
||||
TOKEN_LE, // <=
|
||||
TOKEN_GE, // >=
|
||||
TOKEN_LT, // <
|
||||
TOKEN_GT, // >
|
||||
TOKEN_NE, // !=
|
||||
TOKEN_EQ, // ==
|
||||
TOKEN_AND, // &&
|
||||
TOKEN_OR, // ||
|
||||
TOKEN_NOT_IN, // not in
|
||||
TOKEN_LE, // <=
|
||||
TOKEN_GE, // >=
|
||||
TOKEN_LT, // <
|
||||
TOKEN_GT, // >
|
||||
TOKEN_NE, // !=
|
||||
TOKEN_EQ, // ==
|
||||
TOKEN_PLUS, // +
|
||||
TOKEN_MINUS, // -
|
||||
TOKEN_MODULO, // %
|
||||
|
||||
124
src/list/list.c
124
src/list/list.c
@@ -1,97 +1,99 @@
|
||||
#include "list.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "list.h"
|
||||
|
||||
LinkedList *create_list(size_t data_size) {
|
||||
LinkedList *list = malloc(sizeof(LinkedList));
|
||||
list->head = NULL;
|
||||
list->data_size = data_size;
|
||||
list->length = 0;
|
||||
return list;
|
||||
LinkedList *list = malloc(sizeof(LinkedList));
|
||||
list->head = NULL;
|
||||
list->data_size = data_size;
|
||||
list->length = 0;
|
||||
return list;
|
||||
}
|
||||
|
||||
void append(LinkedList *list, void *element) {
|
||||
Node *new_node = malloc(sizeof(Node));
|
||||
new_node->data = malloc(list->data_size);
|
||||
memcpy(new_node->data, element, list->data_size);
|
||||
new_node->next = NULL;
|
||||
Node *new_node = malloc(sizeof(Node));
|
||||
new_node->data = malloc(list->data_size);
|
||||
memcpy(new_node->data, element, list->data_size);
|
||||
new_node->next = NULL;
|
||||
|
||||
if (!list->head) {
|
||||
list->head = new_node;
|
||||
} else {
|
||||
Node *temp = list->head;
|
||||
while (temp->next) temp = temp->next;
|
||||
temp->next = new_node;
|
||||
}
|
||||
list->length++;
|
||||
if (!list->head) {
|
||||
list->head = new_node;
|
||||
} else {
|
||||
Node *temp = list->head;
|
||||
while (temp->next)
|
||||
temp = temp->next;
|
||||
temp->next = new_node;
|
||||
}
|
||||
list->length++;
|
||||
}
|
||||
|
||||
void *get_element_at(LinkedList *list, size_t index) {
|
||||
if (index >= list->length) return NULL;
|
||||
if (index >= list->length)
|
||||
return NULL;
|
||||
|
||||
Node *current = list->head;
|
||||
for (size_t i = 0; i < index; ++i)
|
||||
current = current->next;
|
||||
Node *current = list->head;
|
||||
for (size_t i = 0; i < index; ++i)
|
||||
current = current->next;
|
||||
|
||||
return current->data;
|
||||
return current->data;
|
||||
}
|
||||
|
||||
int set_element_at(LinkedList *list, size_t index, void *element) {
|
||||
if (index >= list->length) return 0;
|
||||
if (index >= list->length)
|
||||
return 0;
|
||||
|
||||
Node *current = list->head;
|
||||
for (size_t i = 0; i < index; ++i)
|
||||
current = current->next;
|
||||
Node *current = list->head;
|
||||
for (size_t i = 0; i < index; ++i)
|
||||
current = current->next;
|
||||
|
||||
memcpy(current->data, element, list->data_size);
|
||||
return 1;
|
||||
memcpy(current->data, element, list->data_size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int remove_at(LinkedList *list, size_t index) {
|
||||
if (index >= list->length) return 0;
|
||||
if (index >= list->length)
|
||||
return 0;
|
||||
|
||||
Node *temp = list->head;
|
||||
Node *prev = NULL;
|
||||
Node *temp = list->head;
|
||||
Node *prev = NULL;
|
||||
|
||||
for (size_t i = 0; i < index; ++i) {
|
||||
prev = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
for (size_t i = 0; i < index; ++i) {
|
||||
prev = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
if (prev)
|
||||
prev->next = temp->next;
|
||||
else
|
||||
list->head = temp->next;
|
||||
if (prev)
|
||||
prev->next = temp->next;
|
||||
else
|
||||
list->head = temp->next;
|
||||
|
||||
free(temp->data);
|
||||
free(temp);
|
||||
list->length--;
|
||||
return 1;
|
||||
free(temp->data);
|
||||
free(temp);
|
||||
list->length--;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t list_length(LinkedList *list) {
|
||||
return list->length;
|
||||
}
|
||||
size_t list_length(LinkedList *list) { return list->length; }
|
||||
|
||||
void print_list(LinkedList *list, void (*print_func)(void *)) {
|
||||
Node *current = list->head;
|
||||
while (current) {
|
||||
print_func(current->data);
|
||||
current = current->next;
|
||||
}
|
||||
Node *current = list->head;
|
||||
while (current) {
|
||||
print_func(current->data);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
void free_list(LinkedList *list, void (*free_data)(void *)) {
|
||||
Node *current = list->head;
|
||||
while (current) {
|
||||
Node *next = current->next;
|
||||
Node *current = list->head;
|
||||
while (current) {
|
||||
Node *next = current->next;
|
||||
|
||||
if (free_data) // Safe to pass NULL if you don't need it
|
||||
free_data(current->data);
|
||||
if (free_data) // Safe to pass NULL if you don't need it
|
||||
free_data(current->data);
|
||||
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
free(list);
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
free(list);
|
||||
}
|
||||
@@ -5,14 +5,14 @@
|
||||
|
||||
// Node structure (opaque to user)
|
||||
typedef struct Node {
|
||||
void *data;
|
||||
struct Node *next;
|
||||
void *data;
|
||||
struct Node *next;
|
||||
} Node;
|
||||
|
||||
typedef struct LinkedList {
|
||||
Node *head;
|
||||
size_t data_size;
|
||||
size_t length;
|
||||
Node *head;
|
||||
size_t data_size;
|
||||
size_t length;
|
||||
} LinkedList;
|
||||
|
||||
// Create a new list for the given data type size
|
||||
|
||||
40
src/main.c
40
src/main.c
@@ -1,38 +1,34 @@
|
||||
#include "dynamic_array/darray.h"
|
||||
#include "lexer/lexer.h"
|
||||
#include "lexer/token.h"
|
||||
#include "parser/parser.h"
|
||||
#include "memory.h"
|
||||
#include "dynamic_array/darray.h"
|
||||
#include "parser/parser.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
ar_memory_init();
|
||||
char * path = "test.ar";
|
||||
DArray tokens;
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc <= 1)
|
||||
return -1;
|
||||
ar_memory_init();
|
||||
char *path = argv[1];
|
||||
DArray tokens;
|
||||
|
||||
darray_init(&tokens, sizeof(Token));
|
||||
darray_init(&tokens, sizeof(Token));
|
||||
|
||||
LexerState state = {
|
||||
path,
|
||||
fopen(path, "r"),
|
||||
0,
|
||||
&tokens
|
||||
};
|
||||
lexer(state);
|
||||
fclose(state.file);
|
||||
LexerState state = {path, fopen(path, "r"), 0, &tokens};
|
||||
lexer(state);
|
||||
fclose(state.file);
|
||||
|
||||
DArray ast;
|
||||
DArray ast;
|
||||
|
||||
darray_init(&ast, sizeof(ParsedValue));
|
||||
darray_init(&ast, sizeof(ParsedValue));
|
||||
|
||||
parser(path, &ast, &tokens, false);
|
||||
darray_free(&tokens, free_token);
|
||||
|
||||
parser(path,&ast, &tokens, false);
|
||||
darray_free(&tokens, free_token);
|
||||
|
||||
darray_free(&ast,free_parsed);
|
||||
darray_free(&ast, free_parsed);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
24
src/memory.c
24
src/memory.c
@@ -1,23 +1,17 @@
|
||||
#include "memory.h"
|
||||
#include <gc.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h> // for malloc/free (temp arena fallback)
|
||||
#include <string.h>
|
||||
|
||||
void ar_memory_init() {
|
||||
GC_INIT();
|
||||
}
|
||||
void ar_memory_init() { GC_INIT(); }
|
||||
|
||||
void* ar_alloc(size_t size) {
|
||||
return GC_MALLOC(size);
|
||||
}
|
||||
void *ar_alloc(size_t size) { return GC_MALLOC(size); }
|
||||
|
||||
void* ar_alloc_atomic(size_t size) {
|
||||
return GC_MALLOC_ATOMIC(size);
|
||||
}
|
||||
void *ar_alloc_atomic(size_t size) { return GC_MALLOC_ATOMIC(size); }
|
||||
|
||||
char* ar_strdup(const char* str) {
|
||||
size_t len = strlen(str) + 1;
|
||||
char* copy = (char*)GC_MALLOC(len);
|
||||
memcpy(copy, str, len);
|
||||
return copy;
|
||||
char *ar_strdup(const char *str) {
|
||||
size_t len = strlen(str) + 1;
|
||||
char *copy = (char *)GC_MALLOC(len);
|
||||
memcpy(copy, str, len);
|
||||
return copy;
|
||||
}
|
||||
@@ -4,9 +4,9 @@
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
// GC-managed allocations
|
||||
void* ar_alloc(size_t size);
|
||||
void* ar_alloc_atomic(size_t size);
|
||||
char* ar_strdup(const char* str);
|
||||
void *ar_alloc(size_t size);
|
||||
void *ar_alloc_atomic(size_t size);
|
||||
char *ar_strdup(const char *str);
|
||||
|
||||
// Memory init/shutdown
|
||||
void ar_memory_init();
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
ParsedValue *parse_assign(char*file,DArray *parsed, DArray *tokens,
|
||||
ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
|
||||
ParsedValue *assign_to, size_t *index) {
|
||||
Token *token = darray_get(tokens, *index);
|
||||
ParsedAssign *assign = malloc(sizeof(ParsedAssign));
|
||||
@@ -24,20 +24,20 @@ ParsedValue *parse_assign(char*file,DArray *parsed, DArray *tokens,
|
||||
case TOKEN_ASSIGN_STAR:
|
||||
fprintf(stderr, "%s:%u:%u error: invalid syntax\n", file, token->line,
|
||||
token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
default:
|
||||
break;
|
||||
exit(EXIT_FAILURE);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
assign->from = parse_token(file,parsed, tokens, index, true);
|
||||
assign->from = parse_token(file, parsed, tokens, index, true);
|
||||
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_ASSIGN;
|
||||
parsedValue->data = assign;
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
void free_parse_assign(void*ptr) {
|
||||
ParsedValue * parsedValue = ptr;
|
||||
ParsedAssign* parsedAssign = parsedValue->data;
|
||||
void free_parse_assign(void *ptr) {
|
||||
ParsedValue *parsedValue = ptr;
|
||||
ParsedAssign *parsedAssign = parsedValue->data;
|
||||
free_parsed(parsedAssign->to);
|
||||
free_parsed(parsedAssign->from);
|
||||
free(parsedAssign);
|
||||
|
||||
12
src/parser/identifier/identifier.c
Normal file
12
src/parser/identifier/identifier.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "identifier.h"
|
||||
#include "../../lexer/token.h"
|
||||
#include "../parser.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
ParsedValue *parse_identifier(Token *token) {
|
||||
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_IDENTIFIER;
|
||||
parsedValue->data = strcpy(malloc(sizeof(token->value)), token->value);
|
||||
return parsedValue;
|
||||
}
|
||||
11
src/parser/identifier/identifier.h
Normal file
11
src/parser/identifier/identifier.h
Normal file
@@ -0,0 +1,11 @@
|
||||
// parser.h
|
||||
|
||||
#ifndef IDENTIFIER_H
|
||||
#define IDENTIFIER_H
|
||||
#include "../parser.h"
|
||||
#include "../../lexer/token.h" // for Token
|
||||
|
||||
// Function declaration for parsing an identifier
|
||||
ParsedValue * parse_identifier(Token * token);
|
||||
|
||||
#endif // IDENTIFIER_H
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include "../lexer/token.h"
|
||||
#include "assign/assign.h"
|
||||
#include "identifier/identifier.h"
|
||||
#include "string/string.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
@@ -14,8 +15,8 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
||||
Token *token = darray_get(tokens, *index);
|
||||
if (!inline_flag) {
|
||||
switch (token->type) {
|
||||
default:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
switch (token->type) {
|
||||
@@ -23,11 +24,11 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
||||
(*index)++;
|
||||
return parse_string(*token);
|
||||
case TOKEN_NEW_LINE:
|
||||
while (token->type == TOKEN_NEW_LINE && *index+1 <= tokens->size) {
|
||||
(*index)++;
|
||||
while (token->type == TOKEN_NEW_LINE && ++(*index) < tokens->size) {
|
||||
token = darray_get(tokens, *index);
|
||||
}
|
||||
if (token->type == TOKEN_NEW_LINE) return NULL;
|
||||
if (token->type == TOKEN_NEW_LINE)
|
||||
return NULL;
|
||||
return parse_token(file, parsed, tokens, index, inline_flag);
|
||||
case TOKEN_INDENT:
|
||||
fprintf(stderr, "%s:%u:%u error: invalid indentation\n", file, token->line,
|
||||
@@ -43,13 +44,17 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
||||
case TOKEN_ASSIGN_STAR:
|
||||
if (parsed->size == 0) {
|
||||
fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line,
|
||||
token->column);
|
||||
token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ParsedValue *assign_to = malloc(sizeof(ParsedValue));
|
||||
memcpy(assign_to, darray_get(parsed, parsed->size-1), sizeof(ParsedValue));
|
||||
darray_resize(parsed, parsed->size-1);
|
||||
memcpy(assign_to, darray_get(parsed, parsed->size - 1),
|
||||
sizeof(ParsedValue));
|
||||
darray_resize(parsed, parsed->size - 1);
|
||||
return parse_assign(file, parsed, tokens, assign_to, index);
|
||||
case TOKEN_IDENTIFIER:
|
||||
(*index)++;
|
||||
return parse_identifier(token);
|
||||
default:
|
||||
fprintf(stderr, "Panic: unreachable\n");
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -72,6 +77,7 @@ void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag) {
|
||||
void free_parsed(void *ptr) {
|
||||
ParsedValue *parsed = ptr;
|
||||
switch (parsed->type) {
|
||||
case AST_IDENTIFIER:
|
||||
case AST_STRING:
|
||||
free(parsed->data);
|
||||
break;
|
||||
|
||||
@@ -1,29 +1,28 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "../dynamic_array/darray.h"
|
||||
|
||||
|
||||
typedef struct LinkedList LinkedList;
|
||||
|
||||
typedef enum {
|
||||
AST_STRING,
|
||||
AST_ASSIGN,
|
||||
AST_IDENTIFIER,
|
||||
} ValueType;
|
||||
|
||||
typedef struct {
|
||||
ValueType type;
|
||||
void *data;
|
||||
|
||||
} ParsedValue;
|
||||
|
||||
void parser(char*file,DArray *parsed, DArray *tokens, bool inline_flag);
|
||||
void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag);
|
||||
|
||||
ParsedValue *parse_token(char*file,DArray *parsed, DArray *tokens, size_t *index, bool inline_flag);
|
||||
ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
||||
size_t *index, bool inline_flag);
|
||||
|
||||
void free_parsed(void *ptr);
|
||||
|
||||
|
||||
#endif // PARSER_H
|
||||
@@ -42,6 +42,7 @@ char *unquote(char *str) {
|
||||
|
||||
cJSON *json = cJSON_Parse(str);
|
||||
if (!json || !cJSON_IsString(json)) {
|
||||
cJSON_Delete(json);
|
||||
if (swapped)
|
||||
free(swapped);
|
||||
return NULL;
|
||||
@@ -71,9 +72,8 @@ char *unquote(char *str) {
|
||||
return unescaped;
|
||||
}
|
||||
|
||||
ParsedValue * parse_string(Token token) {
|
||||
ParsedValue * parsedValue = malloc(sizeof(ParsedValue));
|
||||
|
||||
ParsedValue *parse_string(Token token) {
|
||||
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_STRING;
|
||||
parsedValue->data = unquote(token.value);
|
||||
return parsedValue;
|
||||
|
||||
@@ -6,69 +6,57 @@
|
||||
|
||||
const char *WHITE_SPACE = " \t\n\r\f\v";
|
||||
|
||||
char *cloneString(char *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
char *cloneString(char *str) {
|
||||
if (str == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
char *clone = malloc((len + 1) * sizeof(char));
|
||||
size_t len = strlen(str);
|
||||
char *clone = malloc((len + 1) * sizeof(char));
|
||||
|
||||
if (clone == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (clone == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(clone, str);
|
||||
return clone;
|
||||
strcpy(clone, str);
|
||||
return clone;
|
||||
}
|
||||
|
||||
void stripString(char *str, const char *chars)
|
||||
{
|
||||
if (str == NULL || chars == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
size_t charsLen = strlen(chars);
|
||||
|
||||
if (len == 0 || charsLen == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
size_t i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
if (strchr(chars, str[i]) == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
memmove(str, str + i, len - i + 1);
|
||||
}
|
||||
size_t j = len-i - 1;
|
||||
while (j > 0)
|
||||
{
|
||||
if (strchr(chars, str[j]) == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
j--;
|
||||
}
|
||||
|
||||
if (j < len)
|
||||
{
|
||||
str[j + 1] = '\0';
|
||||
}
|
||||
|
||||
str = realloc(str, (j + 1) * sizeof(char));
|
||||
|
||||
void stripString(char *str, const char *chars) {
|
||||
if (str == NULL || chars == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
size_t charsLen = strlen(chars);
|
||||
|
||||
if (len == 0 || charsLen == 0) {
|
||||
return;
|
||||
}
|
||||
size_t i = 0;
|
||||
while (i < len) {
|
||||
if (strchr(chars, str[i]) == NULL) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
memmove(str, str + i, len - i + 1);
|
||||
}
|
||||
size_t j = len - i - 1;
|
||||
while (j > 0) {
|
||||
if (strchr(chars, str[j]) == NULL) {
|
||||
break;
|
||||
}
|
||||
j--;
|
||||
}
|
||||
|
||||
if (j < len) {
|
||||
str[j + 1] = '\0';
|
||||
}
|
||||
|
||||
str = realloc(str, (j + 1) * sizeof(char));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
#ifndef CLONESTRING_H
|
||||
#define CLONESTRING_H
|
||||
|
||||
extern const char * WHITE_SPACE;
|
||||
extern const char *WHITE_SPACE;
|
||||
|
||||
char* cloneString(char* str);
|
||||
|
||||
void stripString(char* str, const char* chars);
|
||||
char *cloneString(char *str);
|
||||
|
||||
void stripString(char *str, const char *chars);
|
||||
|
||||
#endif // CLONESTRING_H
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include "translator.h"
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Translated * init_translator() {
|
||||
Translated *init_translator() {
|
||||
Translated *translated = malloc(sizeof(Translated));
|
||||
if (!translated) return NULL;
|
||||
if (!translated)
|
||||
return NULL;
|
||||
|
||||
darray_init(&translated->bytecode, sizeof(uint8_t));
|
||||
return translated;
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
#ifndef TRANSLATOR_H
|
||||
#define TRANSLATOR_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
typedef enum {
|
||||
OP_INIT_STRING
|
||||
} OperationType;
|
||||
|
||||
typedef enum { OP_INIT_STRING } OperationType;
|
||||
|
||||
typedef struct {
|
||||
size_t registerCount;
|
||||
|
||||
484
test.ar
484
test.ar
@@ -1 +1,483 @@
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
x="hello world"
|
||||
"hello world"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user