Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src / Parser.cpp
diff options
context:
space:
mode:
Commit12232668d931b0f3593807d2096a2c2f26844b0b (patch)
Author
 Martin Dobrev <martindobrev0@gmail.com>
2018-11-18 20:32:58 +0200
Committer
 GitHub <noreply@github.com>
2018-11-18 20:32:58 +0200
Fileseb05bfcde4de31a03ce033ed479db29c4b527bcc /JSImpl/src/Parser.cpp
IPL-12232668d931b0f3593807d2096a2c2f26844b0b.tar
IPL-12232668d931b0f3593807d2096a2c2f26844b0b.tar.gz
IPL-12232668d931b0f3593807d2096a2c2f26844b0b.zip
Parent5729bc0ea96fc0b944c243230910c537d692bcad (diff)
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.
1 files changed, 45 insertions, 9 deletions
-rw-r--r--JSImpl/src/Parser.cpp54
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<BinaryExpression>(left, right, type);
+ auto be = IPLMakeSharePtr<BinaryExpression>(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<VariableDefinitionExpression>(id, ae);
+ auto vd = IPLMakeSharePtr<VariableDefinitionExpression>(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<FunctionDeclaration>(name, identifiers, body);
+ auto fd = IPLMakeSharePtr<FunctionDeclaration>(name, identifiers, body);
+ fd->SetLocation(location.Line, location.Column);
+ return fd;
}
else
{