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.
61 lines
1.8 KiB
C++
61 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <unordered_map>
|
|
#include <string>
|
|
#include <memory>
|
|
#include "Value.h"
|
|
#include "Lexer.h"
|
|
|
|
// Forward declaration
|
|
class ErrorReporter;
|
|
|
|
struct Environment {
|
|
public:
|
|
Environment() : parent(nullptr), errorReporter(nullptr) {}
|
|
Environment(std::shared_ptr<Environment> parent_env) : parent(parent_env), errorReporter(nullptr) {}
|
|
|
|
// Copy constructor for closure snapshots - creates a deep copy of the environment chain
|
|
Environment(const Environment& other) : parent(nullptr), errorReporter(other.errorReporter) {
|
|
// Copy all variables normally - arrays will be handled by forceCleanup
|
|
variables = other.variables;
|
|
|
|
// Create a deep copy of the parent environment chain
|
|
if (other.parent) {
|
|
parent = std::make_shared<Environment>(*other.parent);
|
|
}
|
|
}
|
|
|
|
// Set error reporter for enhanced error reporting
|
|
void setErrorReporter(ErrorReporter* reporter) {
|
|
errorReporter = reporter;
|
|
}
|
|
|
|
// Optimized define with inline
|
|
inline void define(const std::string& name, const Value& value) {
|
|
variables[name] = value;
|
|
}
|
|
|
|
// Enhanced assign with error reporting
|
|
void assign(const Token& name, const Value& value);
|
|
|
|
// Enhanced get with error reporting
|
|
Value get(const Token& name);
|
|
|
|
// Get by string name with error reporting
|
|
Value get(const std::string& name);
|
|
|
|
std::shared_ptr<Environment> getParent() const { return parent; }
|
|
inline void clear() { variables.clear(); }
|
|
|
|
// Set parent environment for TCO environment reuse
|
|
inline void setParent(std::shared_ptr<Environment> newParent) {
|
|
parent = newParent;
|
|
}
|
|
|
|
private:
|
|
std::unordered_map<std::string, Value> variables;
|
|
std::shared_ptr<Environment> parent;
|
|
ErrorReporter* errorReporter;
|
|
};
|
|
|