summaryrefslogtreecommitdiff
path: root/src/heads
diff options
context:
space:
mode:
authorczjstmax <jstmaxlol@disroot.org>2026-02-14 18:43:14 +0100
committerczjstmax <jstmaxlol@disroot.org>2026-02-14 18:43:14 +0100
commit2b885f00df619b77bbfcee3ff209cefe4a277df2 (patch)
treef2c5bf0452d350987cc668228d3588493090161d /src/heads
parent9201d978d0c4d4f4bdffd4e686ef116d7fdaa982 (diff)
Cumulative update.
Signed-off-by: czjstmax <jstmaxlol@disroot.org>
Diffstat (limited to 'src/heads')
-rw-r--r--src/heads/abstract.h118
-rw-r--r--src/heads/codegen.h4
-rw-r--r--src/heads/freeast.h34
3 files changed, 98 insertions, 58 deletions
diff --git a/src/heads/abstract.h b/src/heads/abstract.h
index efecaa0..3140742 100644
--- a/src/heads/abstract.h
+++ b/src/heads/abstract.h
@@ -1,6 +1,7 @@
-#ifndef CRBC_ABSTR
-#define CRBC_ABSTR
+#ifndef CRBC_ABSTR_H
+#define CRBC_ABSTR_H
+#include <stdio.h> // FILE, fprintf()
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
@@ -12,39 +13,64 @@ enum ExprKind {
EXPR_IDENT
};
-typedef struct Expr {
+// fwd.
+typedef struct Expr Expr;
+typedef struct ExprInt ExprInt;
+typedef struct ExprChar ExprChar;
+typedef struct ExprBool ExprBool;
+typedef struct ExprString ExprString;
+typedef struct ExprIdent ExprIdent;
+typedef struct ExprBinary ExprBinary;
+
+struct Expr {
enum ExprKind kind;
- void *as;
-} Expr;
+ union {
+ ExprInt *intLiteral;
+ ExprBool *boolLiteral;
+ ExprChar *charLiteral;
+ ExprString *stringLiteral;
+ ExprIdent *ident;
+ } as;
+};
-typedef struct ExprInt {
+struct ExprInt {
int value;
-} ExprInt;
+};
//
-typedef struct ExprChar {
+struct ExprChar {
char value;
-} ExprChar;
+};
//
-typedef struct ExprBool {
+struct ExprBool {
bool value;
-} ExprBool;
+};
//
-typedef struct ExprString {
+struct ExprString {
char *value;
-} ExprString;
+};
//
-typedef struct ExprIdent {
+struct ExprIdent {
char *name;
-} ExprIdent;
+};
+//
+struct ExprBinary {
+ Expr *left;
+ Expr *right;
+ char op;
+};
// ===== STATEMENTS =====
-typedef struct Stmt {
+
+typedef struct Stmt Stmt;
+typedef struct Block Block;
+
+struct Stmt {
int _dummy;
-} Stmt;
+};
-typedef struct Block {
+struct Block {
int _dummy;
-} Block;
+};
// ===== DECLARATIONS =====
enum DeclKind {
@@ -55,48 +81,60 @@ enum DeclKind {
DECL_TYPE_UNION
};
-typedef struct Decl {
+// fwd.
+typedef struct Decl Decl;
+typedef struct DeclVar DeclVar;
+typedef struct DeclFunc DeclFunc;
+typedef struct DeclTypeStruct DeclTypeStruct;
+typedef struct EnumMember EnumMember;
+typedef struct DeclTypeEnum DeclTypeEnum;
+typedef struct DeclTypeUnion DeclTypeUnion;
+
+struct Decl {
enum DeclKind kind;
- void *as;
-} Decl;
+ union {
+ DeclVar *var;
+ DeclFunc *func;
+ DeclTypeStruct *typeStruct;
+ DeclTypeEnum *typeEnum;
+ DeclTypeUnion *typeUnion;
+ } as;
+};
// variable declaration
-typedef struct DeclVar {
+struct DeclVar {
char *ident;
char *type; // TODO: replace with Type*
Expr *init; // NULL => declaration only
-} DeclVar;
+};
-// function declaration
-typedef struct DeclFunc {
+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 {
+struct DeclTypeStruct {
char *ident;
char *members; // TODO: member list
-} DeclTypeStruct;
+};
-// enum type declaration
-typedef struct EnumMember {
+struct EnumMember {
char *name;
char *value; // NULL => implicit
-} EnumMember;
+};
-typedef struct DeclTypeEnum {
+struct DeclTypeEnum {
char *ident;
EnumMember *members;
-} DeclTypeEnum;
+};
// union type declaration
-typedef struct DeclTypeUnion {
+struct DeclTypeUnion {
char *ident;
char *members; // TODO: member list
-} DeclTypeUnion;
+};
// ===== FUNCTIONS =====
static inline Decl *DeclareVariable(const char *ident, const char *type, Expr *init) {
@@ -108,7 +146,7 @@ static inline Decl *DeclareVariable(const char *ident, const char *type, Expr *i
var->init = init;
decl->kind = DECL_VAR;
- decl->as = var;
+ decl->as.var = var;
return decl;
}
@@ -119,8 +157,8 @@ static inline Expr *ExprIntLiteral(int value) {
lit->value = value;
- expr->kind = EXPR_INT;
- expr->as = lit;
+ expr->kind = EXPR_INT;
+ expr->as.intLiteral = lit;
return expr;
}
diff --git a/src/heads/codegen.h b/src/heads/codegen.h
index d629caf..17053e3 100644
--- a/src/heads/codegen.h
+++ b/src/heads/codegen.h
@@ -1,5 +1,5 @@
-#ifndef CRBC_CODEGEN
-#define CRBC_CODEGEN
+#ifndef CRBC_CODEGEN_H
+#define CRBC_CODEGEN_H
// Header file for CReborn's Codegen API
diff --git a/src/heads/freeast.h b/src/heads/freeast.h
index 544d183..c52e4e8 100644
--- a/src/heads/freeast.h
+++ b/src/heads/freeast.h
@@ -1,5 +1,5 @@
-#ifndef CRBC_FREEAST
-#define CRBC_FREEAST
+#ifndef CRBC_FREEAST_H
+#define CRBC_FREEAST_H
// Free memory functions, utilize after having used the AST
@@ -10,19 +10,21 @@ 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;
+ case EXPR_INT:
+ free(expr->as.intLiteral);
+ break;
+ case EXPR_BOOL:
+ free(expr->as.boolLiteral);
+ break;
+ case EXPR_CHAR:
+ free(expr->as.charLiteral);
+ break;
+ case EXPR_STRING:
+ free(expr->as.stringLiteral);
+ break;
+ case EXPR_IDENT:
+ free(expr->as.ident);
+ break;
}
free(expr);
@@ -44,7 +46,7 @@ void FreeDecl(Decl *decl)
switch (decl->kind) {
case DECL_VAR:
- FreeDeclVar((DeclVar *)decl->as);
+ FreeDeclVar(decl->as.var);
break;
case DECL_FUNC: