Skip to content

Commit

Permalink
Use std::shared_ptr<std::string> for lexed/parsed strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ISSOtm authored and Rangi42 committed Mar 22, 2024
1 parent 9ff82c7 commit 0ca44b8
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 263 deletions.
14 changes: 4 additions & 10 deletions include/asm/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define RGBDS_ASM_LEXER_H

#include <deque>
#include <memory>
#include <optional>
#include <stdint.h>
#include <string>
Expand Down Expand Up @@ -31,13 +32,10 @@ enum LexerMode {

struct Expansion {
std::optional<std::string> name;
union {
char const *unowned;
char *owned; // Non-`const` only so it can be `delete []`d
} contents;
size_t size; // Length of the contents
std::shared_ptr<std::string> contents;
size_t offset; // Cursor into the contents
bool owned; // Whether or not to free contents when this expansion is freed

size_t size() const { return contents->size(); }
};

struct IfStackEntry {
Expand Down Expand Up @@ -140,10 +138,6 @@ struct CaptureBody {
size_t size;
};

struct String {
char string[MAXSTRLEN + 1];
};

void lexer_CheckRecursionDepth();
uint32_t lexer_GetLineNo();
uint32_t lexer_GetColNo();
Expand Down
14 changes: 5 additions & 9 deletions include/asm/macro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@
#ifndef RGBDS_MACRO_H
#define RGBDS_MACRO_H

#include <memory>
#include <stdint.h>
#include <stdlib.h>
#include <string>
#include <vector>

#include "helpers.hpp"

#include "asm/warning.hpp"

struct MacroArgs {
unsigned int shift;
std::vector<std::string> args;
std::vector<std::shared_ptr<std::string>> args;

void append(std::string s);
void append(std::shared_ptr<std::string> arg);
};

MacroArgs *macro_GetCurrentArgs();
void macro_UseNewArgs(MacroArgs *args);
char const *macro_GetArg(uint32_t i);
char const *macro_GetAllArgs();
std::shared_ptr<std::string> macro_GetArg(uint32_t i);
std::shared_ptr<std::string> macro_GetAllArgs();

void macro_ShiftCurrentArgs(int32_t count);
uint32_t macro_NbArgs();
Expand Down
15 changes: 8 additions & 7 deletions include/asm/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef RGBDS_SYMBOL_H
#define RGBDS_SYMBOL_H

#include <memory>
#include <optional>
#include <stdint.h>
#include <string.h>
Expand Down Expand Up @@ -35,10 +36,10 @@ struct Symbol {
uint32_t fileLine; // Line where the symbol was defined

std::variant<
int32_t, // If isNumeric()
int32_t (*)(), // If isNumeric() and has a callback
std::string_view *, // For SYM_MACRO
std::string * // For SYM_EQUS
int32_t, // If isNumeric()
int32_t (*)(), // If isNumeric() and has a callback
std::string_view *, // For SYM_MACRO
std::shared_ptr<std::string> // For SYM_EQUS
>
data;

Expand All @@ -61,7 +62,7 @@ struct Symbol {
int32_t getValue() const;
int32_t getOutputValue() const;
std::string_view *getMacro() const;
std::string *getEqus() const;
std::shared_ptr<std::string> getEqus() const;
uint32_t getConstantValue() const;
};

Expand Down Expand Up @@ -89,8 +90,8 @@ Symbol *sym_FindScopedValidSymbol(std::string const &symName);
Symbol const *sym_GetPC();
Symbol *sym_AddMacro(std::string const &symName, int32_t defLineNo, char const *body, size_t size);
Symbol *sym_Ref(std::string const &symName);
Symbol *sym_AddString(std::string const &symName, char const *value);
Symbol *sym_RedefString(std::string const &symName, char const *value);
Symbol *sym_AddString(std::string const &symName, std::shared_ptr<std::string> value);
Symbol *sym_RedefString(std::string const &symName, std::shared_ptr<std::string> value);
void sym_Purge(std::string const &symName);
void sym_Init(time_t now);

Expand Down
Loading

0 comments on commit 0ca44b8

Please sign in to comment.