X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/71a20a4f3d4e5f5278f7d004af710af89dfd7ebc..c4231c6faf4e1b4650b075c641b0bb8c053739e4:/src/repr.h?ds=sidebyside diff --git a/src/repr.h b/src/repr.h index 47a78d7..959e74d 100644 --- a/src/repr.h +++ b/src/repr.h @@ -13,18 +13,22 @@ struct Variable; struct Body; struct Function; struct Struct; +struct Namespace; struct Program; + struct FuncExpr; +struct MethodExpr; struct LitExpr; -struct IdentifierExpr; -struct AccessExpr; -struct BracketsExpr; -struct UnaryOperatorExpr; +struct ParenExpr; +struct DotExpr; +struct PrefixOperatorExpr; +struct PostfixOperatorExpr; struct BinaryOperatorExpr; struct TernaryOperatorExpr; -struct DotExpr; -struct ParenExpr; +struct BracketsExpr; +struct IdentifierExpr; struct Expr; + struct IfStmt; struct SwitchStmt; struct ForStmt; @@ -33,6 +37,16 @@ struct AssignStmt; struct ReturnStmt; struct Stmt; +struct Context +{ + std::optional name; + std::shared_ptr parent; + std::vector variables; + std::vector functions; + std::vector structs; + std::vector namespaces; +}; + enum class TypeModifierType { Pointer, Array @@ -48,8 +62,25 @@ struct TypeModifier struct Type { + std::vector namespacePrefixes; std::string name; std::vector modifiers; + std::vector genericInstantiation; + + bool operator!=(const Type & that) + { + if (this->name != that.name) + return true; + + for (int i = 0; i < this->modifiers.size(); i++) + if (this->modifiers[i].type != that.modifiers[i].type) + return true; + + for (int i = 0; i < this->namespacePrefixes.size(); i++) + if (this->namespacePrefixes[i] != that.namespacePrefixes[i]) + return true; + return false; + } }; struct Variable @@ -60,41 +91,70 @@ struct Variable struct Body { - std::vector variables; + std::shared_ptr ctx; std::vector statements; }; struct Function { - Type returnType; std::string name; + Type returnType; std::vector parameters; + bool defined; + + std::vector genericTypeNames; + std::vector> genericInstantiations; + Body body; }; +template +struct StructMember +{ + T t; + bool isPublic; + operator T() { return t; } +}; + struct Struct { std::string name; - std::vector members; - std::vector methods; + std::vector genericTypeNames; + std::vector> genericInstantiations; + std::vector> members; + std::vector> methods; +}; + +struct Namespace +{ + std::string name; + std::shared_ptr ctx; }; struct Program { - std::vector variables; - std::vector structs; - std::vector functions; + std::shared_ptr ctx; }; enum class ExprType { - Func, Lit, Identifier, Brackets, UnaryOperator, BinaryOperator, TernaryOperator, Dot + Func, Method, Lit, Paren, Dot, PrefixOp, PostfixOp, BinaryOp, TernaryOp, Bracket, Identifier }; struct FuncExpr { + std::vector namespacePrefixes; std::string functionName; std::vector arguments; + std::vector genericInstantiation; +}; + +struct MethodExpr +{ + std::shared_ptr expr; + std::string methodName; + std::vector arguments; + std::vector genericInstantiation; }; enum class LitType @@ -112,24 +172,48 @@ struct LitExpr bool _bool; }; -// TODO: accessExpr -struct IdentifierExpr +struct ParenExpr { - std::string name; + std::shared_ptr expr; }; -struct BracketsExpr +struct DotExpr { - std::shared_ptr lexpr; - std::shared_ptr rexpr; + bool isPointer; + std::shared_ptr expr; + std::string identifier; }; -enum class UnaryOperatorType +enum class PrefixOperatorType { - Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost, + Plus, Minus, Increment, Decrement, LogicalNot, BitwiseNot, Dereference, AddressOf, COUNT }; +static std::string PrefixOperatorTypeStrings[] = +{ + "+", "-", "++", "--", "!", "~", "*", "&" }; + +struct PrefixOperatorExpr +{ + PrefixOperatorType type; + std::shared_ptr expr; +}; + +enum class PostfixOperatorType +{ + Increment, Decrement, + COUNT +}; +static std::string PostfixOperatorTypeStrings[] = +{ + "++", "--" }; + +struct PostfixOperatorExpr +{ + PostfixOperatorType type; + std::shared_ptr expr; +}; enum class BinaryOperatorType { @@ -139,10 +223,6 @@ enum class BinaryOperatorType LeftShiftEquals, RightShiftEquals, COUNT }; -static std::string UnaryOperatorTypeStrings[] = -{ - "+", "-", "++", "--", "++", "--", "!", "~", "*", "&" }; - static std::string BinaryOperatorTypeStrings[] = { "+", "-", "*", "/", "%", "&", "|", "^", "<", ">", @@ -150,12 +230,6 @@ static std::string BinaryOperatorTypeStrings[] = "+=","-=","*=","/=","%=", "<<=",">>=" }; -struct UnaryOperatorExpr -{ - UnaryOperatorType type; - std::shared_ptr expr; -}; - struct BinaryOperatorExpr { BinaryOperatorType type; @@ -170,27 +244,33 @@ struct TernaryOperatorExpr std::shared_ptr rexprFalse; }; -struct DotExpr +struct BracketsExpr { - std::shared_ptr expr; - IdentifierExpr ident; + std::shared_ptr lexpr; + std::shared_ptr rexpr; +}; + +struct IdentifierExpr +{ + std::vector namespacePrefixes; + std::string identifier; }; -// TODO: paren expr struct Expr { ExprType type; - bool parenthesized; - FuncExpr _func; + MethodExpr _method; LitExpr _lit; - IdentifierExpr _identifier; - BracketsExpr _brackets; - UnaryOperatorExpr _unaryOperator; - BinaryOperatorExpr _binaryOperator; - TernaryOperatorExpr _ternaryOperator; + ParenExpr _paren; DotExpr _dot; + PrefixOperatorExpr _prefixOp; + PostfixOperatorExpr _postfixOp; + BinaryOperatorExpr _binaryOp; + TernaryOperatorExpr _ternaryOp; + BracketsExpr _brackets; + IdentifierExpr _identifier; }; enum class StmtType @@ -219,15 +299,14 @@ struct SwitchCase struct SwitchStmt { - IdentifierExpr ident; + std::shared_ptr ident; std::vector cases; }; // TODO: int i = 0 (var decl) struct ForStmt { - std::string varName; - std::shared_ptr initValue; + std::shared_ptr init; std::shared_ptr condition; std::shared_ptr action; Body body; @@ -241,8 +320,7 @@ struct WhileStmt struct AssignStmt { - std::string name; - Expr expr; + Expr lexpr, rexpr; }; struct ReturnStmt