]> gitweb.ps.run Git - toc/blobdiff - src/typeInfo.h
comments
[toc] / src / typeInfo.h
index 10b4097ffe486594dd9cb44e8bf0c80f7bf6e93e..7862ef53282bbd556ebaa4c881d198ce6fc88ee9 100644 (file)
@@ -10,8 +10,9 @@ struct TypeInfo
 \r
 #include "find.h"\r
 \r
-TypeInfo typeType(const Program & p, Type t)\r
+TypeInfo typeType(std::shared_ptr<Context> globalCtx, Type t)\r
 {\r
+  // used to differentiate basic types from user defined types\r
   TypeInfo result;\r
   result.isStruct = true;\r
   if (t.name == "int" || t.name == "float" || t.name == "double" ||\r
@@ -21,10 +22,11 @@ TypeInfo typeType(const Program & p, Type t)
     result.isStruct = false;\r
   }\r
   result.type = t;\r
+\r
   return result;\r
 }\r
 \r
-TypeInfo typeExpr(const Program & p, std::shared_ptr<Context> globalCtx, Expr e)\r
+TypeInfo typeExpr(std::shared_ptr<Context> globalCtx, Expr e)\r
 {\r
   TypeInfo result;\r
 \r
@@ -32,15 +34,17 @@ TypeInfo typeExpr(const Program & p, std::shared_ptr<Context> globalCtx, Expr e)
   {\r
   case ExprType::Func:\r
   {\r
+    // get type info from return type\r
     auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);\r
     if (!f.has_value())\r
       throw "Unknown function";\r
-    result = typeType(p, std::get<0>(*f).returnType);\r
+    result = typeType(globalCtx, std::get<0>(*f).returnType);\r
     break;\r
   }\r
   case ExprType::Method:\r
   {\r
-    TypeInfo tiCaller = typeExpr(p, globalCtx, *e._method.expr);\r
+    // get type info from return type\r
+    TypeInfo tiCaller = typeExpr(globalCtx, *e._method.expr);\r
     if (!tiCaller.isStruct)\r
       throw "Calling method on non-struct";\r
     auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);\r
@@ -49,10 +53,11 @@ TypeInfo typeExpr(const Program & p, std::shared_ptr<Context> globalCtx, Expr e)
     auto m = findStructMethod(e._method.methodName, std::get<0>(*s));\r
     if (!m.has_value())\r
       throw "Unknown method";\r
-    result = typeType(p, m->t.returnType);\r
+    result = typeType(globalCtx, m->t.returnType);\r
     break;\r
   }\r
   case ExprType::Lit:\r
+    // literal types are defined\r
     result.isStruct = false;\r
     switch (e._lit.type)\r
     {\r
@@ -63,11 +68,13 @@ TypeInfo typeExpr(const Program & p, std::shared_ptr<Context> globalCtx, Expr e)
     }\r
     break;\r
   case ExprType::Paren:\r
-    result = typeExpr(p, globalCtx, *e._paren.expr);\r
+    result = typeExpr(globalCtx, *e._paren.expr);\r
     break;\r
   case ExprType::Dot:\r
   {\r
-    auto tiCaller = typeExpr(p, globalCtx, *e._dot.expr);\r
+    // assume dot access is always member access\r
+    // and lookup struct variable\r
+    auto tiCaller = typeExpr(globalCtx, *e._dot.expr);\r
     if (!tiCaller.isStruct)\r
       throw "Accessing member of non-struct";\r
     auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);\r
@@ -76,24 +83,26 @@ TypeInfo typeExpr(const Program & p, std::shared_ptr<Context> globalCtx, Expr e)
     auto sm = findStructMember(e._dot.identifier, std::get<0>(*s));\r
     if (!sm.has_value())\r
       throw "Unknown struct member";\r
-    result = typeType(p, sm->t.type);\r
+    result = typeType(globalCtx, sm->t.type);\r
     break;\r
   }\r
   case ExprType::PrefixOp:\r
-    result = typeExpr(p, globalCtx, *e._prefixOp.expr);\r
+    result = typeExpr(globalCtx, *e._prefixOp.expr);\r
     break;\r
   case ExprType::PostfixOp:\r
-    result = typeExpr(p, globalCtx, *e._postfixOp.expr);\r
+    result = typeExpr(globalCtx, *e._postfixOp.expr);\r
     break;\r
   case ExprType::BinaryOp:\r
-    result = typeExpr(p, globalCtx, *e._binaryOp.lexpr);\r
+    result = typeExpr(globalCtx, *e._binaryOp.lexpr);\r
     break;\r
   case ExprType::TernaryOp:\r
-    result = typeExpr(p, globalCtx, *e._ternaryOp.rexprTrue);\r
+    result = typeExpr(globalCtx, *e._ternaryOp.rexprTrue);\r
     break;\r
   case ExprType::Bracket:\r
   {\r
-    TypeInfo ti = typeExpr(p, globalCtx, *e._brackets.lexpr);\r
+    // get type of expr and remove array/ptr modifier to get\r
+    // type of [] access\r
+    TypeInfo ti = typeExpr(globalCtx, *e._brackets.lexpr);\r
     if (!ti.type.modifiers.empty())\r
     {\r
       result = ti;\r
@@ -106,10 +115,11 @@ TypeInfo typeExpr(const Program & p, std::shared_ptr<Context> globalCtx, Expr e)
   }\r
   case ExprType::Identifier:\r
   {\r
+    // var lookup and return var type\r
     auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);\r
     if (!v.has_value())\r
       throw "Unknown variable";\r
-    result = typeType(p, std::get<0>(*v).type);\r
+    result = typeType(globalCtx, std::get<0>(*v).type);\r
     break;\r
   }\r
   }\r