Skip to content

Commit

Permalink
#18 TypeFlags are not really required
Browse files Browse the repository at this point in the history
  • Loading branch information
albeva committed Mar 4, 2022
1 parent 76b8c60 commit 5ce1e70
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 26 deletions.
18 changes: 3 additions & 15 deletions src/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "Lexer/Token.hpp"
#include "Lexer/TokenSource.hpp"
#include "ParseResult.hpp"
#include "Type/Type.hpp"
#include "Symbol/SymbolTable.hpp"
#include "Type/Type.hpp"
using namespace lbc;

Parser::Parser(Context& context, TokenSource& source, bool isMain, SymbolTable* symbolTable)
Expand All @@ -27,7 +27,6 @@ Parser::Parser(Context& context, TokenSource& source, bool isMain, SymbolTable*
void Parser::reset() noexcept {
m_scope = Scope::Root;
m_exprFlags = {};
m_typeFlags = {};
m_endLoc = {};
m_token = {};
advance();
Expand Down Expand Up @@ -982,10 +981,7 @@ ParseResult<AstContinuationStmt> Parser::kwExit() {
* | TypeOf
* .
*/
ParseResult<AstTypeExpr> Parser::typeExpr(TypeFlags flags) {
RESTORE_ON_EXIT(m_typeFlags);
m_typeFlags = flags;

ParseResult<AstTypeExpr> Parser::typeExpr() {
auto start = m_token.range().Start;
bool parenthesized = accept(TokenKind::ParenOpen);
bool mustBePtr = false;
Expand All @@ -1001,7 +997,7 @@ ParseResult<AstTypeExpr> Parser::typeExpr(TypeFlags flags) {
TRY_ASSIGN(expr, kwTypeOf())
} else {
TRY_DECLARE(ident, identifier())
if (m_typeFlags.consultSymbolTable && m_symbolTable != nullptr) {
if (m_symbolTable != nullptr) {
auto* symbol = m_symbolTable->find(ident->name);
if (symbol == nullptr || !symbol->getFlags().type) {
return ParseResult<AstTypeExpr>::error();
Expand Down Expand Up @@ -1038,13 +1034,6 @@ ParseResult<AstTypeOf> Parser::kwTypeOf() {
auto start = m_token.range().Start;
advance();

if (m_typeFlags.evaluateTypeOf) {
TRY(consume(TokenKind::ParenOpen))
TRY_DECLARE(type, typeExpr(m_typeFlags))
TRY(consume(TokenKind::ParenClose))
return m_context.create<AstTypeOf>(llvm::SMRange{ start, m_endLoc }, type);
}

TRY(consume(TokenKind::ParenOpen))
std::vector<Token> tokens;
int parens = 1;
Expand Down Expand Up @@ -1395,4 +1384,3 @@ void Parser::advance() {
m_endLoc = m_token.range().End;
m_source.next(m_token);
}

8 changes: 1 addition & 7 deletions src/Parser/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,12 @@ class Parser final {
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ CallWithoutParens)
};

struct TypeFlags {
bool evaluateTypeOf: 1;
bool consultSymbolTable: 1;
};

Parser(Context& context, TokenSource& source, bool isMain, SymbolTable* symbolTable = nullptr);
~Parser() noexcept = default;

[[nodiscard]] ParseResult<AstModule> parse();
[[nodiscard]] ParseResult<AstExpr> expression(ExprFlags flags = ExprFlags::None);
[[nodiscard]] ParseResult<AstTypeExpr> typeExpr(TypeFlags flags = {});
[[nodiscard]] ParseResult<AstTypeExpr> typeExpr();

void reset() noexcept;

Expand Down Expand Up @@ -137,7 +132,6 @@ class Parser final {
Token m_token{};
llvm::SMLoc m_endLoc{};
ExprFlags m_exprFlags{};
TypeFlags m_typeFlags{};
};

} // namespace lbc
7 changes: 3 additions & 4 deletions src/Sem/SemanticAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include "Driver/Context.hpp"
#include "Lexer/Token.hpp"
#include "Lexer/TokenProvider.hpp"
#include "Parser/Parser.hpp"
#include "Passes/ForStmtPass.hpp"
#include "Passes/ForwardDeclPass.hpp"
#include "Symbol/Symbol.hpp"
#include "Symbol/SymbolTable.hpp"
#include "Type/Type.hpp"
#include "Type/TypeProxy.hpp"
#include "Type/TypeUdt.hpp"
#include "Parser/Parser.hpp"

using namespace lbc;

Expand Down Expand Up @@ -238,13 +238,12 @@ void SemanticAnalyzer::visit(AstTypeOf& ast) {
TokenProvider provider{ m_astRootModule->fileId, *tokens };
Parser parser{ m_context, provider, /* isMain */ false, getSymbolTable() };

if (auto* type = parser.typeExpr({.evaluateTypeOf = true, .consultSymbolTable = true}).getPointer()) {
if (auto* type = parser.typeExpr().getPointer()) {
ast.typeExpr = type;
} else {
provider.reset();
parser.reset();
auto result = parser.expression();
if (auto* expr = result.getPointer()) {
if (auto* expr = parser.expression().getPointer()) {
ast.typeExpr = expr;
}
}
Expand Down

0 comments on commit 5ce1e70

Please sign in to comment.