?? Ogbe: System & Types

System utilities, type introspection, and CLI argument handling.

Use Case 1: CLI Argument Parser

// cli_parser.ifa - Parse command-line arguments

ise parse_args() {
    ayanmo args = Ogbe.args();
    ayanmo parsed = {
        "command": nil,
        "flags": {},
        "positional": []
    };
    
    ayanmo i = 0;
    nigba (i < Ogunda.len(args)) {
        ayanmo arg = Ogunda.get(args, i);
        
        ti (Ika.starts_with(arg, "--")) {
            // Long flag: --name=value or --flag
            ayanmo flag = Ika.substring(arg, 2, Ika.len(arg));
            ti (Ika.contains(flag, "=")) {
                ayanmo parts = Ika.split(flag, "=");
                parsed["flags"][Ogunda.first(parts)] = Ogunda.last(parts);
            } bib?k? {
                parsed["flags"][flag] = otito;
            }
        } bib?k? ti (Ika.starts_with(arg, "-")) {
            // Short flag: -v, -o value
            ayanmo flag = Ika.substring(arg, 1, Ika.len(arg));
            ti (i + 1 < Ogunda.len(args)) {
                ayanmo next = Ogunda.get(args, i + 1);
                ti (!Ika.starts_with(next, "-")) {
                    parsed["flags"][flag] = next;
                    ayanmo i = i + 1;
                } bib?k? {
                    parsed["flags"][flag] = otito;
                }
            } bib?k? {
                parsed["flags"][flag] = otito;
            }
        } bib?k? ti (parsed["command"] == nil) {
            parsed["command"] = arg;
        } bib?k? {
            parsed["positional"] = Ogunda.push(parsed["positional"], arg);
        }
        
        ayanmo i = i + 1;
    }
    
    padap? parsed;
}

// Usage: ifa run cli_parser.ifa build --target=wasm --verbose -o output.txt
ayanmo args = parse_args();
Irosu.fo("Command: " + args["command"]);
Irosu.fo("Flags: " + Ogbe.format_json(args["flags"]));
Irosu.fo("Positional: " + args["positional"]);

Use Case 2: Type-Safe Serialization

// serialize.ifa - Type-aware JSON handling

ise serialize(value) {
    ayanmo t = Ogbe.type(value);
    
    ti (t == "nil") {
        padap? "null";
    } bib?k? ti (t == "bool") {
        ti (value) { padap? "true"; }
        padap? "false";
    } bib?k? ti (t == "number") {
        padap? Ogbe.to_string(value);
    } bib?k? ti (t == "string") {
        padap? "\"" + Ika.replace(value, "\"", "\\\"") + "\"";
    } bib?k? ti (t == "list") {
        ayanmo items = [];
        fun item ninu value {
            ayanmo items = Ogunda.push(items, serialize(item));
        }
        padap? "[" + Ika.join(items, ",") + "]";
    } bib?k? ti (t == "map") {
        ayanmo items = [];
        fun key ninu Ogbe.keys(value) {
            ayanmo pair = "\"" + key + "\":" + serialize(value[key]);
            ayanmo items = Ogunda.push(items, pair);
        }
        padap? "{" + Ika.join(items, ",") + "}";
    }
    
    padap? "null";
}

ise deserialize(json, expected_type) {
    ayanmo value = Ogbe.parse_json(json);
    ayanmo actual_type = Ogbe.type(value);
    
    ti (expected_type != nil && actual_type != expected_type) {
        padap? Okanran.error("Expected " + expected_type + ", got " + actual_type);
    }
    
    padap? value;
}

// Usage
ayanmo data = {"name": "Ifá", "version": 1.2, "features": ["bilingual", "reactive"]};
ayanmo json = serialize(data);
Irosu.fo("Serialized: " + json);

ayanmo parsed = deserialize(json, "map");
Irosu.fo("Name: " + parsed["name"]);

Use Case 3: Environment Configuration

// config.ifa - Environment-based configuration

ise get_config() {
    ayanmo env = Ogbe.env("ENV");
    ti (env == nil) { ayanmo env = "development"; }
    
    ayanmo config = {
        "env": env,
        "debug": env == "development",
        "log_level": "info"
    };
    
    // Override from environment variables
    ayanmo db_url = Ogbe.env("DATABASE_URL");
    ti (db_url != nil) {
        config["database_url"] = db_url;
    } bib?k? {
        ti (env == "production") {
            config["database_url"] = "postgres://prod-db/app";
        } bib?k? {
            config["database_url"] = "postgres://localhost/app_dev";
        }
    }
    
    ayanmo port = Ogbe.env("PORT");
    ti (port != nil) {
        config["port"] = Ogbe.parse_int(port);
    } bib?k? {
        config["port"] = 3000;
    }
    
    ayanmo log_level = Ogbe.env("LOG_LEVEL");
    ti (log_level != nil) {
        config["log_level"] = log_level;
    }
    
    padap? config;
}

// Usage
ayanmo config = get_config();
Irosu.fo("Environment: " + config["env"]);
Irosu.fo("Debug mode: " + config["debug"]);
Irosu.fo("Database: " + config["database_url"]);
Irosu.fo("Port: " + config["port"]);

Use Case 4: Dynamic Type Validation

// validator.ifa - Runtime type checking

ise validate_schema(data, schema) {
    ayanmo errors = [];
    
    fun field key(schema) {
        ayanmo spec = schema[field];
        ayanmo value = data[field];
        
        // Check required
        ti (spec["required"] == otito && value == nil) {
            ayanmo errors = Ogunda.push(errors, field + " is required");
            tesiwaju;
        }
        
        ti (value == nil) { tesiwaju; }
        
        // Check type
        ayanmo actual_type = Ogbe.type(value);
        ti (spec["type"] != nil && actual_type != spec["type"]) {
            ayanmo errors = Ogunda.push(errors, 
                field + " must be " + spec["type"] + ", got " + actual_type);
        }
        
        // Check min/max for numbers
        ti (actual_type == "number") {
            ti (spec["min"] != nil && value < spec["min"]) {
                ayanmo errors = Ogunda.push(errors, field + " must be >= " + spec["min"]);
            }
            ti (spec["max"] != nil && value > spec["max"]) {
                ayanmo errors = Ogunda.push(errors, field + " must be <= " + spec["max"]);
            }
        }
        
        // Check pattern for strings
        ti (actual_type == "string" && spec["pattern"] != nil) {
            ti (!Ika.matches(value, spec["pattern"])) {
                ayanmo errors = Ogunda.push(errors, field + " doesn't match pattern");
            }
        }
    }
    
    padap? {"valid": Ogunda.len(errors) == 0, "errors": errors};
}

// Usage
ayanmo user_schema = {
    "name": {"type": "string", "required": otito},
    "age": {"type": "number", "required": otito, "min": 0, "max": 120},
    "email": {"type": "string", "pattern": ".*@.*\\..*"}
};

ayanmo user = {"name": "John", "age": 150, "email": "invalid"};
ayanmo result = validate_schema(user, user_schema);

ti (!result["valid"]) {
    Irosu.fo("Validation errors:");
    fun err ninu result["errors"] {
        Irosu.fo("  - " + err);
    }
}