X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/b9322dfb8efe37f6f88a899269bdf21015f4db9a..HEAD:/src/typeInfo.h diff --git a/src/typeInfo.h b/src/typeInfo.h index cbe421c..7862ef5 100644 --- a/src/typeInfo.h +++ b/src/typeInfo.h @@ -10,8 +10,9 @@ struct TypeInfo #include "find.h" -TypeInfo typeType(const Program & p, Type t) +TypeInfo typeType(std::shared_ptr globalCtx, Type t) { + // used to differentiate basic types from user defined types TypeInfo result; result.isStruct = true; if (t.name == "int" || t.name == "float" || t.name == "double" || @@ -21,10 +22,11 @@ TypeInfo typeType(const Program & p, Type t) result.isStruct = false; } result.type = t; + return result; } -TypeInfo typeExpr(const Program & p, const std::vector & globalNamespace, std::shared_ptr globalCtx, Expr e) +TypeInfo typeExpr(std::shared_ptr globalCtx, Expr e) { TypeInfo result; @@ -32,26 +34,30 @@ TypeInfo typeExpr(const Program & p, const std::vector & globalName { case ExprType::Func: { - auto namespacePrefixes = globalNamespace; - namespacePrefixes.insert(namespacePrefixes.end(), - e._func.namespacePrefixes.begin(), - e._func.namespacePrefixes.end()); - auto f = findFunction(p, e._func.functionName, namespacePrefixes); + // get type info from return type + auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx); if (!f.has_value()) throw "Unknown function"; - result = typeType(p, f.value().returnType); + result = typeType(globalCtx, std::get<0>(*f).returnType); break; } case ExprType::Method: { - TypeInfo tiCaller = typeExpr(p, globalNamespace, globalCtx, *e._method.expr); - auto m = findStructMethod(p, e._method.methodName, tiCaller); + // get type info from return type + TypeInfo tiCaller = typeExpr(globalCtx, *e._method.expr); + if (!tiCaller.isStruct) + throw "Calling method on non-struct"; + auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx); + if (!s.has_value()) + throw "Calling method on unknown struct"; + auto m = findStructMethod(e._method.methodName, std::get<0>(*s)); if (!m.has_value()) throw "Unknown method"; - result = typeType(p, m.value().t.returnType); + result = typeType(globalCtx, m->t.returnType); break; } case ExprType::Lit: + // literal types are defined result.isStruct = false; switch (e._lit.type) { @@ -62,32 +68,41 @@ TypeInfo typeExpr(const Program & p, const std::vector & globalName } break; case ExprType::Paren: - result = typeExpr(p, globalNamespace, globalCtx, *e._paren.expr); + result = typeExpr(globalCtx, *e._paren.expr); break; case ExprType::Dot: { - auto sm = findStructMember(p, - typeExpr(p, globalNamespace, globalCtx, *e._dot.expr), e._dot.identifier); + // assume dot access is always member access + // and lookup struct variable + auto tiCaller = typeExpr(globalCtx, *e._dot.expr); + if (!tiCaller.isStruct) + throw "Accessing member of non-struct"; + auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx); + if (!s.has_value()) + throw "Calling method on unknown struct"; + auto sm = findStructMember(e._dot.identifier, std::get<0>(*s)); if (!sm.has_value()) throw "Unknown struct member"; - result = typeType(p, sm.value().t.type); + result = typeType(globalCtx, sm->t.type); break; } case ExprType::PrefixOp: - result = typeExpr(p, globalNamespace, globalCtx, *e._prefixOp.expr); + result = typeExpr(globalCtx, *e._prefixOp.expr); break; case ExprType::PostfixOp: - result = typeExpr(p, globalNamespace, globalCtx, *e._postfixOp.expr); + result = typeExpr(globalCtx, *e._postfixOp.expr); break; case ExprType::BinaryOp: - result = typeExpr(p, globalNamespace, globalCtx, *e._binaryOp.lexpr); + result = typeExpr(globalCtx, *e._binaryOp.lexpr); break; case ExprType::TernaryOp: - result = typeExpr(p, globalNamespace, globalCtx, *e._ternaryOp.rexprTrue); + result = typeExpr(globalCtx, *e._ternaryOp.rexprTrue); break; case ExprType::Bracket: { - TypeInfo ti = typeExpr(p, globalNamespace, globalCtx, *e._brackets.lexpr); + // get type of expr and remove array/ptr modifier to get + // type of [] access + TypeInfo ti = typeExpr(globalCtx, *e._brackets.lexpr); if (!ti.type.modifiers.empty()) { result = ti; @@ -100,14 +115,11 @@ TypeInfo typeExpr(const Program & p, const std::vector & globalName } case ExprType::Identifier: { - auto namespacePrefixes = globalNamespace; - namespacePrefixes.insert(namespacePrefixes.end(), - e._identifier.namespacePrefixes.begin(), - e._identifier.namespacePrefixes.end()); - auto v = findVariable(p, e._identifier.identifier, globalCtx); + // var lookup and return var type + auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx); if (!v.has_value()) throw "Unknown variable"; - result = typeType(p, v.value().type); + result = typeType(globalCtx, std::get<0>(*v).type); break; } }