From cb339d6ae312d887f03626d174e139c1d3077d74 Mon Sep 17 00:00:00 2001 From: Edo Date: Wed, 29 Mar 2023 14:45:50 +0100 Subject: [PATCH] feat(preprocessor): date & time macro (#92) --- include/xsk/gsc/preprocessor.hpp | 4 ++++ include/xsk/stdinc.hpp | 1 + src/gsc/preprocessor.cpp | 24 +++++++++++++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/xsk/gsc/preprocessor.hpp b/include/xsk/gsc/preprocessor.hpp index bb6daee4..63703242 100644 --- a/include/xsk/gsc/preprocessor.hpp +++ b/include/xsk/gsc/preprocessor.hpp @@ -22,6 +22,8 @@ class preprocessor std::set reject_; std::deque tokens_; std::vector 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 diff --git a/include/xsk/stdinc.hpp b/include/xsk/stdinc.hpp index ae4cdc87..797a1376 100644 --- a/include/xsk/stdinc.hpp +++ b/include/xsk/stdinc.hpp @@ -6,6 +6,7 @@ #pragma once #include +#include #include #include diff --git a/src/gsc/preprocessor.cpp b/src/gsc/preprocessor.cpp index da90b9d0..7cf91940 100644 --- a/src/gsc/preprocessor.cpp +++ b/src/gsc/preprocessor.cpp @@ -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(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