mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:11:12 +01:00
Express prelude using Carbon syntax (#955)
This should be clearer, more maintainable, and more scalable than building the prelude AST by hand.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
|
||||
#include <map>
|
||||
#include <optional>
|
||||
|
||||
#include "executable_semantics/common/arena.h"
|
||||
@@ -17,6 +18,19 @@ namespace Carbon {
|
||||
using llvm::cast;
|
||||
using llvm::isa;
|
||||
|
||||
auto IntrinsicExpression::FindIntrinsic(std::string_view name,
|
||||
SourceLocation source_loc)
|
||||
-> Intrinsic {
|
||||
static const auto& intrinsic_map =
|
||||
*new std::map<std::string_view, Intrinsic>({{"print", Intrinsic::Print}});
|
||||
name.remove_prefix(std::strlen("__intrinsic_"));
|
||||
auto it = intrinsic_map.find(name);
|
||||
if (it == intrinsic_map.end()) {
|
||||
FATAL_COMPILATION_ERROR(source_loc) << "Unknown intrinsic '" << name << "'";
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
auto ExpressionFromParenContents(
|
||||
Nonnull<Arena*> arena, SourceLocation source_loc,
|
||||
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
|
||||
|
||||
@@ -404,19 +404,29 @@ class IntrinsicExpression : public Expression {
|
||||
Print,
|
||||
};
|
||||
|
||||
explicit IntrinsicExpression(Intrinsic intrinsic)
|
||||
: AstNode(AstNodeKind::IntrinsicExpression,
|
||||
SourceLocation("<intrinsic>", 0)),
|
||||
intrinsic_(intrinsic) {}
|
||||
explicit IntrinsicExpression(std::string_view intrinsic_name,
|
||||
Nonnull<TupleLiteral*> args,
|
||||
SourceLocation source_loc)
|
||||
: AstNode(AstNodeKind::IntrinsicExpression, source_loc),
|
||||
intrinsic_(FindIntrinsic(intrinsic_name, source_loc)),
|
||||
args_(args) {}
|
||||
|
||||
static auto classof(const AstNode* node) -> bool {
|
||||
return InheritsFromIntrinsicExpression(node->kind());
|
||||
}
|
||||
|
||||
auto intrinsic() const -> Intrinsic { return intrinsic_; }
|
||||
auto args() const -> const TupleLiteral& { return *args_; }
|
||||
auto args() -> TupleLiteral& { return *args_; }
|
||||
|
||||
private:
|
||||
// Returns the enumerator corresponding to the intrinsic named `name`,
|
||||
// or raises a fatal compile error if there is no such enumerator.
|
||||
static auto FindIntrinsic(std::string_view name, SourceLocation source_loc)
|
||||
-> Intrinsic;
|
||||
|
||||
Intrinsic intrinsic_;
|
||||
Nonnull<TupleLiteral*> args_;
|
||||
};
|
||||
|
||||
// Converts paren_contents to an Expression, interpreting the parentheses as
|
||||
|
||||
Reference in New Issue
Block a user