Bob/headers/Parser.h
Bobby Lucero eacb86ec77 feat: comprehensive language enhancements and code quality improvements
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.
2025-08-07 00:12:04 -04:00

101 lines
2.9 KiB
C++

#pragma once
#include <utility>
#include <vector>
#include "Lexer.h"
#include "Expression.h"
#include "Statement.h"
#include "TypeWrapper.h"
#include "helperFunctions/ShortHands.h"
#include "ErrorReporter.h"
class Parser
{
private:
const std::vector<Token> tokens;
int current = 0;
int functionDepth = 0; // Track nesting level of functions
ErrorReporter* errorReporter = nullptr;
public:
explicit Parser(std::vector<Token> tokens) : tokens(std::move(tokens)){};
std::vector<sptr(Stmt)> parse();
void setErrorReporter(ErrorReporter* reporter) { errorReporter = reporter; }
private:
sptr(Expr) expression();
sptr(Expr) logical_or();
sptr(Expr) ternary();
sptr(Expr) logical_and();
sptr(Expr) bitwise_or();
sptr(Expr) bitwise_xor();
sptr(Expr) bitwise_and();
sptr(Expr) shift();
sptr(Expr) equality();
sptr(Expr) comparison();
sptr(Expr) term();
sptr(Expr) factor();
sptr(Expr) unary();
sptr(Expr) primary();
bool match(const std::vector<TokenType>& types);
bool check(TokenType type);
bool isAtEnd();
Token advance();
Token peek();
Token previous();
Token consume(TokenType type, const std::string& message);
sptr(Stmt) statement();
void sync();
std::shared_ptr<Stmt> expressionStatement();
std::shared_ptr<Stmt> returnStatement();
std::shared_ptr<Stmt> ifStatement();
std::shared_ptr<Stmt> whileStatement();
std::shared_ptr<Stmt> doWhileStatement();
std::shared_ptr<Stmt> forStatement();
std::shared_ptr<Stmt> breakStatement();
std::shared_ptr<Stmt> continueStatement();
std::shared_ptr<Stmt> declaration();
std::shared_ptr<Stmt> varDeclaration();
std::shared_ptr<Stmt> functionDeclaration();
std::shared_ptr<Expr> functionExpression();
std::shared_ptr<Stmt> assignmentStatement();
sptr(Expr) assignment();
sptr(Expr) assignmentExpression(); // For for loop increment clauses
sptr(Expr) increment(); // Parse increment/decrement expressions
sptr(Expr) postfix(); // Parse postfix operators
std::vector<std::shared_ptr<Stmt>> block();
sptr(Expr) finishCall(sptr(Expr) callee);
sptr(Expr) finishArrayIndex(sptr(Expr) array);
sptr(Expr) finishArrayAssign(sptr(Expr) array, sptr(Expr) index, sptr(Expr) value);
sptr(Expr) finishDictIndex(sptr(Expr) dict);
sptr(Expr) finishDictAssign(sptr(Expr) dict, sptr(Expr) key, sptr(Expr) value);
sptr(Expr) arrayLiteral();
sptr(Expr) dictLiteral();
sptr(Expr) call(); // Handle call chains (function calls, array indexing, and dict indexing)
// Helper methods for function scope tracking
void enterFunction() { functionDepth++; }
void exitFunction() { functionDepth--; }
bool isInFunction() const { return functionDepth > 0; }
// Helper method for tail call detection
bool isTailCall(const std::shared_ptr<Expr>& expr);
};