From 16231050058131860f710073c17d3bca7c6637eb Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sun, 18 Oct 2026 06:05:53 +0000 Subject: feat(operators): support global binary operator overloads Resolve exact operand types and preserve declared operator return types. Reject implicit STRING to BOOLEAN conversion at typed expression boundaries. Co-Authored-By: Claude Opus 5.5 --- include/cps/CodeGen.h | 5 +++++ include/cps/FunctionAST.h | 3 +++ include/cps/Lexer.h | 1 + include/cps/Operator.h | 28 ++++++++++++++++++++++++++++ include/cps/Parser.h | 1 + 5 files changed, 38 insertions(+) create mode 100644 include/cps/Operator.h (limited to 'include/cps') diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h index d81a82c..30e6d06 100644 --- a/include/cps/CodeGen.h +++ b/include/cps/CodeGen.h @@ -155,6 +155,11 @@ class CodeGen { const std::vector> &ArgExprs, int Line); const FuncSig *resolveCall(const std::string &Name, const std::vector> &Args); + struct OperatorResolution { + const FuncSig *Signature = nullptr; + bool Ambiguous = false; + }; + OperatorResolution resolveOperator(int Op, const TypeInfo *Left, const TypeInfo *Right = nullptr); llvm::Value *emitVariadicArgument(ExprAST *Expr, const std::string &Callee, size_t Position); bool marshalCallArgs(const FuncSig &Signature, const std::vector> &ArgExprs, diff --git a/include/cps/FunctionAST.h b/include/cps/FunctionAST.h index 716f004..90ef5c4 100644 --- a/include/cps/FunctionAST.h +++ b/include/cps/FunctionAST.h @@ -34,6 +34,7 @@ class PrototypeAST { std::string OwnerClass; bool IsPublic; bool IsVariadic = false; + int Operator = 0; public: PrototypeAST(const std::string &Name, @@ -47,6 +48,8 @@ public: const std::vector &getParams() const { return Params; } const std::string &getReturnType() const { return ReturnType; } bool isOverload() const { return IsOverload; } + int getOperator() const { return Operator; } + void setOperator(int Op) { Operator = Op; } bool isExternal() const { return IsExternal; } bool isVariadic() const { return IsVariadic; } void setVariadic(bool V) { IsVariadic = V; } diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h index e2f5fdc..449ff8d 100644 --- a/include/cps/Lexer.h +++ b/include/cps/Lexer.h @@ -67,6 +67,7 @@ enum Token { tok_colon = -300, // : + tok_operator = -398, tok_overload = -399, tok_function = -400, tok_endfunction = -401, diff --git a/include/cps/Operator.h b/include/cps/Operator.h new file mode 100644 index 0000000..7cbb5d9 --- /dev/null +++ b/include/cps/Operator.h @@ -0,0 +1,28 @@ +#pragma once +#include "cps/Lexer.h" +#include + +namespace cps { + +inline std::string operatorSpelling(int Op) { + switch (Op) { + case '+': case '-': case '*': case '/': case '&': case '<': case '>': + return std::string(1, static_cast(Op)); + case tok_div: return "DIV"; + case tok_mod: return "MOD"; + case tok_eq: return "="; + case tok_ne: return "<>"; + case tok_le: return "<="; + case tok_ge: return ">="; + case tok_and: return "AND"; + case tok_or: return "OR"; + case tok_not: return "NOT"; + default: return {}; + } +} + +inline std::string operatorName(int Op) { + return "OPERATOR" + operatorSpelling(Op); +} + +} // namespace cps diff --git a/include/cps/Parser.h b/include/cps/Parser.h index f877ae6..ad80876 100644 --- a/include/cps/Parser.h +++ b/include/cps/Parser.h @@ -43,6 +43,7 @@ class Parser { std::unique_ptr ParseNumberExpr(); std::unique_ptr ParseIdentifierExpr(); + std::string ParseOperatorName(int &Op); std::unique_ptr ParseParenExpr(); std::unique_ptr ParseMemberCall( const std::string &Name, std::vector Accesses, int Line); -- cgit v1.3.1-4-g156e