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, const std::vector<std::string> & globalNamespace, std::shared_ptr<Context> globalCtx, Expr e)
\r
33 case ExprType::Func:
\r
35 auto namespacePrefixes = globalNamespace;
\r
36 namespacePrefixes.insert(namespacePrefixes.end(),
\r
37 e._func.namespacePrefixes.begin(),
\r
38 e._func.namespacePrefixes.end());
\r
39 auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);
\r
41 throw "Unknown function";
\r
42 result = typeType(p, f.value().returnType);
\r
45 case ExprType::Method:
\r
47 TypeInfo tiCaller = typeExpr(p, globalNamespace, globalCtx, *e._method.expr);
\r
48 if (!tiCaller.isStruct)
\r
49 throw "Calling method on non-struct";
\r
50 auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);
\r
52 throw "Calling method on unknown struct";
\r
53 auto m = findStructMethod(e._method.methodName, s.value());
\r
55 throw "Unknown method";
\r
56 result = typeType(p, m.value().t.returnType);
\r
60 result.isStruct = false;
\r
61 switch (e._lit.type)
\r
63 case LitType::Bool: result.type.name = "bool"; break;
\r
64 case LitType::Int: result.type.name = "int"; break;
\r
65 case LitType::Decimal: result.type.name = "double"; break;
\r
66 case LitType::String: result.type.name = "char"; result.type.modifiers.push_back({ TypeModifierType::Pointer, false, -1 }); break;
\r
69 case ExprType::Paren:
\r
70 result = typeExpr(p, globalNamespace, globalCtx, *e._paren.expr);
\r
74 auto tiCaller = typeExpr(p, globalNamespace, globalCtx, *e._dot.expr);
\r
75 if (!tiCaller.isStruct)
\r
76 throw "Accessing member of non-struct";
\r
77 auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);
\r
79 throw "Calling method on unknown struct";
\r
80 auto sm = findStructMember(e._dot.identifier, s.value());
\r
81 if (!sm.has_value())
\r
82 throw "Unknown struct member";
\r
83 result = typeType(p, sm.value().t.type);
\r
86 case ExprType::PrefixOp:
\r
87 result = typeExpr(p, globalNamespace, globalCtx, *e._prefixOp.expr);
\r
89 case ExprType::PostfixOp:
\r
90 result = typeExpr(p, globalNamespace, globalCtx, *e._postfixOp.expr);
\r
92 case ExprType::BinaryOp:
\r
93 result = typeExpr(p, globalNamespace, globalCtx, *e._binaryOp.lexpr);
\r
95 case ExprType::TernaryOp:
\r
96 result = typeExpr(p, globalNamespace, globalCtx, *e._ternaryOp.rexprTrue);
\r
98 case ExprType::Bracket:
\r
100 TypeInfo ti = typeExpr(p, globalNamespace, globalCtx, *e._brackets.lexpr);
\r
101 if (!ti.type.modifiers.empty())
\r
104 result.type.modifiers.pop_back();
\r
108 throw "Indexing non-array";
\r
111 case ExprType::Identifier:
\r
113 auto namespacePrefixes = globalNamespace;
\r
114 namespacePrefixes.insert(namespacePrefixes.end(),
\r
115 e._identifier.namespacePrefixes.begin(),
\r
116 e._identifier.namespacePrefixes.end());
\r
117 auto v = findVariable(e._identifier.identifier, namespacePrefixes, globalCtx);
\r
118 if (!v.has_value())
\r
119 throw "Unknown variable";
\r
120 result = typeType(p, v.value().type);
\r