From 12232668d931b0f3593807d2096a2c2f26844b0b Mon Sep 17 00:00:00 2001 From: Martin Dobrev Date: Sun, 18 Nov 2018 20:32:58 +0200 Subject: Code generation improvements (#56) * Implement code generation for some unary expressions Implement debug code augmentation. Now part of expresions have source location. Extend Tokens with Column information. Refactor byte code generation. Add options for byte code generator. * Fix linux build * Little refactoring in byte code generation * Fix couple bugs in byte code generation and add some tests. * Fix some linux warnings. --- JSImpl/src/Parser.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) (limited to 'JSImpl/src/Parser.cpp') diff --git a/JSImpl/src/Parser.cpp b/JSImpl/src/Parser.cpp index 8c387c8..7c3c039 100644 --- a/JSImpl/src/Parser.cpp +++ b/JSImpl/src/Parser.cpp @@ -63,6 +63,14 @@ private: { unsigned Current; }; + + struct Location + { + unsigned Line; + unsigned Column; + }; + Location GetLocation() const { return { m_Tokens[m_Current].Line , m_Tokens[m_Current].Column }; } + InternalState Snapshot(); void Restore(const InternalState& state); @@ -649,6 +657,7 @@ ExpressionPtr Parser::ConditionalExpression() ExpressionPtr Parser::AssignmentExpression() { + auto location = GetLocation(); auto snapShot = m_Current; auto left = LeftSideExpression(); if (MatchOneOf({ TokenType::Equal, @@ -665,11 +674,21 @@ ExpressionPtr Parser::AssignmentExpression() { auto type = Prev().Type; auto right = AssignmentExpression(); - return IPLMakeSharePtr(left, right, type); + auto be = IPLMakeSharePtr(left, right, type); + if (be) + { + be->SetLocation(location.Line, location.Column); + } + return be; } // revert state; m_Current = snapShot; - return ConditionalExpression(); + auto ce = ConditionalExpression(); + if (ce) + { + ce->SetLocation(location.Line, location.Column); + } + return ce; } ExpressionPtr Parser::Expression() @@ -729,6 +748,7 @@ ExpressionPtr Parser::EmptyStatement() ExpressionPtr Parser::VariableDefinition() { auto VariableDeclaration = [&]() -> ExpressionPtr { + auto location = GetLocation(); if (Match(TokenType::Identifier)) { auto id = Prev().Lexeme; @@ -737,7 +757,9 @@ ExpressionPtr Parser::VariableDefinition() { ae = AssignmentExpression(); } - return IPLMakeSharePtr(id, ae); + auto vd = IPLMakeSharePtr(id, ae); + vd->SetLocation(location.Line, location.Column); + return vd; } return nullptr; }; @@ -790,17 +812,21 @@ ExpressionPtr Parser::Block() ExpressionPtr Parser::LabeledStatement() { + auto location = GetLocation(); if (Match(TokenType::Identifier)) { auto identifier = Prev().Lexeme; auto stament = Statement(); - return IPLMakeSharePtr<::LabeledStatement>(identifier, stament); + auto ls = IPLMakeSharePtr<::LabeledStatement>(identifier, stament); + ls->SetLocation(location.Line, location.Column); + return ls; } return nullptr; } ExpressionPtr Parser::IfStatementfull() { + auto location = GetLocation(); if (Match(TokenType::If)) { auto cond = ParenthesizedExpression(); @@ -810,7 +836,9 @@ ExpressionPtr Parser::IfStatementfull() { elseBody = Statement(); } - return IPLMakeSharePtr<::IfStatement>(cond, ifBody, elseBody); + auto is = IPLMakeSharePtr<::IfStatement>(cond, ifBody, elseBody); + is->SetLocation(location.Line, location.Column); + return is; } return nullptr; } @@ -868,18 +896,22 @@ ExpressionPtr Parser::DoStatement() ExpressionPtr Parser::WhileStatement() { + auto location = GetLocation(); if (Match(TokenType::While)) { auto cond = ParenthesizedExpression(); auto body = Statement(); auto isDoWhile = false; - return IPLMakeSharePtr<::WhileStatement>(cond, body, isDoWhile); + auto ws = IPLMakeSharePtr<::WhileStatement>(cond, body, isDoWhile); + ws->SetLocation(location.Line, location.Column); + return ws; } return nullptr; } ExpressionPtr Parser::ForStatement() { + auto location = GetLocation(); if (Match(TokenType::For)) { if (!Match(TokenType::LeftParen)) @@ -907,7 +939,9 @@ ExpressionPtr Parser::ForStatement() assert(false); } auto body = Statement(); - return IPLMakeSharePtr<::ForStatement>(initializer, cond, iteration, body); + auto fs = IPLMakeSharePtr<::ForStatement>(initializer, cond, iteration, body); + fs->SetLocation(location.Line, location.Column); + return fs; } return nullptr; } @@ -1047,7 +1081,7 @@ ExpressionPtr Parser::FunctionDefinition() } return nullptr; }; - + auto location = GetLocation(); if (Match(TokenType::Function)) { if (Match(TokenType::Identifier)) @@ -1058,7 +1092,9 @@ ExpressionPtr Parser::FunctionDefinition() { if (auto body = Body()) { - return IPLMakeSharePtr(name, identifiers, body); + auto fd = IPLMakeSharePtr(name, identifiers, body); + fd->SetLocation(location.Line, location.Column); + return fd; } else { -- cgit v1.3.1-23-gd82d