13 TypeInfo typeType(std::shared_ptr<Context> globalCtx, Type t)
\r
16 result.isStruct = true;
\r
17 if (t.name == "int" || t.name == "float" || t.name == "double" ||
\r
18 t.name == "char" || t.name == "long" || t.name == "short" || t.name == "bool" ||
\r
21 result.isStruct = false;
\r
28 TypeInfo typeExpr(std::shared_ptr<Context> globalCtx, Expr e)
\r
34 case ExprType::Func:
\r
36 auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);
\r
38 throw "Unknown function";
\r
39 result = typeType(globalCtx, std::get<0>(*f).returnType);
\r
42 case ExprType::Method:
\r
44 TypeInfo tiCaller = typeExpr(globalCtx, *e._method.expr);
\r
45 if (!tiCaller.isStruct)
\r
46 throw "Calling method on non-struct";
\r
47 auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);
\r
49 throw "Calling method on unknown struct";
\r
50 auto m = findStructMethod(e._method.methodName, std::get<0>(*s));
\r
52 throw "Unknown method";
\r
53 result = typeType(globalCtx, m->t.returnType);
\r
57 result.isStruct = false;
\r
58 switch (e._lit.type)
\r
60 case LitType::Bool: result.type.name = "bool"; break;
\r
61 case LitType::Int: result.type.name = "int"; break;
\r
62 case LitType::Decimal: result.type.name = "double"; break;
\r
63 case LitType::String: result.type.name = "char"; result.type.modifiers.push_back({ TypeModifierType::Pointer, false, -1 }); break;
\r
66 case ExprType::Paren:
\r
67 result = typeExpr(globalCtx, *e._paren.expr);
\r
71 auto tiCaller = typeExpr(globalCtx, *e._dot.expr);
\r
72 if (!tiCaller.isStruct)
\r
73 throw "Accessing member of non-struct";
\r
74 auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);
\r
76 throw "Calling method on unknown struct";
\r
77 auto sm = findStructMember(e._dot.identifier, std::get<0>(*s));
\r
78 if (!sm.has_value())
\r
79 throw "Unknown struct member";
\r
80 result = typeType(globalCtx, sm->t.type);
\r
83 case ExprType::PrefixOp:
\r
84 result = typeExpr(globalCtx, *e._prefixOp.expr);
\r
86 case ExprType::PostfixOp:
\r
87 result = typeExpr(globalCtx, *e._postfixOp.expr);
\r
89 case ExprType::BinaryOp:
\r
90 result = typeExpr(globalCtx, *e._binaryOp.lexpr);
\r
92 case ExprType::TernaryOp:
\r
93 result = typeExpr(globalCtx, *e._ternaryOp.rexprTrue);
\r
95 case ExprType::Bracket:
\r
97 TypeInfo ti = typeExpr(globalCtx, *e._brackets.lexpr);
\r
98 if (!ti.type.modifiers.empty())
\r
101 result.type.modifiers.pop_back();
\r
105 throw "Indexing non-array";
\r
108 case ExprType::Identifier:
\r
110 auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);
\r
111 if (!v.has_value())
\r
112 throw "Unknown variable";
\r
113 result = typeType(globalCtx, std::get<0>(*v).type);
\r