Use reference counting for identifiers

This commit is contained in:
2026-06-17 10:11:15 +10:00
parent 6d35acbe60
commit b4e481ed9c
16 changed files with 112 additions and 156 deletions

View File

@@ -1,25 +1,8 @@
#include "../../../include/ground.h"
#include <string.h>
#include <stdlib.h>
GroundArg _GroundNewArgDirectRef(const char* ref) {
size_t len = strlen(ref);
char* copy = malloc(len + 1);
if (copy == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Arg.DirectRef()");
Ground.Flags.error = true;
return (GroundArg) {
.type = GroundArg_DirectRef,
.as.ref = ""
};
}
strcpy(copy, ref);
GroundArg _GroundNewArgDirectRef(GroundIdentifier* ref) {
return (GroundArg) {
.type = GroundArg_DirectRef,
.as.ref = copy
.as.ref = Ground.Copy.Identifier(ref)
};
}

View File

@@ -1,25 +1,8 @@
#include "../../../include/ground.h"
#include <string.h>
#include <stdlib.h>
GroundArg _GroundNewArgFunctionRef(const char* ref) {
size_t len = strlen(ref);
char* copy = malloc(len + 1);
if (copy == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Arg.FunctionRef()");
Ground.Flags.error = true;
return (GroundArg) {
.type = GroundArg_DirectRef,
.as.ref = ""
};
}
strcpy(copy, ref);
GroundArg _GroundNewArgFunctionRef(GroundIdentifier* ref) {
return (GroundArg) {
.type = GroundArg_FunctionRef,
.as.ref = copy
.as.ref = Ground.Copy.Identifier(ref)
};
}

View File

@@ -1,26 +1,9 @@
#include "../../../include/ground.h"
#include <string.h>
#include <stdlib.h>
GroundArg _GroundNewArgLabelRef(const char* ref) {
size_t len = strlen(ref);
char* copy = malloc(len + 1);
if (copy == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Arg.LabelRef()");
Ground.Flags.error = true;
return (GroundArg) {
.type = GroundArg_DirectRef,
.as.ref = ""
};
}
strcpy(copy, ref);
GroundArg _GroundNewArgLabelRef(GroundIdentifier* ref) {
return (GroundArg) {
.type = GroundArg_Label,
.as.ref = copy
.as.ref = Ground.Copy.Identifier(ref)
};
}

View File

@@ -1,25 +1,8 @@
#include "../../../include/ground.h"
#include <string.h>
#include <stdlib.h>
GroundArg _GroundNewArgLineRef(const char* ref) {
size_t len = strlen(ref);
char* copy = malloc(len + 1);
if (copy == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Arg.LineRef()");
Ground.Flags.error = true;
return (GroundArg) {
.type = GroundArg_DirectRef,
.as.ref = ""
};
}
strcpy(copy, ref);
GroundArg _GroundNewArgLineRef(GroundIdentifier* ref) {
return (GroundArg) {
.type = GroundArg_LineRef,
.as.ref = copy
.as.ref = Ground.Copy.Identifier(ref)
};
}

View File

@@ -1,26 +1,9 @@
#include "../../../include/ground.h"
#include <string.h>
#include <stdlib.h>
GroundArg _GroundNewArgTypeRef(const char* ref) {
size_t len = strlen(ref);
char* copy = malloc(len + 1);
if (copy == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Arg.TypeRef()");
Ground.Flags.error = true;
return (GroundArg) {
.type = GroundArg_DirectRef,
.as.ref = ""
};
}
strcpy(copy, ref);
GroundArg _GroundNewArgTypeRef(GroundIdentifier* ref) {
return (GroundArg) {
.type = GroundArg_TypeRef,
.as.ref = copy
.as.ref = Ground.Copy.Identifier(ref)
};
}

View File

@@ -1,26 +1,9 @@
#include "../../../include/ground.h"
#include <string.h>
#include <stdlib.h>
GroundArg _GroundNewArgValueRef(const char* ref) {
size_t len = strlen(ref);
char* copy = malloc(len + 1);
if (copy == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Arg.ValueRef()");
Ground.Flags.error = true;
return (GroundArg) {
.type = GroundArg_DirectRef,
.as.ref = ""
};
}
strcpy(copy, ref);
GroundArg _GroundNewArgValueRef(GroundIdentifier* ref) {
return (GroundArg) {
.type = GroundArg_ValueRef,
.as.ref = copy
.as.ref = Ground.Copy.Identifier(ref)
};
}

22
src/New/Identifier.c Normal file
View File

@@ -0,0 +1,22 @@
#include "../../include/ground.h"
GroundIdentifier* _GroundNewIdentifier(const char* id) {
GroundIdentifier* identifier = malloc(sizeof(GroundIdentifier));
if (identifier == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Identifier");
Ground.Flags.error = true;
return NULL;
}
identifier->string = malloc(sizeof(char) * (strlen(id) + 1));
if (identifier->string == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Identifier");
Ground.Flags.error = true;
return NULL;
}
strcpy(identifier->string, id);
identifier->referenceCount = 1;
return identifier;
}