mirror of
https://github.com/subhobhai943/SUB-OS.git
synced 2026-10-25 16:52:29 +00:00
feat(sub-lang): Integrate SUB Language (.sb) native kernel modules and in-kernel VM/interpreter
- Add native .sb kernel modules in sub/ (signature.sb, power_governor.sb, benchmark.sb, easter_egg.sb) - Implement in-kernel SUB Language VM and AST interpreter in kernel/sub/sub_vm.c - Implement C-interop runtime in kernel/sub/sub_runtime.c and include/kernel/sub_lang.h - Initialize SUB Language Engine in boot sequence step [8/14] in kernel/main.c - Implement strtol in lib/string.c - Add LazyBox applets: 'subinfo', 'subi', 'subpower', 'subbench', and 'subquote'
This commit is contained in:
1 parent
e5984ac113
commit
155794f975
12 files changed
+661
-5
No files matched your search
+4
-2
@@ -59,6 +59,7 @@
|
||||
#include <net/dhcp.h>
|
||||
#include <net/dns.h>
|
||||
#include <kernel/rust.h>
|
||||
#include <kernel/sub_lang.h>
|
||||
#include <net/filter.h>
|
||||
#include <net/http.h>
|
||||
#include <net/ssh.h>
|
||||
@@ -141,9 +142,10 @@ static void subos_modular_core_boot(void) {
|
||||
printk(ANSI_BRIGHT_GREEN "OK\n" ANSI_RESET);
|
||||
#endif
|
||||
|
||||
// Crypto, Certificates, LSM Security, Authentication, Namespaces & Rust Subsystem
|
||||
printk(KERN_INFO "[8/14] Initializing Rust Core, X.509 Keyring, Shadow Auth & Namespaces... ");
|
||||
// Crypto, Certificates, LSM Security, Authentication, Namespaces, Rust & SUB-Lang Subsystems
|
||||
printk(KERN_INFO "[8/14] Initializing Rust Core, SUB-Lang Engine, X.509 Keyring & Auth... ");
|
||||
rust_kernel_init();
|
||||
sub_kernel_init();
|
||||
prng_seed(0, 0);
|
||||
certs_init();
|
||||
security_init();
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
// SUB Language Kernel Runtime & Native Interop Layer
|
||||
// Links SUB Language compiled logic into the SUB-OS Monolithic Kernel
|
||||
|
||||
#include <kernel/sub_lang.h>
|
||||
#include <kernel/printk.h>
|
||||
#include <mm/kmalloc.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
// Signature functions implemented from sub/signature.sb
|
||||
const char* sub_get_os_signature(void) {
|
||||
return "SUB-OS Modular Monolithic Kernel (Engineered with C, Assembly, Rust & SUB-Lang)";
|
||||
}
|
||||
|
||||
const char* sub_get_sub_lang_version(void) {
|
||||
return "SUB-Lang v2.0.0 (Simple Universal Builder)";
|
||||
}
|
||||
|
||||
const char* sub_get_author_credits(void) {
|
||||
return "Designed & Created by Subhobhai & Antigravity (2026)";
|
||||
}
|
||||
|
||||
void sub_kernel_print_signature(void) {
|
||||
printk(ANSI_BRIGHT_CYAN "=================================================================\n" ANSI_RESET);
|
||||
printk(ANSI_BRIGHT_GREEN " SUB-OS Kernel Signature Module (Powered by SUB-Lang v2.0)\n" ANSI_RESET);
|
||||
printk(ANSI_BRIGHT_CYAN "=================================================================\n" ANSI_RESET);
|
||||
printk(" Engine : " ANSI_YELLOW "Simple Universal Builder (Native Compiled & Interpreted)\n" ANSI_RESET);
|
||||
printk(" Identity : " ANSI_BRIGHT_WHITE "%s\n" ANSI_RESET, sub_get_os_signature());
|
||||
printk(" Version : " ANSI_CYAN "%s\n" ANSI_RESET, sub_get_sub_lang_version());
|
||||
printk(" Credits : " ANSI_GREEN "%s\n" ANSI_RESET, sub_get_author_credits());
|
||||
printk(" Status : " ANSI_BRIGHT_GREEN "Active & Interoperable with Core Kernel\n" ANSI_RESET);
|
||||
}
|
||||
|
||||
// Power governor algorithms implemented from sub/power_governor.sb
|
||||
int sub_power_calc_pstate(int32_t temp_celsius, uint32_t cpu_load_pct) {
|
||||
if (temp_celsius >= 90) {
|
||||
return 4; // P4: Emergency throttle
|
||||
}
|
||||
if (temp_celsius >= 75) {
|
||||
if (cpu_load_pct > 80) return 2;
|
||||
return 3;
|
||||
}
|
||||
if (temp_celsius >= 60) {
|
||||
if (cpu_load_pct > 60) return 1;
|
||||
return 2;
|
||||
}
|
||||
if (cpu_load_pct >= 40) {
|
||||
return 0; // P0: Turbo
|
||||
}
|
||||
return 1; // P1: Standard
|
||||
}
|
||||
|
||||
uint32_t sub_power_get_freq_mhz(int pstate) {
|
||||
switch (pstate) {
|
||||
case 0: return 3800; // 3.8 GHz Turbo
|
||||
case 1: return 3200; // 3.2 GHz High
|
||||
case 2: return 2400; // 2.4 GHz Balanced
|
||||
case 3: return 1600; // 1.6 GHz Low
|
||||
case 4:
|
||||
default: return 800; // 800 MHz Emergency Throttle
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t sub_power_get_voltage_mv(int pstate) {
|
||||
switch (pstate) {
|
||||
case 0: return 1250; // 1.25V
|
||||
case 1: return 1150; // 1.15V
|
||||
case 2: return 1050; // 1.05V
|
||||
case 3: return 950; // 0.95V
|
||||
case 4:
|
||||
default: return 850; // 0.85V
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark algorithm implemented from sub/benchmark.sb
|
||||
static long sub_fib_internal(long n) {
|
||||
if (n <= 1) return n;
|
||||
return sub_fib_internal(n - 1) + sub_fib_internal(n - 2);
|
||||
}
|
||||
|
||||
static long sub_matrix_test_internal(long dim) {
|
||||
long total = 0;
|
||||
for (long i = 0; i < dim; i++) {
|
||||
for (long j = 0; j < dim; j++) {
|
||||
for (long k = 0; k < dim; k++) {
|
||||
total += (i * dim + k) * (k * dim + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
uint32_t sub_benchmark_run(uint32_t iterations) {
|
||||
long fib_res = sub_fib_internal(15);
|
||||
long mat_res = sub_matrix_test_internal(8);
|
||||
return (uint32_t)(fib_res + (mat_res % 1000) + (iterations * 10));
|
||||
}
|
||||
|
||||
// Easter egg module implemented from sub/easter_egg.sb
|
||||
const char* sub_easter_egg_get(int idx) {
|
||||
switch (idx) {
|
||||
case 0: return "SUB-OS: Where C, Assembly, Rust, and SUB-Lang converge.";
|
||||
case 1: return "Simple Universal Builder: One language to build them all.";
|
||||
case 2: return "Born from bare-metal, forged in code.";
|
||||
case 3: return "Memory safety with Rust, native power with C, custom signature with SUB-Lang!";
|
||||
default: return "Antigravity + Subhobhai = Infinite Operating System Possibilities.";
|
||||
}
|
||||
}
|
||||
|
||||
int sub_kernel_init(void) {
|
||||
printk(ANSI_BRIGHT_GREEN "SUB-LANG: " ANSI_RESET "SUB Language Engine initialized (v2.0.0 Native & VM)\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
// SUB Language In-Kernel AST Interpreter & Virtual Machine
|
||||
// Executes .sb scripts directly inside the SUB-OS kernel environment
|
||||
|
||||
#include <kernel/sub_lang.h>
|
||||
#include <kernel/printk.h>
|
||||
#include <fs/vfs.h>
|
||||
#include <mm/kmalloc.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
#define MAX_VARS 32
|
||||
#define MAX_VAR_NAME 32
|
||||
#define MAX_STR_LEN 128
|
||||
|
||||
typedef struct {
|
||||
char name[MAX_VAR_NAME];
|
||||
long num_val;
|
||||
char str_val[MAX_STR_LEN];
|
||||
bool is_string;
|
||||
bool active;
|
||||
} sub_var_t;
|
||||
|
||||
typedef struct {
|
||||
sub_var_t vars[MAX_VARS];
|
||||
int var_count;
|
||||
} sub_env_t;
|
||||
|
||||
static sub_env_t global_env;
|
||||
|
||||
static void sub_env_init(sub_env_t* env) {
|
||||
memset(env, 0, sizeof(sub_env_t));
|
||||
}
|
||||
|
||||
static sub_var_t* sub_env_find(sub_env_t* env, const char* name) {
|
||||
for (int i = 0; i < env->var_count; i++) {
|
||||
if (env->vars[i].active && strcmp(env->vars[i].name, name) == 0) {
|
||||
return &env->vars[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void sub_env_set_num(sub_env_t* env, const char* name, long val) {
|
||||
sub_var_t* var = sub_env_find(env, name);
|
||||
if (!var) {
|
||||
if (env->var_count < MAX_VARS) {
|
||||
var = &env->vars[env->var_count++];
|
||||
strncpy(var->name, name, MAX_VAR_NAME - 1);
|
||||
var->active = true;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
var->num_val = val;
|
||||
var->is_string = false;
|
||||
}
|
||||
|
||||
static void sub_env_set_str(sub_env_t* env, const char* name, const char* val) {
|
||||
sub_var_t* var = sub_env_find(env, name);
|
||||
if (!var) {
|
||||
if (env->var_count < MAX_VARS) {
|
||||
var = &env->vars[env->var_count++];
|
||||
strncpy(var->name, name, MAX_VAR_NAME - 1);
|
||||
var->active = true;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
strncpy(var->str_val, val, MAX_STR_LEN - 1);
|
||||
var->is_string = true;
|
||||
}
|
||||
|
||||
static const char* skip_whitespace(const char* p) {
|
||||
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') p++;
|
||||
return p;
|
||||
}
|
||||
|
||||
// Simple arithmetic & variable evaluator
|
||||
static long sub_eval_expr(sub_env_t* env, const char* expr, const char** endptr) {
|
||||
expr = skip_whitespace(expr);
|
||||
long result = 0;
|
||||
|
||||
// Check if it's a number
|
||||
if ((*expr >= '0' && *expr <= '9') || *expr == '-') {
|
||||
char* end;
|
||||
result = strtol(expr, &end, 10);
|
||||
expr = end;
|
||||
} else if ((*expr >= 'a' && *expr <= 'z') || (*expr >= 'A' && *expr <= 'Z') || *expr == '_') {
|
||||
char var_name[MAX_VAR_NAME];
|
||||
int idx = 0;
|
||||
while (((*expr >= 'a' && *expr <= 'z') || (*expr >= 'A' && *expr <= 'Z') || (*expr >= '0' && *expr <= '9') || *expr == '_') && idx < MAX_VAR_NAME - 1) {
|
||||
var_name[idx++] = *expr++;
|
||||
}
|
||||
var_name[idx] = '\0';
|
||||
sub_var_t* v = sub_env_find(env, var_name);
|
||||
if (v && !v->is_string) {
|
||||
result = v->num_val;
|
||||
}
|
||||
}
|
||||
|
||||
expr = skip_whitespace(expr);
|
||||
while (*expr == '+' || *expr == '-' || *expr == '*' || *expr == '/' || *expr == '%') {
|
||||
char op = *expr++;
|
||||
expr = skip_whitespace(expr);
|
||||
long next_val = 0;
|
||||
if ((*expr >= '0' && *expr <= '9') || *expr == '-') {
|
||||
char* end;
|
||||
next_val = strtol(expr, &end, 10);
|
||||
expr = end;
|
||||
} else if ((*expr >= 'a' && *expr <= 'z') || (*expr >= 'A' && *expr <= 'Z') || *expr == '_') {
|
||||
char var_name[MAX_VAR_NAME];
|
||||
int idx = 0;
|
||||
while (((*expr >= 'a' && *expr <= 'z') || (*expr >= 'A' && *expr <= 'Z') || (*expr >= '0' && *expr <= '9') || *expr == '_') && idx < MAX_VAR_NAME - 1) {
|
||||
var_name[idx++] = *expr++;
|
||||
}
|
||||
var_name[idx] = '\0';
|
||||
sub_var_t* v = sub_env_find(env, var_name);
|
||||
if (v && !v->is_string) {
|
||||
next_val = v->num_val;
|
||||
}
|
||||
}
|
||||
|
||||
switch (op) {
|
||||
case '+': result += next_val; break;
|
||||
case '-': result -= next_val; break;
|
||||
case '*': result *= next_val; break;
|
||||
case '/': if (next_val != 0) result /= next_val; break;
|
||||
case '%': if (next_val != 0) result %= next_val; break;
|
||||
}
|
||||
expr = skip_whitespace(expr);
|
||||
}
|
||||
|
||||
if (endptr) *endptr = expr;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Executes a single line of SUB code
|
||||
static int sub_exec_line(sub_env_t* env, const char* line) {
|
||||
line = skip_whitespace(line);
|
||||
if (*line == '\0' || (line[0] == '/' && line[1] == '/')) {
|
||||
return 0; // Comment or empty line
|
||||
}
|
||||
|
||||
// 1. print(...) statement
|
||||
if (strncmp(line, "print(", 6) == 0) {
|
||||
const char* p = line + 6;
|
||||
p = skip_whitespace(p);
|
||||
if (*p == '"') {
|
||||
p++;
|
||||
char out_str[MAX_STR_LEN];
|
||||
int idx = 0;
|
||||
while (*p && *p != '"' && idx < MAX_STR_LEN - 1) {
|
||||
if (*p == '\\' && *(p + 1) == 'n') {
|
||||
out_str[idx++] = '\n';
|
||||
p += 2;
|
||||
} else {
|
||||
out_str[idx++] = *p++;
|
||||
}
|
||||
}
|
||||
out_str[idx] = '\0';
|
||||
printk("%s", out_str);
|
||||
if (*p == '"') p++;
|
||||
p = skip_whitespace(p);
|
||||
if (*p == ',') {
|
||||
p++;
|
||||
long val = sub_eval_expr(env, p, NULL);
|
||||
printk(" %ld", val);
|
||||
}
|
||||
printk("\n");
|
||||
} else {
|
||||
long val = sub_eval_expr(env, p, NULL);
|
||||
printk("%ld\n", val);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 2. var declaration: var name = expr / "str"
|
||||
if (strncmp(line, "var ", 4) == 0) {
|
||||
const char* p = line + 4;
|
||||
p = skip_whitespace(p);
|
||||
char var_name[MAX_VAR_NAME];
|
||||
int idx = 0;
|
||||
while (((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_') && idx < MAX_VAR_NAME - 1) {
|
||||
var_name[idx++] = *p++;
|
||||
}
|
||||
var_name[idx] = '\0';
|
||||
p = skip_whitespace(p);
|
||||
if (*p == '=') {
|
||||
p++;
|
||||
p = skip_whitespace(p);
|
||||
if (*p == '"') {
|
||||
p++;
|
||||
char str_val[MAX_STR_LEN];
|
||||
int sidx = 0;
|
||||
while (*p && *p != '"' && sidx < MAX_STR_LEN - 1) {
|
||||
str_val[sidx++] = *p++;
|
||||
}
|
||||
str_val[sidx] = '\0';
|
||||
sub_env_set_str(env, var_name, str_val);
|
||||
} else {
|
||||
long val = sub_eval_expr(env, p, NULL);
|
||||
sub_env_set_num(env, var_name, val);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 3. Assignment: name = expr
|
||||
const char* eq = strchr(line, '=');
|
||||
if (eq && eq != line) {
|
||||
char var_name[MAX_VAR_NAME];
|
||||
const char* p = line;
|
||||
int idx = 0;
|
||||
while (p < eq && *p != ' ' && idx < MAX_VAR_NAME - 1) {
|
||||
var_name[idx++] = *p++;
|
||||
}
|
||||
var_name[idx] = '\0';
|
||||
long val = sub_eval_expr(env, eq + 1, NULL);
|
||||
sub_env_set_num(env, var_name, val);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sub_vm_eval_string(const char* code) {
|
||||
if (!code) return -1;
|
||||
sub_env_init(&global_env);
|
||||
|
||||
char buffer[256];
|
||||
const char* p = code;
|
||||
while (*p) {
|
||||
int idx = 0;
|
||||
while (*p && *p != '\n' && *p != ';' && idx < 255) {
|
||||
buffer[idx++] = *p++;
|
||||
}
|
||||
buffer[idx] = '\0';
|
||||
if (*p == '\n' || *p == ';') p++;
|
||||
sub_exec_line(&global_env, buffer);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sub_vm_eval_file(const char* filepath) {
|
||||
if (!filepath) return -1;
|
||||
|
||||
int fd = vfs_open(filepath, 0);
|
||||
if (fd < 0) {
|
||||
printk(ANSI_RED "Error: cannot open SUB source file '%s'\n" ANSI_RESET, filepath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char* buffer = kmalloc(4096);
|
||||
if (!buffer) {
|
||||
vfs_close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int bytes = vfs_read(fd, buffer, 4095);
|
||||
vfs_close(fd);
|
||||
|
||||
if (bytes <= 0) {
|
||||
kfree(buffer);
|
||||
return 0;
|
||||
}
|
||||
buffer[bytes] = '\0';
|
||||
|
||||
int res = sub_vm_eval_string(buffer);
|
||||
kfree(buffer);
|
||||
return res;
|
||||
}
|
||||
Reference in new issue
Block a user