Major additions and improvements across the Bob language ecosystem: Core Language Features: - Add comprehensive dictionary support with full CRUD operations - Implement built-in functions: keys(), values(), has() for dictionaries - Add string multiplication operator (string * number) - Enhance error reporting with detailed context and call stacks - Add ternary operator support (condition ? true_expr : false_expr) - Implement do-while loops with break/continue support - Add array increment/decrement operators (++, --) - Add cross-type comparison operators with proper type coercion - Implement toInt() function for float-to-integer conversion - Add float array index auto-truncation (like JavaScript/Lua) Code Quality & Linter Fixes: - Remove all "using namespace std;" statements (best practice) - Add proper std:: prefixes throughout codebase - Fix const correctness in helper functions - Resolve class/struct declaration mismatches - Fix sign comparison warnings in array indexing - Remove unused lambda captures in built-in functions - Fix brace initialization warnings in parser Documentation & Tooling: - Significantly expand BOB_LANGUAGE_REFERENCE.md with new features - Update VS Code extension with enhanced syntax highlighting - Add comprehensive code snippets for new language features - Update version information and package metadata Test Suite: - Add extensive dictionary functionality tests - Add tests for new operators and built-in functions - Add comprehensive copy behavior tests (by value vs by reference) - Add performance and edge case testing Architecture Improvements: - Enhance Value system with proper move semantics - Improve memory management with shared_ptr for complex types - Add trampoline-based tail call optimization - Implement proper error context propagation This represents a major milestone in Bob language development with production-ready dictionary support, comprehensive testing, and significantly improved code quality.
339 lines
11 KiB
Plaintext
339 lines
11 KiB
Plaintext
// Pseudo-OOP Examples using Dictionaries
|
|
print("=== PSEUDO-OOP EXAMPLES ===");
|
|
|
|
// Example 1: Simple Person Object
|
|
print("\n--- Example 1: Person Object ---");
|
|
func createPerson(name, age) {
|
|
var person = {
|
|
"name": name,
|
|
"age": age
|
|
};
|
|
|
|
person["greet"] = func() {
|
|
return "Hello, I'm " + person["name"] + " and I'm " + person["age"] + " years old.";
|
|
};
|
|
|
|
person["haveBirthday"] = func() {
|
|
person["age"] = person["age"] + 1;
|
|
return person["name"] + " is now " + person["age"] + " years old!";
|
|
};
|
|
|
|
return person;
|
|
}
|
|
|
|
var alice = createPerson("Alice", 25);
|
|
var bob = createPerson("Bob", 30);
|
|
|
|
print(alice["greet"]());
|
|
print(bob["greet"]());
|
|
print(alice["haveBirthday"]());
|
|
print(alice["greet"]());
|
|
|
|
// Example 2: Bank Account with Encapsulation
|
|
print("\n--- Example 2: Bank Account ---");
|
|
func createBankAccount(accountNumber, initialBalance) {
|
|
var account = {
|
|
"accountNumber": accountNumber,
|
|
"balance": initialBalance,
|
|
"transactions": []
|
|
};
|
|
|
|
account["addTransaction"] = func(type, amount) {
|
|
push(account["transactions"], {
|
|
"type": type,
|
|
"amount": amount,
|
|
"balance": account["balance"],
|
|
"timestamp": time()
|
|
});
|
|
};
|
|
|
|
account["deposit"] = func(amount) {
|
|
if (amount > 0) {
|
|
account["balance"] = account["balance"] + amount;
|
|
account["addTransaction"]("deposit", amount);
|
|
return "Deposited " + amount + ". New balance: " + account["balance"];
|
|
} else {
|
|
return "Invalid deposit amount";
|
|
}
|
|
};
|
|
|
|
account["withdraw"] = func(amount) {
|
|
if (amount > 0 && amount <= account["balance"]) {
|
|
account["balance"] = account["balance"] - amount;
|
|
account["addTransaction"]("withdrawal", amount);
|
|
return "Withdrew " + amount + ". New balance: " + account["balance"];
|
|
} else {
|
|
return "Invalid withdrawal amount or insufficient funds";
|
|
}
|
|
};
|
|
|
|
account["getStatement"] = func() {
|
|
var statement = "Account: " + account["accountNumber"] + "\n";
|
|
statement = statement + "Balance: " + account["balance"] + "\n";
|
|
statement = statement + "Transactions:\n";
|
|
|
|
for (var i = 0; i < len(account["transactions"]); i++) {
|
|
var tx = account["transactions"][i];
|
|
statement = statement + " " + tx["type"] + ": " + tx["amount"] + " (balance: " + tx["balance"] + ")\n";
|
|
}
|
|
return statement;
|
|
};
|
|
|
|
return account;
|
|
}
|
|
|
|
var account1 = createBankAccount("12345", 1000);
|
|
print(account1["deposit"](500));
|
|
print(account1["withdraw"](200));
|
|
print(account1["withdraw"](50));
|
|
print(account1["getStatement"]());
|
|
|
|
// Example 3: Game Character with Inheritance-like Pattern
|
|
print("\n--- Example 3: Game Character System ---");
|
|
func createCharacter(name, health, attack) {
|
|
var character = {
|
|
"name": name,
|
|
"health": health,
|
|
"maxHealth": health,
|
|
"attack": attack,
|
|
"level": 1,
|
|
"experience": 0,
|
|
"inventory": []
|
|
};
|
|
|
|
character["takeDamage"] = func(damage) {
|
|
character["health"] = character["health"] - damage;
|
|
if (character["health"] < 0) {
|
|
character["health"] = 0;
|
|
}
|
|
return character["name"] + " took " + damage + " damage. Health: " + character["health"] + "/" + character["maxHealth"];
|
|
};
|
|
|
|
character["heal"] = func(amount) {
|
|
var oldHealth = character["health"];
|
|
character["health"] = character["health"] + amount;
|
|
if (character["health"] > character["maxHealth"]) {
|
|
character["health"] = character["maxHealth"];
|
|
}
|
|
var healed = character["health"] - oldHealth;
|
|
return character["name"] + " healed " + healed + " health. Health: " + character["health"] + "/" + character["maxHealth"];
|
|
};
|
|
|
|
character["gainExperience"] = func(exp) {
|
|
character["experience"] = character["experience"] + exp;
|
|
var levelUp = false;
|
|
while (character["experience"] >= character["level"] * 100) {
|
|
character["experience"] = character["experience"] - (character["level"] * 100);
|
|
character["level"] = character["level"] + 1;
|
|
character["maxHealth"] = character["maxHealth"] + 10;
|
|
character["health"] = character["maxHealth"]; // Full heal on level up
|
|
character["attack"] = character["attack"] + 2;
|
|
levelUp = true;
|
|
}
|
|
if (levelUp) {
|
|
return character["name"] + " leveled up to " + character["level"] + "!";
|
|
} else {
|
|
return character["name"] + " gained " + exp + " experience.";
|
|
}
|
|
};
|
|
|
|
character["addItem"] = func(item) {
|
|
push(character["inventory"], item);
|
|
return character["name"] + " picked up " + item["name"];
|
|
};
|
|
|
|
character["getStatus"] = func() {
|
|
return character["name"] + " (Lv." + character["level"] + ") - HP: " + character["health"] + "/" + character["maxHealth"] +
|
|
" ATK: " + character["attack"] + " EXP: " + character["experience"] + "/" + (character["level"] * 100);
|
|
};
|
|
|
|
return character;
|
|
}
|
|
|
|
func createWarrior(name) {
|
|
var warrior = createCharacter(name, 120, 15);
|
|
warrior["class"] = "Warrior";
|
|
warrior["specialAbility"] = func() {
|
|
return warrior["name"] + " uses Battle Cry! Attack increased!";
|
|
};
|
|
return warrior;
|
|
}
|
|
|
|
func createMage(name) {
|
|
var mage = createCharacter(name, 80, 8);
|
|
mage["class"] = "Mage";
|
|
mage["mana"] = 100;
|
|
mage["maxMana"] = 100;
|
|
mage["castSpell"] = func(spellName, manaCost) {
|
|
if (mage["mana"] >= manaCost) {
|
|
mage["mana"] = mage["mana"] - manaCost;
|
|
return mage["name"] + " casts " + spellName + "! Mana: " + mage["mana"] + "/" + mage["maxMana"];
|
|
} else {
|
|
return mage["name"] + " doesn't have enough mana!";
|
|
}
|
|
};
|
|
return mage;
|
|
}
|
|
|
|
var hero = createWarrior("Hero");
|
|
var wizard = createMage("Wizard");
|
|
|
|
print(hero["getStatus"]());
|
|
print(wizard["getStatus"]());
|
|
|
|
print(hero["takeDamage"](20));
|
|
print(hero["gainExperience"](150));
|
|
print(hero["getStatus"]());
|
|
|
|
print(wizard["castSpell"]("Fireball", 30));
|
|
print(wizard["castSpell"]("Lightning", 50));
|
|
print(wizard["getStatus"]());
|
|
|
|
// Example 4: Simple Game Engine
|
|
print("\n--- Example 4: Simple Game Engine ---");
|
|
func createGameEngine() {
|
|
var engine = {
|
|
"entities": [],
|
|
"time": 0
|
|
};
|
|
|
|
engine["addEntity"] = func(entity) {
|
|
push(engine["entities"], entity);
|
|
return "Added " + entity["name"] + " to the game";
|
|
};
|
|
|
|
engine["removeEntity"] = func(entityName) {
|
|
for (var i = 0; i < len(engine["entities"]); i++) {
|
|
if (engine["entities"][i]["name"] == entityName) {
|
|
// Remove by shifting elements (simple implementation)
|
|
for (var j = i; j < len(engine["entities"]) - 1; j++) {
|
|
engine["entities"][j] = engine["entities"][j + 1];
|
|
}
|
|
engine["entities"][len(engine["entities"]) - 1] = none;
|
|
return "Removed " + entityName + " from the game";
|
|
}
|
|
}
|
|
return "Entity " + entityName + " not found";
|
|
};
|
|
|
|
engine["update"] = func() {
|
|
engine["time"] = engine["time"] + 1;
|
|
var updates = [];
|
|
for (var i = 0; i < len(engine["entities"]); i++) {
|
|
var entity = engine["entities"][i];
|
|
if (has(entity, "update")) {
|
|
push(updates, entity["update"]());
|
|
}
|
|
}
|
|
return "Game updated. Time: " + engine["time"] + ". Updates: " + len(updates);
|
|
};
|
|
|
|
engine["getEntityCount"] = func() {
|
|
return len(engine["entities"]);
|
|
};
|
|
|
|
return engine;
|
|
}
|
|
|
|
func createNPC(name, behavior) {
|
|
var npc = {
|
|
"name": name,
|
|
"behavior": behavior,
|
|
"position": {"x": 0, "y": 0}
|
|
};
|
|
|
|
npc["update"] = func() {
|
|
if (npc["behavior"] == "wander") {
|
|
npc["position"]["x"] = npc["position"]["x"] + (random() * 2 - 1);
|
|
npc["position"]["y"] = npc["position"]["y"] + (random() * 2 - 1);
|
|
return npc["name"] + " wandered to (" + npc["position"]["x"] + ", " + npc["position"]["y"] + ")";
|
|
} else if (npc["behavior"] == "patrol") {
|
|
npc["position"]["x"] = npc["position"]["x"] + 1;
|
|
if (npc["position"]["x"] > 10) {
|
|
npc["position"]["x"] = 0;
|
|
}
|
|
return npc["name"] + " patrolled to (" + npc["position"]["x"] + ", " + npc["position"]["y"] + ")";
|
|
}
|
|
return npc["name"] + " did nothing";
|
|
};
|
|
|
|
return npc;
|
|
}
|
|
|
|
var game = createGameEngine();
|
|
var guard = createNPC("Guard", "patrol");
|
|
var merchant = createNPC("Merchant", "wander");
|
|
|
|
print(game["addEntity"](guard));
|
|
print(game["addEntity"](merchant));
|
|
print("Entity count: " + game["getEntityCount"]());
|
|
|
|
for (var i = 0; i < 3; i++) {
|
|
print(game["update"]());
|
|
}
|
|
|
|
// Example 5: Event System
|
|
print("\n--- Example 5: Event System ---");
|
|
func createEventSystem() {
|
|
var eventSystem = {
|
|
"listeners": {}
|
|
};
|
|
|
|
eventSystem["on"] = func(event, callback) {
|
|
if (!has(eventSystem["listeners"], event)) {
|
|
eventSystem["listeners"][event] = [];
|
|
}
|
|
push(eventSystem["listeners"][event], callback);
|
|
return "Added listener for " + event;
|
|
};
|
|
|
|
eventSystem["emit"] = func(event, data) {
|
|
if (has(eventSystem["listeners"], event)) {
|
|
var results = [];
|
|
for (var i = 0; i < len(eventSystem["listeners"][event]); i++) {
|
|
var result = eventSystem["listeners"][event][i](data);
|
|
push(results, result);
|
|
}
|
|
return "Emitted " + event + " to " + len(results) + " listeners";
|
|
} else {
|
|
return "No listeners for " + event;
|
|
}
|
|
};
|
|
|
|
eventSystem["removeListener"] = func(event, callback) {
|
|
if (has(eventSystem["listeners"], event)) {
|
|
for (var i = 0; i < len(eventSystem["listeners"][event]); i++) {
|
|
if (eventSystem["listeners"][event][i] == callback) {
|
|
// Remove by shifting elements
|
|
for (var j = i; j < len(eventSystem["listeners"][event]) - 1; j++) {
|
|
eventSystem["listeners"][event][j] = eventSystem["listeners"][event][j + 1];
|
|
}
|
|
eventSystem["listeners"][event][len(eventSystem["listeners"][event]) - 1] = none;
|
|
return "Removed listener for " + event;
|
|
}
|
|
}
|
|
}
|
|
return "Listener not found for " + event;
|
|
};
|
|
|
|
return eventSystem;
|
|
}
|
|
|
|
var events = createEventSystem();
|
|
|
|
func logEvent(data) {
|
|
return "Logger: " + data;
|
|
}
|
|
|
|
func alertEvent(data) {
|
|
return "Alert: " + data + " happened!";
|
|
}
|
|
|
|
events["on"]("user_login", logEvent);
|
|
events["on"]("user_login", alertEvent);
|
|
events["on"]("error", logEvent);
|
|
|
|
print(events["emit"]("user_login", "Alice logged in"));
|
|
print(events["emit"]("error", "Database connection failed"));
|
|
|
|
print("\n=== ALL PSEUDO-OOP EXAMPLES COMPLETED ==="); |