srctree

Robin Linden parent d9eb03b1 1f2bd5ab
js: Add support for ExpressionStatements

inlinesplit
js/ast.h added: 14, removed: 10, total 4
@@ -87,10 +87,6 @@ struct Identifier {
std::string name;
};
 
struct ExpressionStatement {
std::shared_ptr<Expression> expression;
};
 
// TODO(robinlinden): Support more operators.
enum class BinaryOperator {
Minus,
@@ -143,6 +139,10 @@ struct VariableDeclaration {
Kind kind{Kind::Var};
};
 
struct ExpressionStatement {
Expression expression;
};
 
struct ReturnStatement {
std::optional<Expression> argument;
};
 
js/ast_executor.h added: 14, removed: 10, total 4
@@ -7,7 +7,6 @@
 
#include "js/ast.h"
 
#include <exception>
#include <functional>
#include <map>
#include <string>
@@ -30,8 +29,7 @@ public:
Value operator()(Declaration const &v) { return std::visit(*this, v); }
Value operator()(Statement const &v) { return std::visit(*this, v); }
 
// TODO(robinlinden): Implement.
Value operator()(ExpressionStatement const &) { std::terminate(); }
Value operator()(ExpressionStatement const &v) { return execute(v.expression); }
 
Value operator()(BinaryExpression const &v) {
auto lhs = execute(*v.lhs);
 
js/ast_executor_test.cpp added: 14, removed: 10, total 4
@@ -127,5 +127,11 @@ int main() {
expect_eq(e.execute(call), Value{});
});
 
etest::test("expression statement", [] {
AstExecutor e;
expect_eq(e.execute(ExpressionStatement{StringLiteral{"hi"}}), Value{"hi"});
expect_eq(e.execute(ExpressionStatement{NumericLiteral{1213}}), Value{1213});
});
 
return etest::run_all_tests();
}