feat(s1duo): implement pure C AiraLang S1 Duo native engine with military-grade security hardening

This commit is contained in:
Adam Eehan committed 2026-10-28 12:13:33 +05:30
1 parent 97146cb5c3
commit 8ac2b4c48e
25 files changed
+3681 -1

No files matched your search

+191
View File
@@ -0,0 +1,191 @@
#ifndef AIRA_AST_H
#define AIRA_AST_H
#include "token.h"
#include "value.h"
typedef enum {
STMT_PROGRAM,
STMT_LET,
STMT_ASSIGN,
STMT_INDEX_ASSIGN,
STMT_MEMBER_ASSIGN,
STMT_SAY,
STMT_PRINT,
STMT_IF,
STMT_WHILE,
STMT_FOR_IN,
STMT_FN_DEF,
STMT_RETURN,
STMT_BREAK,
STMT_CONTINUE,
STMT_BLOCK,
STMT_EXPR,
STMT_IMPORT
} StmtType;
typedef enum {
EXPR_LITERAL,
EXPR_IDENTIFIER,
EXPR_BINARY,
EXPR_UNARY,
EXPR_CALL,
EXPR_INDEX,
EXPR_MEMBER,
EXPR_LIST,
EXPR_DICT
} ExprType;
typedef struct ExprNode ExprNode;
typedef struct StmtNode StmtNode;
struct ExprNode {
ExprType type;
int line;
int col;
uint32_t canary;
union {
AiraValue literal;
char* identifier;
struct {
TokenType op;
ExprNode* left;
ExprNode* right;
} binary;
struct {
TokenType op;
ExprNode* right;
} unary;
struct {
ExprNode* callee;
ExprNode** args;
int arg_count;
} call;
struct {
ExprNode* target;
ExprNode* index;
} index_access;
struct {
ExprNode* target;
char* member;
} member_access;
struct {
ExprNode** elements;
int count;
} list;
struct {
char** keys;
ExprNode** values;
int count;
} dict;
} as;
};
struct StmtNode {
StmtType type;
int line;
int col;
uint32_t canary;
union {
struct {
StmtNode** statements;
int count;
} program;
struct {
char* name;
ExprNode* init_expr;
} let_stmt;
struct {
char* name;
TokenType op;
ExprNode* expr;
} assign;
struct {
ExprNode* target;
ExprNode* index;
ExprNode* expr;
} index_assign;
struct {
ExprNode* target;
char* member;
ExprNode* expr;
} member_assign;
struct {
ExprNode** expressions;
int count;
} say_stmt;
struct {
ExprNode** expressions;
int count;
} print_stmt;
struct {
ExprNode* condition;
StmtNode* then_branch;
StmtNode* else_branch;
} if_stmt;
struct {
ExprNode* condition;
StmtNode* body;
} while_stmt;
struct {
char* var_name;
ExprNode* iterable;
StmtNode* body;
} for_in_stmt;
struct {
char* name;
char** params;
int param_count;
StmtNode* body;
} fn_def;
struct {
ExprNode* expr;
} return_stmt;
struct {
StmtNode** statements;
int count;
} block;
struct {
ExprNode* expr;
} expr_stmt;
struct {
char* module_name;
char* source;
char* alias;
} import_stmt;
} as;
};
/* AST Constructors */
ExprNode* expr_literal(AiraValue val, int line, int col);
ExprNode* expr_identifier(const char* name, int line, int col);
ExprNode* expr_binary(TokenType op, ExprNode* left, ExprNode* right, int line, int col);
ExprNode* expr_unary(TokenType op, ExprNode* right, int line, int col);
ExprNode* expr_call(ExprNode* callee, ExprNode** args, int count, int line, int col);
ExprNode* expr_index(ExprNode* target, ExprNode* index, int line, int col);
ExprNode* expr_member(ExprNode* target, const char* member, int line, int col);
ExprNode* expr_list(ExprNode** elements, int count, int line, int col);
ExprNode* expr_dict(char** keys, ExprNode** values, int count, int line, int col);
StmtNode* stmt_program(StmtNode** statements, int count, int line, int col);
StmtNode* stmt_let(const char* name, ExprNode* init_expr, int line, int col);
StmtNode* stmt_assign(const char* name, TokenType op, ExprNode* expr, int line, int col);
StmtNode* stmt_index_assign(ExprNode* target, ExprNode* index, ExprNode* expr, int line, int col);
StmtNode* stmt_member_assign(ExprNode* target, const char* member, ExprNode* expr, int line, int col);
StmtNode* stmt_say(ExprNode** expressions, int count, int line, int col);
StmtNode* stmt_print(ExprNode** expressions, int count, int line, int col);
StmtNode* stmt_if(ExprNode* condition, StmtNode* then_b, StmtNode* else_b, int line, int col);
StmtNode* stmt_while(ExprNode* condition, StmtNode* body, int line, int col);
StmtNode* stmt_for_in(const char* var_name, ExprNode* iterable, StmtNode* body, int line, int col);
StmtNode* stmt_fn_def(const char* name, char** params, int param_count, StmtNode* body, int line, int col);
StmtNode* stmt_return(ExprNode* expr, int line, int col);
StmtNode* stmt_break(int line, int col);
StmtNode* stmt_continue(int line, int col);
StmtNode* stmt_block(StmtNode** statements, int count, int line, int col);
StmtNode* stmt_expr(ExprNode* expr, int line, int col);
StmtNode* stmt_import(const char* mod, const char* source, const char* alias, int line, int col);
void expr_free(ExprNode* expr);
void stmt_free(StmtNode* stmt);
#endif /* AIRA_AST_H */
+59
View File
@@ -0,0 +1,59 @@
#ifndef AIRA_COMMON_H
#define AIRA_COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#define AIRA_VERSION "3.0.0 (S1 Duo Edition)"
#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' */
/* ANSI Color Formatting */
#define COLOR_RESET "\033[0m"
#define COLOR_RED "\033[1;31m"
#define COLOR_GREEN "\033[1;32m"
#define COLOR_YELLOW "\033[1;33m"
#define COLOR_BLUE "\033[1;34m"
#define COLOR_MAGENTA "\033[1;35m"
#define COLOR_CYAN "\033[1;36m"
#define COLOR_WHITE "\033[1;37m"
#define COLOR_BOLD "\033[1m"
#define COLOR_DIM "\033[2m"
/* Safe Memory Allocation Functions */
void* aira_malloc(size_t size);
void* aira_calloc(size_t num, size_t size);
void* aira_realloc(void* ptr, size_t new_size);
void aira_free(void* ptr);
char* aira_strdup(const char* s);
char* aira_strndup(const char* s, size_t n);
/* Diagnostic & Security Logging */
void aira_panic(const char* fmt, ...);
void aira_error_at(const char* filename, int line, int col, const char* fmt, ...);
/* Dynamic String Buffer */
typedef struct {
char* data;
size_t length;
size_t capacity;
} AiraStringBuilder;
void sb_init(AiraStringBuilder* sb);
void sb_append(AiraStringBuilder* sb, const char* str);
void sb_append_len(AiraStringBuilder* sb, const char* str, size_t len);
void sb_append_char(AiraStringBuilder* sb, char c);
char* sb_finish(AiraStringBuilder* sb);
void sb_free(AiraStringBuilder* sb);
#endif /* AIRA_COMMON_H */
+21
View File
@@ -0,0 +1,21 @@
#ifndef AIRA_ENV_H
#define AIRA_ENV_H
#include "value.h"
typedef struct Environment {
struct Environment* parent;
char** names;
AiraValue* values;
int count;
int capacity;
uint32_t canary;
} Environment;
Environment* env_new(Environment* parent);
void env_free(Environment* env);
void env_define(Environment* env, const char* name, AiraValue val);
bool env_get(Environment* env, const char* name, AiraValue* out_val);
bool env_assign(Environment* env, const char* name, AiraValue val);
#endif /* AIRA_ENV_H */
+32
View File
@@ -0,0 +1,32 @@
#ifndef AIRA_EVALUATOR_H
#define AIRA_EVALUATOR_H
#include "ast.h"
#include "env.h"
typedef enum {
EXEC_OK = 0,
EXEC_RETURN,
EXEC_BREAK,
EXEC_CONTINUE,
EXEC_ERROR
} ExecResult;
typedef struct {
ExecResult type;
AiraValue value;
} EvalSignal;
typedef struct {
Environment* global_env;
int call_depth;
bool had_runtime_error;
} Evaluator;
void evaluator_init(Evaluator* eval);
void evaluator_cleanup(Evaluator* eval);
AiraValue eval_expression(Evaluator* eval, Environment* env, ExprNode* expr);
EvalSignal eval_statement(Evaluator* eval, Environment* env, StmtNode* stmt);
void eval_program(Evaluator* eval, StmtNode* program);
#endif /* AIRA_EVALUATOR_H */
+18
View File
@@ -0,0 +1,18 @@
#ifndef AIRA_LEXER_H
#define AIRA_LEXER_H
#include "token.h"
typedef struct {
const char* source;
const char* filename;
size_t length;
size_t current;
int line;
int col;
} Lexer;
void lexer_init(Lexer* lexer, const char* source, const char* filename);
Token lexer_next_token(Lexer* lexer);
#endif /* AIRA_LEXER_H */
+13
View File
@@ -0,0 +1,13 @@
#ifndef AIRA_MODULES_H
#define AIRA_MODULES_H
#include "value.h"
#include "env.h"
void register_builtin_globals(Environment* env);
AiraValue load_builtin_module(const char* name);
/* Pure C SHA256 */
void aira_sha256(const uint8_t* data, size_t len, char output[65]);
#endif /* AIRA_MODULES_H */
+19
View File
@@ -0,0 +1,19 @@
#ifndef AIRA_PARSER_H
#define AIRA_PARSER_H
#include "lexer.h"
#include "ast.h"
typedef struct {
Lexer lexer;
Token current;
Token previous;
bool had_error;
bool panic_mode;
const char* filename;
} Parser;
void parser_init(Parser* parser, const char* source, const char* filename);
StmtNode* parser_parse(Parser* parser);
#endif /* AIRA_PARSER_H */
+92
View File
@@ -0,0 +1,92 @@
#ifndef AIRA_TOKEN_H
#define AIRA_TOKEN_H
#include "common.h"
typedef enum {
TOKEN_EOF = 0,
TOKEN_ERROR,
/* Literals */
TOKEN_NUMBER,
TOKEN_STRING,
TOKEN_IDENTIFIER,
/* Keywords */
TOKEN_LET,
TOKEN_SAY,
TOKEN_PRINT,
TOKEN_IF,
TOKEN_ELSE,
TOKEN_WHILE,
TOKEN_FOR,
TOKEN_IN,
TOKEN_FN,
TOKEN_RETURN,
TOKEN_IMPORT,
TOKEN_FROM,
TOKEN_TRUE,
TOKEN_FALSE,
TOKEN_NULL,
TOKEN_AND,
TOKEN_OR,
TOKEN_NOT,
TOKEN_TRY,
TOKEN_CATCH,
TOKEN_THROW,
TOKEN_CLASS,
TOKEN_NEW,
TOKEN_THIS,
TOKEN_BREAK,
TOKEN_CONTINUE,
/* Arithmetic & Assignment */
TOKEN_PLUS, /* + */
TOKEN_MINUS, /* - */
TOKEN_STAR, /* * */
TOKEN_SLASH, /* / */
TOKEN_PERCENT, /* % */
TOKEN_ASSIGN, /* = */
TOKEN_PLUS_ASSIGN, /* += */
TOKEN_MINUS_ASSIGN, /* -= */
TOKEN_STAR_ASSIGN, /* *= */
TOKEN_SLASH_ASSIGN, /* /= */
TOKEN_MOD_ASSIGN, /* %= */
/* Comparison & Logical */
TOKEN_EQUAL_EQUAL, /* == */
TOKEN_BANG_EQUAL, /* != */
TOKEN_LESS, /* < */
TOKEN_LESS_EQUAL, /* <= */
TOKEN_GREATER, /* > */
TOKEN_GREATER_EQUAL,/* >= */
TOKEN_BANG, /* ! */
TOKEN_AND_AND, /* && */
TOKEN_OR_OR, /* || */
/* Delimiters & Punctuation */
TOKEN_LPAREN, /* ( */
TOKEN_RPAREN, /* ) */
TOKEN_LBRACE, /* { */
TOKEN_RBRACE, /* } */
TOKEN_LBRACKET, /* [ */
TOKEN_RBRACKET, /* ] */
TOKEN_COMMA, /* , */
TOKEN_DOT, /* . */
TOKEN_COLON, /* : */
TOKEN_SEMICOLON, /* ; */
TOKEN_AT /* @ */
} TokenType;
typedef struct {
TokenType type;
char* text;
int line;
int col;
double num_val;
} Token;
const char* token_type_name(TokenType type);
void token_free(Token* token);
#endif /* AIRA_TOKEN_H */
+111
View File
@@ -0,0 +1,111 @@
#ifndef AIRA_VALUE_H
#define AIRA_VALUE_H
#include "common.h"
typedef enum {
VAL_NULL = 0,
VAL_BOOL,
VAL_INT,
VAL_FLOAT,
VAL_STRING,
VAL_LIST,
VAL_DICT,
VAL_BUILTIN_FN,
VAL_FN,
VAL_MODULE
} ValueType;
struct AiraValue;
struct Environment;
struct StmtNode;
typedef struct AiraValue (*AiraBuiltinFn)(int arg_count, struct AiraValue* args);
typedef struct {
struct AiraValue* elements;
int count;
int capacity;
uint32_t canary;
} AiraList;
typedef struct {
char** keys;
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;
double floating;
char* string;
AiraList* list;
AiraDict* dict;
AiraBuiltinFn builtin;
AiraFunction* fn;
AiraModule* module;
} as;
} AiraValue;
/* Value Constructors */
AiraValue val_null(void);
AiraValue val_bool(bool b);
AiraValue val_int(int64_t i);
AiraValue val_float(double f);
AiraValue val_string(const char* s);
AiraValue val_string_take(char* s);
AiraValue val_list(void);
AiraValue val_dict(void);
AiraValue val_builtin(AiraBuiltinFn fn);
AiraValue val_function(const char* name, struct StmtNode* node, struct Environment* closure);
AiraValue val_module(const char* name);
/* Value Operations */
bool val_is_truthy(AiraValue v);
bool val_equals(AiraValue a, AiraValue b);
int val_compare(AiraValue a, AiraValue b);
AiraValue val_add(AiraValue a, AiraValue b);
AiraValue val_sub(AiraValue a, AiraValue b);
AiraValue val_mul(AiraValue a, AiraValue b);
AiraValue val_div(AiraValue a, AiraValue b);
AiraValue val_mod(AiraValue a, AiraValue b);
/* String representation & Print */
char* val_to_string(AiraValue v);
void val_print(AiraValue v);
void val_free(AiraValue* v);
AiraValue val_copy(AiraValue v);
/* List and Dict Security & Bounds Helpers */
void list_append(AiraList* list, AiraValue v);
AiraValue list_get(AiraList* list, int index);
void list_set(AiraList* list, int index, AiraValue v);
AiraValue list_pop(AiraList* list);
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);
#endif /* AIRA_VALUE_H */