diff options
| -rw-r--r-- | .exrc | 1 | ||||
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | LICENSE | 2 | ||||
| -rw-r--r-- | headers/abstract.h | 129 | ||||
| -rw-r--r-- | headers/codegen.h | 13 | ||||
| -rw-r--r-- | headers/freeast.h | 70 | ||||
| -rw-r--r-- | headers/kat.h (renamed from kat.h) | 0 | ||||
| -rw-r--r-- | rbc.c | 63 |
8 files changed, 274 insertions, 5 deletions
@@ -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 @@ -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 <stdlib.h> +#include <string.h> +#include <stdbool.h> + +// ===== 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 <stdlib.h> +#include <string.h> + +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 <stdlib.h> + +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 @@ -51,11 +51,68 @@ #include <string.h> // 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 |