Fabrique

A build language for complex systems

 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
literals.cc
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 #include "AST/literals.h"
33 #include "AST/Visitor.h"
34 #include "DAG/Primitive.h"
35 #include "Support/Bytestream.h"
36 
37 using namespace fabrique;
38 using namespace fabrique::ast;
39 
40 
41 std::string BoolLiteral::str() const
42 {
43  return (value() ? "true" : "false");
44 }
45 
46 void BoolLiteral::PrettyPrint(Bytestream& out, size_t /*indent*/) const
47 {
48  out
49  << Bytestream::Literal << str()
50  << Bytestream::Reset;
51 }
52 
53 void BoolLiteral::Accept(Visitor& v) const { v.Enter(*this); v.Leave(*this); }
54 
55 dag::ValuePtr BoolLiteral::evaluate(EvalContext&) const
56 {
57  return dag::ValuePtr(new dag::Boolean(value(), type(), source()));
58 }
59 
60 
61 std::string IntLiteral::str() const
62 {
63  return std::to_string(value());
64 }
65 
66 void IntLiteral::PrettyPrint(Bytestream& out, size_t /*indent*/) const
67 {
68  out << Bytestream::Literal << value() << Bytestream::Reset;
69 }
70 
71 void IntLiteral::Accept(Visitor& v) const { v.Enter(*this); v.Leave(*this); }
72 
73 dag::ValuePtr IntLiteral::evaluate(EvalContext&) const
74 {
75  return dag::ValuePtr(new dag::Integer(value(), type(), source()));
76 }
77 
78 
79 std::string StringLiteral::str() const { return value(); }
80 
81 void StringLiteral::PrettyPrint(Bytestream& out, size_t /*indent*/) const
82 {
83  out << Bytestream::Literal << "'";
84 
85  std::string s = value();
86  size_t i = 0;
87  do
88  {
89  // Highlight variable references within strings.
90  size_t dollarSign = s.find("$", i);
91  out << s.substr(i, dollarSign - i);
92 
93  if (dollarSign == std::string::npos)
94  break;
95 
96  size_t end;
97  if (s[dollarSign + 1] == '{')
98  end = s.find("}", dollarSign + 1) + 1;
99 
100  else
101  end = std::min(
102  s.find(" ", dollarSign + 1),
103  s.find(".", dollarSign + 1)
104  );
105 
106  out
107  << Bytestream::Reference
108  << s.substr(dollarSign, end - dollarSign)
109  << Bytestream::Literal
110  ;
111 
112  i = end;
113 
114  } while (i < s.length());
115 
116  out << "'" << Bytestream::Reset;
117 }
118 
119 void StringLiteral::Accept(Visitor& v) const { v.Enter(*this); v.Leave(*this); }
120 
121 dag::ValuePtr StringLiteral::evaluate(EvalContext&) const
122 {
123  return dag::ValuePtr(new dag::String(value(), type(), source()));
124 }
An integer (of unspecified precision).
Definition: Primitive.h:88
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
Representation of the Abstract Syntax Tree for Fabrique source code.
Definition: Action.h:47
An ASCII string (for now, we should make this Unicode soon).
Definition: Primitive.h:102
Declaration of several literal expression types.
Declaration of fabrique::dag::Primitive.
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
Declaration of fabrique::ast::Visitor.
A context for evaluating AST Expression objects.
Definition: EvalContext.h:69
Declaration of fabrique::Bytestream.
Interface for visitors that walk the AST.
Definition: Visitor.h:49
A boolean (true/false) value.
Definition: Primitive.h:71