feat(preprocessor): date & time macro (#92)

This commit is contained in:
Edo 2023-03-29 14:45:50 +01:00 committed by GitHub
parent 68ff757f96
commit cb339d6ae3
3 changed files with 26 additions and 3 deletions

View File

@ -22,6 +22,8 @@ class preprocessor
std::set<std::string> reject_;
std::deque<token> tokens_;
std::vector<token> expr_;
std::string date_;
std::string time_;
usize curr_expr_;
u32 expand_;
u32 skip_;
@ -81,6 +83,8 @@ private:
auto eval_expr_factor() -> i32;
auto eval_expr_unary() -> i32;
auto eval_expr_primary() -> i32;
auto get_date_define(std::tm* time_p) -> void;
auto get_time_define(std::tm* time_p) -> void;
};
} // namespace xsk::gsc

View File

@ -6,6 +6,7 @@
#pragma once
#include <cstdio>
#include <ctime>
#include <algorithm>
#include <array>

View File

@ -38,6 +38,10 @@ preprocessor::preprocessor(context* ctx, std::string const& name, char const* da
directives_.insert({ "include", directive::INCLUDE });
directives_.insert({ "inline", directive::INLINE });
directives_.insert({ "using_animtree", directive::USINGTREE });
std::tm l_time = {};
get_date_define(&l_time);
get_time_define(&l_time);
}
auto preprocessor::process() -> token
@ -746,11 +750,11 @@ auto preprocessor::expand(token& tok, define& def) -> void
}
else if (tok.data == "__DATE__")
{
tokens_.push_front(token{ token::STRING, tok.space, tok.pos, tok.data }); // TODO!
tokens_.push_front(token{ token::STRING, tok.space, tok.pos, date_ });
}
else if (tok.data == "__TIME__")
{
tokens_.push_front(token{ token::STRING, tok.space, tok.pos, tok.data }); // TODO!
tokens_.push_front(token{ token::STRING, tok.space, tok.pos, time_ });
}
}
else if (def.type == define::OBJECT)
@ -1320,7 +1324,7 @@ auto preprocessor::eval_expr_primary() -> i32
if (eval_match(token::INT))
return static_cast<i32>(std::stoi(eval_prev().data));
if (eval_match(token::LPAREN))
if (eval_match(token::LPAREN))
{
auto val = eval_expr();
eval_consume(token::RPAREN, "expect ')' after expression.");
@ -1353,4 +1357,18 @@ auto preprocessor::eval_expr_primary() -> i32
throw ppr_error(eval_peek().pos, "invalid preprocessor expression");
}
auto preprocessor::get_date_define(std::tm* time_p) -> void
{
char buf[] = "??? ?? ????";
std::strftime(buf, sizeof(buf), "%b %d %Y", time_p);
date_ = std::string("\"").append(buf).append("\"");
}
auto preprocessor::get_time_define(std::tm* time_p) -> void
{
char buf[] = "??:??:??";
std::strftime(buf, sizeof(buf), "%T", time_p);
time_ = std::string("\"").append(buf).append("\"");
}
} // namespace xsk::gsc