|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r73557 - trunk/libs/spirit/example/qi/compiler_tutorial/conjure3
From: joel_at_[hidden]
Date: 2011-08-05 20:57:12
Author: djowel
Date: 2011-08-05 20:57:10 EDT (Fri, 05 Aug 2011)
New Revision: 73557
URL: http://svn.boost.org/trac/boost/changeset/73557
Log:
Updates. Preparation for supporting more operators (starting with assignment operators).
Text files modified:
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/ast.hpp | 28 ++++++----------------------
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp | 19 +++----------------
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.hpp | 2 --
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression.hpp | 2 --
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp | 14 --------------
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.hpp | 1 +
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp | 1 +
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/statement.hpp | 2 +-
trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/statement_def.hpp | 17 +++++++++++------
9 files changed, 23 insertions(+), 63 deletions(-)
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/ast.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/ast.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/ast.hpp 2011-08-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -43,7 +43,7 @@
struct literal :
tagged,
- boost::spirit::adapted_variant<
+ boost::spirit::extended_variant<
nil
, bool
, unsigned int>
@@ -83,27 +83,12 @@
std::list<expression> args;
};
- struct binary
+ struct expression
{
operand first;
std::list<operation> rest;
};
- struct assignment;
-
- struct expression :
- boost::spirit::adapted_variant<
- nil
- , boost::recursive_wrapper<assignment>
- , binary>
- {
- expression() : base_type() {}
- expression(assignment const& val) : base_type(val) {}
- expression(binary const& val) : base_type(val) {}
- expression(expression const& rhs)
- : base_type(rhs.get()) {}
- };
-
struct assignment
{
identifier lhs;
@@ -122,11 +107,10 @@
struct statement_list;
struct return_statement;
- typedef boost::optional<expression> expression_statement;
-
typedef boost::variant<
- variable_declaration
- , expression_statement
+ nil
+ , variable_declaration
+ , assignment
, boost::recursive_wrapper<if_statement>
, boost::recursive_wrapper<while_statement>
, boost::recursive_wrapper<return_statement>
@@ -195,7 +179,7 @@
)
BOOST_FUSION_ADAPT_STRUCT(
- client::ast::binary,
+ client::ast::expression,
(client::ast::operand, first)
(std::list<client::ast::operation>, rest)
)
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-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -224,7 +224,7 @@
return lhs;
}
- llvm::Value* compiler::operator()(ast::binary const& x)
+ llvm::Value* compiler::operator()(ast::expression const& x)
{
llvm::Value* lhs = boost::apply_visitor(*this, x.first);
if (lhs == 0)
@@ -233,11 +233,6 @@
return compile_expression(0, lhs, rest_begin, x.rest.end());
}
- llvm::Value* compiler::operator()(ast::expression const& x)
- {
- return boost::apply_visitor(*this, x.get());
- }
-
llvm::Value* compiler::operator()(ast::assignment const& x)
{
llvm::Value* lhs = named_values[x.lhs.name];
@@ -308,6 +303,8 @@
bool compiler::operator()(ast::statement const& x)
{
+ if (boost::get<ast::nil>(&x) != 0) // empty statement
+ return true;
return boost::apply_visitor(as_statement(), x);
}
@@ -321,16 +318,6 @@
return true;
}
- bool compiler::operator()(ast::expression_statement const& x)
- {
- if (x)
- {
- if ((*this)(*x) == 0)
- return false;
- }
- return true;
- }
-
bool compiler::operator()(ast::if_statement const& x)
{
llvm::Value* condition = (*this)(x.condition);
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.hpp 2011-08-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -62,14 +62,12 @@
llvm::Value* operator()(ast::identifier const& x);
llvm::Value* operator()(ast::unary const& x);
llvm::Value* operator()(ast::function_call const& x);
- llvm::Value* operator()(ast::binary const& x);
llvm::Value* operator()(ast::expression const& x);
llvm::Value* operator()(ast::assignment const& x);
bool operator()(ast::variable_declaration const& x);
bool operator()(ast::statement_list const& x);
bool operator()(ast::statement const& x);
- bool operator()(ast::expression_statement const& x);
bool operator()(ast::if_statement const& x);
bool operator()(ast::while_statement const& x);
bool operator()(ast::return_statement const& x);
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression.hpp 2011-08-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -46,8 +46,6 @@
Lexer const& lexer;
qi::rule<Iterator, ast::expression()> expr;
- qi::rule<Iterator, ast::assignment()> assign_expr;
- qi::rule<Iterator, ast::binary()> binary_expr;
qi::rule<Iterator, ast::operand()> unary_expr, primary_expr;
qi::rule<Iterator, ast::function_call()> function_call;
qi::rule<Iterator, std::list<ast::expression>()> argument_list;
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp 2011-08-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -39,19 +39,7 @@
///////////////////////////////////////////////////////////////////////
// Main expression grammar
-
expr =
- assign_expr
- | binary_expr
- ;
-
- assign_expr =
- identifier
- >> tokenid_mask(token_ids::op_assign)
- >> expr
- ;
-
- binary_expr =
unary_expr
>> *(tokenid_mask(token_ids::op_binary) > unary_expr)
;
@@ -87,8 +75,6 @@
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(expr)
- (assign_expr)
- (binary_expr)
(unary_expr)
(primary_expr)
(literal)
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.hpp 2011-08-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -1,5 +1,6 @@
/*=============================================================================
Copyright (c) 2001-2011 Hartmut Kaiser
+ Copyright (c) 2001-2011 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
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-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -1,5 +1,6 @@
/*=============================================================================
Copyright (c) 2001-2011 Hartmut Kaiser
+ Copyright (c) 2001-2011 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/statement.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/statement.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/statement.hpp 2011-08-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -30,7 +30,7 @@
qi::rule<Iterator, ast::statement()> statement_;
qi::rule<Iterator, ast::variable_declaration()> variable_declaration;
- qi::rule<Iterator, ast::expression_statement()> expr_statement;
+ qi::rule<Iterator, ast::assignment()> assignment;
qi::rule<Iterator, ast::if_statement()> if_statement;
qi::rule<Iterator, ast::while_statement()> while_statement;
qi::rule<Iterator, ast::return_statement()> return_statement;
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/statement_def.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/statement_def.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/statement_def.hpp 2011-08-05 20:57:10 EDT (Fri, 05 Aug 2011)
@@ -24,6 +24,7 @@
qi::_val_type _val;
qi::raw_token_type raw_token;
+ qi::tokenid_mask_type tokenid_mask;
using qi::on_error;
using qi::on_success;
@@ -40,9 +41,10 @@
;
statement_ =
- variable_declaration
+ ';'
+ | variable_declaration
+ | assignment
| compound_statement
- | expr_statement
| if_statement
| while_statement
| return_statement
@@ -51,12 +53,15 @@
variable_declaration =
l("int")
> expr.identifier
- > -(l("=") > expr.binary_expr)
+ > -(l("=") > expr)
> ';'
;
- expr_statement =
- ';' | (expr > ';')
+ assignment =
+ expr.identifier
+ > tokenid_mask(token_ids::op_assign)
+ > expr
+ > ';'
;
if_statement =
@@ -95,7 +100,7 @@
(statement_list)
(statement_)
(variable_declaration)
- (expr_statement)
+ (assignment)
(if_statement)
(while_statement)
(compound_statement)
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