Nearly CC
An educational compiler skeleton
literal_value.h
Go to the documentation of this file.
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef LITERAL_VALUE_H
22 #define LITERAL_VALUE_H
23 
24 #include <cstdint>
25 #include "location.h"
26 
33 
35 enum class LiteralValueKind {
36  NONE,
37  INTEGER,
38  CHARACTER,
39  STRING,
40 };
41 
45 class LiteralValue {
46 private:
47  LiteralValueKind m_kind;
48  int64_t m_intval;
49  std::string m_strval; // this is also used for character literals
50 
51  // integer literals can be specified as being unsigned and/or long
52  bool m_is_unsigned;
53  bool m_is_long;
54 
55 public:
57  LiteralValue();
58 
63  LiteralValue(int64_t val, bool is_unsigned, bool is_long);
64 
67  LiteralValue(char c);
68 
71  LiteralValue(const std::string &s);
72 
75  LiteralValue(const LiteralValue &other);
76 
77  ~LiteralValue();
78 
81  LiteralValue &operator=(const LiteralValue &rhs);
82 
85  LiteralValueKind get_kind() const;
86 
89  int64_t get_int_value() const;
90 
93  char get_char_value() const;
94 
101  std::string get_str_value() const;
102 
106  std::string get_str_value_escaped() const;
107 
110  bool is_unsigned() const;
111 
114  bool is_long() const;
115 
116  // helper functions to create a LiteralValue from a token lexeme
117 
122  static LiteralValue from_char_literal(const std::string &lexeme, const Location &loc);
123 
128  static LiteralValue from_int_literal(const std::string &lexeme, const Location &loc);
129 
134  static LiteralValue from_str_literal(const std::string &lexeme, const Location &loc);
135 
136 private:
137  static std::string strip_quotes(const std::string &lexeme, char quote);
138 };
139 
140 #endif // LITERAL_VALUE_H
Helper class for representing literal values (integer, character, and string).
Definition: literal_value.h:45
static LiteralValue from_str_literal(const std::string &lexeme, const Location &loc)
Create a LiteralValue from the lexeme of a string literal token.
Definition: literal_value.cpp:197
LiteralValue()
Default constuctor. The LiteralValueKind is set to NONE.
Definition: literal_value.cpp:26
bool is_unsigned() const
Check whether an INTEGER value is unsigned.
Definition: literal_value.cpp:121
char get_char_value() const
Get the character value of an CHAR literal value.
Definition: literal_value.cpp:88
bool is_long() const
Check whether an INTEGER value is long.
Definition: literal_value.cpp:126
int64_t get_int_value() const
Get the integer value of an INTEGER literal value.
Definition: literal_value.cpp:83
std::string get_str_value() const
Get the string value of an STRING literal value.
Definition: literal_value.cpp:94
LiteralValueKind get_kind() const
Get the LiteralValueKind of this literal value.
Definition: literal_value.cpp:79
std::string get_str_value_escaped() const
Get the true value of a STRING literal value (as opposed to the contents of the original string liter...
Definition: literal_value.cpp:99
static LiteralValue from_char_literal(const std::string &lexeme, const Location &loc)
Create a LiteralValue from the lexeme of a character literal token.
Definition: literal_value.cpp:131
static LiteralValue from_int_literal(const std::string &lexeme, const Location &loc)
Create a LiteralValue from the lexeme of a integer literal token.
Definition: literal_value.cpp:161
LiteralValue & operator=(const LiteralValue &rhs)
Assignment operator.
Definition: literal_value.cpp:68
Definition: location.h:26
LiteralValueKind
Kinds of literal values.
Definition: literal_value.h:35