update lexer to just return the number and then do the other logic somewhere else
This commit is contained in:
@@ -1,15 +1,24 @@
|
||||
#include "parser.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../lexer/token.h"
|
||||
#include "../list/list.h"
|
||||
#include "string/string.h"
|
||||
|
||||
TaggedValue parse_token(LinkedList * tokens, size_t *index) {
|
||||
TaggedValue * parse_token(LinkedList * tokens, size_t *index) {
|
||||
Token * token = get_element_at(tokens, *index);
|
||||
switch (token->type) {
|
||||
case TOKEN_STRING:
|
||||
(*index)++;
|
||||
return parse_string(*token);
|
||||
case TOKEN_NEW_LINE:
|
||||
(*index)++;
|
||||
return NULL;
|
||||
default:
|
||||
perror("unreachable");
|
||||
exit(0);
|
||||
fprintf(stderr, "Panic: %s\n", "unreachable"); \
|
||||
exit(EXIT_FAILURE); \
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +26,19 @@ void parser(LinkedList * parsed, LinkedList * tokens, bool inline_flag) {
|
||||
size_t index = 0;
|
||||
size_t length = list_length(tokens);
|
||||
while (index < length) {
|
||||
TaggedValue parsed_code = parse_token(tokens, &index);
|
||||
append(parsed,&parsed_code);
|
||||
TaggedValue * parsed_code = parse_token(tokens, &index);
|
||||
if (parsed_code)
|
||||
append(parsed,parsed_code);
|
||||
}
|
||||
}
|
||||
|
||||
void free_tagged_value(void *ptr) {
|
||||
TaggedValue *tagged = ptr;
|
||||
switch (tagged->type) {
|
||||
case AST_STRING:
|
||||
free(tagged->data);
|
||||
break;
|
||||
// Add cases if needed
|
||||
}
|
||||
free(tagged); // Always free the TaggedValue itself
|
||||
}
|
||||
@@ -1,9 +1,27 @@
|
||||
#include "../lexer/token.h"
|
||||
#include "string/string.h"
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void parser(LinkedList * parsed, LinkedList * tokens, bool inline_flag);
|
||||
|
||||
TaggedValue parse_token(LinkedList * tokens, size_t *index);
|
||||
typedef struct LinkedList LinkedList;
|
||||
|
||||
typedef enum {
|
||||
AST_STRING,
|
||||
} ValueType;
|
||||
|
||||
typedef struct {
|
||||
ValueType type;
|
||||
void *data;
|
||||
|
||||
} TaggedValue;
|
||||
|
||||
void parser(LinkedList *parsed, LinkedList *tokens, bool inline_flag);
|
||||
|
||||
TaggedValue *parse_token(LinkedList *tokens, size_t *index);
|
||||
|
||||
void free_tagged_value(void *ptr);
|
||||
|
||||
|
||||
#endif // PARSER_H
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *swap_quotes(char *input) {
|
||||
char *swap_quotes(char *input, char quote) {
|
||||
size_t len = strlen(input);
|
||||
char *result = malloc(len + 1);
|
||||
if (!result)
|
||||
@@ -15,8 +15,8 @@ char *swap_quotes(char *input) {
|
||||
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
if (input[i] == '"')
|
||||
result[i] = '\'';
|
||||
else if (input[i] == '\'')
|
||||
result[i] = quote;
|
||||
else if (input[i] == quote)
|
||||
result[i] = '"';
|
||||
else
|
||||
result[i] = input[i];
|
||||
@@ -33,8 +33,8 @@ char *unquote(char *str) {
|
||||
char *swapped = NULL;
|
||||
char *unescaped = NULL;
|
||||
|
||||
if (quote == '\'') {
|
||||
swapped = swap_quotes(str);
|
||||
if (quote != '"') {
|
||||
swapped = swap_quotes(str, quote);
|
||||
if (!swapped)
|
||||
return NULL;
|
||||
str = swapped;
|
||||
@@ -62,8 +62,8 @@ char *unquote(char *str) {
|
||||
free(swapped);
|
||||
|
||||
// If input was single-quoted, swap quotes back in the output
|
||||
if (quote == '\'') {
|
||||
char *final = swap_quotes(unescaped);
|
||||
if (quote != '"') {
|
||||
char *final = swap_quotes(unescaped, quote);
|
||||
free(unescaped);
|
||||
return final;
|
||||
}
|
||||
@@ -71,9 +71,10 @@ char *unquote(char *str) {
|
||||
return unescaped;
|
||||
}
|
||||
|
||||
TaggedValue parse_string(Token token) {
|
||||
return (TaggedValue){
|
||||
AST_STRING,
|
||||
unquote(token.value),
|
||||
};
|
||||
TaggedValue * parse_string(Token token) {
|
||||
TaggedValue * taggedValue = malloc(sizeof(TaggedValue));
|
||||
|
||||
taggedValue->type = AST_STRING;
|
||||
taggedValue->data = unquote(token.value);
|
||||
return taggedValue;
|
||||
}
|
||||
@@ -1,8 +1,15 @@
|
||||
#include "../../lexer/token.h"
|
||||
#include "../taggedValue.h"
|
||||
#ifndef STRING_UTILS_H
|
||||
#define STRING_UTILS_H
|
||||
|
||||
char *swap_quotes(char *input);
|
||||
#include "../../lexer/token.h"
|
||||
#include "../parser.h"
|
||||
|
||||
// Declare functions related to string processing in parser
|
||||
|
||||
char *swap_quotes(char *input, char quote);
|
||||
|
||||
char *unquote(char *str);
|
||||
|
||||
TaggedValue parse_string(Token token);
|
||||
TaggedValue *parse_string(Token token);
|
||||
|
||||
#endif // STRING_UTILS_H
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "../list/list.h"
|
||||
|
||||
typedef enum {
|
||||
AST_STRING,
|
||||
} ValueType;
|
||||
|
||||
typedef struct {
|
||||
ValueType type;
|
||||
void *data;
|
||||
|
||||
} TaggedValue;
|
||||
Reference in New Issue
Block a user