update lexer to just return the number and then do the other logic somewhere else

This commit is contained in:
2025-05-30 02:12:51 +01:00
parent 626445a906
commit 68341db0b0
13 changed files with 165 additions and 350 deletions

View File

@@ -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;
}

View File

@@ -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