add license to shell

This commit is contained in:
William Bell
2025-09-12 01:21:08 +01:00
parent 1a5abd9543
commit daa8056b7a
18 changed files with 3031 additions and 102 deletions

View File

@@ -2,46 +2,52 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
# Default FLEX tool
BINARY = bin/argon
FLEX_TOOL = flex
CFILES = external/xxhash/xxhash.c external/cwalk/src/cwalk.c external/libdye/src/dye.c external/linenoise/linenoise.c $(shell find src -name '*.c')
SRC_DIRS = src external/xxhash external/cwalk/src external/libdye/src external/linenoise
CFILES = $(shell find $(SRC_DIRS) -name '*.c' \
! -path "*/tests/*" \
! -path "*/fuzz/*" \
! -path "*/cli/*" \
! -path "*/example*") \
$(LEXER_C)
OBJDIR = build
OBJS = $(CFILES:%.c=$(OBJDIR)/%.o)
LEXER_SRC = src/lexer/lex.l
LEXER_C = src/lexer/lex.yy.c
LEXER_H = src/lexer/lex.yy.h
CFLAGS = $(ARCHFLAGS) -lm -lgc -lgmp -Wall -Wextra -Wno-unused-function -Werror=unused-result -Iexternal/cwalk/include -Iexternal/libdye/include
CFLAGS = $(ARCHFLAGS) -Wall -Wextra -Wno-unused-function -Werror=unused-result \
-Iexternal/cwalk/include -Iexternal/libdye/include
LDFLAGS = -lgc -lgmp -lm
all: $(BINARY)
# Rule to build lexer
$(LEXER_C) $(LEXER_H): $(LEXER_SRC)
$(FLEX_TOOL) --header-file=$(LEXER_H) -o $(LEXER_C) $(LEXER_SRC)
$(BINARY): $(CFILES) $(LEXER_C) $(LEXER_H)
mkdir -p bin
gcc -O3 -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS} -s
# Pattern rule for compiling .c -> .o
$(OBJDIR)/%.o: %.c $(LEXER_C) $(LEXER_H)
@mkdir -p $(dir $@)
gcc -O3 -c $< -o $@ $(CFLAGS)
native: $(CFILES) $(LEXER_C) $(LEXER_H)
mkdir -p bin
gcc -O3 -march=native -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS}
# Link final binary
$(BINARY): $(OBJS)
@mkdir -p bin
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS) -s
debug: $(CFILES) $(LEXER_C) $(LEXER_H)
mkdir -p bin
gcc -g -O3 -o $(BINARY) $(CFILES) $(CFLAGS)
native: CFLAGS += -march=native
native: $(BINARY)
full-debug: $(CFILES) $(LEXER_C) $(LEXER_H)
mkdir -p bin
gcc -g -O3 -fsanitize=address -fno-omit-frame-pointer -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS}
debug: CFLAGS += -g
debug: $(BINARY)
optimised: $(CFILES) $(LEXER_C) $(LEXER_H)
mkdir -p bin
gcc -O3 -fprofile-generate -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS}
full-debug: CFLAGS += -g -fsanitize=address -fno-omit-frame-pointer
full-debug: $(BINARY)
optimised: CFLAGS += -fprofile-generate
optimised: $(BINARY)
${BINARY} rand_test.ar
gcc -O3 -fprofile-use -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS}
$(MAKE) CFLAGS="$(CFLAGS:-fprofile-generate=-fprofile-use)" $(BINARY)
clean:
rm -rf build bin

2893
src/LICENSE_c.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -7,6 +7,7 @@
#include "err.h"
#include "../external/libdye/include/dye.h"
#include <ctype.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
@@ -14,7 +15,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#ifdef _WIN32
ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
@@ -61,19 +61,19 @@ ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
}
#endif
const ArErr no_err = (ArErr){false};
const ArErr no_err = (ArErr){"", "", "", 0, 0, 0, false};
ArErr create_err(int64_t line, int64_t column, int length, char *path,
const char *type, const char *fmt, ...) {
ArErr err;
err.exists = true;
err.path = path;
strcpy(err.path, path);
err.line = line;
err.column = column;
err.length = length;
// Copy error type safely
snprintf(err.type, sizeof(err.type), "%s",(char*)type);
snprintf(err.type, sizeof(err.type), "%s", (char *)type);
// Format error message
va_list args;
@@ -103,7 +103,7 @@ void output_err(ArErr err) {
dyefg(stderr, DYE_RESET);
fprintf(stderr, "\n");
if (err.path && err.line) {
if (strlen(err.path) && err.line) {
dyefg(stderr, DYE_GRAY);
fprintf(stderr, " --> ");
dyefg(stderr, DYE_CYAN);
@@ -111,7 +111,7 @@ void output_err(ArErr err) {
dyefg(stderr, DYE_GRAY);
fprintf(stderr, ":");
dyefg(stderr, DYE_YELLOW);
fprintf(stderr, "%" PRIu64 , err.line);
fprintf(stderr, "%" PRIu64, err.line);
dyefg(stderr, DYE_GRAY);
fprintf(stderr, ":");
dyefg(stderr, DYE_YELLOW);
@@ -140,7 +140,7 @@ void output_err(ArErr err) {
fprintf(stderr, " ");
}
fprintf(stderr, "|\n");
for (ssize_t i = 0;i<len;i++) {
for (ssize_t i = 0; i < len; i++) {
if (buffer[i] == '\n') {
buffer[i] = '\0';
break;
@@ -164,7 +164,7 @@ void output_err(ArErr err) {
dyefg(stderr, DYE_RESET);
fprintf(stderr, "%.*s",
(int)len - (int)skipped_chars - (int)err.column -
(int)err.length+1,
(int)err.length + 1,
line_starts + (int)err.column + err.length - 1);
for (int64_t i = 0; i < err.column - 1; i++) {
fprintf(stderr, " ");

View File

@@ -90,6 +90,7 @@ const char CACHE_FOLDER[] = "__arcache__";
const char FILE_IDENTIFIER[5] = "ARBI";
const char BYTECODE_EXTENTION[] = "arbin";
const uint32_t version_number = 0;
const char version_string[] = "4.0.0";
bool file_exists(const char *path) {
struct stat st;

View File

@@ -10,6 +10,8 @@
extern char*CWD;
extern const char version_string[];
Stack *ar_import(char *current_directory, char *path_relative, ArErr *err);
#endif // IMPORT_H

View File

@@ -8,7 +8,7 @@
#include "hashmap/hashmap.h"
#include "import.h"
#include "memory.h"
#include "runtime/objects/object.h"
#include "runtime/runtime.h"
#include "shell.h"
#include "hash_data/hash_data.h"
@@ -65,6 +65,7 @@ int main(int argc, char *argv[]) {
return 1;
}
free(CWD);
ar_memory_shutdown();
if (runtime_hash_table)
hashmap_free(runtime_hash_table, NULL);
if (err.exists) {

View File

@@ -7,10 +7,11 @@
#include "memory.h"
#include <gc.h>
#include <gc/gc.h>
#include <gmp.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h> // for malloc/free (temp arena fallback)
#include <string.h>
#include <pthread.h>
void *checked_malloc(size_t size) {
void *ptr = malloc(size);
@@ -21,19 +22,23 @@ void *checked_malloc(size_t size) {
return ptr;
}
void *gmp_gc_realloc(void *ptr, size_t old_size, size_t new_size) {
(void)old_size; // Ignore old_size, Boehm doesn't need it
return GC_realloc(ptr, new_size);
}
void gmp_gc_free(void *ptr, size_t size) {
(void)size; // Boehm GC manages this itself
// No-op — memory will be collected automatically
GC_FREE(ptr);
}
struct allocation*memory_allocations = NULL;
size_t memory_allocations_size = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void ar_memory_init() {
GC_INIT();
// memory_allocations_size = 8;
// memory_allocations = malloc(memory_allocations_size*sizeof(struct allocation));
}
void ar_memory_shutdown() {
// for (size_t i = 0; i<memory_allocations_size;i++) {
// if (memory_allocations[i].status != allocation_fully_freed) {
// free(memory_allocations[i].ptr);
// }
// }
// free(memory_allocations);
}
void *ar_alloc(size_t size) { return GC_MALLOC(size); }

View File

@@ -8,10 +8,23 @@
#define ARGON_MEMORY_H
#include <stddef.h> // for size_t
#include <stdbool.h>
#include <gc/gc.h>
// GC-managed allocations
typedef enum allocation_status {
allocation_used,
allocation_soft_free, // avaiable for use, since it hasnt been freed but isnt in use.
allocation_fully_freed,
} allocation_status;
struct allocation {
void*ptr;
size_t size;
allocation_status status;
};
void ar_finalizer(void *obj, GC_finalization_proc fn, void *client_data,
GC_finalization_proc *old_fn, void **old_client_data);
void *ar_alloc(size_t size);
@@ -21,6 +34,7 @@ char *ar_strdup(const char *str);
// Memory init/shutdown
void ar_memory_init();
void ar_memory_shutdown();
void *checked_malloc(size_t size);

View File

@@ -8,16 +8,16 @@
#define RETURN_TYPES_H
#include <stdint.h>
#include "arobject.h"
#include <stdio.h>
#define ERR_MSG_MAX_LEN 32
typedef struct ArErr {
char *path;
char path[FILENAME_MAX];
char message[128];
char type[64];
int64_t line;
int64_t column;
int length;
char message[ERR_MSG_MAX_LEN];
char type[16];
bool exists;
} ArErr;
#endif // RETURN_TYPES_

View File

@@ -85,15 +85,13 @@ void run_call(ArgonObject *original_object, size_t argc, ArgonObject **argv,
if (object->type != TYPE_FUNCTION && object->type != TYPE_NATIVE_FUNCTION &&
object->type != TYPE_METHOD) {
ArgonObject *call_method = get_builtin_field_for_class(
get_builtin_field(object, __class__), __call__,
original_object);
get_builtin_field(object, __class__), __call__, original_object);
if (call_method) {
object = call_method;
}
}
if (object->type == TYPE_METHOD) {
ArgonObject *binding_object =
get_builtin_field(object, __binding__);
ArgonObject *binding_object = get_builtin_field(object, __binding__);
if (binding_object) {
ArgonObject **new_call_args =
ar_alloc(sizeof(ArgonObject *) * (argc + 1));
@@ -104,16 +102,14 @@ void run_call(ArgonObject *original_object, size_t argc, ArgonObject **argv,
argv = new_call_args;
argc++;
}
ArgonObject *function_object =
get_builtin_field(object, __function__);
ArgonObject *function_object = get_builtin_field(object, __function__);
if (function_object)
object = function_object;
}
if (object->type == TYPE_FUNCTION) {
if (argc != object->value.argon_fn->number_of_parameters) {
ArgonObject *type_object_name = get_builtin_field_for_class(
get_builtin_field(object, __class__), __name__,
original_object);
get_builtin_field(object, __class__), __name__, original_object);
ArgonObject *object_name =
get_builtin_field_for_class(object, __name__, original_object);
*err = create_err(
@@ -122,7 +118,8 @@ void run_call(ArgonObject *original_object, size_t argc, ArgonObject **argv,
"%.*s %.*s takes %" PRIu64 " argument(s) but %" PRIu64 " was given",
(int)type_object_name->value.as_str->length,
type_object_name->value.as_str->data,
(int)object_name->value.as_str->length, object_name->value.as_str->data,
(int)object_name->value.as_str->length,
object_name->value.as_str->data,
object->value.argon_fn->number_of_parameters, argc);
}
Stack *scope = create_scope(object->value.argon_fn->stack, true);
@@ -191,13 +188,12 @@ void run_call(ArgonObject *original_object, size_t argc, ArgonObject **argv,
err->line = state->source_location.line;
err->column = state->source_location.column;
err->length = state->source_location.length;
err->path = state->path;
strcpy(err->path, state->path);
}
return;
}
ArgonObject *type_object_name = get_builtin_field_for_class(
get_builtin_field(original_object, __class__), __name__,
original_object);
get_builtin_field(original_object, __class__), __name__, original_object);
*err = create_err(state->source_location.line, state->source_location.column,
state->source_location.length, state->path, "Type Error",
"'%.*s' object is not callable",

View File

@@ -13,9 +13,8 @@
ArgonObject *ARGON_STRING_TYPE = NULL;
ArgonObject *new_string_object_without_memcpy(char *data, size_t length, uint64_t prehash,
void init_string(ArgonObject*object,char *data, size_t length, uint64_t prehash,
uint64_t hash) {
ArgonObject *object = new_instance(ARGON_STRING_TYPE);
add_builtin_field(object, field_length,
new_number_object_from_int64(length));
object->type = TYPE_STRING;
@@ -26,6 +25,12 @@ ArgonObject *new_string_object_without_memcpy(char *data, size_t length, uint64_
object->value.as_str->hash = hash;
object->value.as_str->length = length;
object->as_bool = length;
}
ArgonObject *new_string_object_without_memcpy(char *data, size_t length, uint64_t prehash,
uint64_t hash) {
ArgonObject *object = new_instance(ARGON_STRING_TYPE);
init_string(object,data,length,prehash,hash);
return object;
}

View File

@@ -10,6 +10,9 @@
extern ArgonObject *ARGON_STRING_TYPE;
void init_string(ArgonObject*object,char *data, size_t length, uint64_t prehash,
uint64_t hash);
ArgonObject *new_string_object_without_memcpy(char *data, size_t length, uint64_t prehash,
uint64_t hash);

View File

@@ -64,23 +64,23 @@ ArgonObject *BASE_CLASS___getattribute__(size_t argc, ArgonObject **argv,
access->value.as_str->hash = hash;
access->value.as_str->hash_computed = true;
}
ArgonObject *value = get_field_l(to_access, access->value.as_str->data, hash,
ArgonObject *value =
get_field_l(to_access, access->value.as_str->data, hash,
access->value.as_str->length, true, false);
if (value)
return value;
ArgonObject *cls__get_attr__ = get_builtin_field_for_class(
get_builtin_field(to_access, __class__), __get_attr__,
to_access);
get_builtin_field(to_access, __class__), __get_attr__, to_access);
if (cls__get_attr__) {
value = argon_call(cls__get_attr__, 1, (ArgonObject *[]){access}, err, state);
value =
argon_call(cls__get_attr__, 1, (ArgonObject *[]){access}, err, state);
if (err->exists) {
return ARGON_NULL;
}
return value;
}
ArgonObject *name = get_builtin_field_for_class(
get_builtin_field(to_access, __class__), __name__,
to_access);
get_builtin_field(to_access, __class__), __name__, to_access);
*err = create_err(
0, 0, 0, "", "Runtime Error", "'%.*s' object has no attribute '%.*s'",
(int)name->value.as_str->length, name->value.as_str->data,
@@ -101,8 +101,7 @@ ArgonObject *ARGON_ADDITION_FUNCTION(size_t argc, ArgonObject **argv,
ArgonObject *object__add__ = get_builtin_field_for_class(
get_builtin_field(output, __class__), __add__, output);
if (!object__add__) {
ArgonObject *cls___name__ =
get_builtin_field(output, __name__);
ArgonObject *cls___name__ = get_builtin_field(output, __name__);
*err = create_err(0, 0, 0, "", "Runtime Error",
"Object '%.*s' is missing __add__ method",
(int)cls___name__->value.as_str->length,
@@ -126,11 +125,9 @@ ArgonObject *ARGON_SUBTRACTION_FUNCTION(size_t argc, ArgonObject **argv,
ArgonObject *output = argv[0];
for (size_t i = 1; i < argc; i++) {
ArgonObject *function__subtract__ = get_builtin_field_for_class(
get_builtin_field(output, __class__), __subtract__,
output);
get_builtin_field(output, __class__), __subtract__, output);
if (!function__subtract__) {
ArgonObject *cls___name__ =
get_builtin_field(output, __name__);
ArgonObject *cls___name__ = get_builtin_field(output, __name__);
*err = create_err(0, 0, 0, "", "Runtime Error",
"Object '%.*s' is missing __subtract__ method",
(int)cls___name__->value.as_str->length,
@@ -154,11 +151,9 @@ ArgonObject *ARGON_MULTIPLY_FUNCTION(size_t argc, ArgonObject **argv,
ArgonObject *output = argv[0];
for (size_t i = 1; i < argc; i++) {
ArgonObject *function__multiply__ = get_builtin_field_for_class(
get_builtin_field(output, __class__), __multiply__,
output);
get_builtin_field(output, __class__), __multiply__, output);
if (!function__multiply__) {
ArgonObject *cls___name__ =
get_builtin_field(output, __name__);
ArgonObject *cls___name__ = get_builtin_field(output, __name__);
*err = create_err(0, 0, 0, "", "Runtime Error",
"Object '%.*s' is missing __multiply__ method",
(int)cls___name__->value.as_str->length,
@@ -182,11 +177,9 @@ ArgonObject *ARGON_DIVISION_FUNCTION(size_t argc, ArgonObject **argv,
ArgonObject *output = argv[0];
for (size_t i = 1; i < argc; i++) {
ArgonObject *function__division__ = get_builtin_field_for_class(
get_builtin_field(output, __class__), __division__,
output);
get_builtin_field(output, __class__), __division__, output);
if (!function__division__) {
ArgonObject *cls___name__ =
get_builtin_field(output, __name__);
ArgonObject *cls___name__ = get_builtin_field(output, __name__);
*err = create_err(0, 0, 0, "", "Runtime Error",
"Object '%.*s' is missing __division__ method",
(int)cls___name__->value.as_str->length,
@@ -218,8 +211,7 @@ ArgonObject *ARGON_TYPE_TYPE___call__(size_t argc, ArgonObject **argv,
ArgonObject *cls___new__ =
get_builtin_field_for_class(argv[0], __new__, NULL);
if (!cls___new__) {
ArgonObject *cls___name__ =
get_builtin_field(argv[0], __name__);
ArgonObject *cls___name__ = get_builtin_field(argv[0], __name__);
*err = create_err(
0, 0, 0, "", "Runtime Error",
"Object '%.*s' is missing __new__ method, so cannot be initialised",
@@ -239,8 +231,7 @@ ArgonObject *ARGON_TYPE_TYPE___call__(size_t argc, ArgonObject **argv,
ArgonObject *cls___init__ =
get_builtin_field_for_class(argv[0], __init__, new_object);
if (!cls___init__) {
ArgonObject *cls___name__ =
get_builtin_field(argv[0], __name__);
ArgonObject *cls___name__ = get_builtin_field(argv[0], __name__);
*err = create_err(
0, 0, 0, "", "Runtime Error",
"Object '%.*s' is missing __init__ method, so cannot be initialised",
@@ -338,10 +329,6 @@ ArgonObject *ARGON_STRING_TYPE___init__(size_t argc, ArgonObject **argv,
}
ArgonObject *self = argv[0];
ArgonObject *object = argv[1];
self->value.as_str->data = NULL;
self->value.as_str->length = 0;
self->type = TYPE_STRING;
ArgonObject *string_convert_method = get_builtin_field_for_class(
get_builtin_field(object, __class__), __string__, object);
if (string_convert_method) {
@@ -349,12 +336,14 @@ ArgonObject *ARGON_STRING_TYPE___init__(size_t argc, ArgonObject **argv,
argon_call(string_convert_method, 0, NULL, err, state);
if (err->exists)
return ARGON_NULL;
self->value.as_str->data =
ar_alloc_atomic(string_object->value.as_str->length);
memcpy(self->value.as_str->data, string_object->value.as_str->data,
string_object->value.as_str->length);
self->value.as_str->length = string_object->value.as_str->length;
init_string(self, string_object->value.as_str->data,
string_object->value.as_str->length,
string_object->value.as_str->prehash,
string_object->value.as_str->hash);
return ARGON_NULL;
}
*err = create_err(0, 0, 0, "", "String Conversion Error",
"cannot convert to string");
return ARGON_NULL;
}
@@ -622,8 +611,8 @@ void bootstrap_types() {
add_builtin_field(
ARGON_BOOL_TYPE, __number__,
create_argon_native_function("__number__", ARGON_BOOL_TYPE___number__));
GETATTRIBUTE_FUNCTION = create_argon_native_function("__get_attr__",
BASE_CLASS___getattribute__);
GETATTRIBUTE_FUNCTION =
create_argon_native_function("__get_attr__", BASE_CLASS___getattribute__);
ADDITION_FUNCTION =
create_argon_native_function("add", ARGON_ADDITION_FUNCTION);
SUBTRACTION_FUNCTION =
@@ -874,8 +863,8 @@ void runtime(Translated _translated, RuntimeState _state, Stack *stack,
continue;
DO_LOAD_GETATTRIBUTE_FUNCTION:
state->registers[0] = get_builtin_field_for_class(
get_builtin_field(state->registers[0], __class__),
__getattribute__, state->registers[0]);
get_builtin_field(state->registers[0], __class__), __getattribute__,
state->registers[0]);
if (!state->registers[0]) {
*err = create_err(
state->source_location.line, state->source_location.column,

View File

@@ -89,6 +89,8 @@ RuntimeState init_runtime_state(Translated translated, char *path);
Stack *create_scope(Stack *prev, bool force);
void add_to_scope(Stack *stack, char *name, ArgonObject *value);
void runtime(Translated translated, RuntimeState state, Stack *stack,
ArErr *err);

View File

@@ -9,6 +9,10 @@
#include "./runtime/objects/term/term.h"
#include "./runtime/runtime.h"
#include "./translator/translator.h"
#include "LICENSE_c.h"
#include "import.h"
#include "runtime/objects/literals/literals.h"
#include "runtime/objects/string/string.h"
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
@@ -180,10 +184,14 @@ int shell() {
free(data);
return resp;
}
add_to_scope(main_scope, "license",
new_string_object_without_memcpy((char *)LICENSE_txt,
LICENSE_txt_len, 0, 0));
signal(SIGINT, handle_sigint);
printf("Welcome to the Argon shell!\n\n");
printf("Argon (Chloride %s)\nType \"license\" for more information.\n",
version_string);
ArgonObject *output_object = create_argon_native_function("log", term_log);
char *totranslate = NULL;
@@ -266,7 +274,7 @@ int shell() {
if (resp) {
continue;
}
if (runtime_state.registers[0]) {
if (runtime_state.registers[0]&&runtime_state.registers[0] != ARGON_NULL) {
ArErr err = no_err;
argon_call(output_object, 1,
(ArgonObject *[]){runtime_state.registers[0]}, &err,

4
tests/iteration-test.py Normal file
View File

@@ -0,0 +1,4 @@
i = 1000000
while i:
print(i)
i=i-1