Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73567 - in trunk/libs/spirit/example/qi/compiler_tutorial: conjure3 conjure_samples
From: joel_at_[hidden]
Date: 2011-08-06 00:27:51


Author: djowel
Date: 2011-08-06 00:27:35 EDT (Sat, 06 Aug 2011)
New Revision: 73567
URL: http://svn.boost.org/trac/boost/changeset/73567

Log:
unary ops + tests
Text files modified:
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp | 23 ++++++++++++++++++++---
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp | 3 +++
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure_samples/operators.cnj | 14 ++++++++++++--
   3 files changed, 35 insertions(+), 5 deletions(-)

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp 2011-08-06 00:27:35 EDT (Sat, 06 Aug 2011)
@@ -67,6 +67,17 @@
         return builder.CreateLoad(value, x.name.c_str());
     }
 
+ namespace
+ {
+ llvm::Value*
+ neg1(llvm::LLVMContext& context, llvm::IRBuilder<>& builder)
+ {
+ llvm::Value* one = llvm::ConstantInt::get(context,
+ llvm::APInt(int_size, 1));
+ return builder.CreateNeg(one, "neg1");
+ }
+ }
+
     llvm::Value* compiler::operator()(ast::unary const& x)
     {
         llvm::Value* operand = boost::apply_visitor(*this, x.operand_);
@@ -75,9 +86,15 @@
 
         switch (x.operator_)
         {
- case token_ids::minus: return builder.CreateNeg(operand, "negtmp");
- case token_ids::not_: return builder.CreateNot(operand, "nottmp");
- case token_ids::plus: return operand;
+ case token_ids::compl_:
+ return builder.CreateXor(
+ operand, neg1(context(), builder), "compltmp");
+ case token_ids::minus:
+ return builder.CreateNeg(operand, "negtmp");
+ case token_ids::not_:
+ return builder.CreateNot(operand, "nottmp");
+ case token_ids::plus:
+ return operand;
             default: BOOST_ASSERT(0); return 0;
         }
     }

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp 2011-08-06 00:27:35 EDT (Sat, 06 Aug 2011)
@@ -57,6 +57,9 @@
                 ("\\*", token_ids::times)
                 ("\\/", token_ids::divide)
                 ("%", token_ids::mod)
+ ("\\+\\+", token_ids::plus_plus)
+ ("\\-\\-", token_ids::minus_minus)
+ ("~", token_ids::compl_)
                 ("!", token_ids::not_)
             ;
 

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure_samples/operators.cnj
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure_samples/operators.cnj (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure_samples/operators.cnj 2011-08-06 00:27:35 EDT (Sat, 06 Aug 2011)
@@ -118,7 +118,17 @@
     return l;
 }
 
+int zero() { return 0; }
+
+int unary()
+{
+ int i = ~zero(); // -1
+ int j = -i; // 1
+ return j;
+}
+
 int main()
 {
- return assign() ^ binary(); // 0
-}
\ No newline at end of file
+ return (assign() ^ binary()) + unary(); // -1
+}
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk