Fabrique

A build language for complex systems

 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
literals.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2013 Jonathan Anderson
4  * All rights reserved.
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef LITERALS_H
33 #define LITERALS_H
34 
35 #include "AST/Expression.h"
36 
37 #include <string>
38 
39 namespace fabrique {
40 namespace ast {
41 
45 template<class T>
46 class Literal : public Expression
47 {
48 public:
49  const T& value() const { return value_; }
50  virtual std::string str() const = 0;
51 
52 protected:
53  Literal(const T& value, const Type& ty, const SourceRange& loc)
54  : Expression(ty, loc), value_(value)
55  {
56  }
57 
58 private:
59  const T value_;
60 };
61 
62 
64 class BoolLiteral : public Literal<bool>
65 {
66 public:
67  BoolLiteral(bool value, const Type& ty, const SourceRange& loc)
68  : Literal(value, ty, loc)
69  {
70  }
71 
72  std::string str() const;
73 
74  virtual dag::ValuePtr evaluate(EvalContext&) const override;
75  virtual void PrettyPrint(Bytestream&, size_t indent = 0) const override;
76  virtual void Accept(Visitor&) const;
77 };
78 
79 
81 class IntLiteral : public Literal<int>
82 {
83 public:
84  IntLiteral(int value, const Type& ty, const SourceRange& loc)
85  : Literal(value, ty, loc)
86  {
87  }
88 
89  std::string str() const;
90 
91  virtual dag::ValuePtr evaluate(EvalContext&) const override;
92  virtual void PrettyPrint(Bytestream&, size_t indent = 0) const override;
93  virtual void Accept(Visitor&) const;
94 };
95 
96 
98 class StringLiteral : public Literal<std::string>
99 {
100 public:
101  StringLiteral(const std::string& s, const Type& ty,
102  const SourceRange& loc)
103  : Literal(s, ty, loc)
104  {
105  }
106 
107  std::string str() const;
108 
109  virtual dag::ValuePtr evaluate(EvalContext&) const override;
110  virtual void PrettyPrint(Bytestream&, size_t indent = 0) const override;
111  virtual void Accept(Visitor&) const;
112 };
113 
114 } // namespace ast
115 } // namespace fabrique
116 
117 #endif
virtual void PrettyPrint(Bytestream &, size_t indent=0) const override
Print a human-readable representation to an output stream and return that output stream.
Definition: literals.cc:66
virtual void PrettyPrint(Bytestream &, size_t indent=0) const override
Print a human-readable representation to an output stream and return that output stream.
Definition: literals.cc:81
An expression whose value is literally expressed in the source file.
Definition: literals.h:46
A literal 'true' or 'false' value in code.
Definition: literals.h:64
A string value enclosed by single or double quotes.
Definition: literals.h:98
Base class for expressions that can be evaluated.
Definition: Expression.h:49
virtual void PrettyPrint(Bytestream &, size_t indent=0) const override
Print a human-readable representation to an output stream and return that output stream.
Definition: literals.cc:46
A ostream-like class that may support formatting.
Definition: Bytestream.h:43
An integer value in code.
Definition: literals.h:81
A range of characters in source code.
A context for evaluating AST Expression objects.
Definition: EvalContext.h:69
The name of a value, function, parameter or argument.
Definition: Type.h:53
Interface for visitors that walk the AST.
Definition: Visitor.h:49
Declaration of fabrique::ast::Expression.