]> gitweb.ps.run Git - toc/blobdiff - src/repr.h
generic functions and structs
[toc] / src / repr.h
index c745ba17b3c3ae65d4f68a08b0a2a3cfe4f3f2f4..b30aecd1a8f15aa1fc9c827804ef6c01693856e8 100644 (file)
@@ -13,16 +13,22 @@ struct Variable;
 struct Body;\r
 struct Function;\r
 struct Struct;\r
 struct Body;\r
 struct Function;\r
 struct Struct;\r
+struct Namespace;\r
 struct Program;\r
 struct Program;\r
+\r
 struct FuncExpr;\r
 struct FuncExpr;\r
+struct MethodExpr;\r
 struct LitExpr;\r
 struct LitExpr;\r
-struct IdentifierExpr;\r
-struct BracketsExpr;\r
-struct UnaryOperatorExpr;\r
+struct ParenExpr;\r
+struct DotExpr;\r
+struct PrefixOperatorExpr;\r
+struct PostfixOperatorExpr;\r
 struct BinaryOperatorExpr;\r
 struct TernaryOperatorExpr;\r
 struct BinaryOperatorExpr;\r
 struct TernaryOperatorExpr;\r
-struct DotExpr;\r
+struct BracketsExpr;\r
+struct IdentifierExpr;\r
 struct Expr;\r
 struct Expr;\r
+\r
 struct IfStmt;\r
 struct SwitchStmt;\r
 struct ForStmt;\r
 struct IfStmt;\r
 struct SwitchStmt;\r
 struct ForStmt;\r
@@ -31,64 +37,118 @@ struct AssignStmt;
 struct ReturnStmt;\r
 struct Stmt;\r
 \r
 struct ReturnStmt;\r
 struct Stmt;\r
 \r
-enum class TypeModifierType {\r
+struct Context\r
+{\r
+  std::shared_ptr<Context> parent;\r
+  std::vector<Variable> variables;\r
+};\r
+\r
+enum class TypeModifierType\r
+{\r
   Pointer, Array\r
 };\r
 \r
   Pointer, Array\r
 };\r
 \r
-struct TypeModifier {\r
+struct TypeModifier\r
+{\r
   TypeModifierType type;\r
 \r
   TypeModifierType type;\r
 \r
+  bool _staticArray;\r
   int _arraySize;\r
 };\r
 \r
   int _arraySize;\r
 };\r
 \r
-struct Type {\r
+struct Type\r
+{\r
+  std::vector<std::string> namespacePrefixes;\r
   std::string name;\r
   std::vector<TypeModifier> modifiers;\r
   std::string name;\r
   std::vector<TypeModifier> modifiers;\r
+  std::vector<Type> genericInstantiation;\r
 };\r
 \r
 };\r
 \r
-struct Variable {\r
+struct Variable\r
+{\r
   std::string name;\r
   Type type;\r
 };\r
 \r
   std::string name;\r
   Type type;\r
 };\r
 \r
-struct Body {\r
-  std::vector<Variable> variables;\r
+struct Body\r
+{\r
+  std::shared_ptr<Context> ctx;\r
   std::vector<Stmt> statements;\r
 };\r
 \r
   std::vector<Stmt> statements;\r
 };\r
 \r
-struct Function {\r
-  Type returnType;\r
+struct Function\r
+{\r
   std::string name;\r
   std::string name;\r
+  std::vector<std::string> genericTypeNames;\r
+  std::vector<std::vector<Type>> genericInstantiations;\r
+  Type returnType;\r
   std::vector<Variable> parameters;\r
   std::vector<Variable> parameters;\r
+  bool defined;\r
   Body body;\r
 };\r
 \r
   Body body;\r
 };\r
 \r
-struct Struct {\r
+template<typename T>\r
+struct StructMember\r
+{\r
+  T t;\r
+  bool isPublic;\r
+  operator T() { return t; }\r
+};\r
+\r
+struct Struct\r
+{\r
   std::string name;\r
   std::string name;\r
-  std::vector<Variable> members;\r
-  std::vector<Function> methods;\r
+  std::vector<std::string> genericTypeNames;\r
+  std::vector<std::vector<Type>> genericInstantiations;\r
+  std::vector<StructMember<Variable>> members;\r
+  std::vector<StructMember<Function>> methods;\r
 };\r
 \r
 };\r
 \r
-struct Program {\r
-  std::vector<Variable> variables;\r
+struct Namespace\r
+{\r
+  std::string name;\r
+  std::shared_ptr<Context> ctx;\r
+  std::vector<Struct> structs;\r
+  std::vector<Function> functions;\r
+  std::vector<Namespace> namespaces;\r
+};\r
+\r
+struct Program\r
+{\r
+  std::shared_ptr<Context> ctx;\r
   std::vector<Struct> structs;\r
   std::vector<Function> functions;\r
   std::vector<Struct> structs;\r
   std::vector<Function> functions;\r
+  std::vector<Namespace> namespaces;\r
 };\r
 \r
 };\r
 \r
-enum class ExprType {\r
-  Func, Lit, Identifier, Brackets, UnaryOperator, BinaryOperator, TernaryOperator, Dot\r
+enum class ExprType\r
+{\r
+  Func, Method, Lit, Paren, Dot, PrefixOp, PostfixOp, BinaryOp, TernaryOp, Bracket, Identifier\r
 };\r
 \r
 };\r
 \r
-struct FuncExpr {\r
+struct FuncExpr\r
+{\r
+  std::vector<std::string> namespacePrefixes;\r
   std::string functionName;\r
   std::vector<Expr> arguments;\r
   std::string functionName;\r
   std::vector<Expr> arguments;\r
+  std::vector<Type> genericInstantiation;\r
+};\r
+\r
+struct MethodExpr\r
+{\r
+  std::shared_ptr<Expr> expr;\r
+  std::string methodName;\r
+  std::vector<Expr> arguments;\r
+  std::vector<Type> genericInstantiation;\r
 };\r
 \r
 };\r
 \r
-enum class LitType {\r
+enum class LitType\r
+{\r
   Int, Decimal, String, Bool\r
 };\r
 \r
   Int, Decimal, String, Bool\r
 };\r
 \r
-struct LitExpr {\r
+struct LitExpr\r
+{\r
   LitType type;\r
 \r
   int _int;\r
   LitType type;\r
 \r
   int _int;\r
@@ -97,116 +157,164 @@ struct LitExpr {
   bool _bool;\r
 };\r
 \r
   bool _bool;\r
 };\r
 \r
-struct IdentifierExpr {\r
-  std::string name;\r
+struct ParenExpr\r
+{\r
+  std::shared_ptr<Expr> expr;\r
 };\r
 \r
 };\r
 \r
-struct BracketsExpr {\r
-  std::shared_ptr<Expr> lexpr;\r
-  std::shared_ptr<Expr> rexpr;\r
+struct DotExpr\r
+{\r
+  bool isPointer;\r
+  std::shared_ptr<Expr> expr;\r
+  std::string identifier;\r
 };\r
 \r
 };\r
 \r
-enum class UnaryOperatorType {\r
-  Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost, LogicalNot, BitwiseNot, Dereference, AddressOf\r
+enum class PrefixOperatorType\r
+{\r
+  Plus, Minus, Increment, Decrement,\r
+  LogicalNot, BitwiseNot, Dereference, AddressOf,\r
+  COUNT\r
 };\r
 };\r
+static std::string PrefixOperatorTypeStrings[] =\r
+{\r
+  "+", "-", "++", "--", "!", "~", "*", "&" };\r
 \r
 \r
-enum class BinaryOperatorType {\r
+struct PrefixOperatorExpr\r
+{\r
+  PrefixOperatorType type;\r
+  std::shared_ptr<Expr> expr;\r
+};\r
+\r
+enum class PostfixOperatorType\r
+{\r
+  Increment, Decrement,\r
+  COUNT\r
+};\r
+static std::string PostfixOperatorTypeStrings[] =\r
+{\r
+  "++", "--" };\r
+\r
+struct PostfixOperatorExpr\r
+{\r
+  PostfixOperatorType type;\r
+  std::shared_ptr<Expr> expr;\r
+};\r
+\r
+enum class BinaryOperatorType\r
+{\r
   Plus, Minus, Multiply, Divide, Modulo, BitwiseAnd, BitwiseOr, BitwiseXor, LessThan, GreaterThan,\r
   LeftShift, RightShift, LogicalAnd, LogicalOr, Equals, NotEquals, LessThanEquals, GreaterThanEquals, BitwiseAndEquals, BitwiseOrEquals, BitwiseXorEquals,\r
   PlusEquals, MinusEquals, MultiplyEquals, DivideEquals, ModuloEquals,\r
   Plus, Minus, Multiply, Divide, Modulo, BitwiseAnd, BitwiseOr, BitwiseXor, LessThan, GreaterThan,\r
   LeftShift, RightShift, LogicalAnd, LogicalOr, Equals, NotEquals, LessThanEquals, GreaterThanEquals, BitwiseAndEquals, BitwiseOrEquals, BitwiseXorEquals,\r
   PlusEquals, MinusEquals, MultiplyEquals, DivideEquals, ModuloEquals,\r
-  LeftShiftEquals, RightShiftEquals\r
+  LeftShiftEquals, RightShiftEquals,\r
+  COUNT\r
 };\r
 };\r
-static std::string UnaryOperatorTypeStrings[] = {\r
-  "+", "-", "++", "--", "++", "--", "!", "~", "*", "&" };\r
-\r
-static std::string BinaryOperatorTypeStrings[] = {\r
+static std::string BinaryOperatorTypeStrings[] =\r
+{\r
   "+", "-", "*", "/", "%", "&", "|", "^", "<", ">",\r
   "<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=",\r
   "+=","-=","*=","/=","%=",\r
   "<<=",">>=" };\r
 \r
   "+", "-", "*", "/", "%", "&", "|", "^", "<", ">",\r
   "<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=",\r
   "+=","-=","*=","/=","%=",\r
   "<<=",">>=" };\r
 \r
-struct UnaryOperatorExpr {\r
-  UnaryOperatorType type;\r
-  std::shared_ptr<Expr> expr;\r
-};\r
-\r
-struct BinaryOperatorExpr {\r
+struct BinaryOperatorExpr\r
+{\r
   BinaryOperatorType type;\r
   std::shared_ptr<Expr> lexpr;\r
   std::shared_ptr<Expr> rexpr;\r
 };\r
 \r
   BinaryOperatorType type;\r
   std::shared_ptr<Expr> lexpr;\r
   std::shared_ptr<Expr> rexpr;\r
 };\r
 \r
-struct TernaryOperatorExpr {\r
+struct TernaryOperatorExpr\r
+{\r
   std::shared_ptr<Expr> lexpr;\r
   std::shared_ptr<Expr> rexprTrue;\r
   std::shared_ptr<Expr> rexprFalse;\r
 };\r
 \r
   std::shared_ptr<Expr> lexpr;\r
   std::shared_ptr<Expr> rexprTrue;\r
   std::shared_ptr<Expr> rexprFalse;\r
 };\r
 \r
-struct DotExpr {\r
+struct BracketsExpr\r
+{\r
   std::shared_ptr<Expr> lexpr;\r
   std::shared_ptr<Expr> lexpr;\r
-  IdentifierExpr ident;\r
+  std::shared_ptr<Expr> rexpr;\r
 };\r
 \r
 };\r
 \r
-struct Expr {\r
+struct IdentifierExpr\r
+{\r
+  std::vector<std::string> namespacePrefixes;\r
+  std::string identifier;\r
+};\r
+\r
+struct Expr\r
+{\r
   ExprType type;\r
 \r
   FuncExpr            _func;\r
   ExprType type;\r
 \r
   FuncExpr            _func;\r
+  MethodExpr          _method;\r
   LitExpr             _lit;\r
   LitExpr             _lit;\r
-  IdentifierExpr      _identifier;\r
-  BracketsExpr        _brackets;\r
-  UnaryOperatorExpr   _unaryOperator;\r
-  BinaryOperatorExpr  _binaryOperator;\r
-  TernaryOperatorExpr _ternaryOperator;\r
+  ParenExpr           _paren;\r
   DotExpr             _dot;\r
   DotExpr             _dot;\r
+  PrefixOperatorExpr  _prefixOp;\r
+  PostfixOperatorExpr _postfixOp;\r
+  BinaryOperatorExpr  _binaryOp;\r
+  TernaryOperatorExpr _ternaryOp;\r
+  BracketsExpr        _brackets;\r
+  IdentifierExpr      _identifier;\r
 };\r
 \r
 };\r
 \r
-enum class StmtType {\r
+enum class StmtType\r
+{\r
   If, Switch, For, While, Assign, Return, Expr\r
 };\r
 \r
   If, Switch, For, While, Assign, Return, Expr\r
 };\r
 \r
-struct ElseStmt {\r
+struct ElseStmt\r
+{\r
   bool _if;\r
   std::shared_ptr<Expr> expr;\r
   Body body;\r
 };\r
   bool _if;\r
   std::shared_ptr<Expr> expr;\r
   Body body;\r
 };\r
-struct IfStmt {\r
+struct IfStmt\r
+{\r
   Expr condition;\r
   Body body;\r
   std::vector<ElseStmt> elses;\r
 };\r
 \r
   Expr condition;\r
   Body body;\r
   std::vector<ElseStmt> elses;\r
 };\r
 \r
-struct SwitchCase {\r
+struct SwitchCase\r
+{\r
   std::shared_ptr<Expr> expr;\r
   Body body;\r
 };\r
 \r
   std::shared_ptr<Expr> expr;\r
   Body body;\r
 };\r
 \r
-struct SwitchStmt {\r
-  IdentifierExpr ident;\r
+struct SwitchStmt\r
+{\r
+  std::shared_ptr<Expr> ident;\r
   std::vector<SwitchCase> cases;\r
 };\r
 \r
   std::vector<SwitchCase> cases;\r
 };\r
 \r
-struct ForStmt {\r
-  AssignStmt assign;\r
+// TODO: int i = 0 (var decl)\r
+struct ForStmt\r
+{\r
+  std::shared_ptr<AssignStmt> init;\r
   std::shared_ptr<Expr> condition;\r
   std::shared_ptr<Expr> action;\r
   Body body;\r
 };\r
 \r
   std::shared_ptr<Expr> condition;\r
   std::shared_ptr<Expr> action;\r
   Body body;\r
 };\r
 \r
-struct WhileStmt {\r
+struct WhileStmt\r
+{\r
   Expr condition;\r
   Body body;\r
 };\r
 \r
   Expr condition;\r
   Body body;\r
 };\r
 \r
-struct AssignStmt {\r
-  Expr lexpr;\r
-  Expr rexpr;\r
+struct AssignStmt\r
+{\r
+  Expr lexpr, rexpr;\r
 };\r
 \r
 };\r
 \r
-struct ReturnStmt {\r
+struct ReturnStmt\r
+{\r
   Expr expr;\r
 };\r
 \r
   Expr expr;\r
 };\r
 \r
-struct Stmt {\r
+struct Stmt\r
+{\r
   StmtType type;\r
   \r
   IfStmt      _if;\r
   StmtType type;\r
   \r
   IfStmt      _if;\r