Skip to content

Commit 631ca0b

Browse files
committed
Handle sizeof, alignof
1 parent 6cfe724 commit 631ca0b

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

README.md.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Universal printf
22

3-
*uprintf* is a header-only library for printing anything in C.
3+
*uprintf* is a header-only library for printing any type in C.
44

55
## Examples
66

@@ -51,12 +51,12 @@ Examples and their outputs can be found in [examples](examples), and can be buil
5151

5252
1. [Download](https://raw.githubusercontent.com/spevnev/uprintf/refs/heads/main/uprintf.h) the library, or clone the repo
5353

54-
2. Define `UPRINTF_IMPLEMENTATION` in *one* of the files:
54+
2. Define `UPRINTF_IMPLEMENTATION` in one of the files:
5555
```c
5656
#define UPRINTF_IMPLEMENTATION
5757
#include "uprintf.h"
5858
```
59-
The implementation must be included in only one of the files. \
59+
The implementation must be included in **only one** of the files. \
6060
A rarely modified file should be used because it increases the build time. \
6161
[Options](#options) can be defined before the implementation.
6262

@@ -68,7 +68,7 @@ Examples and their outputs can be found in [examples](examples), and can be buil
6868

6969
### Options
7070

71-
Options must be defined *before the implementation*:
71+
Options must be defined **before** the implementation:
7272

7373
```c
7474
#define OPTION_NAME VALUE

uprintf.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,8 @@ typedef enum {
533533
_UPF_TOK_TYPE_QUALIFIER,
534534
_UPF_TOK_ATOMIC,
535535
_UPF_TOK_GENERIC,
536+
_UPF_TOK_SIZEOF,
537+
_UPF_TOK_ALIGNOF,
536538
_UPF_TOK_OPEN_PAREN,
537539
_UPF_TOK_CLOSE_PAREN,
538540
_UPF_TOK_OPEN_BRACKET,
@@ -2356,6 +2358,9 @@ static void _upf_tokenize(const char *string) {
23562358
{_UPF_TOK_TYPE_QUALIFIER, "restrict"},
23572359
{_UPF_TOK_ATOMIC, "_Atomic"},
23582360
{_UPF_TOK_GENERIC, "_Generic"},
2361+
{_UPF_TOK_SIZEOF, "sizeof"},
2362+
{_UPF_TOK_ALIGNOF, "_Alignof"},
2363+
{_UPF_TOK_ALIGNOF, "alignof"},
23592364
};
23602365

23612366
const char *ch = string;
@@ -2722,6 +2727,22 @@ static _upf_type *_upf_identifier(void) {
27222727

27232728
static _upf_type *_upf_generic(void) { _UPF_ERROR("Generics are not supported at %s:%d.", _upf_state.file_path, _upf_state.line); }
27242729

2730+
static _upf_type *_upf_sizeof(void) {
2731+
_upf_consume_token();
2732+
if (_upf_match_token(_UPF_TOK_OPEN_PAREN)) {
2733+
_upf_consume_parens(_UPF_TOK_OPEN_PAREN, _UPF_TOK_CLOSE_PAREN);
2734+
} else {
2735+
_upf_parse(_UPF_PREC_UNARY);
2736+
}
2737+
return _upf_get_number_type();
2738+
}
2739+
2740+
static _upf_type *_upf_alignof(void) {
2741+
_upf_consume_token();
2742+
_upf_consume_parens(_UPF_TOK_OPEN_PAREN, _UPF_TOK_CLOSE_PAREN);
2743+
return _upf_get_number_type();
2744+
}
2745+
27252746
static _upf_type *_upf_unary(void) {
27262747
_upf_token unop = _upf_consume_token();
27272748
_upf_type *type = _upf_parse(_UPF_PREC_UNARY);
@@ -2849,6 +2870,8 @@ static const _upf_parse_rule _upf_parse_rules[_UPF_TOK_COUNT] = {
28492870
[_UPF_TOK_STRING] = { _upf_string, NULL, _UPF_PREC_NONE },
28502871
[_UPF_TOK_IDENTIFIER] = { _upf_identifier, NULL, _UPF_PREC_NONE },
28512872
[_UPF_TOK_GENERIC] = { _upf_generic, NULL, _UPF_PREC_NONE },
2873+
[_UPF_TOK_SIZEOF] = { _upf_sizeof, NULL, _UPF_PREC_NONE },
2874+
[_UPF_TOK_ALIGNOF] = { _upf_alignof, NULL, _UPF_PREC_NONE },
28522875
[_UPF_TOK_UNARY] = { _upf_unary, NULL, _UPF_PREC_NONE },
28532876
[_UPF_TOK_OPEN_PAREN] = { _upf_paren, _upf_call, _UPF_PREC_POSTFIX },
28542877
[_UPF_TOK_OPEN_BRACKET] = { NULL, _upf_index, _UPF_PREC_POSTFIX },

0 commit comments

Comments
 (0)