Skip to content

Commit

Permalink
Builder style!
Browse files Browse the repository at this point in the history
  • Loading branch information
liamnaddell committed Jul 20, 2024
1 parent 9491d70 commit 753b95b
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions gcc/rust/expand/rust-macro-builtins-utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

//TODO: Make sure all of these are needded
#include "rust-fmt.h"
#include <string>
#include "rust-expr.h"
#include "rust-ast-builder.h"
#include "rust-macro-builtins.h"
#include "rust-macro-builtins-helpers.h"

Expand Down Expand Up @@ -234,15 +237,10 @@ tl::optional<AST::Fragment>
MacroBuiltin::option_env_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
//TODO: Use ast/rust-ast-builder.h instead.
auto invoc_token_tree = invoc.get_delim_tok_tree ();
MacroInvocLexer lex (invoc_token_tree.to_token_stream ());
Parser<MacroInvocLexer> parser (lex);
//create copy of option
AST::PathExprSegment p_option ("Option",invoc_locus);
AST::PathExprSegment p_none ("None",invoc_locus);
AST::PathExprSegment p_some ("Some",invoc_locus);
std::unique_ptr<AST::PathInExpression> none (new AST::PathInExpression(std::vector({p_option,p_none}),std::vector<AST::Attribute> (), invoc_locus));
std::unique_ptr<AST::PathInExpression> some (new AST::PathInExpression(std::vector({p_option,p_some}),std::vector<AST::Attribute> (), invoc_locus));

auto last_token_id = macro_end_token (invoc_token_tree, parser);
std::unique_ptr<AST::LiteralExpr> lit_expr = nullptr;
Expand Down Expand Up @@ -281,28 +279,42 @@ MacroBuiltin::option_env_handler (location_t invoc_locus, AST::MacroInvocData &i
}
}

//TODO: Comments
//TODO: Pass tokens through
parser.skip_token (last_token_id);

auto env_value = getenv (lit_expr->as_string ().c_str ());
AST::Builder b(invoc_locus);
std::string option = "Option";

if (env_value == nullptr)
{
//TODO: Fix
//TODO: Fix token
auto tok = make_token (Token::make_string (invoc_locus, std::move ("DELETE ME")));
auto node = AST::SingleASTNode (std::move(none));
std::vector<AST::SingleASTNode> nodes({node});

std::string none = "None";
std::unique_ptr<AST::Expr> none_expr = std::unique_ptr<AST::Expr>(new AST::PathInExpression(b.path_in_expression(std::vector({option,none}))));

auto node = AST::SingleASTNode (std::move(none_expr));
std::vector<AST::SingleASTNode> nodes;
nodes.push_back(node);

return AST::Fragment (nodes, std::move (tok));
}
std::vector<std::unique_ptr<AST::Expr>> es;
es.push_back(make_string (invoc_locus, env_value));
std::unique_ptr<AST::CallExpr> e_some (new AST::CallExpr(std::move(some), std::move(es),std::vector<AST::Attribute> (), invoc_locus));
auto node = AST::SingleASTNode (std::move(e_some));
std::string some = "Some";
//TODO: Vector constructors
std::vector<std::unique_ptr<AST::Expr>> args;
args.push_back(b.literal_string(env_value));

//TODO: FIx
auto tok
= make_token (Token::make_string (invoc_locus, std::move (env_value)));
std::unique_ptr<AST::Expr> some_expr = b.call(std::unique_ptr<AST::Expr>(new AST::PathInExpression(b.path_in_expression(std::vector({option,some})))),std::move(args));

auto node = AST::SingleASTNode (std::move(some_expr));

//TODO: Fix
auto tok = make_token (Token::make_string (invoc_locus, std::move (env_value)));

std::vector<AST::SingleASTNode> nodes({node});
std::vector<AST::SingleASTNode> nodes;
nodes.push_back(node);

return AST::Fragment (nodes, std::move (tok));
}
Expand Down

0 comments on commit 753b95b

Please sign in to comment.