Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
CPP builtins
Browse files Browse the repository at this point in the history
* __DATE__
* __TIME__
  • Loading branch information
m6w6 committed Dec 10, 2018
1 parent f9a22bf commit 195175b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
* real integral type of enums

* #pragma PSI lib "foo" instead of lib "foo";
* #line directive
42 changes: 41 additions & 1 deletion src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ static bool has_feature(struct psi_cpp *cpp, struct psi_token *target, struct ps
static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool BASE_FILE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool DATE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool FILE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool INCLUDE_LEVEL__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool LINE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool TIME__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool TIMESTAMP__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);

static inline struct psi_plist *builtin_sig(token_t typ, ...)
Expand Down Expand Up @@ -109,9 +111,11 @@ PHP_MINIT_FUNCTION(psi_builtin)

PSI_BUILTIN(BASE_FILE__, -1);
PSI_BUILTIN(COUNTER__, -1);
PSI_BUILTIN(DATE__, -1);
PSI_BUILTIN(FILE__, -1);
PSI_BUILTIN(INCLUDE_LEVEL__, -1);
PSI_BUILTIN(LINE__, -1);
PSI_BUILTIN(TIME__, -1);
PSI_BUILTIN(TIMESTAMP__, -1);

return SUCCESS;
Expand Down Expand Up @@ -214,7 +218,7 @@ static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target,
#define ADD_UNSIGNED_NUMBER(u) do { \
char buf[0x20]; \
unsigned u_ = u; \
size_t len = sprintf(buf, "%u", u_); \
size_t len = snprintf(buf, sizeof(buf), "%u", u_); \
struct psi_token *tok_ = NEW_TOKEN(PSI_T_NUMBER, buf, len); \
tok_->flags |= PSI_NUMBER_INT | PSI_NUMBER_U; \
ADD_TOKEN(tok_); \
Expand All @@ -234,6 +238,24 @@ static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target,
return true;
}

static bool DATE__(struct psi_cpp *cpp, struct psi_token *target,
struct psi_plist **args, struct psi_plist **res)
{
char buf[12];
struct tm *tp;
time_t t = time(NULL);
#if HAVE_LOCALTIME_R
struct tm tm = {0};
tp = localtime_r(&t, &tm);
#else
tp = localtime(&t);
#endif

strftime(buf, sizeof(buf), "%b %e %Y", tp);
ADD_QUOTED_STRING(buf, 11);
return true;
}

static bool FILE__(struct psi_cpp *cpp, struct psi_token *target,
struct psi_plist **args, struct psi_plist **res)
{
Expand All @@ -255,6 +277,24 @@ static bool LINE__(struct psi_cpp *cpp, struct psi_token *target,
return true;
}

static bool TIME__(struct psi_cpp *cpp, struct psi_token *target,
struct psi_plist **args, struct psi_plist **res)
{
char buf[9];
struct tm *tp;
time_t t = time(NULL);
#if HAVE_LOCALTIME_R
struct tm tm = {0};
tp = localtime_r(&t, &tm);
#else
tp = localtime(&t);
#endif

strftime(buf, sizeof(buf), "%H:%M:%S", tp);
ADD_QUOTED_STRING(buf, 8);
return true;
}

static bool TIMESTAMP__(struct psi_cpp *cpp, struct psi_token *target,
struct psi_plist **args, struct psi_plist **res)
{
Expand Down
4 changes: 3 additions & 1 deletion tests/parser/cpp004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LC_TIME=C
===TEST===
<?php
var_dump(CPP_TEST0, CPP_TEST1, CPP_TEST2, CPP_TEST3, CPP_TEST4, CPP_TEST5);
var_dump(base_file, include_level, timestamp, file, line);
var_dump(base_file, include_level, timestamp, file, line, date, time);
?>
===DONE===
--EXPECTF--
Expand All @@ -28,4 +28,6 @@ int(2)
string(24) "%s %s %w%d %d:%d:%d %d"
string(54) "/%s/tests/parser/cpp004/include_level.h"
int(3)
string(11) "%s %d %d"
string(8) "%d:%d:%d"
===DONE===
3 changes: 3 additions & 0 deletions tests/parser/cpp004/builtins.psi
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ const \CPP_TEST5 = c(500);

#include "base_file.h" // includes include_level.h
#include "timestamp.h"

const \date = __DATE__;
const \time = __TIME__;

0 comments on commit 195175b

Please sign in to comment.