perf: remove canaries, artificial security panics and runtime checks for maximum speed

This commit is contained in:
Adam Eehan committed 2026-10-28 19:53:53 +05:30
1 parent 12f4e93bc2
commit c495c9786a
13 files changed
+106 -243

No files matched your search

BIN
View File
Binary file not shown.
-2
View File
@@ -43,7 +43,6 @@ struct ExprNode {
ExprType type;
int line;
int col;
uint32_t canary;
union {
AiraValue literal;
char* identifier;
@@ -85,7 +84,6 @@ struct StmtNode {
StmtType type;
int line;
int col;
uint32_t canary;
union {
struct {
StmtNode** statements;
+4 -5
View File
@@ -13,10 +13,9 @@
#define AIRA_CODENAME "S1 Duo Flagship"
#define AIRA_ARCHITECT "Adam Eehan (Founder & CEO, Aira Group of Technology)"
/* Security & Hardening Limits */
#define AIRA_MAX_RECURSION_DEPTH 1000
#define AIRA_MAX_STRING_LEN (32 * 1024 * 1024) /* 32 MB */
#define AIRA_CANARY_VALUE 0x41495241 /* 'AIRA' */
/* Execution Limits */
#define AIRA_MAX_RECURSION_DEPTH 10000
#define AIRA_MAX_STRING_LEN (128 * 1024 * 1024) /* 128 MB */
/* ANSI Color Formatting */
#define COLOR_RESET "\033[0m"
@@ -38,7 +37,7 @@ void aira_free(void* ptr);
char* aira_strdup(const char* s);
char* aira_strndup(const char* s, size_t n);
/* Diagnostic & Security Logging */
/* Diagnostic Logging */
void aira_panic(const char* fmt, ...);
void aira_error_at(const char* filename, int line, int col, const char* fmt, ...);
-1
View File
@@ -23,7 +23,6 @@ typedef struct {
size_t data_cap;
uint64_t base_vaddr;
uint32_t canary;
} ElfEmitter;
void elf_emitter_init(ElfEmitter* emitter, TargetArch arch);
-1
View File
@@ -9,7 +9,6 @@ typedef struct Environment {
AiraValue* values;
int count;
int capacity;
uint32_t canary;
} Environment;
Environment* env_new(Environment* parent);
+3 -8
View File
@@ -26,7 +26,6 @@ typedef struct {
struct AiraValue* elements;
int count;
int capacity;
uint32_t canary;
} AiraList;
typedef struct {
@@ -34,25 +33,21 @@ typedef struct {
struct AiraValue* values;
int count;
int capacity;
uint32_t canary;
} AiraDict;
typedef struct {
const char* name;
struct StmtNode* node;
struct Environment* closure;
uint32_t canary;
} AiraFunction;
typedef struct {
const char* name;
AiraDict* members;
uint32_t canary;
} AiraModule;
typedef struct AiraValue {
ValueType type;
uint32_t canary;
union {
bool boolean;
int64_t integer;
@@ -95,7 +90,7 @@ void val_print(AiraValue v);
void val_free(AiraValue* v);
AiraValue val_copy(AiraValue v);
/* List and Dict Security & Bounds Helpers */
/* List and Dict Helpers */
void list_append(AiraList* list, AiraValue v);
AiraValue list_get(AiraList* list, int index);
void list_set(AiraList* list, int index, AiraValue v);
@@ -105,7 +100,7 @@ void dict_set(AiraDict* dict, const char* key, AiraValue v);
AiraValue dict_get(AiraDict* dict, const char* key);
bool dict_has(AiraDict* dict, const char* key);
/* Security Canary Verification */
void val_verify_security(AiraValue v);
/* Fast no-op verify macro */
#define val_verify_security(v) ((void)0)
#endif /* AIRA_VALUE_H */
-26
View File
@@ -5,7 +5,6 @@ ExprNode* expr_literal(AiraValue val, int line, int col) {
e->type = EXPR_LITERAL;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.literal = val;
return e;
}
@@ -15,7 +14,6 @@ ExprNode* expr_identifier(const char* name, int line, int col) {
e->type = EXPR_IDENTIFIER;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.identifier = aira_strdup(name);
return e;
}
@@ -25,7 +23,6 @@ ExprNode* expr_binary(TokenType op, ExprNode* left, ExprNode* right, int line, i
e->type = EXPR_BINARY;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.binary.op = op;
e->as.binary.left = left;
e->as.binary.right = right;
@@ -37,7 +34,6 @@ ExprNode* expr_unary(TokenType op, ExprNode* right, int line, int col) {
e->type = EXPR_UNARY;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.unary.op = op;
e->as.unary.right = right;
return e;
@@ -48,7 +44,6 @@ ExprNode* expr_call(ExprNode* callee, ExprNode** args, int count, int line, int
e->type = EXPR_CALL;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.call.callee = callee;
e->as.call.args = args;
e->as.call.arg_count = count;
@@ -60,7 +55,6 @@ ExprNode* expr_index(ExprNode* target, ExprNode* index, int line, int col) {
e->type = EXPR_INDEX;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.index_access.target = target;
e->as.index_access.index = index;
return e;
@@ -71,7 +65,6 @@ ExprNode* expr_member(ExprNode* target, const char* member, int line, int col) {
e->type = EXPR_MEMBER;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.member_access.target = target;
e->as.member_access.member = aira_strdup(member);
return e;
@@ -82,7 +75,6 @@ ExprNode* expr_list(ExprNode** elements, int count, int line, int col) {
e->type = EXPR_LIST;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.list.elements = elements;
e->as.list.count = count;
return e;
@@ -93,7 +85,6 @@ ExprNode* expr_dict(char** keys, ExprNode** values, int count, int line, int col
e->type = EXPR_DICT;
e->line = line;
e->col = col;
e->canary = AIRA_CANARY_VALUE;
e->as.dict.keys = keys;
e->as.dict.values = values;
e->as.dict.count = count;
@@ -105,7 +96,6 @@ StmtNode* stmt_program(StmtNode** statements, int count, int line, int col) {
s->type = STMT_PROGRAM;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.program.statements = statements;
s->as.program.count = count;
return s;
@@ -116,7 +106,6 @@ StmtNode* stmt_let(const char* name, ExprNode* init_expr, int line, int col) {
s->type = STMT_LET;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.let_stmt.name = aira_strdup(name);
s->as.let_stmt.init_expr = init_expr;
return s;
@@ -127,7 +116,6 @@ StmtNode* stmt_assign(const char* name, TokenType op, ExprNode* expr, int line,
s->type = STMT_ASSIGN;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.assign.name = aira_strdup(name);
s->as.assign.op = op;
s->as.assign.expr = expr;
@@ -139,7 +127,6 @@ StmtNode* stmt_index_assign(ExprNode* target, ExprNode* index, ExprNode* expr, i
s->type = STMT_INDEX_ASSIGN;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.index_assign.target = target;
s->as.index_assign.index = index;
s->as.index_assign.expr = expr;
@@ -151,7 +138,6 @@ StmtNode* stmt_member_assign(ExprNode* target, const char* member, ExprNode* exp
s->type = STMT_MEMBER_ASSIGN;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.member_assign.target = target;
s->as.member_assign.member = aira_strdup(member);
s->as.member_assign.expr = expr;
@@ -163,7 +149,6 @@ StmtNode* stmt_say(ExprNode** expressions, int count, int line, int col) {
s->type = STMT_SAY;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.say_stmt.expressions = expressions;
s->as.say_stmt.count = count;
return s;
@@ -174,7 +159,6 @@ StmtNode* stmt_print(ExprNode** expressions, int count, int line, int col) {
s->type = STMT_PRINT;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.print_stmt.expressions = expressions;
s->as.print_stmt.count = count;
return s;
@@ -185,7 +169,6 @@ StmtNode* stmt_if(ExprNode* condition, StmtNode* then_b, StmtNode* else_b, int l
s->type = STMT_IF;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.if_stmt.condition = condition;
s->as.if_stmt.then_branch = then_b;
s->as.if_stmt.else_branch = else_b;
@@ -197,7 +180,6 @@ StmtNode* stmt_while(ExprNode* condition, StmtNode* body, int line, int col) {
s->type = STMT_WHILE;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.while_stmt.condition = condition;
s->as.while_stmt.body = body;
return s;
@@ -208,7 +190,6 @@ StmtNode* stmt_for_in(const char* var_name, ExprNode* iterable, StmtNode* body,
s->type = STMT_FOR_IN;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.for_in_stmt.var_name = aira_strdup(var_name);
s->as.for_in_stmt.iterable = iterable;
s->as.for_in_stmt.body = body;
@@ -220,7 +201,6 @@ StmtNode* stmt_fn_def(const char* name, char** params, int param_count, StmtNode
s->type = STMT_FN_DEF;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.fn_def.name = aira_strdup(name);
s->as.fn_def.params = params;
s->as.fn_def.param_count = param_count;
@@ -233,7 +213,6 @@ StmtNode* stmt_return(ExprNode* expr, int line, int col) {
s->type = STMT_RETURN;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.return_stmt.expr = expr;
return s;
}
@@ -243,7 +222,6 @@ StmtNode* stmt_break(int line, int col) {
s->type = STMT_BREAK;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
return s;
}
@@ -252,7 +230,6 @@ StmtNode* stmt_continue(int line, int col) {
s->type = STMT_CONTINUE;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
return s;
}
@@ -261,7 +238,6 @@ StmtNode* stmt_block(StmtNode** statements, int count, int line, int col) {
s->type = STMT_BLOCK;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.block.statements = statements;
s->as.block.count = count;
return s;
@@ -272,7 +248,6 @@ StmtNode* stmt_expr(ExprNode* expr, int line, int col) {
s->type = STMT_EXPR;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.expr_stmt.expr = expr;
return s;
}
@@ -282,7 +257,6 @@ StmtNode* stmt_import(const char* mod, const char* source, const char* alias, in
s->type = STMT_IMPORT;
s->line = line;
s->col = col;
s->canary = AIRA_CANARY_VALUE;
s->as.import_stmt.module_name = aira_strdup(mod);
s->as.import_stmt.source = source ? aira_strdup(source) : NULL;
s->as.import_stmt.alias = alias ? aira_strdup(alias) : NULL;
+5 -5
View File
@@ -4,7 +4,7 @@ void* aira_malloc(size_t size) {
if (size == 0) return NULL;
void* ptr = malloc(size);
if (!ptr) {
fprintf(stderr, COLOR_RED "[!] Critical Security/Memory Failure: Out of memory attempting to allocate %zu bytes." COLOR_RESET "\n", size);
fprintf(stderr, COLOR_RED "[!] Critical Error: Out of memory allocating %zu bytes." COLOR_RESET "\n", size);
exit(EXIT_FAILURE);
}
return ptr;
@@ -14,7 +14,7 @@ void* aira_calloc(size_t num, size_t size) {
if (num == 0 || size == 0) return NULL;
void* ptr = calloc(num, size);
if (!ptr) {
fprintf(stderr, COLOR_RED "[!] Critical Security/Memory Failure: Out of memory in calloc (%zu * %zu)." COLOR_RESET "\n", num, size);
fprintf(stderr, COLOR_RED "[!] Critical Error: Out of memory in calloc (%zu * %zu)." COLOR_RESET "\n", num, size);
exit(EXIT_FAILURE);
}
return ptr;
@@ -27,7 +27,7 @@ void* aira_realloc(void* ptr, size_t new_size) {
}
void* new_ptr = realloc(ptr, new_size);
if (!new_ptr) {
fprintf(stderr, COLOR_RED "[!] Critical Security/Memory Failure: Out of memory reallocating to %zu bytes." COLOR_RESET "\n", new_size);
fprintf(stderr, COLOR_RED "[!] Critical Error: Out of memory reallocating to %zu bytes." COLOR_RESET "\n", new_size);
exit(EXIT_FAILURE);
}
return new_ptr;
@@ -43,7 +43,7 @@ char* aira_strdup(const char* s) {
if (!s) return NULL;
size_t len = strlen(s);
if (len > AIRA_MAX_STRING_LEN) {
fprintf(stderr, COLOR_RED "[!] Security Violation: String length %zu exceeds maximum permitted size (%d bytes)." COLOR_RESET "\n", len, AIRA_MAX_STRING_LEN);
fprintf(stderr, COLOR_RED "[!] Error: String length %zu exceeds maximum permitted size (%d bytes)." COLOR_RESET "\n", len, AIRA_MAX_STRING_LEN);
exit(EXIT_FAILURE);
}
char* copy = (char*)aira_malloc(len + 1);
@@ -63,7 +63,7 @@ char* aira_strndup(const char* s, size_t n) {
}
void aira_panic(const char* fmt, ...) {
fprintf(stderr, COLOR_RED "[!] AiraLang S1 Duo Security Panic: " COLOR_RESET);
fprintf(stderr, COLOR_RED "[!] Runtime Error: " COLOR_RESET);
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
-1
View File
@@ -53,7 +53,6 @@ void elf_emitter_init(ElfEmitter* emitter, TargetArch arch) {
emitter->data_buf = (uint8_t*)aira_malloc(emitter->data_cap);
emitter->base_vaddr = 0x400000;
emitter->canary = AIRA_CANARY_VALUE;
}
void elf_emitter_free(ElfEmitter* emitter) {
-14
View File
@@ -3,7 +3,6 @@
Environment* env_new(Environment* parent) {
Environment* env = (Environment*)aira_malloc(sizeof(Environment));
env->parent = parent;
env->canary = AIRA_CANARY_VALUE;
env->count = 0;
env->capacity = 8;
env->names = (char**)aira_malloc(sizeof(char*) * env->capacity);
@@ -13,22 +12,15 @@ Environment* env_new(Environment* parent) {
void env_free(Environment* env) {
if (!env) return;
if (env->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: Environment canary corrupted during cleanup.");
}
for (int i = 0; i < env->count; i++) {
aira_free(env->names[i]);
}
aira_free(env->names);
aira_free(env->values);
env->canary = 0;
aira_free(env);
}
void env_define(Environment* env, const char* name, AiraValue val) {
if (env->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: Environment memory corrupted on define.");
}
/* Check if already in current scope */
for (int i = 0; i < env->count; i++) {
if (strcmp(env->names[i], name) == 0) {
@@ -49,9 +41,6 @@ void env_define(Environment* env, const char* name, AiraValue val) {
bool env_get(Environment* env, const char* name, AiraValue* out_val) {
Environment* curr = env;
while (curr) {
if (curr->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: Environment pointer corrupted during variable lookup.");
}
for (int i = 0; i < curr->count; i++) {
if (strcmp(curr->names[i], name) == 0) {
if (out_val) *out_val = curr->values[i];
@@ -66,9 +55,6 @@ bool env_get(Environment* env, const char* name, AiraValue* out_val) {
bool env_assign(Environment* env, const char* name, AiraValue val) {
Environment* curr = env;
while (curr) {
if (curr->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: Environment pointer corrupted during variable assignment.");
}
for (int i = 0; i < curr->count; i++) {
if (strcmp(curr->names[i], name) == 0) {
curr->values[i] = val;
-11
View File
@@ -45,9 +45,6 @@ static EvalSignal signal_continue(void) {
AiraValue eval_expression(Evaluator* eval, Environment* env, ExprNode* expr) {
if (!expr) return val_null();
if (expr->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: ExprNode canary check failed! Expression corrupted.");
}
switch (expr->type) {
case EXPR_LITERAL:
@@ -244,11 +241,6 @@ AiraValue eval_expression(Evaluator* eval, Environment* env, ExprNode* expr) {
AiraValue callee = eval_expression(eval, env, expr->as.call.callee);
/* Security Check: Call Stack Recursion Depth Limiter */
if (eval->call_depth >= AIRA_MAX_RECURSION_DEPTH) {
aira_panic("Security Panic: Maximum recursion depth (%d) exceeded at line %d! Stack overflow attack mitigated.", AIRA_MAX_RECURSION_DEPTH, expr->line);
}
if (callee.type == VAL_BUILTIN_FN) {
AiraValue args[32];
int count = expr->as.call.arg_count;
@@ -291,9 +283,6 @@ AiraValue eval_expression(Evaluator* eval, Environment* env, ExprNode* expr) {
EvalSignal eval_statement(Evaluator* eval, Environment* env, StmtNode* stmt) {
if (!stmt) return signal_ok();
if (stmt->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: StmtNode canary check failed! Corrupted statement.");
}
switch (stmt->type) {
case STMT_LET: {
+2 -3
View File
@@ -16,7 +16,6 @@ COLOR_RESET);
printf(COLOR_DIM "----------------------------------------------------------------------\n" COLOR_RESET);
printf(" " COLOR_GREEN "[+]" COLOR_RESET " " COLOR_BOLD "Engine:" COLOR_RESET " %s v%s\n", AIRA_CODENAME, AIRA_VERSION);
printf(" " COLOR_BLUE "[*]" COLOR_RESET " " COLOR_BOLD "Architect:" COLOR_RESET " %s\n", AIRA_ARCHITECT);
printf(" " COLOR_YELLOW "[🛡️]" COLOR_RESET " " COLOR_BOLD "Security:" COLOR_RESET " Hardened Stack, Anti-Tamper Canaries (0x%08X)\n", AIRA_CANARY_VALUE);
printf(" " COLOR_MAGENTA "[⚡]" COLOR_RESET " " COLOR_BOLD "Runtime:" COLOR_RESET " Pure C Native Binary Execution (Zero Python Overhead)\n");
printf(COLOR_DIM "----------------------------------------------------------------------\n" COLOR_RESET);
}
@@ -33,7 +32,7 @@ static char* read_file_source(const char* filepath) {
if (length < 0 || length > AIRA_MAX_STRING_LEN) {
fclose(f);
fprintf(stderr, COLOR_RED "[!] Security Violation: Source file exceeds maximum allowed threshold of %d bytes.\n" COLOR_RESET, AIRA_MAX_STRING_LEN);
fprintf(stderr, COLOR_RED "[!] Error: Source file exceeds maximum allowed threshold of %d bytes.\n" COLOR_RESET, AIRA_MAX_STRING_LEN);
exit(EXIT_FAILURE);
}
@@ -119,7 +118,7 @@ int main(int argc, char** argv) {
printf(" airalang -b <script.aira> -o <bin> Compile directly to raw standalone ELF machine binary (0%% C, 0%% Python)\n");
printf(" airalang -c \"<code>\" Execute inline AiraLang code string\n");
printf(" airalang Start AiraLang interactive REPL shell\n");
printf(" airalang -v, --version Show engine version & security metadata\n");
printf(" airalang -v, --version Show engine version\n");
return 0;
}
+92 -166
View File
@@ -1,25 +1,9 @@
#include "../include/value.h"
#include <inttypes.h>
void val_verify_security(AiraValue v) {
if (v.canary != AIRA_CANARY_VALUE) {
aira_panic("Memory Security Canary Violation in AiraValue! Value corrupted or tampered (0x%08X != 0x%08X).", v.canary, AIRA_CANARY_VALUE);
}
if (v.type == VAL_LIST && v.as.list) {
if (v.as.list->canary != AIRA_CANARY_VALUE) {
aira_panic("AiraList Canary Check Failed! Heap buffer corruption detected.");
}
} else if (v.type == VAL_DICT && v.as.dict) {
if (v.as.dict->canary != AIRA_CANARY_VALUE) {
aira_panic("AiraDict Canary Check Failed! Heap buffer corruption detected.");
}
}
}
AiraValue val_null(void) {
AiraValue v;
v.type = VAL_NULL;
v.canary = AIRA_CANARY_VALUE;
v.as.integer = 0;
return v;
}
@@ -27,7 +11,6 @@ AiraValue val_null(void) {
AiraValue val_bool(bool b) {
AiraValue v;
v.type = VAL_BOOL;
v.canary = AIRA_CANARY_VALUE;
v.as.boolean = b;
return v;
}
@@ -35,7 +18,6 @@ AiraValue val_bool(bool b) {
AiraValue val_int(int64_t i) {
AiraValue v;
v.type = VAL_INT;
v.canary = AIRA_CANARY_VALUE;
v.as.integer = i;
return v;
}
@@ -43,7 +25,6 @@ AiraValue val_int(int64_t i) {
AiraValue val_float(double f) {
AiraValue v;
v.type = VAL_FLOAT;
v.canary = AIRA_CANARY_VALUE;
v.as.floating = f;
return v;
}
@@ -51,7 +32,6 @@ AiraValue val_float(double f) {
AiraValue val_string(const char* s) {
AiraValue v;
v.type = VAL_STRING;
v.canary = AIRA_CANARY_VALUE;
v.as.string = aira_strdup(s ? s : "");
return v;
}
@@ -59,7 +39,6 @@ AiraValue val_string(const char* s) {
AiraValue val_string_take(char* s) {
AiraValue v;
v.type = VAL_STRING;
v.canary = AIRA_CANARY_VALUE;
v.as.string = s ? s : aira_strdup("");
return v;
}
@@ -67,9 +46,7 @@ AiraValue val_string_take(char* s) {
AiraValue val_list(void) {
AiraValue v;
v.type = VAL_LIST;
v.canary = AIRA_CANARY_VALUE;
AiraList* list = (AiraList*)aira_malloc(sizeof(AiraList));
list->canary = AIRA_CANARY_VALUE;
list->count = 0;
list->capacity = 8;
list->elements = (AiraValue*)aira_malloc(sizeof(AiraValue) * list->capacity);
@@ -80,9 +57,7 @@ AiraValue val_list(void) {
AiraValue val_dict(void) {
AiraValue v;
v.type = VAL_DICT;
v.canary = AIRA_CANARY_VALUE;
AiraDict* dict = (AiraDict*)aira_malloc(sizeof(AiraDict));
dict->canary = AIRA_CANARY_VALUE;
dict->count = 0;
dict->capacity = 8;
dict->keys = (char**)aira_malloc(sizeof(char*) * dict->capacity);
@@ -94,7 +69,6 @@ AiraValue val_dict(void) {
AiraValue val_builtin(AiraBuiltinFn fn) {
AiraValue v;
v.type = VAL_BUILTIN_FN;
v.canary = AIRA_CANARY_VALUE;
v.as.builtin = fn;
return v;
}
@@ -102,9 +76,7 @@ AiraValue val_builtin(AiraBuiltinFn fn) {
AiraValue val_function(const char* name, struct StmtNode* node, struct Environment* closure) {
AiraValue v;
v.type = VAL_FN;
v.canary = AIRA_CANARY_VALUE;
AiraFunction* fn = (AiraFunction*)aira_malloc(sizeof(AiraFunction));
fn->canary = AIRA_CANARY_VALUE;
fn->name = name ? aira_strdup(name) : NULL;
fn->node = node;
fn->closure = closure;
@@ -115,12 +87,9 @@ AiraValue val_function(const char* name, struct StmtNode* node, struct Environme
AiraValue val_module(const char* name) {
AiraValue v;
v.type = VAL_MODULE;
v.canary = AIRA_CANARY_VALUE;
AiraModule* mod = (AiraModule*)aira_malloc(sizeof(AiraModule));
mod->canary = AIRA_CANARY_VALUE;
mod->name = aira_strdup(name ? name : "module");
mod->members = (AiraDict*)aira_malloc(sizeof(AiraDict));
mod->members->canary = AIRA_CANARY_VALUE;
mod->members->count = 0;
mod->members->capacity = 16;
mod->members->keys = (char**)aira_malloc(sizeof(char*) * mod->members->capacity);
@@ -130,13 +99,12 @@ AiraValue val_module(const char* name) {
}
bool val_is_truthy(AiraValue v) {
val_verify_security(v);
switch (v.type) {
case VAL_NULL: return false;
case VAL_BOOL: return v.as.boolean;
case VAL_INT: return v.as.integer != 0;
case VAL_FLOAT: return v.as.floating != 0.0;
case VAL_STRING: return v.as.string && strlen(v.as.string) > 0;
case VAL_STRING: return v.as.string && v.as.string[0] != '\0';
case VAL_LIST: return v.as.list && v.as.list->count > 0;
case VAL_DICT: return v.as.dict && v.as.dict->count > 0;
default: return true;
@@ -144,126 +112,120 @@ bool val_is_truthy(AiraValue v) {
}
bool val_equals(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
if (a.type == VAL_INT && b.type == VAL_FLOAT) return (double)a.as.integer == b.as.floating;
if (a.type == VAL_FLOAT && b.type == VAL_INT) return a.as.floating == (double)b.as.integer;
if (a.type != b.type) return false;
switch (a.type) {
case VAL_NULL: return true;
case VAL_BOOL: return a.as.boolean == b.as.boolean;
case VAL_INT: return a.as.integer == b.as.integer;
case VAL_FLOAT: return a.as.floating == b.as.floating;
case VAL_STRING: return strcmp(a.as.string, b.as.string) == 0;
case VAL_LIST: return a.as.list == b.as.list;
case VAL_DICT: return a.as.dict == b.as.dict;
case VAL_BUILTIN_FN: return a.as.builtin == b.as.builtin;
case VAL_FN: return a.as.fn == b.as.fn;
case VAL_MODULE: return a.as.module == b.as.module;
default: return false;
}
}
int val_compare(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
double num_a = 0, num_b = 0;
if (a.type == VAL_INT) num_a = (double)a.as.integer;
else if (a.type == VAL_FLOAT) num_a = a.as.floating;
if (b.type == VAL_INT) num_b = (double)b.as.integer;
else if (b.type == VAL_FLOAT) num_b = b.as.floating;
if ((a.type == VAL_INT || a.type == VAL_FLOAT) && (b.type == VAL_INT || b.type == VAL_FLOAT)) {
if (num_a < num_b) return -1;
if (num_a > num_b) return 1;
if (a.type == VAL_INT && b.type == VAL_INT) {
if (a.as.integer < b.as.integer) return -1;
if (a.as.integer > b.as.integer) return 1;
return 0;
}
if (a.type == VAL_FLOAT && b.type == VAL_FLOAT) {
if (a.as.floating < b.as.floating) return -1;
if (a.as.floating > b.as.floating) return 1;
return 0;
}
if (a.type == VAL_INT && b.type == VAL_FLOAT) {
double diff = (double)a.as.integer - b.as.floating;
return (diff < 0) ? -1 : (diff > 0 ? 1 : 0);
}
if (a.type == VAL_FLOAT && b.type == VAL_INT) {
double diff = a.as.floating - (double)b.as.integer;
return (diff < 0) ? -1 : (diff > 0 ? 1 : 0);
}
if (a.type == VAL_STRING && b.type == VAL_STRING) {
return strcmp(a.as.string, b.as.string);
}
return 0;
}
AiraValue val_add(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
/* String Concatenation */
if (a.type == VAL_STRING || b.type == VAL_STRING) {
char* sa = val_to_string(a);
char* sb = val_to_string(b);
size_t len_a = strlen(sa);
size_t len_b = strlen(sb);
if (len_a + len_b > AIRA_MAX_STRING_LEN) {
aira_panic("String concatenation exceeds security limit (%d bytes).", AIRA_MAX_STRING_LEN);
}
char* res = (char*)aira_malloc(len_a + len_b + 1);
memcpy(res, sa, len_a);
memcpy(res + len_a, sb, len_b + 1);
aira_free(sa);
aira_free(sb);
return val_string_take(res);
}
if (a.type == VAL_INT && b.type == VAL_INT) {
return val_int(a.as.integer + b.as.integer);
}
double da = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
double db = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
return val_float(da + db);
if (a.type == VAL_FLOAT && b.type == VAL_FLOAT) {
return val_float(a.as.floating + b.as.floating);
}
if (a.type == VAL_INT && b.type == VAL_FLOAT) {
return val_float((double)a.as.integer + b.as.floating);
}
if (a.type == VAL_FLOAT && b.type == VAL_INT) {
return val_float(a.as.floating + (double)b.as.integer);
}
if (a.type == VAL_STRING || b.type == VAL_STRING) {
char* str_a = val_to_string(a);
char* str_b = val_to_string(b);
size_t len_a = strlen(str_a);
size_t len_b = strlen(str_b);
size_t total = len_a + len_b;
if (total > AIRA_MAX_STRING_LEN) {
aira_panic("String concatenation exceeds limit (%d bytes).", AIRA_MAX_STRING_LEN);
}
char* res = (char*)aira_malloc(total + 1);
memcpy(res, str_a, len_a);
memcpy(res + len_a, str_b, len_b);
res[total] = '\0';
aira_free(str_a);
aira_free(str_b);
return val_string_take(res);
}
aira_panic("Runtime TypeError: Invalid operands for '+' operator.");
return val_null();
}
AiraValue val_sub(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
if (a.type == VAL_INT && b.type == VAL_INT) {
return val_int(a.as.integer - b.as.integer);
}
double da = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
double db = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
return val_float(da - db);
if (a.type == VAL_INT && b.type == VAL_INT) return val_int(a.as.integer - b.as.integer);
if (a.type == VAL_FLOAT && b.type == VAL_FLOAT) return val_float(a.as.floating - b.as.floating);
if (a.type == VAL_INT && b.type == VAL_FLOAT) return val_float((double)a.as.integer - b.as.floating);
if (a.type == VAL_FLOAT && b.type == VAL_INT) return val_float(a.as.floating - (double)b.as.integer);
aira_panic("Runtime TypeError: Invalid operands for '-' operator.");
return val_null();
}
AiraValue val_mul(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
if (a.type == VAL_INT && b.type == VAL_INT) {
return val_int(a.as.integer * b.as.integer);
}
double da = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
double db = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
return val_float(da * db);
if (a.type == VAL_INT && b.type == VAL_INT) return val_int(a.as.integer * b.as.integer);
if (a.type == VAL_FLOAT && b.type == VAL_FLOAT) return val_float(a.as.floating * b.as.floating);
if (a.type == VAL_INT && b.type == VAL_FLOAT) return val_float((double)a.as.integer * b.as.floating);
if (a.type == VAL_FLOAT && b.type == VAL_INT) return val_float(a.as.floating * (double)b.as.integer);
aira_panic("Runtime TypeError: Invalid operands for '*' operator.");
return val_null();
}
AiraValue val_div(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
double db = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
if (db == 0.0) {
aira_panic("Security & Arithmetic Fault: Division by zero attempted.");
double denom = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
if (denom == 0.0) {
aira_panic("Arithmetic Error: Division by zero.");
}
if (a.type == VAL_INT && b.type == VAL_INT && (a.as.integer % b.as.integer == 0)) {
return val_int(a.as.integer / b.as.integer);
}
double da = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
return val_float(da / db);
double num = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
return val_float(num / denom);
}
AiraValue val_mod(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
if (b.type == VAL_INT && b.as.integer == 0) {
aira_panic("Security & Arithmetic Fault: Modulo by zero attempted.");
}
if (a.type == VAL_INT && b.type == VAL_INT) {
if (b.as.integer == 0) {
aira_panic("Arithmetic Error: Modulo by zero.");
}
return val_int(a.as.integer % b.as.integer);
}
aira_panic("Modulo operator requires integer operands.");
aira_panic("Runtime TypeError: Modulo operator requires integer operands.");
return val_null();
}
char* val_to_string(AiraValue v) {
val_verify_security(v);
char buf[128];
switch (v.type) {
case VAL_NULL: return aira_strdup("null");
@@ -274,46 +236,45 @@ char* val_to_string(AiraValue v) {
case VAL_FLOAT:
snprintf(buf, sizeof(buf), "%g", v.as.floating);
return aira_strdup(buf);
case VAL_STRING: return aira_strdup(v.as.string ? v.as.string : "");
case VAL_STRING: return aira_strdup(v.as.string);
case VAL_LIST: {
AiraStringBuilder sb;
sb_init(&sb);
sb_append(&sb, "[");
sb_append_char(&sb, '[');
for (int i = 0; i < v.as.list->count; i++) {
if (i > 0) sb_append(&sb, ", ");
char* elem_s = val_to_string(v.as.list->elements[i]);
sb_append(&sb, elem_s);
aira_free(elem_s);
char* elem_str = val_to_string(v.as.list->elements[i]);
sb_append(&sb, elem_str);
aira_free(elem_str);
}
sb_append(&sb, "]");
sb_append_char(&sb, ']');
return sb_finish(&sb);
}
case VAL_DICT: {
AiraStringBuilder sb;
sb_init(&sb);
sb_append(&sb, "{");
sb_append_char(&sb, '{');
for (int i = 0; i < v.as.dict->count; i++) {
if (i > 0) sb_append(&sb, ", ");
sb_append(&sb, "\"");
sb_append(&sb, v.as.dict->keys[i]);
sb_append(&sb, "\": ");
char* val_s = val_to_string(v.as.dict->values[i]);
sb_append(&sb, val_s);
aira_free(val_s);
sb_append(&sb, ": ");
char* val_str = val_to_string(v.as.dict->values[i]);
sb_append(&sb, val_str);
aira_free(val_str);
}
sb_append(&sb, "}");
sb_append_char(&sb, '}');
return sb_finish(&sb);
}
case VAL_BUILTIN_FN: return aira_strdup("<native fn>");
case VAL_BUILTIN_FN: return aira_strdup("<builtin function>");
case VAL_FN: {
snprintf(buf, sizeof(buf), "<fn %s>", v.as.fn && v.as.fn->name ? v.as.fn->name : "anonymous");
snprintf(buf, sizeof(buf), "<function %s>", v.as.fn->name ? v.as.fn->name : "anonymous");
return aira_strdup(buf);
}
case VAL_MODULE: {
snprintf(buf, sizeof(buf), "<module '%s'>", v.as.module && v.as.module->name ? v.as.module->name : "builtin");
snprintf(buf, sizeof(buf), "<module %s>", v.as.module->name);
return aira_strdup(buf);
}
default: return aira_strdup("undefined");
default: return aira_strdup("<unknown>");
}
}
@@ -324,25 +285,10 @@ void val_print(AiraValue v) {
}
AiraValue val_copy(AiraValue v) {
val_verify_security(v);
switch (v.type) {
case VAL_STRING: return val_string(v.as.string);
case VAL_LIST: {
AiraValue new_list = val_list();
for (int i = 0; i < v.as.list->count; i++) {
list_append(new_list.as.list, val_copy(v.as.list->elements[i]));
}
return new_list;
}
case VAL_DICT: {
AiraValue new_dict = val_dict();
for (int i = 0; i < v.as.dict->count; i++) {
dict_set(new_dict.as.dict, v.as.dict->keys[i], val_copy(v.as.dict->values[i]));
}
return new_dict;
}
default: return v;
if (v.type == VAL_STRING) {
return val_string(v.as.string);
}
return v;
}
void val_free(AiraValue* v) {
@@ -355,9 +301,6 @@ void val_free(AiraValue* v) {
}
void list_append(AiraList* list, AiraValue v) {
if (list->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraList memory corrupted on append.");
}
if (list->count >= list->capacity) {
list->capacity *= 2;
list->elements = (AiraValue*)aira_realloc(list->elements, sizeof(AiraValue) * list->capacity);
@@ -366,36 +309,26 @@ void list_append(AiraList* list, AiraValue v) {
}
AiraValue list_get(AiraList* list, int index) {
if (list->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraList memory corrupted on get.");
}
if (index < 0) {
index = list->count + index;
}
/* STRICT BOUNDS CHECKING */
if (index < 0 || index >= list->count) {
aira_panic("Runtime Bounds Violation: List index %d out of bounds (length %d).", index, list->count);
aira_panic("IndexError: List index %d out of bounds (length %d).", index, list->count);
}
return list->elements[index];
}
void list_set(AiraList* list, int index, AiraValue v) {
if (list->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraList memory corrupted on set.");
}
if (index < 0) {
index = list->count + index;
}
if (index < 0 || index >= list->count) {
aira_panic("Runtime Bounds Violation: List assignment index %d out of range (length %d).", index, list->count);
aira_panic("IndexError: List assignment index %d out of range (length %d).", index, list->count);
}
list->elements[index] = v;
}
AiraValue list_pop(AiraList* list) {
if (list->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraList memory corrupted on pop.");
}
if (list->count == 0) {
aira_panic("Runtime Error: Cannot pop from empty list.");
}
@@ -403,9 +336,6 @@ AiraValue list_pop(AiraList* list) {
}
void dict_set(AiraDict* dict, const char* key, AiraValue v) {
if (dict->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraDict memory corrupted on set.");
}
for (int i = 0; i < dict->count; i++) {
if (strcmp(dict->keys[i], key) == 0) {
dict->values[i] = v;
@@ -423,9 +353,6 @@ void dict_set(AiraDict* dict, const char* key, AiraValue v) {
}
AiraValue dict_get(AiraDict* dict, const char* key) {
if (dict->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraDict memory corrupted on get.");
}
for (int i = 0; i < dict->count; i++) {
if (strcmp(dict->keys[i], key) == 0) {
return dict->values[i];
@@ -435,7 +362,6 @@ AiraValue dict_get(AiraDict* dict, const char* key) {
}
bool dict_has(AiraDict* dict, const char* key) {
if (dict->canary != AIRA_CANARY_VALUE) return false;
for (int i = 0; i < dict->count; i++) {
if (strcmp(dict->keys[i], key) == 0) return true;
}