13 TypeInfo typeType(const Program & p, 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
27 TypeInfo typeExpr(const Program & p, std::shared_ptr<Context> globalCtx, Expr e)
\r
33 case ExprType::Func:
\r
35 auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);
\r
37 throw "Unknown function";
\r
38 result = typeType(p, std::get<0>(*f).returnType);
\r
41 case ExprType::Method:
\r
43 TypeInfo tiCaller = typeExpr(p, globalCtx, *e._method.expr);
\r
44 if (!tiCaller.isStruct)
\r
45 throw "Calling method on non-struct";
\r
46 auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);
\r
48 throw "Calling method on unknown struct";
\r
49 auto m = findStructMethod(e._method.methodName, std::get<0>(*s));
\r
51 throw "Unknown method";
\r
52 result = typeType(p, m->t.returnType);
\r
56 result.isStruct = false;
\r
57 switch (e._lit.type)
\r
59 case LitType::Bool: result.type.name = "bool"; break;
\r
60 case LitType::Int: result.type.name = "int"; break;
\r
61 case LitType::Decimal: result.type.name = "double"; break;
\r
62 case LitType::String: result.type.name = "char"; result.type.modifiers.push_back({ TypeModifierType::Pointer, false, -1 }); break;
\r
65 case ExprType::Paren:
\r
66 result = typeExpr(p, globalCtx, *e._paren.expr);
\r
70 auto tiCaller = typeExpr(p, globalCtx, *e._dot.expr);
\r
71 if (!tiCaller.isStruct)
\r
72 throw "Accessing member of non-struct";
\r
73 auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);
\r
75 throw "Calling method on unknown struct";
\r
76 auto sm = findStructMember(e._dot.identifier, std::get<0>(*s));
\r
77 if (!sm.has_value())
\r
78 throw "Unknown struct member";
\r
79 result = typeType(p, sm->t.type);
\r
82 case ExprType::PrefixOp:
\r
83 result = typeExpr(p, globalCtx, *e._prefixOp.expr);
\r
85 case ExprType::PostfixOp:
\r
86 result = typeExpr(p, globalCtx, *e._postfixOp.expr);
\r
88 case ExprType::BinaryOp:
\r
89 result = typeExpr(p, globalCtx, *e._binaryOp.lexpr);
\r
91 case ExprType::TernaryOp:
\r
92 result = typeExpr(p, globalCtx, *e._ternaryOp.rexprTrue);
\r
94 case ExprType::Bracket:
\r
96 TypeInfo ti = typeExpr(p, globalCtx, *e._brackets.lexpr);
\r
97 if (!ti.type.modifiers.empty())
\r
100 result.type.modifiers.pop_back();
\r
104 throw "Indexing non-array";
\r
107 case ExprType::Identifier:
\r
109 auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);
\r
110 if (!v.has_value())
\r
111 throw "Unknown variable";
\r
112 result = typeType(p, std::get<0>(*v).type);
\r