Purple  0.1
Standard Language Specification
number.h
Go to the documentation of this file.
1
8#ifndef NUMBER
9#define NUMBER
10
11#include <limits.h>
12
16typedef enum
17{
24
28static const int numberTypeByteSizes[] = {
29 1, 1, 2, 4, 8,
30};
31
35static const int numberTypeBitSizes[] = {1, 8, 16, 32, 64};
36
40static const unsigned long long int numberTypeMaxValues[] = {1, CHAR_MAX, SHRT_MAX, INT_MAX,
41 LONG_MAX};
42
46static const char* numberTypeNames[] = {"bool", "char", "short", "int", "long"};
47
51static const char* numberTypeFormatStrings[] = {"%d", "%d", "%d", "%d", "%ld"};
52
56typedef struct Number {
60 long long int value;
64
68#define NUMBER_BOOL(v) \
69 (Number) \
70 { \
71 .number_type = NT_INT1, .value = v \
72 }
73
77#define NUMBER_CHAR(v) \
78 (Number) \
79 { \
80 .number_type = NT_INT8, .value = v \
81 }
82
87#define NUMBER_SHORT(v) \
88 (Number) \
89 { \
90 .number_type = NT_INT16, .value = v \
91 }
92
96#define NUMBER_INT(v) \
97 (Number) \
98 { \
99 .number_type = NT_INT32, .value = v \
100 }
101
105#define NUMBER_LONG(v) \
106 (Number) \
107 { \
108 .number_type = NT_INT64, .value = v \
109 }
110
114#define NUMBER_FROM_TYPE_VAL(t, v) \
115 (Number) \
116 { \
117 .number_type = t, .value = v \
118 }
119
123#define NUMBERS_TYPEQUIV(A, B) \
124 (A.number_type == B.number_type && A.pointer_depth == B.pointer_depth)
125
127int number_to_token_type(Number number);
128NumberType max_numbertype_for_val(long long int value);
129
130#endif /* NUMBER */
NumberType token_type_to_number_type(int token_type)
Convert a TokenType to a NumberType.
Definition: number.c:20
struct Number Number
Container for various kinds of number data.
static const char * numberTypeFormatStrings[]
Format strings for each data type.
Definition: number.h:51
NumberType max_numbertype_for_val(long long int value)
Finds the maximum NumberType possible for a given value.
Definition: number.c:66
int number_to_token_type(Number number)
Definition: number.c:44
static const int numberTypeByteSizes[]
Size of each NumberType.
Definition: number.h:28
static const unsigned long long int numberTypeMaxValues[]
Max value of each NumberType.
Definition: number.h:40
static const char * numberTypeNames[]
Names of each NumberType.
Definition: number.h:46
static const int numberTypeBitSizes[]
Bit size of each NumberType.
Definition: number.h:35
NumberType
Types of numbers supported by Purple.
Definition: number.h:17
@ NT_INT16
Definition: number.h:20
@ NT_INT64
Definition: number.h:22
@ NT_INT1
Definition: number.h:18
@ NT_INT32
Definition: number.h:21
@ NT_INT8
Definition: number.h:19
Container for various kinds of number data.
Definition: number.h:56
long long int value
Definition: number.h:60
int pointer_depth
Definition: number.h:62
NumberType number_type
Definition: number.h:58