From fe0479de0c0305eafb1d3aa1456850af9e3b4fca Mon Sep 17 00:00:00 2001 From: czjstmax Date: Fri, 19 Dec 2025 23:09:12 +0100 Subject: Development started, cumulative commit #1 --- .exrc | 1 - .gitignore | 1 + LICENSE | 2 +- headers/abstract.h | 129 +++++++++++++++++++++++++++++++++++++++ headers/codegen.h | 13 ++++ headers/freeast.h | 70 +++++++++++++++++++++ headers/kat.h | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++ kat.h | 175 ----------------------------------------------------- rbc.c | 63 ++++++++++++++++++- 9 files changed, 449 insertions(+), 180 deletions(-) create mode 100644 .gitignore create mode 100644 headers/abstract.h create mode 100644 headers/codegen.h create mode 100644 headers/freeast.h create mode 100644 headers/kat.h delete mode 100644 kat.h diff --git a/.exrc b/.exrc index cb545e9..b5fbcd2 100644 --- a/.exrc +++ b/.exrc @@ -37,7 +37,6 @@ function! s:Compile(debug) execute '!' . l:cmd endfunction -" user commands command! Compn call s:Compile(0) command! Cn call s:Compile(0) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e58d6a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +catfiles diff --git a/LICENSE b/LICENSE index 17643cf..9250407 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2025 REBORN-lang +Copyright (c) 2025 REBORN-lang Organization, the organization behind REBORN-lang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/headers/abstract.h b/headers/abstract.h new file mode 100644 index 0000000..efecaa0 --- /dev/null +++ b/headers/abstract.h @@ -0,0 +1,129 @@ +#ifndef CRBC_ABSTR +#define CRBC_ABSTR + +#include +#include +#include + +// ===== EXPRESSIONS ===== +enum ExprKind { + EXPR_INT, EXPR_BOOL, + EXPR_CHAR, EXPR_STRING, + EXPR_IDENT +}; + +typedef struct Expr { + enum ExprKind kind; + void *as; +} Expr; + +typedef struct ExprInt { + int value; +} ExprInt; +// +typedef struct ExprChar { + char value; +} ExprChar; +// +typedef struct ExprBool { + bool value; +} ExprBool; +// +typedef struct ExprString { + char *value; +} ExprString; +// +typedef struct ExprIdent { + char *name; +} ExprIdent; + +// ===== STATEMENTS ===== +typedef struct Stmt { + int _dummy; +} Stmt; + +typedef struct Block { + int _dummy; +} Block; + +// ===== DECLARATIONS ===== +enum DeclKind { + DECL_VAR, + DECL_FUNC, + DECL_TYPE_STRUCT, + DECL_TYPE_ENUM, + DECL_TYPE_UNION +}; + +typedef struct Decl { + enum DeclKind kind; + void *as; +} Decl; + +// variable declaration +typedef struct DeclVar { + char *ident; + char *type; // TODO: replace with Type* + Expr *init; // NULL => declaration only +} DeclVar; + +// function declaration +typedef struct DeclFunc { + char *ident; + char *return_type; // NULL => inferred + char *params; // TODO: param list + Block *body; // NULL => forward declaration +} DeclFunc; + +// struct type declaration +typedef struct DeclTypeStruct { + char *ident; + char *members; // TODO: member list +} DeclTypeStruct; + +// enum type declaration +typedef struct EnumMember { + char *name; + char *value; // NULL => implicit +} EnumMember; + +typedef struct DeclTypeEnum { + char *ident; + EnumMember *members; +} DeclTypeEnum; + +// union type declaration +typedef struct DeclTypeUnion { + char *ident; + char *members; // TODO: member list +} DeclTypeUnion; + +// ===== FUNCTIONS ===== +static inline Decl *DeclareVariable(const char *ident, const char *type, Expr *init) { + Decl *decl = malloc(sizeof *decl); + DeclVar *var = malloc(sizeof *var); + + var->ident = strdup(ident); + var->type = type ? strdup(type) : NULL; + var->init = init; + + decl->kind = DECL_VAR; + decl->as = var; + + return decl; +} + +static inline Expr *ExprIntLiteral(int value) { + Expr *expr = malloc(sizeof *expr); + ExprInt *lit = malloc(sizeof *lit); + + lit->value = value; + + expr->kind = EXPR_INT; + expr->as = lit; + + return expr; +} + +#endif + diff --git a/headers/codegen.h b/headers/codegen.h new file mode 100644 index 0000000..d629caf --- /dev/null +++ b/headers/codegen.h @@ -0,0 +1,13 @@ +#ifndef CRBC_CODEGEN +#define CRBC_CODEGEN + +// Header file for CReborn's Codegen API + +#include +#include + +static inline int fb(int x) { + return x; +} + +#endif diff --git a/headers/freeast.h b/headers/freeast.h new file mode 100644 index 0000000..544d183 --- /dev/null +++ b/headers/freeast.h @@ -0,0 +1,70 @@ +#ifndef CRBC_FREEAST +#define CRBC_FREEAST + +// Free memory functions, utilize after having used the AST + +#include + +void FreeExpr(Expr *expr) +{ + if (!expr) return; + + switch (expr->kind) { + case EXPR_INT: + free(expr->as); + break; + case EXPR_BOOL: + break; + case EXPR_CHAR: + break; + case EXPR_STRING: + break; + case EXPR_IDENT: + break; + default: + break; + } + + free(expr); +} + +static inline void FreeDeclVar(DeclVar *var) +{ + if (!var) return; + + free(var->ident); + free(var->type); + + free(var); +} + +void FreeDecl(Decl *decl) +{ + if (!decl) return; + + switch (decl->kind) { + case DECL_VAR: + FreeDeclVar((DeclVar *)decl->as); + break; + + case DECL_FUNC: + /* FreeDeclFunc(...) later */ + break; + + case DECL_TYPE_STRUCT: + /* FreeDeclTypeStruct(...) later */ + break; + + case DECL_TYPE_ENUM: + /* ... */ + break; + + case DECL_TYPE_UNION: + /* ... */ + break; + } + + free(decl); +} + +#endif diff --git a/headers/kat.h b/headers/kat.h new file mode 100644 index 0000000..f5a3a00 --- /dev/null +++ b/headers/kat.h @@ -0,0 +1,175 @@ +// kat.cfg (kat.h) +// with love by czjstmax +// header version 2 + +#pragma once + +#include +#include +#include + +#ifndef KAT +#define KAT + +struct kat_api { + int (*create)(const char*); + int (*add)(const char*, const char*, const char*); + int (*read)(const char*, const char*, char*, size_t); + int (*del)(const char*, const char*); + int (*edit)(const char*, const char*, const char*); + int (*addComment)(const char*, const char*); +}; + +// Creates a file +static inline int katcreate(const char *path) { + FILE *f = fopen(path, "w"); + if (!f) return 1; + fclose(f); + return 0; // success +} + +// Appends a key=val to file +static inline int katadd(const char *key, const char *val, const char *path) { + FILE *f = fopen(path, "a"); + if (f) { + fprintf(f, "%s=%s\n", key, val); + fclose(f); + return 0; + } + return 1; +} + +// Appends a comment to file +static inline int kataddcomment(const char *str, const char *path) { + FILE *f = fopen(path, "a"); + if (f) { + fprintf(f, "#%s\n", str); + fclose(f); + return 0; + } + return 1; +} + +// Reads a key from file +static inline int katreadkey(const char *key, const char *path, char *buffer, size_t bufsize) { + FILE *f = fopen(path, "r"); + if (!f) return 1; + + char line[1024]; + size_t key_len = strlen(key); + + while (fgets(line, sizeof(line), f)) { + // TODO: skip leading whitespace maybe? + // check if line starts with key and '=' right after + if (strncmp(line, key, key_len) == 0 && line[key_len] == '=') { + if (line[0] == '#' || line[0] == ';' || line[0] == '\n') continue; + // copy everything after '=' until newline or buffer full (aka bad) + char *val_start = line + key_len + 1; + size_t len = strcspn(val_start, "\r\n"); // length until newline + if (len >= bufsize) len = bufsize - 1; + strncpy(buffer, val_start, len); + buffer[len] = '\0'; + fclose(f); + return 0; // success + } + } + + fclose(f); + return 1; +} + +// Deletes a key from file +static inline int katdelkey(const char *key, const char *path) { + FILE *f = fopen(path, "r"); + if (!f) return 1; + + char temp_path[1024]; + snprintf(temp_path, sizeof(temp_path), "%s.tmp", path); + FILE *temp = fopen(temp_path, "w"); + if (!temp) { + fclose(f); + return 1; + } + + char line[1024]; + size_t key_len = strlen(key); + int deleted = 0; + + while (fgets(line, sizeof(line), f)) { + // skip line if it matches 'key=' + if (strncmp(line, key, key_len) == 0 && line[key_len] == '=') { + deleted = 1; + continue; + } + fputs(line, temp); + } + + fclose(f); + fclose(temp); + + if (deleted) { + // replace original file with temp + if (remove(path) != 0) return 1; + if (rename(temp_path, path) != 0) return 1; + } else { + remove(temp_path); + } + + return deleted ? 0 : 1; // 0 if deleted, 1 if not found +} + +// Edits a key's value from file +static inline int kateditkey(const char *key, const char *new_val, const char *path) { + FILE *f = fopen(path, "r"); + if (!f) return 1; + + char temp_path[1024]; + snprintf(temp_path, sizeof(temp_path), "%s.tmp", path); + FILE *temp = fopen(temp_path, "w"); + if (!temp) { + fclose(f); + return 1; + } + + char line[1024]; + size_t key_len = strlen(key); + int found = 0; + + while (fgets(line, sizeof(line), f)) { + // skip leading whitespace for accurate matching + char *start = line; + while (*start == ' ' || *start == '\t') start++; + + if (strncmp(start, key, key_len) == 0 && start[key_len] == '=') { + // replace line with new key=val + fprintf(temp, "%s=%s\n", key, new_val); + found = 1; + } else { + fputs(line, temp); + } + } + + if (!found) { + // key not found, add it at the end + fprintf(temp, "%s=%s\n", key, new_val); + } + + fclose(f); + fclose(temp); + + if (remove(path) != 0) return 1; + if (rename(temp_path, path) != 0) return 1; + + return 0; +} + +struct kat_api kat = { + .create = katcreate, + .add = katadd, + .read = katreadkey, + .del = katdelkey, + .edit = kateditkey, + .addComment = kataddcomment, +}; + +#endif diff --git a/kat.h b/kat.h deleted file mode 100644 index f5a3a00..0000000 --- a/kat.h +++ /dev/null @@ -1,175 +0,0 @@ -// kat.cfg (kat.h) -// with love by czjstmax -// header version 2 - -#pragma once - -#include -#include -#include - -#ifndef KAT -#define KAT - -struct kat_api { - int (*create)(const char*); - int (*add)(const char*, const char*, const char*); - int (*read)(const char*, const char*, char*, size_t); - int (*del)(const char*, const char*); - int (*edit)(const char*, const char*, const char*); - int (*addComment)(const char*, const char*); -}; - -// Creates a file -static inline int katcreate(const char *path) { - FILE *f = fopen(path, "w"); - if (!f) return 1; - fclose(f); - return 0; // success -} - -// Appends a key=val to file -static inline int katadd(const char *key, const char *val, const char *path) { - FILE *f = fopen(path, "a"); - if (f) { - fprintf(f, "%s=%s\n", key, val); - fclose(f); - return 0; - } - return 1; -} - -// Appends a comment to file -static inline int kataddcomment(const char *str, const char *path) { - FILE *f = fopen(path, "a"); - if (f) { - fprintf(f, "#%s\n", str); - fclose(f); - return 0; - } - return 1; -} - -// Reads a key from file -static inline int katreadkey(const char *key, const char *path, char *buffer, size_t bufsize) { - FILE *f = fopen(path, "r"); - if (!f) return 1; - - char line[1024]; - size_t key_len = strlen(key); - - while (fgets(line, sizeof(line), f)) { - // TODO: skip leading whitespace maybe? - // check if line starts with key and '=' right after - if (strncmp(line, key, key_len) == 0 && line[key_len] == '=') { - if (line[0] == '#' || line[0] == ';' || line[0] == '\n') continue; - // copy everything after '=' until newline or buffer full (aka bad) - char *val_start = line + key_len + 1; - size_t len = strcspn(val_start, "\r\n"); // length until newline - if (len >= bufsize) len = bufsize - 1; - strncpy(buffer, val_start, len); - buffer[len] = '\0'; - fclose(f); - return 0; // success - } - } - - fclose(f); - return 1; -} - -// Deletes a key from file -static inline int katdelkey(const char *key, const char *path) { - FILE *f = fopen(path, "r"); - if (!f) return 1; - - char temp_path[1024]; - snprintf(temp_path, sizeof(temp_path), "%s.tmp", path); - FILE *temp = fopen(temp_path, "w"); - if (!temp) { - fclose(f); - return 1; - } - - char line[1024]; - size_t key_len = strlen(key); - int deleted = 0; - - while (fgets(line, sizeof(line), f)) { - // skip line if it matches 'key=' - if (strncmp(line, key, key_len) == 0 && line[key_len] == '=') { - deleted = 1; - continue; - } - fputs(line, temp); - } - - fclose(f); - fclose(temp); - - if (deleted) { - // replace original file with temp - if (remove(path) != 0) return 1; - if (rename(temp_path, path) != 0) return 1; - } else { - remove(temp_path); - } - - return deleted ? 0 : 1; // 0 if deleted, 1 if not found -} - -// Edits a key's value from file -static inline int kateditkey(const char *key, const char *new_val, const char *path) { - FILE *f = fopen(path, "r"); - if (!f) return 1; - - char temp_path[1024]; - snprintf(temp_path, sizeof(temp_path), "%s.tmp", path); - FILE *temp = fopen(temp_path, "w"); - if (!temp) { - fclose(f); - return 1; - } - - char line[1024]; - size_t key_len = strlen(key); - int found = 0; - - while (fgets(line, sizeof(line), f)) { - // skip leading whitespace for accurate matching - char *start = line; - while (*start == ' ' || *start == '\t') start++; - - if (strncmp(start, key, key_len) == 0 && start[key_len] == '=') { - // replace line with new key=val - fprintf(temp, "%s=%s\n", key, new_val); - found = 1; - } else { - fputs(line, temp); - } - } - - if (!found) { - // key not found, add it at the end - fprintf(temp, "%s=%s\n", key, new_val); - } - - fclose(f); - fclose(temp); - - if (remove(path) != 0) return 1; - if (rename(temp_path, path) != 0) return 1; - - return 0; -} - -struct kat_api kat = { - .create = katcreate, - .add = katadd, - .read = katreadkey, - .del = katdelkey, - .edit = kateditkey, - .addComment = kataddcomment, -}; - -#endif diff --git a/rbc.c b/rbc.c index 7b555aa..5316cda 100644 --- a/rbc.c +++ b/rbc.c @@ -51,11 +51,68 @@ #include // git@github.com:jstmaxlol/kat -#include "kat.h" +#include "headers/kat.h" + +// CReborn headers (CReborn APIs) +#include "headers/codegen.h" +#include "headers/abstract.h" +#include "headers/freeast.h" + +// ASTs in abstract.h +// + +// Functions (fwd. declars. and externs) +// +// headers/abstract.h +// headers/freeast.h +// headers/codegen.h +// +// local / helpers +static inline int Eval(int x); + +// main +int main(int argc, char **argv) { + printf(":> Checking for arguments...\n"); + if (argc >= 2) { + for (int i = 1; i < argc; i++) { + printf("argv[%d] = %s\n", i, argv[i]); + } + } else { + printf(":> No arguments were found. (%d)\n\n", argc-1); + } + + const char *ident_buff = "number"; + const char *type_buff = "int"; + + int value_buff = 2; + + if (argc == 3) { + if (strlen(argv[1]) >= 1) + ident_buff = argv[1]; + //if (strlen(argv[2]) >= 1) + // type_buff = argv[2]; + if (strlen(argv[2]) >= 1) + value_buff = atoi(argv[2]); + } + + Expr *e = ExprIntLiteral(value_buff); + Decl *decl = DeclareVariable(ident_buff, type_buff, e); + + if (decl->kind == DECL_VAR) { + DeclVar *v = decl->as; + printf("v->ident=%s, v->type=%s\nlet %s: %s = %d;", v->ident, v->type, v->ident, v->type, value_buff); + } + + FreeExpr(e); + FreeDecl(decl); -int main(/*int argc, char **argv*/) { - printf(":> [work-in-progress]\n"); return 0; } +// Functions (definitions) +// +static inline int Eval(int x) { + return x; +} + #endif -- cgit v1.3.1