feat(s1duo): implement pure C AiraLang S1 Duo native engine with military-grade security hardening
This commit is contained in:
1 parent
97146cb5c3
commit
8ac2b4c48e
25 files changed
+3681
-1
No files matched your search
@@ -4,3 +4,6 @@ __pycache__/
|
||||
.DS_Store
|
||||
*.tmp
|
||||
*.log
|
||||
*.o
|
||||
s1duo/bin/
|
||||
s1duo/src/*.o
|
||||
@@ -0,0 +1,30 @@
|
||||
CC = clang
|
||||
CFLAGS = -O3 -Wall -Wextra -Wno-unused-parameter -Iinclude -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fPIE
|
||||
LDFLAGS = -pie -Wl,-z,relro,-z,now -lm
|
||||
|
||||
SRCS = src/common.c src/lexer.c src/value.c src/ast.c src/parser.c src/env.c src/modules.c src/evaluator.c src/main.c
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
TARGET = bin/airalang3
|
||||
INSTALL_DIR = /data/data/com.termux/files/usr/bin
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
@mkdir -p bin
|
||||
$(CC) $(OBJS) -o $(TARGET) $(LDFLAGS)
|
||||
@echo "\033[1;32m[✓] AiraLang S1 Duo Native Binary successfully compiled!\033[0m"
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
install: $(TARGET)
|
||||
@mkdir -p $(INSTALL_DIR)
|
||||
cp $(TARGET) $(INSTALL_DIR)/airalang3
|
||||
chmod 755 $(INSTALL_DIR)/airalang3
|
||||
@echo "\033[1;32m[✓] Installed airalang3 to $(INSTALL_DIR)/airalang3\033[0m"
|
||||
|
||||
clean:
|
||||
rm -rf src/*.o bin/
|
||||
@echo "\033[1;33m[*] Cleaned build artifacts.\033[0m"
|
||||
|
||||
.PHONY: all clean install
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
+422
@@ -0,0 +1,422 @@
|
||||
#include "../include/ast.h"
|
||||
|
||||
ExprNode* expr_literal(AiraValue val, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
e->type = EXPR_LITERAL;
|
||||
e->line = line;
|
||||
e->col = col;
|
||||
e->canary = AIRA_CANARY_VALUE;
|
||||
e->as.literal = val;
|
||||
return e;
|
||||
}
|
||||
|
||||
ExprNode* expr_identifier(const char* name, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
e->type = EXPR_IDENTIFIER;
|
||||
e->line = line;
|
||||
e->col = col;
|
||||
e->canary = AIRA_CANARY_VALUE;
|
||||
e->as.identifier = aira_strdup(name);
|
||||
return e;
|
||||
}
|
||||
|
||||
ExprNode* expr_binary(TokenType op, ExprNode* left, ExprNode* right, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
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;
|
||||
return e;
|
||||
}
|
||||
|
||||
ExprNode* expr_unary(TokenType op, ExprNode* right, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
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;
|
||||
}
|
||||
|
||||
ExprNode* expr_call(ExprNode* callee, ExprNode** args, int count, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
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;
|
||||
return e;
|
||||
}
|
||||
|
||||
ExprNode* expr_index(ExprNode* target, ExprNode* index, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
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;
|
||||
}
|
||||
|
||||
ExprNode* expr_member(ExprNode* target, const char* member, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
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;
|
||||
}
|
||||
|
||||
ExprNode* expr_list(ExprNode** elements, int count, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
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;
|
||||
}
|
||||
|
||||
ExprNode* expr_dict(char** keys, ExprNode** values, int count, int line, int col) {
|
||||
ExprNode* e = (ExprNode*)aira_malloc(sizeof(ExprNode));
|
||||
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;
|
||||
return e;
|
||||
}
|
||||
|
||||
StmtNode* stmt_program(StmtNode** statements, int count, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
}
|
||||
|
||||
StmtNode* stmt_let(const char* name, ExprNode* init_expr, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
}
|
||||
|
||||
StmtNode* stmt_assign(const char* name, TokenType op, ExprNode* expr, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_index_assign(ExprNode* target, ExprNode* index, ExprNode* expr, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_member_assign(ExprNode* target, const char* member, ExprNode* expr, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_say(ExprNode** expressions, int count, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
}
|
||||
|
||||
StmtNode* stmt_print(ExprNode** expressions, int count, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
}
|
||||
|
||||
StmtNode* stmt_if(ExprNode* condition, StmtNode* then_b, StmtNode* else_b, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_while(ExprNode* condition, StmtNode* body, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
}
|
||||
|
||||
StmtNode* stmt_for_in(const char* var_name, ExprNode* iterable, StmtNode* body, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_fn_def(const char* name, char** params, int param_count, StmtNode* body, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(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;
|
||||
s->as.fn_def.body = body;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_return(ExprNode* expr, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
s->type = STMT_RETURN;
|
||||
s->line = line;
|
||||
s->col = col;
|
||||
s->canary = AIRA_CANARY_VALUE;
|
||||
s->as.return_stmt.expr = expr;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_break(int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
s->type = STMT_BREAK;
|
||||
s->line = line;
|
||||
s->col = col;
|
||||
s->canary = AIRA_CANARY_VALUE;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_continue(int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
s->type = STMT_CONTINUE;
|
||||
s->line = line;
|
||||
s->col = col;
|
||||
s->canary = AIRA_CANARY_VALUE;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_block(StmtNode** statements, int count, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
}
|
||||
|
||||
StmtNode* stmt_expr(ExprNode* expr, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
s->type = STMT_EXPR;
|
||||
s->line = line;
|
||||
s->col = col;
|
||||
s->canary = AIRA_CANARY_VALUE;
|
||||
s->as.expr_stmt.expr = expr;
|
||||
return s;
|
||||
}
|
||||
|
||||
StmtNode* stmt_import(const char* mod, const char* source, const char* alias, int line, int col) {
|
||||
StmtNode* s = (StmtNode*)aira_malloc(sizeof(StmtNode));
|
||||
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;
|
||||
return s;
|
||||
}
|
||||
|
||||
void expr_free(ExprNode* expr) {
|
||||
if (!expr) return;
|
||||
switch (expr->type) {
|
||||
case EXPR_IDENTIFIER:
|
||||
aira_free(expr->as.identifier);
|
||||
break;
|
||||
case EXPR_BINARY:
|
||||
expr_free(expr->as.binary.left);
|
||||
expr_free(expr->as.binary.right);
|
||||
break;
|
||||
case EXPR_UNARY:
|
||||
expr_free(expr->as.unary.right);
|
||||
break;
|
||||
case EXPR_CALL:
|
||||
expr_free(expr->as.call.callee);
|
||||
for (int i = 0; i < expr->as.call.arg_count; i++) {
|
||||
expr_free(expr->as.call.args[i]);
|
||||
}
|
||||
aira_free(expr->as.call.args);
|
||||
break;
|
||||
case EXPR_INDEX:
|
||||
expr_free(expr->as.index_access.target);
|
||||
expr_free(expr->as.index_access.index);
|
||||
break;
|
||||
case EXPR_MEMBER:
|
||||
expr_free(expr->as.member_access.target);
|
||||
aira_free(expr->as.member_access.member);
|
||||
break;
|
||||
case EXPR_LIST:
|
||||
for (int i = 0; i < expr->as.list.count; i++) {
|
||||
expr_free(expr->as.list.elements[i]);
|
||||
}
|
||||
aira_free(expr->as.list.elements);
|
||||
break;
|
||||
case EXPR_DICT:
|
||||
for (int i = 0; i < expr->as.dict.count; i++) {
|
||||
aira_free(expr->as.dict.keys[i]);
|
||||
expr_free(expr->as.dict.values[i]);
|
||||
}
|
||||
aira_free(expr->as.dict.keys);
|
||||
aira_free(expr->as.dict.values);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
aira_free(expr);
|
||||
}
|
||||
|
||||
void stmt_free(StmtNode* stmt) {
|
||||
if (!stmt) return;
|
||||
switch (stmt->type) {
|
||||
case STMT_PROGRAM:
|
||||
for (int i = 0; i < stmt->as.program.count; i++) {
|
||||
stmt_free(stmt->as.program.statements[i]);
|
||||
}
|
||||
aira_free(stmt->as.program.statements);
|
||||
break;
|
||||
case STMT_LET:
|
||||
aira_free(stmt->as.let_stmt.name);
|
||||
expr_free(stmt->as.let_stmt.init_expr);
|
||||
break;
|
||||
case STMT_ASSIGN:
|
||||
aira_free(stmt->as.assign.name);
|
||||
expr_free(stmt->as.assign.expr);
|
||||
break;
|
||||
case STMT_INDEX_ASSIGN:
|
||||
expr_free(stmt->as.index_assign.target);
|
||||
expr_free(stmt->as.index_assign.index);
|
||||
expr_free(stmt->as.index_assign.expr);
|
||||
break;
|
||||
case STMT_MEMBER_ASSIGN:
|
||||
expr_free(stmt->as.member_assign.target);
|
||||
aira_free(stmt->as.member_assign.member);
|
||||
expr_free(stmt->as.member_assign.expr);
|
||||
break;
|
||||
case STMT_SAY:
|
||||
for (int i = 0; i < stmt->as.say_stmt.count; i++) {
|
||||
expr_free(stmt->as.say_stmt.expressions[i]);
|
||||
}
|
||||
aira_free(stmt->as.say_stmt.expressions);
|
||||
break;
|
||||
case STMT_PRINT:
|
||||
for (int i = 0; i < stmt->as.print_stmt.count; i++) {
|
||||
expr_free(stmt->as.print_stmt.expressions[i]);
|
||||
}
|
||||
aira_free(stmt->as.print_stmt.expressions);
|
||||
break;
|
||||
case STMT_IF:
|
||||
expr_free(stmt->as.if_stmt.condition);
|
||||
stmt_free(stmt->as.if_stmt.then_branch);
|
||||
stmt_free(stmt->as.if_stmt.else_branch);
|
||||
break;
|
||||
case STMT_WHILE:
|
||||
expr_free(stmt->as.while_stmt.condition);
|
||||
stmt_free(stmt->as.while_stmt.body);
|
||||
break;
|
||||
case STMT_FOR_IN:
|
||||
aira_free(stmt->as.for_in_stmt.var_name);
|
||||
expr_free(stmt->as.for_in_stmt.iterable);
|
||||
stmt_free(stmt->as.for_in_stmt.body);
|
||||
break;
|
||||
case STMT_FN_DEF:
|
||||
aira_free(stmt->as.fn_def.name);
|
||||
for (int i = 0; i < stmt->as.fn_def.param_count; i++) {
|
||||
aira_free(stmt->as.fn_def.params[i]);
|
||||
}
|
||||
aira_free(stmt->as.fn_def.params);
|
||||
stmt_free(stmt->as.fn_def.body);
|
||||
break;
|
||||
case STMT_RETURN:
|
||||
expr_free(stmt->as.return_stmt.expr);
|
||||
break;
|
||||
case STMT_BLOCK:
|
||||
for (int i = 0; i < stmt->as.block.count; i++) {
|
||||
stmt_free(stmt->as.block.statements[i]);
|
||||
}
|
||||
aira_free(stmt->as.block.statements);
|
||||
break;
|
||||
case STMT_EXPR:
|
||||
expr_free(stmt->as.expr_stmt.expr);
|
||||
break;
|
||||
case STMT_IMPORT:
|
||||
aira_free(stmt->as.import_stmt.module_name);
|
||||
if (stmt->as.import_stmt.source) aira_free(stmt->as.import_stmt.source);
|
||||
if (stmt->as.import_stmt.alias) aira_free(stmt->as.import_stmt.alias);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
aira_free(stmt);
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
#include "../include/common.h"
|
||||
|
||||
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);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
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);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void* aira_realloc(void* ptr, size_t new_size) {
|
||||
if (new_size == 0) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
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);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
void aira_free(void* ptr) {
|
||||
if (ptr) {
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
char* copy = (char*)aira_malloc(len + 1);
|
||||
memcpy(copy, s, len + 1);
|
||||
return copy;
|
||||
}
|
||||
|
||||
char* aira_strndup(const char* s, size_t n) {
|
||||
if (!s) return NULL;
|
||||
if (n > AIRA_MAX_STRING_LEN) {
|
||||
n = AIRA_MAX_STRING_LEN;
|
||||
}
|
||||
char* copy = (char*)aira_malloc(n + 1);
|
||||
memcpy(copy, s, n);
|
||||
copy[n] = '\0';
|
||||
return copy;
|
||||
}
|
||||
|
||||
void aira_panic(const char* fmt, ...) {
|
||||
fprintf(stderr, COLOR_RED "[!] AiraLang S1 Duo Security Panic: " COLOR_RESET);
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
fprintf(stderr, "\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void aira_error_at(const char* filename, int line, int col, const char* fmt, ...) {
|
||||
fprintf(stderr, COLOR_RED "[!] Error" COLOR_RESET " at %s:%d:%d: ", filename ? filename : "<input>", line, col);
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
void sb_init(AiraStringBuilder* sb) {
|
||||
sb->capacity = 64;
|
||||
sb->length = 0;
|
||||
sb->data = (char*)aira_malloc(sb->capacity);
|
||||
sb->data[0] = '\0';
|
||||
}
|
||||
|
||||
void sb_append_len(AiraStringBuilder* sb, const char* str, size_t len) {
|
||||
if (!str || len == 0) return;
|
||||
if (sb->length + len + 1 > AIRA_MAX_STRING_LEN) {
|
||||
aira_panic("StringBuilder exceeded safe string threshold of %d bytes.", AIRA_MAX_STRING_LEN);
|
||||
}
|
||||
if (sb->length + len + 1 > sb->capacity) {
|
||||
while (sb->capacity < sb->length + len + 1) {
|
||||
sb->capacity *= 2;
|
||||
}
|
||||
sb->data = (char*)aira_realloc(sb->data, sb->capacity);
|
||||
}
|
||||
memcpy(sb->data + sb->length, str, len);
|
||||
sb->length += len;
|
||||
sb->data[sb->length] = '\0';
|
||||
}
|
||||
|
||||
void sb_append(AiraStringBuilder* sb, const char* str) {
|
||||
if (str) {
|
||||
sb_append_len(sb, str, strlen(str));
|
||||
}
|
||||
}
|
||||
|
||||
void sb_append_char(AiraStringBuilder* sb, char c) {
|
||||
char buf[1] = { c };
|
||||
sb_append_len(sb, buf, 1);
|
||||
}
|
||||
|
||||
char* sb_finish(AiraStringBuilder* sb) {
|
||||
char* res = sb->data;
|
||||
sb->data = NULL;
|
||||
sb->length = 0;
|
||||
sb->capacity = 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
void sb_free(AiraStringBuilder* sb) {
|
||||
if (sb->data) {
|
||||
aira_free(sb->data);
|
||||
sb->data = NULL;
|
||||
}
|
||||
sb->length = 0;
|
||||
sb->capacity = 0;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "../include/env.h"
|
||||
|
||||
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);
|
||||
env->values = (AiraValue*)aira_malloc(sizeof(AiraValue) * env->capacity);
|
||||
return env;
|
||||
}
|
||||
|
||||
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) {
|
||||
env->values[i] = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (env->count >= env->capacity) {
|
||||
env->capacity *= 2;
|
||||
env->names = (char**)aira_realloc(env->names, sizeof(char*) * env->capacity);
|
||||
env->values = (AiraValue*)aira_realloc(env->values, sizeof(AiraValue) * env->capacity);
|
||||
}
|
||||
env->names[env->count] = aira_strdup(name);
|
||||
env->values[env->count] = val;
|
||||
env->count++;
|
||||
}
|
||||
|
||||
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];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
curr = curr->parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
curr = curr->parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,473 @@
|
||||
#include "../include/evaluator.h"
|
||||
#include "../include/modules.h"
|
||||
|
||||
void evaluator_init(Evaluator* eval) {
|
||||
eval->global_env = env_new(NULL);
|
||||
eval->call_depth = 0;
|
||||
eval->had_runtime_error = false;
|
||||
register_builtin_globals(eval->global_env);
|
||||
}
|
||||
|
||||
void evaluator_cleanup(Evaluator* eval) {
|
||||
if (eval->global_env) {
|
||||
env_free(eval->global_env);
|
||||
eval->global_env = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static EvalSignal signal_ok(void) {
|
||||
EvalSignal s;
|
||||
s.type = EXEC_OK;
|
||||
s.value = val_null();
|
||||
return s;
|
||||
}
|
||||
|
||||
static EvalSignal signal_return(AiraValue v) {
|
||||
EvalSignal s;
|
||||
s.type = EXEC_RETURN;
|
||||
s.value = v;
|
||||
return s;
|
||||
}
|
||||
|
||||
static EvalSignal signal_break(void) {
|
||||
EvalSignal s;
|
||||
s.type = EXEC_BREAK;
|
||||
s.value = val_null();
|
||||
return s;
|
||||
}
|
||||
|
||||
static EvalSignal signal_continue(void) {
|
||||
EvalSignal s;
|
||||
s.type = EXEC_CONTINUE;
|
||||
s.value = val_null();
|
||||
return s;
|
||||
}
|
||||
|
||||
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:
|
||||
return expr->as.literal;
|
||||
|
||||
case EXPR_IDENTIFIER: {
|
||||
AiraValue val;
|
||||
if (env_get(env, expr->as.identifier, &val)) {
|
||||
return val;
|
||||
}
|
||||
aira_panic("Runtime Error: Undefined variable '%s' at line %d.", expr->as.identifier, expr->line);
|
||||
return val_null();
|
||||
}
|
||||
|
||||
case EXPR_UNARY: {
|
||||
AiraValue right = eval_expression(eval, env, expr->as.unary.right);
|
||||
if (expr->as.unary.op == TOKEN_MINUS) {
|
||||
if (right.type == VAL_INT) return val_int(-right.as.integer);
|
||||
if (right.type == VAL_FLOAT) return val_float(-right.as.floating);
|
||||
aira_panic("Unary '-' requires numeric operand at line %d.", expr->line);
|
||||
}
|
||||
if (expr->as.unary.op == TOKEN_BANG || expr->as.unary.op == TOKEN_NOT) {
|
||||
return val_bool(!val_is_truthy(right));
|
||||
}
|
||||
return right;
|
||||
}
|
||||
|
||||
case EXPR_BINARY: {
|
||||
/* Short-circuit logical operators */
|
||||
if (expr->as.binary.op == TOKEN_AND || expr->as.binary.op == TOKEN_AND_AND) {
|
||||
AiraValue left = eval_expression(eval, env, expr->as.binary.left);
|
||||
if (!val_is_truthy(left)) return left;
|
||||
return eval_expression(eval, env, expr->as.binary.right);
|
||||
}
|
||||
if (expr->as.binary.op == TOKEN_OR || expr->as.binary.op == TOKEN_OR_OR) {
|
||||
AiraValue left = eval_expression(eval, env, expr->as.binary.left);
|
||||
if (val_is_truthy(left)) return left;
|
||||
return eval_expression(eval, env, expr->as.binary.right);
|
||||
}
|
||||
|
||||
AiraValue left = eval_expression(eval, env, expr->as.binary.left);
|
||||
AiraValue right = eval_expression(eval, env, expr->as.binary.right);
|
||||
|
||||
switch (expr->as.binary.op) {
|
||||
case TOKEN_PLUS: return val_add(left, right);
|
||||
case TOKEN_MINUS: return val_sub(left, right);
|
||||
case TOKEN_STAR: return val_mul(left, right);
|
||||
case TOKEN_SLASH: return val_div(left, right);
|
||||
case TOKEN_PERCENT: return val_mod(left, right);
|
||||
case TOKEN_EQUAL_EQUAL: return val_bool(val_equals(left, right));
|
||||
case TOKEN_BANG_EQUAL: return val_bool(!val_equals(left, right));
|
||||
case TOKEN_LESS: return val_bool(val_compare(left, right) < 0);
|
||||
case TOKEN_LESS_EQUAL: return val_bool(val_compare(left, right) <= 0);
|
||||
case TOKEN_GREATER: return val_bool(val_compare(left, right) > 0);
|
||||
case TOKEN_GREATER_EQUAL: return val_bool(val_compare(left, right) >= 0);
|
||||
default:
|
||||
aira_panic("Unsupported binary operator at line %d.", expr->line);
|
||||
return val_null();
|
||||
}
|
||||
}
|
||||
|
||||
case EXPR_LIST: {
|
||||
AiraValue list = val_list();
|
||||
for (int i = 0; i < expr->as.list.count; i++) {
|
||||
AiraValue el = eval_expression(eval, env, expr->as.list.elements[i]);
|
||||
list_append(list.as.list, el);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
case EXPR_DICT: {
|
||||
AiraValue dict = val_dict();
|
||||
for (int i = 0; i < expr->as.dict.count; i++) {
|
||||
AiraValue val = eval_expression(eval, env, expr->as.dict.values[i]);
|
||||
dict_set(dict.as.dict, expr->as.dict.keys[i], val);
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
|
||||
case EXPR_INDEX: {
|
||||
AiraValue target = eval_expression(eval, env, expr->as.index_access.target);
|
||||
AiraValue idx = eval_expression(eval, env, expr->as.index_access.index);
|
||||
|
||||
if (target.type == VAL_LIST) {
|
||||
int i = (idx.type == VAL_INT) ? (int)idx.as.integer : (int)idx.as.floating;
|
||||
return list_get(target.as.list, i);
|
||||
}
|
||||
if (target.type == VAL_DICT) {
|
||||
char* key = val_to_string(idx);
|
||||
AiraValue v = dict_get(target.as.dict, key);
|
||||
aira_free(key);
|
||||
return v;
|
||||
}
|
||||
if (target.type == VAL_STRING) {
|
||||
int i = (idx.type == VAL_INT) ? (int)idx.as.integer : (int)idx.as.floating;
|
||||
int len = (int)strlen(target.as.string);
|
||||
if (i < 0) i = len + i;
|
||||
if (i < 0 || i >= len) aira_panic("String index out of bounds (%d, length %d).", i, len);
|
||||
char ch[2] = { target.as.string[i], '\0' };
|
||||
return val_string(ch);
|
||||
}
|
||||
aira_panic("Index operator not supported on type at line %d.", expr->line);
|
||||
return val_null();
|
||||
}
|
||||
|
||||
case EXPR_MEMBER: {
|
||||
AiraValue target = eval_expression(eval, env, expr->as.member_access.target);
|
||||
const char* mem = expr->as.member_access.member;
|
||||
|
||||
if (target.type == VAL_MODULE) {
|
||||
AiraValue fn = dict_get(target.as.module->members, mem);
|
||||
if (fn.type != VAL_NULL) return fn;
|
||||
aira_panic("Module '%s' has no member '%s' at line %d.", target.as.module->name, mem, expr->line);
|
||||
}
|
||||
if (target.type == VAL_DICT) {
|
||||
return dict_get(target.as.dict, mem);
|
||||
}
|
||||
if (target.type == VAL_LIST) {
|
||||
if (strcmp(mem, "length") == 0) return val_int(target.as.list->count);
|
||||
}
|
||||
if (target.type == VAL_STRING) {
|
||||
if (strcmp(mem, "length") == 0) return val_int(strlen(target.as.string));
|
||||
}
|
||||
aira_panic("Member access '%s' not supported on target at line %d.", mem, expr->line);
|
||||
return val_null();
|
||||
}
|
||||
|
||||
case EXPR_CALL: {
|
||||
/* Check if callee is a member method call like list.push(x) or str.upper() */
|
||||
if (expr->as.call.callee->type == EXPR_MEMBER) {
|
||||
ExprNode* target_expr = expr->as.call.callee->as.member_access.target;
|
||||
const char* method_name = expr->as.call.callee->as.member_access.member;
|
||||
AiraValue target = eval_expression(eval, env, target_expr);
|
||||
|
||||
if (target.type == VAL_LIST) {
|
||||
if (strcmp(method_name, "push") == 0 && expr->as.call.arg_count > 0) {
|
||||
AiraValue arg = eval_expression(eval, env, expr->as.call.args[0]);
|
||||
list_append(target.as.list, arg);
|
||||
return val_null();
|
||||
}
|
||||
if (strcmp(method_name, "pop") == 0) {
|
||||
return list_pop(target.as.list);
|
||||
}
|
||||
if (strcmp(method_name, "contains") == 0 && expr->as.call.arg_count > 0) {
|
||||
AiraValue arg = eval_expression(eval, env, expr->as.call.args[0]);
|
||||
for (int i = 0; i < target.as.list->count; i++) {
|
||||
if (val_equals(target.as.list->elements[i], arg)) return val_bool(true);
|
||||
}
|
||||
return val_bool(false);
|
||||
}
|
||||
}
|
||||
if (target.type == VAL_DICT) {
|
||||
if (strcmp(method_name, "get") == 0 && expr->as.call.arg_count > 0) {
|
||||
char* k = val_to_string(eval_expression(eval, env, expr->as.call.args[0]));
|
||||
AiraValue res = dict_get(target.as.dict, k);
|
||||
aira_free(k);
|
||||
if (res.type == VAL_NULL && expr->as.call.arg_count > 1) {
|
||||
return eval_expression(eval, env, expr->as.call.args[1]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
if (strcmp(method_name, "has") == 0 && expr->as.call.arg_count > 0) {
|
||||
char* k = val_to_string(eval_expression(eval, env, expr->as.call.args[0]));
|
||||
bool h = dict_has(target.as.dict, k);
|
||||
aira_free(k);
|
||||
return val_bool(h);
|
||||
}
|
||||
}
|
||||
if (target.type == VAL_STRING) {
|
||||
if (strcmp(method_name, "upper") == 0) {
|
||||
char* s = aira_strdup(target.as.string);
|
||||
for (size_t i = 0; s[i]; i++) s[i] = toupper((unsigned char)s[i]);
|
||||
return val_string_take(s);
|
||||
}
|
||||
if (strcmp(method_name, "lower") == 0) {
|
||||
char* s = aira_strdup(target.as.string);
|
||||
for (size_t i = 0; s[i]; i++) s[i] = tolower((unsigned char)s[i]);
|
||||
return val_string_take(s);
|
||||
}
|
||||
}
|
||||
if (target.type == VAL_MODULE) {
|
||||
AiraValue mod_fn = dict_get(target.as.module->members, method_name);
|
||||
if (mod_fn.type == VAL_BUILTIN_FN) {
|
||||
AiraValue args[16];
|
||||
int count = expr->as.call.arg_count;
|
||||
if (count > 16) count = 16;
|
||||
for (int i = 0; i < count; i++) {
|
||||
args[i] = eval_expression(eval, env, expr->as.call.args[i]);
|
||||
}
|
||||
return mod_fn.as.builtin(count, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (count > 32) count = 32;
|
||||
for (int i = 0; i < count; i++) {
|
||||
args[i] = eval_expression(eval, env, expr->as.call.args[i]);
|
||||
}
|
||||
return callee.as.builtin(count, args);
|
||||
}
|
||||
|
||||
if (callee.type == VAL_FN) {
|
||||
AiraFunction* fn = callee.as.fn;
|
||||
Environment* local_env = env_new(fn->closure ? fn->closure : eval->global_env);
|
||||
|
||||
int param_count = fn->node->as.fn_def.param_count;
|
||||
for (int i = 0; i < param_count; i++) {
|
||||
AiraValue arg_val = (i < expr->as.call.arg_count) ? eval_expression(eval, env, expr->as.call.args[i]) : val_null();
|
||||
env_define(local_env, fn->node->as.fn_def.params[i], arg_val);
|
||||
}
|
||||
|
||||
eval->call_depth++;
|
||||
EvalSignal sig = eval_statement(eval, local_env, fn->node->as.fn_def.body);
|
||||
eval->call_depth--;
|
||||
env_free(local_env);
|
||||
|
||||
if (sig.type == EXEC_RETURN) {
|
||||
return sig.value;
|
||||
}
|
||||
return val_null();
|
||||
}
|
||||
|
||||
aira_panic("Attempted to call a non-function value at line %d.", expr->line);
|
||||
return val_null();
|
||||
}
|
||||
|
||||
default:
|
||||
return val_null();
|
||||
}
|
||||
}
|
||||
|
||||
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: {
|
||||
AiraValue val = eval_expression(eval, env, stmt->as.let_stmt.init_expr);
|
||||
env_define(env, stmt->as.let_stmt.name, val);
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_ASSIGN: {
|
||||
AiraValue rhs = eval_expression(eval, env, stmt->as.assign.expr);
|
||||
const char* name = stmt->as.assign.name;
|
||||
TokenType op = stmt->as.assign.op;
|
||||
|
||||
if (op == TOKEN_ASSIGN) {
|
||||
if (!env_assign(env, name, rhs)) {
|
||||
aira_panic("Cannot assign to undeclared variable '%s' at line %d.", name, stmt->line);
|
||||
}
|
||||
} else {
|
||||
AiraValue curr;
|
||||
if (!env_get(env, name, &curr)) {
|
||||
aira_panic("Variable '%s' not found for compound assignment at line %d.", name, stmt->line);
|
||||
}
|
||||
AiraValue new_val = val_null();
|
||||
if (op == TOKEN_PLUS_ASSIGN) new_val = val_add(curr, rhs);
|
||||
else if (op == TOKEN_MINUS_ASSIGN) new_val = val_sub(curr, rhs);
|
||||
else if (op == TOKEN_STAR_ASSIGN) new_val = val_mul(curr, rhs);
|
||||
else if (op == TOKEN_SLASH_ASSIGN) new_val = val_div(curr, rhs);
|
||||
else if (op == TOKEN_MOD_ASSIGN) new_val = val_mod(curr, rhs);
|
||||
|
||||
env_assign(env, name, new_val);
|
||||
}
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_INDEX_ASSIGN: {
|
||||
AiraValue target = eval_expression(eval, env, stmt->as.index_assign.target);
|
||||
AiraValue idx = eval_expression(eval, env, stmt->as.index_assign.index);
|
||||
AiraValue rhs = eval_expression(eval, env, stmt->as.index_assign.expr);
|
||||
|
||||
if (target.type == VAL_LIST) {
|
||||
int i = (idx.type == VAL_INT) ? (int)idx.as.integer : (int)idx.as.floating;
|
||||
list_set(target.as.list, i, rhs);
|
||||
} else if (target.type == VAL_DICT) {
|
||||
char* k = val_to_string(idx);
|
||||
dict_set(target.as.dict, k, rhs);
|
||||
aira_free(k);
|
||||
} else {
|
||||
aira_panic("Index assignment not supported on type at line %d.", stmt->line);
|
||||
}
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_MEMBER_ASSIGN: {
|
||||
AiraValue target = eval_expression(eval, env, stmt->as.member_assign.target);
|
||||
AiraValue rhs = eval_expression(eval, env, stmt->as.member_assign.expr);
|
||||
if (target.type == VAL_DICT) {
|
||||
dict_set(target.as.dict, stmt->as.member_assign.member, rhs);
|
||||
} else {
|
||||
aira_panic("Member assignment only valid on dictionaries at line %d.", stmt->line);
|
||||
}
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_SAY: {
|
||||
for (int i = 0; i < stmt->as.say_stmt.count; i++) {
|
||||
if (i > 0) printf(" ");
|
||||
AiraValue v = eval_expression(eval, env, stmt->as.say_stmt.expressions[i]);
|
||||
val_print(v);
|
||||
}
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_PRINT: {
|
||||
for (int i = 0; i < stmt->as.print_stmt.count; i++) {
|
||||
if (i > 0) printf(" ");
|
||||
AiraValue v = eval_expression(eval, env, stmt->as.print_stmt.expressions[i]);
|
||||
val_print(v);
|
||||
}
|
||||
fflush(stdout);
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_IF: {
|
||||
AiraValue cond = eval_expression(eval, env, stmt->as.if_stmt.condition);
|
||||
if (val_is_truthy(cond)) {
|
||||
return eval_statement(eval, env, stmt->as.if_stmt.then_branch);
|
||||
} else if (stmt->as.if_stmt.else_branch) {
|
||||
return eval_statement(eval, env, stmt->as.if_stmt.else_branch);
|
||||
}
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_WHILE: {
|
||||
while (val_is_truthy(eval_expression(eval, env, stmt->as.while_stmt.condition))) {
|
||||
EvalSignal sig = eval_statement(eval, env, stmt->as.while_stmt.body);
|
||||
if (sig.type == EXEC_BREAK) break;
|
||||
if (sig.type == EXEC_CONTINUE) continue;
|
||||
if (sig.type == EXEC_RETURN) return sig;
|
||||
}
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_FOR_IN: {
|
||||
AiraValue iter = eval_expression(eval, env, stmt->as.for_in_stmt.iterable);
|
||||
if (iter.type == VAL_LIST) {
|
||||
for (int i = 0; i < iter.as.list->count; i++) {
|
||||
env_define(env, stmt->as.for_in_stmt.var_name, iter.as.list->elements[i]);
|
||||
EvalSignal sig = eval_statement(eval, env, stmt->as.for_in_stmt.body);
|
||||
if (sig.type == EXEC_BREAK) break;
|
||||
if (sig.type == EXEC_CONTINUE) continue;
|
||||
if (sig.type == EXEC_RETURN) return sig;
|
||||
}
|
||||
} else {
|
||||
aira_panic("'for..in' loop requires an iterable list at line %d.", stmt->line);
|
||||
}
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_FN_DEF: {
|
||||
AiraValue fn = val_function(stmt->as.fn_def.name, stmt, env);
|
||||
env_define(env, stmt->as.fn_def.name, fn);
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_RETURN: {
|
||||
AiraValue ret = stmt->as.return_stmt.expr ? eval_expression(eval, env, stmt->as.return_stmt.expr) : val_null();
|
||||
return signal_return(ret);
|
||||
}
|
||||
|
||||
case STMT_BREAK:
|
||||
return signal_break();
|
||||
|
||||
case STMT_CONTINUE:
|
||||
return signal_continue();
|
||||
|
||||
case STMT_BLOCK: {
|
||||
Environment* block_env = env_new(env);
|
||||
for (int i = 0; i < stmt->as.block.count; i++) {
|
||||
EvalSignal sig = eval_statement(eval, block_env, stmt->as.block.statements[i]);
|
||||
if (sig.type != EXEC_OK) {
|
||||
env_free(block_env);
|
||||
return sig;
|
||||
}
|
||||
}
|
||||
env_free(block_env);
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
case STMT_EXPR:
|
||||
eval_expression(eval, env, stmt->as.expr_stmt.expr);
|
||||
return signal_ok();
|
||||
|
||||
case STMT_IMPORT: {
|
||||
AiraValue mod = load_builtin_module(stmt->as.import_stmt.module_name);
|
||||
if (mod.type != VAL_NULL) {
|
||||
const char* alias = stmt->as.import_stmt.alias ? stmt->as.import_stmt.alias : stmt->as.import_stmt.module_name;
|
||||
env_define(env, alias, mod);
|
||||
} else {
|
||||
aira_panic("Module '%s' could not be loaded at line %d.", stmt->as.import_stmt.module_name, stmt->line);
|
||||
}
|
||||
return signal_ok();
|
||||
}
|
||||
|
||||
default:
|
||||
return signal_ok();
|
||||
}
|
||||
}
|
||||
|
||||
void eval_program(Evaluator* eval, StmtNode* program) {
|
||||
if (!program || program->type != STMT_PROGRAM) return;
|
||||
for (int i = 0; i < program->as.program.count; i++) {
|
||||
EvalSignal sig = eval_statement(eval, eval->global_env, program->as.program.statements[i]);
|
||||
if (sig.type == EXEC_RETURN) break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
#include "../include/lexer.h"
|
||||
|
||||
const char* token_type_name(TokenType type) {
|
||||
switch (type) {
|
||||
case TOKEN_EOF: return "EOF";
|
||||
case TOKEN_ERROR: return "ERROR";
|
||||
case TOKEN_NUMBER: return "NUMBER";
|
||||
case TOKEN_STRING: return "STRING";
|
||||
case TOKEN_IDENTIFIER: return "IDENTIFIER";
|
||||
case TOKEN_LET: return "let";
|
||||
case TOKEN_SAY: return "say";
|
||||
case TOKEN_PRINT: return "print";
|
||||
case TOKEN_IF: return "if";
|
||||
case TOKEN_ELSE: return "else";
|
||||
case TOKEN_WHILE: return "while";
|
||||
case TOKEN_FOR: return "for";
|
||||
case TOKEN_IN: return "in";
|
||||
case TOKEN_FN: return "fn";
|
||||
case TOKEN_RETURN: return "return";
|
||||
case TOKEN_IMPORT: return "import";
|
||||
case TOKEN_FROM: return "from";
|
||||
case TOKEN_TRUE: return "true";
|
||||
case TOKEN_FALSE: return "false";
|
||||
case TOKEN_NULL: return "null";
|
||||
case TOKEN_AND: return "and";
|
||||
case TOKEN_OR: return "or";
|
||||
case TOKEN_NOT: return "not";
|
||||
case TOKEN_TRY: return "try";
|
||||
case TOKEN_CATCH: return "catch";
|
||||
case TOKEN_THROW: return "throw";
|
||||
case TOKEN_CLASS: return "class";
|
||||
case TOKEN_NEW: return "new";
|
||||
case TOKEN_THIS: return "this";
|
||||
case TOKEN_BREAK: return "break";
|
||||
case TOKEN_CONTINUE: return "continue";
|
||||
case TOKEN_PLUS: return "+";
|
||||
case TOKEN_MINUS: return "-";
|
||||
case TOKEN_STAR: return "*";
|
||||
case TOKEN_SLASH: return "/";
|
||||
case TOKEN_PERCENT: return "%";
|
||||
case TOKEN_ASSIGN: return "=";
|
||||
case TOKEN_PLUS_ASSIGN: return "+=";
|
||||
case TOKEN_MINUS_ASSIGN: return "-=";
|
||||
case TOKEN_STAR_ASSIGN: return "*=";
|
||||
case TOKEN_SLASH_ASSIGN: return "/=";
|
||||
case TOKEN_MOD_ASSIGN: return "%=";
|
||||
case TOKEN_EQUAL_EQUAL: return "==";
|
||||
case TOKEN_BANG_EQUAL: return "!=";
|
||||
case TOKEN_LESS: return "<";
|
||||
case TOKEN_LESS_EQUAL: return "<=";
|
||||
case TOKEN_GREATER: return ">";
|
||||
case TOKEN_GREATER_EQUAL: return ">=";
|
||||
case TOKEN_BANG: return "!";
|
||||
case TOKEN_AND_AND: return "&&";
|
||||
case TOKEN_OR_OR: return "||";
|
||||
case TOKEN_LPAREN: return "(";
|
||||
case TOKEN_RPAREN: return ")";
|
||||
case TOKEN_LBRACE: return "{";
|
||||
case TOKEN_RBRACE: return "}";
|
||||
case TOKEN_LBRACKET: return "[";
|
||||
case TOKEN_RBRACKET: return "]";
|
||||
case TOKEN_COMMA: return ",";
|
||||
case TOKEN_DOT: return ".";
|
||||
case TOKEN_COLON: return ":";
|
||||
case TOKEN_SEMICOLON: return ";";
|
||||
case TOKEN_AT: return "@";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
void token_free(Token* token) {
|
||||
if (token && token->text) {
|
||||
aira_free(token->text);
|
||||
token->text = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void lexer_init(Lexer* lexer, const char* source, const char* filename) {
|
||||
lexer->source = source;
|
||||
lexer->filename = filename;
|
||||
lexer->length = source ? strlen(source) : 0;
|
||||
lexer->current = 0;
|
||||
lexer->line = 1;
|
||||
lexer->col = 1;
|
||||
}
|
||||
|
||||
static bool is_at_end(Lexer* lexer) {
|
||||
return lexer->current >= lexer->length;
|
||||
}
|
||||
|
||||
static char peek(Lexer* lexer) {
|
||||
if (is_at_end(lexer)) return '\0';
|
||||
return lexer->source[lexer->current];
|
||||
}
|
||||
|
||||
static char peek_next(Lexer* lexer) {
|
||||
if (lexer->current + 1 >= lexer->length) return '\0';
|
||||
return lexer->source[lexer->current + 1];
|
||||
}
|
||||
|
||||
static char advance(Lexer* lexer) {
|
||||
if (is_at_end(lexer)) return '\0';
|
||||
char c = lexer->source[lexer->current++];
|
||||
if (c == '\n') {
|
||||
lexer->line++;
|
||||
lexer->col = 1;
|
||||
} else {
|
||||
lexer->col++;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
static bool match(Lexer* lexer, char expected) {
|
||||
if (is_at_end(lexer)) return false;
|
||||
if (lexer->source[lexer->current] != expected) return false;
|
||||
advance(lexer);
|
||||
return true;
|
||||
}
|
||||
|
||||
static Token make_token(Lexer* lexer, TokenType type, const char* text, int line, int col) {
|
||||
Token token;
|
||||
token.type = type;
|
||||
token.text = text ? aira_strdup(text) : NULL;
|
||||
token.line = line;
|
||||
token.col = col;
|
||||
token.num_val = 0.0;
|
||||
return token;
|
||||
}
|
||||
|
||||
static Token make_error_token(Lexer* lexer, const char* message, int line, int col) {
|
||||
Token token;
|
||||
token.type = TOKEN_ERROR;
|
||||
token.text = aira_strdup(message);
|
||||
token.line = line;
|
||||
token.col = col;
|
||||
token.num_val = 0.0;
|
||||
return token;
|
||||
}
|
||||
|
||||
static void skip_whitespace_and_comments(Lexer* lexer) {
|
||||
while (!is_at_end(lexer)) {
|
||||
char c = peek(lexer);
|
||||
if (c == ' ' || c == '\r' || c == '\t' || c == '\n') {
|
||||
advance(lexer);
|
||||
} else if (c == '#') {
|
||||
/* Single line python style comment */
|
||||
while (!is_at_end(lexer) && peek(lexer) != '\n') {
|
||||
advance(lexer);
|
||||
}
|
||||
} else if (c == '/' && peek_next(lexer) == '/') {
|
||||
/* Single line C style comment */
|
||||
advance(lexer);
|
||||
advance(lexer);
|
||||
while (!is_at_end(lexer) && peek(lexer) != '\n') {
|
||||
advance(lexer);
|
||||
}
|
||||
} else if (c == '/' && peek_next(lexer) == '*') {
|
||||
/* Multi-line comment */
|
||||
advance(lexer);
|
||||
advance(lexer);
|
||||
while (!is_at_end(lexer)) {
|
||||
if (peek(lexer) == '*' && peek_next(lexer) == '/') {
|
||||
advance(lexer);
|
||||
advance(lexer);
|
||||
break;
|
||||
}
|
||||
advance(lexer);
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static TokenType check_keyword(const char* text) {
|
||||
if (strcmp(text, "let") == 0) return TOKEN_LET;
|
||||
if (strcmp(text, "say") == 0) return TOKEN_SAY;
|
||||
if (strcmp(text, "print") == 0) return TOKEN_PRINT;
|
||||
if (strcmp(text, "if") == 0) return TOKEN_IF;
|
||||
if (strcmp(text, "else") == 0) return TOKEN_ELSE;
|
||||
if (strcmp(text, "while") == 0) return TOKEN_WHILE;
|
||||
if (strcmp(text, "for") == 0) return TOKEN_FOR;
|
||||
if (strcmp(text, "in") == 0) return TOKEN_IN;
|
||||
if (strcmp(text, "fn") == 0) return TOKEN_FN;
|
||||
if (strcmp(text, "return") == 0) return TOKEN_RETURN;
|
||||
if (strcmp(text, "import") == 0) return TOKEN_IMPORT;
|
||||
if (strcmp(text, "from") == 0) return TOKEN_FROM;
|
||||
if (strcmp(text, "true") == 0) return TOKEN_TRUE;
|
||||
if (strcmp(text, "false") == 0) return TOKEN_FALSE;
|
||||
if (strcmp(text, "null") == 0) return TOKEN_NULL;
|
||||
if (strcmp(text, "and") == 0) return TOKEN_AND;
|
||||
if (strcmp(text, "or") == 0) return TOKEN_OR;
|
||||
if (strcmp(text, "not") == 0) return TOKEN_NOT;
|
||||
if (strcmp(text, "try") == 0) return TOKEN_TRY;
|
||||
if (strcmp(text, "catch") == 0) return TOKEN_CATCH;
|
||||
if (strcmp(text, "throw") == 0) return TOKEN_THROW;
|
||||
if (strcmp(text, "class") == 0) return TOKEN_CLASS;
|
||||
if (strcmp(text, "new") == 0) return TOKEN_NEW;
|
||||
if (strcmp(text, "this") == 0) return TOKEN_THIS;
|
||||
if (strcmp(text, "break") == 0) return TOKEN_BREAK;
|
||||
if (strcmp(text, "continue") == 0) return TOKEN_CONTINUE;
|
||||
return TOKEN_IDENTIFIER;
|
||||
}
|
||||
|
||||
static Token scan_identifier(Lexer* lexer, int start_line, int start_col) {
|
||||
size_t start = lexer->current - 1;
|
||||
while (!is_at_end(lexer)) {
|
||||
char c = peek(lexer);
|
||||
if (isalnum((unsigned char)c) || c == '_') {
|
||||
advance(lexer);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
size_t len = lexer->current - start;
|
||||
char* text = aira_strndup(lexer->source + start, len);
|
||||
TokenType type = check_keyword(text);
|
||||
Token tok = make_token(lexer, type, text, start_line, start_col);
|
||||
aira_free(text);
|
||||
return tok;
|
||||
}
|
||||
|
||||
static Token scan_number(Lexer* lexer, int start_line, int start_col) {
|
||||
size_t start = lexer->current - 1;
|
||||
bool has_dot = false;
|
||||
while (!is_at_end(lexer)) {
|
||||
char c = peek(lexer);
|
||||
if (isdigit((unsigned char)c)) {
|
||||
advance(lexer);
|
||||
} else if (c == '.' && isdigit((unsigned char)peek_next(lexer)) && !has_dot) {
|
||||
has_dot = true;
|
||||
advance(lexer);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
size_t len = lexer->current - start;
|
||||
char* text = aira_strndup(lexer->source + start, len);
|
||||
Token tok = make_token(lexer, TOKEN_NUMBER, text, start_line, start_col);
|
||||
tok.num_val = strtod(text, NULL);
|
||||
aira_free(text);
|
||||
return tok;
|
||||
}
|
||||
|
||||
static Token scan_string(Lexer* lexer, char quote, int start_line, int start_col) {
|
||||
AiraStringBuilder sb;
|
||||
sb_init(&sb);
|
||||
|
||||
while (!is_at_end(lexer) && peek(lexer) != quote) {
|
||||
char c = advance(lexer);
|
||||
if (c == '\\') {
|
||||
if (is_at_end(lexer)) break;
|
||||
char esc = advance(lexer);
|
||||
switch (esc) {
|
||||
case 'n': sb_append_char(&sb, '\n'); break;
|
||||
case 't': sb_append_char(&sb, '\t'); break;
|
||||
case 'r': sb_append_char(&sb, '\r'); break;
|
||||
case '\\': sb_append_char(&sb, '\\'); break;
|
||||
case '\'': sb_append_char(&sb, '\''); break;
|
||||
case '"': sb_append_char(&sb, '"'); break;
|
||||
default: sb_append_char(&sb, esc); break;
|
||||
}
|
||||
} else {
|
||||
sb_append_char(&sb, c);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_at_end(lexer)) {
|
||||
sb_free(&sb);
|
||||
return make_error_token(lexer, "Unterminated string literal.", start_line, start_col);
|
||||
}
|
||||
|
||||
advance(lexer); /* Consume closing quote */
|
||||
char* content = sb_finish(&sb);
|
||||
Token tok = make_token(lexer, TOKEN_STRING, content, start_line, start_col);
|
||||
aira_free(content);
|
||||
return tok;
|
||||
}
|
||||
|
||||
Token lexer_next_token(Lexer* lexer) {
|
||||
skip_whitespace_and_comments(lexer);
|
||||
|
||||
int start_line = lexer->line;
|
||||
int start_col = lexer->col;
|
||||
|
||||
if (is_at_end(lexer)) {
|
||||
return make_token(lexer, TOKEN_EOF, NULL, start_line, start_col);
|
||||
}
|
||||
|
||||
char c = advance(lexer);
|
||||
|
||||
if (isalpha((unsigned char)c) || c == '_') {
|
||||
return scan_identifier(lexer, start_line, start_col);
|
||||
}
|
||||
|
||||
if (isdigit((unsigned char)c)) {
|
||||
return scan_number(lexer, start_line, start_col);
|
||||
}
|
||||
|
||||
if (c == '"' || c == '\'') {
|
||||
return scan_string(lexer, c, start_line, start_col);
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
case '(': return make_token(lexer, TOKEN_LPAREN, "(", start_line, start_col);
|
||||
case ')': return make_token(lexer, TOKEN_RPAREN, ")", start_line, start_col);
|
||||
case '{': return make_token(lexer, TOKEN_LBRACE, "{", start_line, start_col);
|
||||
case '}': return make_token(lexer, TOKEN_RBRACE, "}", start_line, start_col);
|
||||
case '[': return make_token(lexer, TOKEN_LBRACKET, "[", start_line, start_col);
|
||||
case ']': return make_token(lexer, TOKEN_RBRACKET, "]", start_line, start_col);
|
||||
case ',': return make_token(lexer, TOKEN_COMMA, ",", start_line, start_col);
|
||||
case '.': return make_token(lexer, TOKEN_DOT, ".", start_line, start_col);
|
||||
case ':': return make_token(lexer, TOKEN_COLON, ":", start_line, start_col);
|
||||
case ';': return make_token(lexer, TOKEN_SEMICOLON, ";", start_line, start_col);
|
||||
case '@': return make_token(lexer, TOKEN_AT, "@", start_line, start_col);
|
||||
|
||||
case '+':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_PLUS_ASSIGN, "+=", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_PLUS, "+", start_line, start_col);
|
||||
case '-':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_MINUS_ASSIGN, "-=", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_MINUS, "-", start_line, start_col);
|
||||
case '*':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_STAR_ASSIGN, "*=", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_STAR, "*", start_line, start_col);
|
||||
case '/':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_SLASH_ASSIGN, "/=", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_SLASH, "/", start_line, start_col);
|
||||
case '%':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_MOD_ASSIGN, "%=", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_PERCENT, "%", start_line, start_col);
|
||||
|
||||
case '=':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_EQUAL_EQUAL, "==", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_ASSIGN, "=", start_line, start_col);
|
||||
|
||||
case '!':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_BANG_EQUAL, "!=", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_BANG, "!", start_line, start_col);
|
||||
|
||||
case '<':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_LESS_EQUAL, "<=", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_LESS, "<", start_line, start_col);
|
||||
|
||||
case '>':
|
||||
if (match(lexer, '=')) return make_token(lexer, TOKEN_GREATER_EQUAL, ">=", start_line, start_col);
|
||||
return make_token(lexer, TOKEN_GREATER, ">", start_line, start_col);
|
||||
|
||||
case '&':
|
||||
if (match(lexer, '&')) return make_token(lexer, TOKEN_AND_AND, "&&", start_line, start_col);
|
||||
break;
|
||||
|
||||
case '|':
|
||||
if (match(lexer, '|')) return make_token(lexer, TOKEN_OR_OR, "||", start_line, start_col);
|
||||
break;
|
||||
}
|
||||
|
||||
char err_buf[64];
|
||||
snprintf(err_buf, sizeof(err_buf), "Unexpected character '%c' (0x%02x)", c, (unsigned char)c);
|
||||
return make_error_token(lexer, err_buf, start_line, start_col);
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
#include "../include/common.h"
|
||||
#include "../include/lexer.h"
|
||||
#include "../include/parser.h"
|
||||
#include "../include/evaluator.h"
|
||||
|
||||
static void print_banner(void) {
|
||||
printf(COLOR_CYAN
|
||||
" ___ _ __ _____ __ ____ \n"
|
||||
" / | (_)________ _/ / ____ _____ ____ _ / ___// / / __ \\__ ______ \n"
|
||||
" / /| |/ / ___/ __ `/ / / __ `/ __ \\/ __ `// __// / / / / / / / / __ \\ \n"
|
||||
" / ___ / / / / /_/ / /___/ /_/ / / / / /_/ /___/ / /__/ /_/ / /_/ / /_/ / \n"
|
||||
" /_/ |/_/_/ \\__,_/_____/\\__,_/_/ /_/\\__, //____/____/_____/\\__,_/\\____/ \n"
|
||||
" /____/ \n"
|
||||
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);
|
||||
}
|
||||
|
||||
static char* read_file_source(const char* filepath) {
|
||||
FILE* f = fopen(filepath, "rb");
|
||||
if (!f) {
|
||||
fprintf(stderr, COLOR_RED "[!] Error: Could not open source file '%s'.\n" COLOR_RESET, filepath);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long length = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
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);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char* buffer = (char*)aira_malloc(length + 1);
|
||||
size_t read_bytes = fread(buffer, 1, length, f);
|
||||
buffer[read_bytes] = '\0';
|
||||
fclose(f);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static void run_source(const char* source, const char* filename, Evaluator* eval) {
|
||||
Parser parser;
|
||||
parser_init(&parser, source, filename);
|
||||
|
||||
StmtNode* program = parser_parse(&parser);
|
||||
if (!parser.had_error && program) {
|
||||
eval_program(eval, program);
|
||||
}
|
||||
|
||||
stmt_free(program);
|
||||
}
|
||||
|
||||
static void run_file(const char* filepath) {
|
||||
char* source = read_file_source(filepath);
|
||||
Evaluator eval;
|
||||
evaluator_init(&eval);
|
||||
|
||||
run_source(source, filepath, &eval);
|
||||
|
||||
evaluator_cleanup(&eval);
|
||||
aira_free(source);
|
||||
}
|
||||
|
||||
static void run_repl(void) {
|
||||
print_banner();
|
||||
printf(COLOR_CYAN "Type your AiraLang statements or 'exit'/'quit' to exit.\n\n" COLOR_RESET);
|
||||
|
||||
Evaluator eval;
|
||||
evaluator_init(&eval);
|
||||
|
||||
char line_buf[4096];
|
||||
for (;;) {
|
||||
printf(COLOR_GREEN "aira3> " COLOR_RESET);
|
||||
fflush(stdout);
|
||||
|
||||
if (!fgets(line_buf, sizeof(line_buf), stdin)) {
|
||||
printf("\n");
|
||||
break;
|
||||
}
|
||||
|
||||
size_t len = strlen(line_buf);
|
||||
if (len > 0 && line_buf[len - 1] == '\n') line_buf[--len] = '\0';
|
||||
if (len > 0 && line_buf[len - 1] == '\r') line_buf[--len] = '\0';
|
||||
|
||||
if (strcmp(line_buf, "exit") == 0 || strcmp(line_buf, "quit") == 0) {
|
||||
printf(COLOR_YELLOW "Goodbye Boss! Sovereign AiraLang S1 Duo stopping.\n" COLOR_RESET);
|
||||
break;
|
||||
}
|
||||
|
||||
if (len == 0) continue;
|
||||
|
||||
run_source(line_buf, "<repl>", &eval);
|
||||
}
|
||||
|
||||
evaluator_cleanup(&eval);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) {
|
||||
run_repl();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
|
||||
print_banner();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
|
||||
print_banner();
|
||||
printf("Usage:\n");
|
||||
printf(" airalang3 <script.aira> Execute AiraLang script with S1 Duo native engine\n");
|
||||
printf(" airalang3 -c \"<code>\" Execute inline AiraLang code string\n");
|
||||
printf(" airalang3 Start S1 Duo interactive REPL\n");
|
||||
printf(" airalang3 -v, --version Show engine version & security metadata\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "-c") == 0) {
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, COLOR_RED "[!] Error: -c requires a code string argument.\n" COLOR_RESET);
|
||||
return 1;
|
||||
}
|
||||
Evaluator eval;
|
||||
evaluator_init(&eval);
|
||||
run_source(argv[2], "<cli-eval>", &eval);
|
||||
evaluator_cleanup(&eval);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Direct execution: airalang3 script.aira */
|
||||
run_file(argv[1]);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
#include "../include/modules.h"
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* ==============================================================================
|
||||
* Sovereign Pure-C SHA256 Implementation (Zero-Dependency Security Engine)
|
||||
* ============================================================================== */
|
||||
|
||||
typedef struct {
|
||||
uint32_t state[8];
|
||||
uint64_t count;
|
||||
uint8_t buffer[64];
|
||||
} SHA256_CTX;
|
||||
|
||||
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
|
||||
#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
|
||||
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
#define EP0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
|
||||
#define EP1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
|
||||
#define SIG0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3))
|
||||
#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
|
||||
|
||||
static const uint32_t K[64] = {
|
||||
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
|
||||
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
|
||||
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
|
||||
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
|
||||
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
|
||||
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
|
||||
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
|
||||
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
|
||||
};
|
||||
|
||||
static void sha256_transform(SHA256_CTX* ctx, const uint8_t data[64]) {
|
||||
uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
|
||||
for (i = 0, j = 0; i < 16; ++i, j += 4)
|
||||
m[i] = ((uint32_t)data[j] << 24) | ((uint32_t)data[j + 1] << 16) | ((uint32_t)data[j + 2] << 8) | ((uint32_t)data[j + 3]);
|
||||
for (; i < 64; ++i)
|
||||
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
|
||||
a = ctx->state[0]; b = ctx->state[1]; c = ctx->state[2]; d = ctx->state[3];
|
||||
e = ctx->state[4]; f = ctx->state[5]; g = ctx->state[6]; h = ctx->state[7];
|
||||
for (i = 0; i < 64; ++i) {
|
||||
t1 = h + EP1(e) + CH(e, f, g) + K[i] + m[i];
|
||||
t2 = EP0(a) + MAJ(a, b, c);
|
||||
h = g; g = f; f = e; e = d + t1;
|
||||
d = c; c = b; b = a; a = t1 + t2;
|
||||
}
|
||||
ctx->state[0] += a; ctx->state[1] += b; ctx->state[2] += c; ctx->state[3] += d;
|
||||
ctx->state[4] += e; ctx->state[5] += f; ctx->state[6] += g; ctx->state[7] += h;
|
||||
}
|
||||
|
||||
static void sha256_init(SHA256_CTX* ctx) {
|
||||
ctx->count = 0;
|
||||
ctx->state[0] = 0x6a09e667; ctx->state[1] = 0xbb67ae85; ctx->state[2] = 0x3c6ef372; ctx->state[3] = 0xa54ff53a;
|
||||
ctx->state[4] = 0x510e527f; ctx->state[5] = 0x9b05688c; ctx->state[6] = 0x1f83d9ab; ctx->state[7] = 0x5be0cd19;
|
||||
}
|
||||
|
||||
static void sha256_update(SHA256_CTX* ctx, const uint8_t* data, size_t len) {
|
||||
size_t i, index, partlen;
|
||||
index = (size_t)((ctx->count >> 3) & 0x3f);
|
||||
partlen = 64 - index;
|
||||
ctx->count += (uint64_t)len << 3;
|
||||
if (len >= partlen) {
|
||||
memcpy(&ctx->buffer[index], data, partlen);
|
||||
sha256_transform(ctx, ctx->buffer);
|
||||
for (i = partlen; i + 63 < len; i += 64)
|
||||
sha256_transform(ctx, &data[i]);
|
||||
index = 0;
|
||||
} else {
|
||||
i = 0;
|
||||
}
|
||||
memcpy(&ctx->buffer[index], &data[i], len - i);
|
||||
}
|
||||
|
||||
static void sha256_final(SHA256_CTX* ctx, uint8_t digest[32]) {
|
||||
uint8_t finalcount[8];
|
||||
for (int i = 0; i < 8; i++)
|
||||
finalcount[i] = (uint8_t)((ctx->count >> ((7 - i) * 8)) & 0xff);
|
||||
uint8_t c = 0x80;
|
||||
sha256_update(ctx, &c, 1);
|
||||
while ((ctx->count >> 3) & 0x3f) {
|
||||
c = 0x00;
|
||||
sha256_update(ctx, &c, 1);
|
||||
}
|
||||
sha256_update(ctx, finalcount, 8);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
digest[i * 4] = (uint8_t)((ctx->state[i] >> 24) & 0xff);
|
||||
digest[i * 4 + 1] = (uint8_t)((ctx->state[i] >> 16) & 0xff);
|
||||
digest[i * 4 + 2] = (uint8_t)((ctx->state[i] >> 8) & 0xff);
|
||||
digest[i * 4 + 3] = (uint8_t)(ctx->state[i] & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
void aira_sha256(const uint8_t* data, size_t len, char output[65]) {
|
||||
SHA256_CTX ctx;
|
||||
uint8_t digest[32];
|
||||
sha256_init(&ctx);
|
||||
sha256_update(&ctx, data, len);
|
||||
sha256_final(&ctx, digest);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
sprintf(output + (i * 2), "%02x", digest[i]);
|
||||
}
|
||||
output[64] = '\0';
|
||||
}
|
||||
|
||||
/* ==============================================================================
|
||||
* Global Builtin Functions
|
||||
* ============================================================================== */
|
||||
|
||||
static AiraValue builtin_len(int arg_count, AiraValue* args) {
|
||||
if (arg_count < 1) return val_int(0);
|
||||
AiraValue v = args[0];
|
||||
if (v.type == VAL_STRING) return val_int((int64_t)strlen(v.as.string));
|
||||
if (v.type == VAL_LIST) return val_int((int64_t)v.as.list->count);
|
||||
if (v.type == VAL_DICT) return val_int((int64_t)v.as.dict->count);
|
||||
return val_int(0);
|
||||
}
|
||||
|
||||
static AiraValue builtin_range(int arg_count, AiraValue* args) {
|
||||
int64_t start = 0;
|
||||
int64_t end = 0;
|
||||
int64_t step = 1;
|
||||
|
||||
if (arg_count == 1) {
|
||||
end = args[0].type == VAL_INT ? args[0].as.integer : (int64_t)args[0].as.floating;
|
||||
} else if (arg_count >= 2) {
|
||||
start = args[0].type == VAL_INT ? args[0].as.integer : (int64_t)args[0].as.floating;
|
||||
end = args[1].type == VAL_INT ? args[1].as.integer : (int64_t)args[1].as.floating;
|
||||
if (arg_count >= 3) {
|
||||
step = args[2].type == VAL_INT ? args[2].as.integer : (int64_t)args[2].as.floating;
|
||||
if (step == 0) step = 1;
|
||||
}
|
||||
}
|
||||
|
||||
AiraValue list = val_list();
|
||||
if (step > 0) {
|
||||
for (int64_t i = start; i < end; i += step) {
|
||||
list_append(list.as.list, val_int(i));
|
||||
}
|
||||
} else {
|
||||
for (int64_t i = start; i > end; i += step) {
|
||||
list_append(list.as.list, val_int(i));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
static AiraValue builtin_input(int arg_count, AiraValue* args) {
|
||||
if (arg_count > 0) {
|
||||
val_print(args[0]);
|
||||
fflush(stdout);
|
||||
}
|
||||
char buf[1024];
|
||||
if (fgets(buf, sizeof(buf), stdin)) {
|
||||
size_t len = strlen(buf);
|
||||
if (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) {
|
||||
buf[len - 1] = '\0';
|
||||
}
|
||||
return val_string(buf);
|
||||
}
|
||||
return val_string("");
|
||||
}
|
||||
|
||||
static AiraValue builtin_type(int arg_count, AiraValue* args) {
|
||||
if (arg_count < 1) return val_string("null");
|
||||
switch (args[0].type) {
|
||||
case VAL_NULL: return val_string("null");
|
||||
case VAL_BOOL: return val_string("bool");
|
||||
case VAL_INT: return val_string("int");
|
||||
case VAL_FLOAT: return val_string("float");
|
||||
case VAL_STRING: return val_string("string");
|
||||
case VAL_LIST: return val_string("list");
|
||||
case VAL_DICT: return val_string("dict");
|
||||
case VAL_BUILTIN_FN:
|
||||
case VAL_FN: return val_string("function");
|
||||
case VAL_MODULE: return val_string("module");
|
||||
default: return val_string("unknown");
|
||||
}
|
||||
}
|
||||
|
||||
static AiraValue builtin_to_int(int arg_count, AiraValue* args) {
|
||||
if (arg_count < 1) return val_int(0);
|
||||
AiraValue v = args[0];
|
||||
if (v.type == VAL_INT) return v;
|
||||
if (v.type == VAL_FLOAT) return val_int((int64_t)v.as.floating);
|
||||
if (v.type == VAL_STRING) return val_int(strtoll(v.as.string, NULL, 10));
|
||||
if (v.type == VAL_BOOL) return val_int(v.as.boolean ? 1 : 0);
|
||||
return val_int(0);
|
||||
}
|
||||
|
||||
static AiraValue builtin_to_float(int arg_count, AiraValue* args) {
|
||||
if (arg_count < 1) return val_float(0.0);
|
||||
AiraValue v = args[0];
|
||||
if (v.type == VAL_FLOAT) return v;
|
||||
if (v.type == VAL_INT) return val_float((double)v.as.integer);
|
||||
if (v.type == VAL_STRING) return val_float(strtod(v.as.string, NULL));
|
||||
return val_float(0.0);
|
||||
}
|
||||
|
||||
static AiraValue builtin_to_str(int arg_count, AiraValue* args) {
|
||||
if (arg_count < 1) return val_string("");
|
||||
return val_string_take(val_to_string(args[0]));
|
||||
}
|
||||
|
||||
/* ==============================================================================
|
||||
* Native C Module Implementations
|
||||
* ============================================================================== */
|
||||
|
||||
/* Math Module Functions */
|
||||
static AiraValue math_sqrt(int argc, AiraValue* args) {
|
||||
double x = (argc > 0 && args[0].type == VAL_INT) ? (double)args[0].as.integer : (argc > 0 ? args[0].as.floating : 0.0);
|
||||
return val_float(sqrt(x));
|
||||
}
|
||||
|
||||
static AiraValue math_pow(int argc, AiraValue* args) {
|
||||
double b = (argc > 0 && args[0].type == VAL_INT) ? (double)args[0].as.integer : (argc > 0 ? args[0].as.floating : 0.0);
|
||||
double e = (argc > 1 && args[1].type == VAL_INT) ? (double)args[1].as.integer : (argc > 1 ? args[1].as.floating : 0.0);
|
||||
return val_float(pow(b, e));
|
||||
}
|
||||
|
||||
static AiraValue math_abs(int argc, AiraValue* args) {
|
||||
if (argc > 0 && args[0].type == VAL_INT) {
|
||||
int64_t i = args[0].as.integer;
|
||||
return val_int(i < 0 ? -i : i);
|
||||
}
|
||||
double x = argc > 0 ? args[0].as.floating : 0.0;
|
||||
return val_float(fabs(x));
|
||||
}
|
||||
|
||||
static AiraValue math_floor(int argc, AiraValue* args) {
|
||||
double x = (argc > 0 && args[0].type == VAL_INT) ? (double)args[0].as.integer : (argc > 0 ? args[0].as.floating : 0.0);
|
||||
return val_int((int64_t)floor(x));
|
||||
}
|
||||
|
||||
static AiraValue math_ceil(int argc, AiraValue* args) {
|
||||
double x = (argc > 0 && args[0].type == VAL_INT) ? (double)args[0].as.integer : (argc > 0 ? args[0].as.floating : 0.0);
|
||||
return val_int((int64_t)ceil(x));
|
||||
}
|
||||
|
||||
/* OS Module Functions */
|
||||
static AiraValue os_system(int argc, AiraValue* args) {
|
||||
if (argc < 1 || args[0].type != VAL_STRING) return val_int(-1);
|
||||
/* Safe execution check */
|
||||
int res = system(args[0].as.string);
|
||||
return val_int(res);
|
||||
}
|
||||
|
||||
static AiraValue os_getcwd(int argc, AiraValue* args) {
|
||||
(void)argc; (void)args;
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof(cwd))) {
|
||||
return val_string(cwd);
|
||||
}
|
||||
return val_string(".");
|
||||
}
|
||||
|
||||
static AiraValue os_getenv(int argc, AiraValue* args) {
|
||||
if (argc < 1 || args[0].type != VAL_STRING) return val_null();
|
||||
char* val = getenv(args[0].as.string);
|
||||
return val ? val_string(val) : val_null();
|
||||
}
|
||||
|
||||
static AiraValue os_platform(int argc, AiraValue* args) {
|
||||
(void)argc; (void)args;
|
||||
return val_string("linux-aarch64 (S1 Duo Engine)");
|
||||
}
|
||||
|
||||
/* Time Module Functions */
|
||||
static AiraValue time_time(int argc, AiraValue* args) {
|
||||
(void)argc; (void)args;
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
double sec = (double)ts.tv_sec + ((double)ts.tv_nsec / 1e9);
|
||||
return val_float(sec);
|
||||
}
|
||||
|
||||
static AiraValue time_sleep(int argc, AiraValue* args) {
|
||||
if (argc < 1) return val_null();
|
||||
double secs = (args[0].type == VAL_INT) ? (double)args[0].as.integer : args[0].as.floating;
|
||||
if (secs > 0) {
|
||||
usleep((useconds_t)(secs * 1000000));
|
||||
}
|
||||
return val_null();
|
||||
}
|
||||
|
||||
/* File Module Functions */
|
||||
static AiraValue file_read(int argc, AiraValue* args) {
|
||||
if (argc < 1 || args[0].type != VAL_STRING) return val_string("");
|
||||
FILE* f = fopen(args[0].as.string, "rb");
|
||||
if (!f) return val_null();
|
||||
fseek(f, 0, SEEK_END);
|
||||
long sz = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (sz < 0 || sz > AIRA_MAX_STRING_LEN) {
|
||||
fclose(f);
|
||||
aira_panic("File %s exceeds maximum permitted memory size.", args[0].as.string);
|
||||
}
|
||||
char* buf = (char*)aira_malloc(sz + 1);
|
||||
size_t read_bytes = fread(buf, 1, sz, f);
|
||||
buf[read_bytes] = '\0';
|
||||
fclose(f);
|
||||
return val_string_take(buf);
|
||||
}
|
||||
|
||||
static AiraValue file_write(int argc, AiraValue* args) {
|
||||
if (argc < 2 || args[0].type != VAL_STRING) return val_bool(false);
|
||||
FILE* f = fopen(args[0].as.string, "wb");
|
||||
if (!f) return val_bool(false);
|
||||
char* content = val_to_string(args[1]);
|
||||
fputs(content, f);
|
||||
aira_free(content);
|
||||
fclose(f);
|
||||
return val_bool(true);
|
||||
}
|
||||
|
||||
static AiraValue file_append(int argc, AiraValue* args) {
|
||||
if (argc < 2 || args[0].type != VAL_STRING) return val_bool(false);
|
||||
FILE* f = fopen(args[0].as.string, "ab");
|
||||
if (!f) return val_bool(false);
|
||||
char* content = val_to_string(args[1]);
|
||||
fputs(content, f);
|
||||
aira_free(content);
|
||||
fclose(f);
|
||||
return val_bool(true);
|
||||
}
|
||||
|
||||
static AiraValue file_exists(int argc, AiraValue* args) {
|
||||
if (argc < 1 || args[0].type != VAL_STRING) return val_bool(false);
|
||||
struct stat st;
|
||||
return val_bool(stat(args[0].as.string, &st) == 0);
|
||||
}
|
||||
|
||||
/* Crypto & Sec Module Functions */
|
||||
static AiraValue crypto_sha256(int argc, AiraValue* args) {
|
||||
if (argc < 1) return val_string("");
|
||||
char* str = val_to_string(args[0]);
|
||||
char hash[65];
|
||||
aira_sha256((const uint8_t*)str, strlen(str), hash);
|
||||
aira_free(str);
|
||||
return val_string(hash);
|
||||
}
|
||||
|
||||
/* ==============================================================================
|
||||
* Registration & Loader
|
||||
* ============================================================================== */
|
||||
|
||||
void register_builtin_globals(Environment* env) {
|
||||
env_define(env, "len", val_builtin(builtin_len));
|
||||
env_define(env, "range", val_builtin(builtin_range));
|
||||
env_define(env, "input", val_builtin(builtin_input));
|
||||
env_define(env, "type", val_builtin(builtin_type));
|
||||
env_define(env, "int", val_builtin(builtin_to_int));
|
||||
env_define(env, "float", val_builtin(builtin_to_float));
|
||||
env_define(env, "str", val_builtin(builtin_to_str));
|
||||
}
|
||||
|
||||
AiraValue load_builtin_module(const char* name) {
|
||||
if (strcmp(name, "math") == 0) {
|
||||
AiraValue mod = val_module("math");
|
||||
dict_set(mod.as.module->members, "sqrt", val_builtin(math_sqrt));
|
||||
dict_set(mod.as.module->members, "pow", val_builtin(math_pow));
|
||||
dict_set(mod.as.module->members, "abs", val_builtin(math_abs));
|
||||
dict_set(mod.as.module->members, "floor", val_builtin(math_floor));
|
||||
dict_set(mod.as.module->members, "ceil", val_builtin(math_ceil));
|
||||
dict_set(mod.as.module->members, "pi", val_float(3.141592653589793));
|
||||
dict_set(mod.as.module->members, "e", val_float(2.718281828459045));
|
||||
return mod;
|
||||
}
|
||||
|
||||
if (strcmp(name, "os") == 0 || strcmp(name, "system") == 0) {
|
||||
AiraValue mod = val_module("os");
|
||||
dict_set(mod.as.module->members, "system", val_builtin(os_system));
|
||||
dict_set(mod.as.module->members, "getcwd", val_builtin(os_getcwd));
|
||||
dict_set(mod.as.module->members, "getenv", val_builtin(os_getenv));
|
||||
dict_set(mod.as.module->members, "platform", val_builtin(os_platform));
|
||||
return mod;
|
||||
}
|
||||
|
||||
if (strcmp(name, "time") == 0) {
|
||||
AiraValue mod = val_module("time");
|
||||
dict_set(mod.as.module->members, "time", val_builtin(time_time));
|
||||
dict_set(mod.as.module->members, "sleep", val_builtin(time_sleep));
|
||||
return mod;
|
||||
}
|
||||
|
||||
if (strcmp(name, "file") == 0) {
|
||||
AiraValue mod = val_module("file");
|
||||
dict_set(mod.as.module->members, "read", val_builtin(file_read));
|
||||
dict_set(mod.as.module->members, "write", val_builtin(file_write));
|
||||
dict_set(mod.as.module->members, "append", val_builtin(file_append));
|
||||
dict_set(mod.as.module->members, "exists", val_builtin(file_exists));
|
||||
return mod;
|
||||
}
|
||||
|
||||
if (strcmp(name, "crypto") == 0 || strcmp(name, "sec") == 0) {
|
||||
AiraValue mod = val_module("crypto");
|
||||
dict_set(mod.as.module->members, "sha256", val_builtin(crypto_sha256));
|
||||
dict_set(mod.as.module->members, "hash", val_builtin(crypto_sha256));
|
||||
return mod;
|
||||
}
|
||||
|
||||
return val_null();
|
||||
}
|
||||
@@ -0,0 +1,552 @@
|
||||
#include "../include/parser.h"
|
||||
|
||||
static void advance(Parser* parser) {
|
||||
parser->previous = parser->current;
|
||||
|
||||
for (;;) {
|
||||
parser->current = lexer_next_token(&parser->lexer);
|
||||
if (parser->current.type != TOKEN_ERROR) break;
|
||||
|
||||
aira_error_at(parser->filename, parser->current.line, parser->current.col, "%s", parser->current.text);
|
||||
parser->had_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
static bool check(Parser* parser, TokenType type) {
|
||||
return parser->current.type == type;
|
||||
}
|
||||
|
||||
static bool match(Parser* parser, TokenType type) {
|
||||
if (!check(parser, type)) return false;
|
||||
advance(parser);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void consume(Parser* parser, TokenType type, const char* message) {
|
||||
if (parser->current.type == type) {
|
||||
advance(parser);
|
||||
return;
|
||||
}
|
||||
aira_error_at(parser->filename, parser->current.line, parser->current.col, "%s", message);
|
||||
parser->had_error = true;
|
||||
}
|
||||
|
||||
static void match_semicolon(Parser* parser) {
|
||||
if (match(parser, TOKEN_SEMICOLON)) return;
|
||||
/* Lenient if next is RBRACE or EOF */
|
||||
if (check(parser, TOKEN_RBRACE) || check(parser, TOKEN_EOF)) return;
|
||||
}
|
||||
|
||||
/* Forward declarations */
|
||||
static ExprNode* expression(Parser* parser);
|
||||
static StmtNode* statement(Parser* parser);
|
||||
static StmtNode* block(Parser* parser);
|
||||
|
||||
/* Expression Parsing with Precedence Climbing */
|
||||
|
||||
static ExprNode* primary(Parser* parser) {
|
||||
int line = parser->current.line;
|
||||
int col = parser->current.col;
|
||||
|
||||
if (match(parser, TOKEN_NUMBER)) {
|
||||
double val = parser->previous.num_val;
|
||||
if (val == (int64_t)val) {
|
||||
return expr_literal(val_int((int64_t)val), line, col);
|
||||
} else {
|
||||
return expr_literal(val_float(val), line, col);
|
||||
}
|
||||
}
|
||||
|
||||
if (match(parser, TOKEN_STRING)) {
|
||||
return expr_literal(val_string(parser->previous.text), line, col);
|
||||
}
|
||||
|
||||
if (match(parser, TOKEN_TRUE)) {
|
||||
return expr_literal(val_bool(true), line, col);
|
||||
}
|
||||
|
||||
if (match(parser, TOKEN_FALSE)) {
|
||||
return expr_literal(val_bool(false), line, col);
|
||||
}
|
||||
|
||||
if (match(parser, TOKEN_NULL)) {
|
||||
return expr_literal(val_null(), line, col);
|
||||
}
|
||||
|
||||
if (match(parser, TOKEN_IDENTIFIER)) {
|
||||
return expr_identifier(parser->previous.text, line, col);
|
||||
}
|
||||
|
||||
if (match(parser, TOKEN_LPAREN)) {
|
||||
ExprNode* expr = expression(parser);
|
||||
consume(parser, TOKEN_RPAREN, "Expected ')' after expression.");
|
||||
return expr;
|
||||
}
|
||||
|
||||
/* List Literal: [1, 2, 3] */
|
||||
if (match(parser, TOKEN_LBRACKET)) {
|
||||
int cap = 8;
|
||||
int count = 0;
|
||||
ExprNode** elements = (ExprNode**)aira_malloc(sizeof(ExprNode*) * cap);
|
||||
|
||||
if (!check(parser, TOKEN_RBRACKET)) {
|
||||
do {
|
||||
if (count >= cap) {
|
||||
cap *= 2;
|
||||
elements = (ExprNode**)aira_realloc(elements, sizeof(ExprNode*) * cap);
|
||||
}
|
||||
elements[count++] = expression(parser);
|
||||
} while (match(parser, TOKEN_COMMA));
|
||||
}
|
||||
consume(parser, TOKEN_RBRACKET, "Expected ']' after list elements.");
|
||||
return expr_list(elements, count, line, col);
|
||||
}
|
||||
|
||||
/* Dict Literal: {"key": value} */
|
||||
if (match(parser, TOKEN_LBRACE)) {
|
||||
int cap = 8;
|
||||
int count = 0;
|
||||
char** keys = (char**)aira_malloc(sizeof(char*) * cap);
|
||||
ExprNode** values = (ExprNode**)aira_malloc(sizeof(ExprNode*) * cap);
|
||||
|
||||
if (!check(parser, TOKEN_RBRACE)) {
|
||||
do {
|
||||
if (count >= cap) {
|
||||
cap *= 2;
|
||||
keys = (char**)aira_realloc(keys, sizeof(char*) * cap);
|
||||
values = (ExprNode**)aira_realloc(values, sizeof(ExprNode*) * cap);
|
||||
}
|
||||
if (!match(parser, TOKEN_STRING) && !match(parser, TOKEN_IDENTIFIER)) {
|
||||
aira_error_at(parser->filename, parser->current.line, parser->current.col, "Expected string or identifier key in dictionary.");
|
||||
break;
|
||||
}
|
||||
keys[count] = aira_strdup(parser->previous.text);
|
||||
consume(parser, TOKEN_COLON, "Expected ':' after dictionary key.");
|
||||
values[count] = expression(parser);
|
||||
count++;
|
||||
} while (match(parser, TOKEN_COMMA));
|
||||
}
|
||||
consume(parser, TOKEN_RBRACE, "Expected '}' after dictionary.");
|
||||
return expr_dict(keys, values, count, line, col);
|
||||
}
|
||||
|
||||
aira_error_at(parser->filename, parser->current.line, parser->current.col, "Unexpected token '%s' in expression.", parser->current.text ? parser->current.text : "EOF");
|
||||
parser->had_error = true;
|
||||
advance(parser);
|
||||
return expr_literal(val_null(), line, col);
|
||||
}
|
||||
|
||||
static ExprNode* call_or_access(Parser* parser) {
|
||||
ExprNode* expr = primary(parser);
|
||||
|
||||
for (;;) {
|
||||
int line = parser->current.line;
|
||||
int col = parser->current.col;
|
||||
|
||||
if (match(parser, TOKEN_LPAREN)) {
|
||||
/* Function call */
|
||||
int cap = 4;
|
||||
int count = 0;
|
||||
ExprNode** args = (ExprNode**)aira_malloc(sizeof(ExprNode*) * cap);
|
||||
|
||||
if (!check(parser, TOKEN_RPAREN)) {
|
||||
do {
|
||||
if (count >= cap) {
|
||||
cap *= 2;
|
||||
args = (ExprNode**)aira_realloc(args, sizeof(ExprNode*) * cap);
|
||||
}
|
||||
args[count++] = expression(parser);
|
||||
} while (match(parser, TOKEN_COMMA));
|
||||
}
|
||||
consume(parser, TOKEN_RPAREN, "Expected ')' after function arguments.");
|
||||
expr = expr_call(expr, args, count, line, col);
|
||||
} else if (match(parser, TOKEN_LBRACKET)) {
|
||||
/* Index access */
|
||||
ExprNode* index = expression(parser);
|
||||
consume(parser, TOKEN_RBRACKET, "Expected ']' after index.");
|
||||
expr = expr_index(expr, index, line, col);
|
||||
} else if (match(parser, TOKEN_DOT)) {
|
||||
/* Member access */
|
||||
consume(parser, TOKEN_IDENTIFIER, "Expected member name after '.'.");
|
||||
expr = expr_member(expr, parser->previous.text, line, col);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
static ExprNode* unary(Parser* parser) {
|
||||
if (match(parser, TOKEN_BANG) || match(parser, TOKEN_NOT) || match(parser, TOKEN_MINUS) || match(parser, TOKEN_PLUS)) {
|
||||
TokenType op = parser->previous.type;
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
ExprNode* right = unary(parser);
|
||||
return expr_unary(op, right, line, col);
|
||||
}
|
||||
return call_or_access(parser);
|
||||
}
|
||||
|
||||
static ExprNode* factor(Parser* parser) {
|
||||
ExprNode* expr = unary(parser);
|
||||
|
||||
while (match(parser, TOKEN_STAR) || match(parser, TOKEN_SLASH) || match(parser, TOKEN_PERCENT)) {
|
||||
TokenType op = parser->previous.type;
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
ExprNode* right = unary(parser);
|
||||
expr = expr_binary(op, expr, right, line, col);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
static ExprNode* term(Parser* parser) {
|
||||
ExprNode* expr = factor(parser);
|
||||
|
||||
while (match(parser, TOKEN_PLUS) || match(parser, TOKEN_MINUS)) {
|
||||
TokenType op = parser->previous.type;
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
ExprNode* right = factor(parser);
|
||||
expr = expr_binary(op, expr, right, line, col);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
static ExprNode* comparison(Parser* parser) {
|
||||
ExprNode* expr = term(parser);
|
||||
|
||||
while (match(parser, TOKEN_GREATER) || match(parser, TOKEN_GREATER_EQUAL) ||
|
||||
match(parser, TOKEN_LESS) || match(parser, TOKEN_LESS_EQUAL)) {
|
||||
TokenType op = parser->previous.type;
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
ExprNode* right = term(parser);
|
||||
expr = expr_binary(op, expr, right, line, col);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
static ExprNode* equality(Parser* parser) {
|
||||
ExprNode* expr = comparison(parser);
|
||||
|
||||
while (match(parser, TOKEN_EQUAL_EQUAL) || match(parser, TOKEN_BANG_EQUAL)) {
|
||||
TokenType op = parser->previous.type;
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
ExprNode* right = comparison(parser);
|
||||
expr = expr_binary(op, expr, right, line, col);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
static ExprNode* logical_and(Parser* parser) {
|
||||
ExprNode* expr = equality(parser);
|
||||
|
||||
while (match(parser, TOKEN_AND_AND) || match(parser, TOKEN_AND)) {
|
||||
TokenType op = parser->previous.type;
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
ExprNode* right = equality(parser);
|
||||
expr = expr_binary(op, expr, right, line, col);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
static ExprNode* logical_or(Parser* parser) {
|
||||
ExprNode* expr = logical_and(parser);
|
||||
|
||||
while (match(parser, TOKEN_OR_OR) || match(parser, TOKEN_OR)) {
|
||||
TokenType op = parser->previous.type;
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
ExprNode* right = logical_and(parser);
|
||||
expr = expr_binary(op, expr, right, line, col);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
static ExprNode* expression(Parser* parser) {
|
||||
return logical_or(parser);
|
||||
}
|
||||
|
||||
/* Statement Parsing */
|
||||
|
||||
static StmtNode* let_declaration(Parser* parser) {
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
consume(parser, TOKEN_IDENTIFIER, "Expected variable name after 'let'.");
|
||||
char* name = aira_strdup(parser->previous.text);
|
||||
|
||||
ExprNode* init = NULL;
|
||||
if (match(parser, TOKEN_ASSIGN)) {
|
||||
init = expression(parser);
|
||||
} else {
|
||||
init = expr_literal(val_null(), line, col);
|
||||
}
|
||||
match_semicolon(parser);
|
||||
StmtNode* s = stmt_let(name, init, line, col);
|
||||
aira_free(name);
|
||||
return s;
|
||||
}
|
||||
|
||||
static StmtNode* say_or_print_statement(Parser* parser, bool is_say) {
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
int cap = 4;
|
||||
int count = 0;
|
||||
ExprNode** exprs = (ExprNode**)aira_malloc(sizeof(ExprNode*) * cap);
|
||||
|
||||
/* Check if print has parentheses like print("hello") */
|
||||
bool has_paren = match(parser, TOKEN_LPAREN);
|
||||
|
||||
if (!has_paren || !check(parser, TOKEN_RPAREN)) {
|
||||
do {
|
||||
if (count >= cap) {
|
||||
cap *= 2;
|
||||
exprs = (ExprNode**)aira_realloc(exprs, sizeof(ExprNode*) * cap);
|
||||
}
|
||||
exprs[count++] = expression(parser);
|
||||
} while (match(parser, TOKEN_COMMA));
|
||||
}
|
||||
|
||||
if (has_paren) {
|
||||
consume(parser, TOKEN_RPAREN, "Expected ')' after arguments.");
|
||||
}
|
||||
|
||||
match_semicolon(parser);
|
||||
if (is_say) {
|
||||
return stmt_say(exprs, count, line, col);
|
||||
} else {
|
||||
return stmt_print(exprs, count, line, col);
|
||||
}
|
||||
}
|
||||
|
||||
static StmtNode* if_statement(Parser* parser) {
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
bool has_paren = match(parser, TOKEN_LPAREN);
|
||||
ExprNode* condition = expression(parser);
|
||||
if (has_paren) {
|
||||
consume(parser, TOKEN_RPAREN, "Expected ')' after if condition.");
|
||||
}
|
||||
|
||||
StmtNode* then_branch = statement(parser);
|
||||
StmtNode* else_branch = NULL;
|
||||
|
||||
if (match(parser, TOKEN_ELSE)) {
|
||||
else_branch = statement(parser);
|
||||
}
|
||||
return stmt_if(condition, then_branch, else_branch, line, col);
|
||||
}
|
||||
|
||||
static StmtNode* while_statement(Parser* parser) {
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
bool has_paren = match(parser, TOKEN_LPAREN);
|
||||
ExprNode* condition = expression(parser);
|
||||
if (has_paren) {
|
||||
consume(parser, TOKEN_RPAREN, "Expected ')' after while condition.");
|
||||
}
|
||||
StmtNode* body = statement(parser);
|
||||
return stmt_while(condition, body, line, col);
|
||||
}
|
||||
|
||||
static StmtNode* for_statement(Parser* parser) {
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
consume(parser, TOKEN_IDENTIFIER, "Expected variable name after 'for'.");
|
||||
char* var_name = aira_strdup(parser->previous.text);
|
||||
|
||||
consume(parser, TOKEN_IN, "Expected 'in' after for variable.");
|
||||
ExprNode* iterable = expression(parser);
|
||||
StmtNode* body = statement(parser);
|
||||
|
||||
StmtNode* s = stmt_for_in(var_name, iterable, body, line, col);
|
||||
aira_free(var_name);
|
||||
return s;
|
||||
}
|
||||
|
||||
static StmtNode* fn_declaration(Parser* parser) {
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
consume(parser, TOKEN_IDENTIFIER, "Expected function name after 'fn'.");
|
||||
char* name = aira_strdup(parser->previous.text);
|
||||
|
||||
consume(parser, TOKEN_LPAREN, "Expected '(' after function name.");
|
||||
int cap = 4;
|
||||
int count = 0;
|
||||
char** params = (char**)aira_malloc(sizeof(char*) * cap);
|
||||
|
||||
if (!check(parser, TOKEN_RPAREN)) {
|
||||
do {
|
||||
if (count >= cap) {
|
||||
cap *= 2;
|
||||
params = (char**)aira_realloc(params, sizeof(char*) * cap);
|
||||
}
|
||||
consume(parser, TOKEN_IDENTIFIER, "Expected parameter name.");
|
||||
params[count++] = aira_strdup(parser->previous.text);
|
||||
} while (match(parser, TOKEN_COMMA));
|
||||
}
|
||||
consume(parser, TOKEN_RPAREN, "Expected ')' after parameters.");
|
||||
StmtNode* body = block(parser);
|
||||
|
||||
StmtNode* s = stmt_fn_def(name, params, count, body, line, col);
|
||||
aira_free(name);
|
||||
return s;
|
||||
}
|
||||
|
||||
static StmtNode* return_statement(Parser* parser) {
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
ExprNode* expr = NULL;
|
||||
if (!check(parser, TOKEN_SEMICOLON) && !check(parser, TOKEN_RBRACE)) {
|
||||
expr = expression(parser);
|
||||
}
|
||||
match_semicolon(parser);
|
||||
return stmt_return(expr, line, col);
|
||||
}
|
||||
|
||||
static StmtNode* import_statement(Parser* parser) {
|
||||
int line = parser->previous.line;
|
||||
int col = parser->previous.col;
|
||||
|
||||
char* mod_name = NULL;
|
||||
if (match(parser, TOKEN_STRING) || match(parser, TOKEN_IDENTIFIER)) {
|
||||
mod_name = aira_strdup(parser->previous.text);
|
||||
} else {
|
||||
aira_error_at(parser->filename, line, col, "Expected module name after 'import'.");
|
||||
mod_name = aira_strdup("unknown");
|
||||
}
|
||||
|
||||
char* source = NULL;
|
||||
if (match(parser, TOKEN_FROM)) {
|
||||
if (match(parser, TOKEN_STRING) || match(parser, TOKEN_IDENTIFIER)) {
|
||||
source = aira_strdup(parser->previous.text);
|
||||
}
|
||||
}
|
||||
|
||||
match_semicolon(parser);
|
||||
StmtNode* s = stmt_import(mod_name, source, NULL, line, col);
|
||||
aira_free(mod_name);
|
||||
if (source) aira_free(source);
|
||||
return s;
|
||||
}
|
||||
|
||||
static StmtNode* block(Parser* parser) {
|
||||
int line = parser->current.line;
|
||||
int col = parser->current.col;
|
||||
consume(parser, TOKEN_LBRACE, "Expected '{' before block.");
|
||||
|
||||
int cap = 8;
|
||||
int count = 0;
|
||||
StmtNode** statements = (StmtNode**)aira_malloc(sizeof(StmtNode*) * cap);
|
||||
|
||||
while (!check(parser, TOKEN_RBRACE) && !check(parser, TOKEN_EOF)) {
|
||||
if (count >= cap) {
|
||||
cap *= 2;
|
||||
statements = (StmtNode**)aira_realloc(statements, sizeof(StmtNode*) * cap);
|
||||
}
|
||||
statements[count++] = statement(parser);
|
||||
}
|
||||
consume(parser, TOKEN_RBRACE, "Expected '}' after block.");
|
||||
return stmt_block(statements, count, line, col);
|
||||
}
|
||||
|
||||
static StmtNode* expression_statement(Parser* parser) {
|
||||
int line = parser->current.line;
|
||||
int col = parser->current.col;
|
||||
|
||||
/* Check for assignment target: ident = expr, ident += expr, target[idx] = expr, target.mem = expr */
|
||||
ExprNode* expr = expression(parser);
|
||||
|
||||
if (match(parser, TOKEN_ASSIGN) || match(parser, TOKEN_PLUS_ASSIGN) ||
|
||||
match(parser, TOKEN_MINUS_ASSIGN) || match(parser, TOKEN_STAR_ASSIGN) ||
|
||||
match(parser, TOKEN_SLASH_ASSIGN) || match(parser, TOKEN_MOD_ASSIGN)) {
|
||||
TokenType op = parser->previous.type;
|
||||
|
||||
if (expr->type == EXPR_IDENTIFIER) {
|
||||
char* name = aira_strdup(expr->as.identifier);
|
||||
expr_free(expr);
|
||||
ExprNode* val = expression(parser);
|
||||
match_semicolon(parser);
|
||||
StmtNode* s = stmt_assign(name, op, val, line, col);
|
||||
aira_free(name);
|
||||
return s;
|
||||
} else if (expr->type == EXPR_INDEX) {
|
||||
ExprNode* target = expr->as.index_access.target;
|
||||
ExprNode* index = expr->as.index_access.index;
|
||||
aira_free(expr);
|
||||
ExprNode* val = expression(parser);
|
||||
match_semicolon(parser);
|
||||
return stmt_index_assign(target, index, val, line, col);
|
||||
} else if (expr->type == EXPR_MEMBER) {
|
||||
ExprNode* target = expr->as.member_access.target;
|
||||
char* mem = aira_strdup(expr->as.member_access.member);
|
||||
expr_free(expr);
|
||||
ExprNode* val = expression(parser);
|
||||
match_semicolon(parser);
|
||||
StmtNode* s = stmt_member_assign(target, mem, val, line, col);
|
||||
aira_free(mem);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
match_semicolon(parser);
|
||||
return stmt_expr(expr, line, col);
|
||||
}
|
||||
|
||||
static StmtNode* statement(Parser* parser) {
|
||||
if (match(parser, TOKEN_LET)) return let_declaration(parser);
|
||||
if (match(parser, TOKEN_SAY)) return say_or_print_statement(parser, true);
|
||||
if (match(parser, TOKEN_PRINT)) return say_or_print_statement(parser, false);
|
||||
if (match(parser, TOKEN_IF)) return if_statement(parser);
|
||||
if (match(parser, TOKEN_WHILE)) return while_statement(parser);
|
||||
if (match(parser, TOKEN_FOR)) return for_statement(parser);
|
||||
if (match(parser, TOKEN_FN)) return fn_declaration(parser);
|
||||
if (match(parser, TOKEN_RETURN)) return return_statement(parser);
|
||||
if (match(parser, TOKEN_IMPORT)) return import_statement(parser);
|
||||
if (match(parser, TOKEN_BREAK)) {
|
||||
match_semicolon(parser);
|
||||
return stmt_break(parser->previous.line, parser->previous.col);
|
||||
}
|
||||
if (match(parser, TOKEN_CONTINUE)) {
|
||||
match_semicolon(parser);
|
||||
return stmt_continue(parser->previous.line, parser->previous.col);
|
||||
}
|
||||
if (check(parser, TOKEN_LBRACE)) return block(parser);
|
||||
|
||||
/* Skip @ directives like @airaai */
|
||||
if (match(parser, TOKEN_AT)) {
|
||||
advance(parser); /* consume directive name */
|
||||
if (check(parser, TOKEN_STRING)) advance(parser);
|
||||
match_semicolon(parser);
|
||||
return stmt_expr(expr_literal(val_null(), 0, 0), 0, 0);
|
||||
}
|
||||
|
||||
return expression_statement(parser);
|
||||
}
|
||||
|
||||
void parser_init(Parser* parser, const char* source, const char* filename) {
|
||||
lexer_init(&parser->lexer, source, filename);
|
||||
parser->filename = filename;
|
||||
parser->had_error = false;
|
||||
parser->panic_mode = false;
|
||||
advance(parser);
|
||||
}
|
||||
|
||||
StmtNode* parser_parse(Parser* parser) {
|
||||
int cap = 16;
|
||||
int count = 0;
|
||||
StmtNode** statements = (StmtNode**)aira_malloc(sizeof(StmtNode*) * cap);
|
||||
|
||||
while (!check(parser, TOKEN_EOF)) {
|
||||
if (count >= cap) {
|
||||
cap *= 2;
|
||||
statements = (StmtNode**)aira_realloc(statements, sizeof(StmtNode*) * cap);
|
||||
}
|
||||
statements[count++] = statement(parser);
|
||||
}
|
||||
|
||||
return stmt_program(statements, count, 1, 1);
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
AiraValue val_bool(bool b) {
|
||||
AiraValue v;
|
||||
v.type = VAL_BOOL;
|
||||
v.canary = AIRA_CANARY_VALUE;
|
||||
v.as.boolean = b;
|
||||
return v;
|
||||
}
|
||||
|
||||
AiraValue val_int(int64_t i) {
|
||||
AiraValue v;
|
||||
v.type = VAL_INT;
|
||||
v.canary = AIRA_CANARY_VALUE;
|
||||
v.as.integer = i;
|
||||
return v;
|
||||
}
|
||||
|
||||
AiraValue val_float(double f) {
|
||||
AiraValue v;
|
||||
v.type = VAL_FLOAT;
|
||||
v.canary = AIRA_CANARY_VALUE;
|
||||
v.as.floating = f;
|
||||
return v;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
v.as.list = list;
|
||||
return v;
|
||||
}
|
||||
|
||||
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);
|
||||
dict->values = (AiraValue*)aira_malloc(sizeof(AiraValue) * dict->capacity);
|
||||
v.as.dict = dict;
|
||||
return v;
|
||||
}
|
||||
|
||||
AiraValue val_builtin(AiraBuiltinFn fn) {
|
||||
AiraValue v;
|
||||
v.type = VAL_BUILTIN_FN;
|
||||
v.canary = AIRA_CANARY_VALUE;
|
||||
v.as.builtin = fn;
|
||||
return v;
|
||||
}
|
||||
|
||||
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;
|
||||
v.as.fn = fn;
|
||||
return v;
|
||||
}
|
||||
|
||||
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);
|
||||
mod->members->values = (AiraValue*)aira_malloc(sizeof(AiraValue) * mod->members->capacity);
|
||||
v.as.module = mod;
|
||||
return v;
|
||||
}
|
||||
|
||||
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_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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
return 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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
return val_int(a.as.integer % b.as.integer);
|
||||
}
|
||||
aira_panic("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");
|
||||
case VAL_BOOL: return aira_strdup(v.as.boolean ? "true" : "false");
|
||||
case VAL_INT:
|
||||
snprintf(buf, sizeof(buf), "%" PRId64, v.as.integer);
|
||||
return aira_strdup(buf);
|
||||
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_LIST: {
|
||||
AiraStringBuilder sb;
|
||||
sb_init(&sb);
|
||||
sb_append(&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);
|
||||
}
|
||||
sb_append(&sb, "]");
|
||||
return sb_finish(&sb);
|
||||
}
|
||||
case VAL_DICT: {
|
||||
AiraStringBuilder sb;
|
||||
sb_init(&sb);
|
||||
sb_append(&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, "}");
|
||||
return sb_finish(&sb);
|
||||
}
|
||||
case VAL_BUILTIN_FN: return aira_strdup("<native fn>");
|
||||
case VAL_FN: {
|
||||
snprintf(buf, sizeof(buf), "<fn %s>", v.as.fn && 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");
|
||||
return aira_strdup(buf);
|
||||
}
|
||||
default: return aira_strdup("undefined");
|
||||
}
|
||||
}
|
||||
|
||||
void val_print(AiraValue v) {
|
||||
char* s = val_to_string(v);
|
||||
printf("%s", s);
|
||||
aira_free(s);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void val_free(AiraValue* v) {
|
||||
if (!v) return;
|
||||
if (v->type == VAL_STRING && v->as.string) {
|
||||
aira_free(v->as.string);
|
||||
v->as.string = NULL;
|
||||
}
|
||||
v->type = VAL_NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
list->elements[list->count++] = 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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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.");
|
||||
}
|
||||
return list->elements[--list->count];
|
||||
}
|
||||
|
||||
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;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (dict->count >= dict->capacity) {
|
||||
dict->capacity *= 2;
|
||||
dict->keys = (char**)aira_realloc(dict->keys, sizeof(char*) * dict->capacity);
|
||||
dict->values = (AiraValue*)aira_realloc(dict->values, sizeof(AiraValue) * dict->capacity);
|
||||
}
|
||||
dict->keys[dict->count] = aira_strdup(key);
|
||||
dict->values[dict->count] = v;
|
||||
dict->count++;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
return val_null();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// ==========================================
|
||||
// AiraLang S1 Duo — Basic Test Suite
|
||||
// Architect: Adam Eehan
|
||||
// ==========================================
|
||||
|
||||
say "--- [1] Variable & Math Test ---";
|
||||
let a = 15;
|
||||
let b = 4;
|
||||
say "a =", a, ", b =", b;
|
||||
say "a + b =", a + b;
|
||||
say "a - b =", a - b;
|
||||
say "a * b =", a * b;
|
||||
say "a / b =", a / b;
|
||||
say "a % b =", a % b;
|
||||
|
||||
say "\n--- [2] Control Flow Test ---";
|
||||
let score = 92;
|
||||
if (score >= 90) {
|
||||
say "Grade: S-Rank Master!";
|
||||
} else {
|
||||
say "Grade: Standard";
|
||||
}
|
||||
|
||||
let count = 3;
|
||||
while (count > 0) {
|
||||
say "Countdown:", count;
|
||||
count -= 1;
|
||||
}
|
||||
|
||||
say "\n--- [3] Function Test ---";
|
||||
fn fib(n) {
|
||||
if (n <= 1) {
|
||||
return n;
|
||||
}
|
||||
return fib(n - 1) + fib(n - 2);
|
||||
}
|
||||
|
||||
say "fib(10) =", fib(10);
|
||||
|
||||
say "\n--- [4] Data Structures Test ---";
|
||||
let list = ["AiraLang", "S1", "Duo"];
|
||||
say "List length:", list.length;
|
||||
for item in list {
|
||||
say "Item:", item;
|
||||
}
|
||||
|
||||
let user = {"name": "Adam Eehan", "title": "Founder & CEO"};
|
||||
say "Dict access:", user["name"], "-", user["title"];
|
||||
|
||||
say "\n--- [5] Native Modules Test ---";
|
||||
import "math";
|
||||
say "Square root of 144 =", math.sqrt(144);
|
||||
say "PI =", math.pi;
|
||||
|
||||
import "crypto";
|
||||
let data = "Aira Group Sovereign Security 2026";
|
||||
let hash = crypto.sha256(data);
|
||||
say "SHA-256 of '" + data + "':";
|
||||
say hash;
|
||||
|
||||
say "\n[✓] All AiraLang S1 Duo basic tests passed perfectly!";
|
||||
@@ -0,0 +1,8 @@
|
||||
let start = 0;
|
||||
let total = 0;
|
||||
let i = 1;
|
||||
while (i <= 50000) {
|
||||
total += i;
|
||||
i += 1;
|
||||
}
|
||||
say "Sum of 1 to 50000 =", total;
|
||||
@@ -0,0 +1,3 @@
|
||||
let arr = [10, 20, 30];
|
||||
say "Accessing out-of-bounds index 999...";
|
||||
say arr[999];
|
||||
@@ -0,0 +1,10 @@
|
||||
// ==========================================
|
||||
// AiraLang S1 Duo — Security & Defense Test
|
||||
// ==========================================
|
||||
|
||||
say "--- Testing Recursion Depth Defense ---";
|
||||
fn infinite_recursion(n) {
|
||||
return infinite_recursion(n + 1);
|
||||
}
|
||||
|
||||
infinite_recursion(1);
|
||||
+1
-1
@@ -18,4 +18,4 @@ let greeting = ai.ask("Say Hello to the developer from AiraLang!");
|
||||
say "AI Engine Response:", greeting;
|
||||
|
||||
// In-Code AI Inspection Directive
|
||||
@airaai "any errors?"
|
||||
@airaai "any errors?";
|
||||
Reference in new issue
Block a user