[phc-internals] [phc commit] r1125 - in trunk: . plugins/tools src/parsing

codesite-noreply at google.com codesite-noreply at google.com
Tue Apr 1 12:55:25 IST 2008


Author: paul.biggar
Date: Tue Apr  1 04:54:16 2008
New Revision: 1125

Added:
   trunk/src/parsing/Parse_buffer.cpp
   trunk/src/parsing/Parse_buffer.h
Modified:
   trunk/Makefile.am
   trunk/Makefile.in
   trunk/plugins/tools/debug_zval.cpp

Log:
This adds the parse_buffer, a simple way of abstracting away all the 
boiler-plate code for creating nodes.

The very old way of creating nodes was:

	out->push_back (new Eval_expr
		(new Assignment (
			result->clone (),
			NULL,
			new Bin_op (
				temp1->clone (),
				new String ("+"),
				temp2->clone ()))));

The slightly newer way of doing it is (this varies depending on when I 
used it:

	stringstream ss;
	ss << "$" << *(result->get_value_as_string) << " = $" << 
*(temp2->get_value_as_string) << " + " << *(temp2->get_value_as_string) << ";"
	out->push_back_all (lower_ast (new String ("tidyp"), parse_to_ast (new 
AST_unparser (new String (ss.str()), true), in)));

The latest way of doing this is to use the new Parse_buffer and the 
stream operators:
	(*out
		<< "$" << result << " = $" << temp1 << " + $" << temp2 ";"
	).to_pass (s("tidy_p"), in);

which is cleaner and more obvious, if not hugely shorter.

Note that neither of the parsing approaches does a complete job, since 
clone copies the attributes aswell. I think this can be accomplished 
using the Clone_blank_mixins, but I'll need to work on this in the future.


Modified: trunk/Makefile.am
==============================================================================
--- trunk/Makefile.am	(original)
+++ trunk/Makefile.am	Tue Apr  1 04:54:16 2008
@@ -136,6 +136,8 @@
 	src/lib/Object.h											\
 	src/lib/String.cpp										\
 	src/lib/String.h											\
+	src/parsing/Parse_buffer.cpp							\
+	src/parsing/Parse_buffer.h								\
 	src/parsing/parse.cpp									\
 	src/parsing/parse.h										\
 	src/parsing/PHP_context.cpp							\

Modified: trunk/Makefile.in
==============================================================================
--- trunk/Makefile.in	(original)
+++ trunk/Makefile.in	Tue Apr  1 04:54:16 2008
@@ -291,8 +291,8 @@
 	HIR_lower_expr.$(OBJEXT) Lower_control_flow.$(OBJEXT) \
 	AttrMap.$(OBJEXT) base64.$(OBJEXT) Boolean.$(OBJEXT) \
 	demangle.$(OBJEXT) error.$(OBJEXT) Integer.$(OBJEXT) \
-	Object.$(OBJEXT) String.$(OBJEXT) parse.$(OBJEXT) \
-	PHP_context.$(OBJEXT) XML_parser.$(OBJEXT) \
+	Object.$(OBJEXT) String.$(OBJEXT) Parse_buffer.$(OBJEXT) \
+	parse.$(OBJEXT) PHP_context.$(OBJEXT) XML_parser.$(OBJEXT) \
 	Pass_manager.$(OBJEXT) Plugin_pass.$(OBJEXT) phc.$(OBJEXT) \
 	AST_unparser.$(OBJEXT) DOT_unparser.$(OBJEXT) \
 	Invalid_check.$(OBJEXT) Note_top_level_declarations.$(OBJEXT) \
@@ -659,6 +659,8 @@
 	src/lib/Object.h											\
 	src/lib/String.cpp										\
 	src/lib/String.h											\
+	src/parsing/Parse_buffer.cpp							\
+	src/parsing/Parse_buffer.h								\
 	src/parsing/parse.cpp									\
 	src/parsing/parse.h										\
 	src/parsing/PHP_context.cpp							\
@@ -1169,6 +1171,7 @@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/Object.Po at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/PHP_context.Po at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/PHP_unparser.Po at am__quote@
+ at AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/Parse_buffer.Po at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/Pass_manager.Po at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/Plugin_pass.Po at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/Pre_post_op_shredder.Po at am__quote@
@@ -2056,6 +2059,20 @@
  @AMDEP_TRUE@@am__fastdepCXX_FALSE@	source='src/lib/String.cpp' 
object='String.obj' libtool=no @AMDEPBACKSLASH@
  @AMDEP_TRUE@@am__fastdepCXX_FALSE@	DEPDIR=$(DEPDIR) $(CXXDEPMODE) 
$(depcomp) @AMDEPBACKSLASH@
  @am__fastdepCXX_FALSE@	$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) 
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o String.obj 
`if test -f 'src/lib/String.cpp'; then 
$(CYGPATH_W) 'src/lib/String.cpp'; else 
$(CYGPATH_W) '$(srcdir)/src/lib/String.cpp'; fi`
+
+Parse_buffer.o: src/parsing/Parse_buffer.cpp
+ at am__fastdepCXX_TRUE@	$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) 
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT 
Parse_buffer.o -MD -MP -MF $(DEPDIR)/Parse_buffer.Tpo -c -o 
Parse_buffer.o `test -f 'src/parsing/Parse_buffer.cpp' || echo '$(srcdir)/'`src/parsing/Parse_buffer.cpp
+ at am__fastdepCXX_TRUE@	mv -f $(DEPDIR)/Parse_buffer.Tpo $(DEPDIR)/Parse_buffer.Po
+ at AMDEP_TRUE@@am__fastdepCXX_FALSE@	
source='src/parsing/Parse_buffer.cpp' object='Parse_buffer.o' 
libtool=no @AMDEPBACKSLASH@
+ at AMDEP_TRUE@@am__fastdepCXX_FALSE@	DEPDIR=$(DEPDIR) $(CXXDEPMODE) 
$(depcomp) @AMDEPBACKSLASH@
+ at am__fastdepCXX_FALSE@	$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) 
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o 
Parse_buffer.o `test -f 'src/parsing/Parse_buffer.cpp' || echo '$(srcdir)/'`src/parsing/Parse_buffer.cpp
+
+Parse_buffer.obj: src/parsing/Parse_buffer.cpp
+ at am__fastdepCXX_TRUE@	$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) 
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT 
Parse_buffer.obj -MD -MP -MF $(DEPDIR)/Parse_buffer.Tpo -c -o 
Parse_buffer.obj `if test -f 'src/parsing/Parse_buffer.cpp'; then 
$(CYGPATH_W) 'src/parsing/Parse_buffer.cpp'; else 
$(CYGPATH_W) '$(srcdir)/src/parsing/Parse_buffer.cpp'; fi`
+ at am__fastdepCXX_TRUE@	mv -f $(DEPDIR)/Parse_buffer.Tpo $(DEPDIR)/Parse_buffer.Po
+ at AMDEP_TRUE@@am__fastdepCXX_FALSE@	
source='src/parsing/Parse_buffer.cpp' object='Parse_buffer.obj' 
libtool=no @AMDEPBACKSLASH@
+ at AMDEP_TRUE@@am__fastdepCXX_FALSE@	DEPDIR=$(DEPDIR) $(CXXDEPMODE) 
$(depcomp) @AMDEPBACKSLASH@
+ at am__fastdepCXX_FALSE@	$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) 
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o 
Parse_buffer.obj `if test -f 'src/parsing/Parse_buffer.cpp'; then 
$(CYGPATH_W) 'src/parsing/Parse_buffer.cpp'; else 
$(CYGPATH_W) '$(srcdir)/src/parsing/Parse_buffer.cpp'; fi`

 parse.o: src/parsing/parse.cpp
  @am__fastdepCXX_TRUE@	$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) 
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT parse.o -MD 
-MP -MF $(DEPDIR)/parse.Tpo -c -o parse.o `test 
-f 'src/parsing/parse.cpp' || echo '$(srcdir)/'`src/parsing/parse.cpp

Modified: trunk/plugins/tools/debug_zval.cpp
==============================================================================
--- trunk/plugins/tools/debug_zval.cpp	(original)
+++ trunk/plugins/tools/debug_zval.cpp	Tue Apr  1 04:54:16 2008
@@ -8,6 +8,7 @@
 #include "AST_transform.h"
 #include "process_ast/AST_unparser.h"
 #include "pass_manager/Plugin_pass.h"
+#include "parsing/Parse_buffer.h"
 #include "process_ir/General.h"

 using namespace AST;
@@ -37,23 +38,10 @@

 	Variable* post_variable (Variable* var)
 	{
-		stringstream var_name;
-		var->visit (new AST_unparser (var_name));
-		var_name << ": ";
-
-		// If we dont shred it, we cant generate code for it
-
-		// printf ("\$var: ");
-		Statement* print = new Eval_expr (
-				new Method_invocation (
-					NULL,
-					new METHOD_NAME (new String ("printf")),
-					new List<Actual_parameter*> (
-						new Actual_parameter (false,
-							new STRING (new String (var_name.str ())))//,
-//						new Actual_parameter (false,
-//							new STRING (new String (": ")))
-							)));
+		// printf ("$var: ");
+		(*debugs
+			<< "printf (\"\\" << var << ": \");"
+		).to_pass (s("tidyp"), var); // TODO do relative to current pass

 		// Replace $x[] with just $x
 		Variable* dumped_var = var;
@@ -64,23 +52,9 @@
 		}

 		// debug_zval_dump ($var);
-		Statement* dump = new Eval_expr (
-				new Method_invocation (
-					NULL,
-					new METHOD_NAME (new String ("debug_zval_dump")),
-					new List<Actual_parameter*> (
-						new Actual_parameter (false, dumped_var))));
-
-
-		/* In order to shred these, they must be wrapped in a PHP_script
-		 * first
-		 * (Its hard to decide if this is a bug or not. Declaring it a bug would
-		 * mean that we'd have to support passing any Node to any point in
-		 * the pass queue. I dont think that's a good idea). */
-		List<Statement*>* shredded = new List<Statement*> (print, dump);
-		PHP_script* ir = new PHP_script (shredded);
-		pm->run_from_until (new String ("lcf"), new String ("shred"), ir);
-		debugs->push_back_all (shredded);
+		(*debugs
+			<< "debug_zval_dump (" << dumped_var << ");"
+		).to_pass (s("tidyp"), var); // TODO do relative to current pass

 		return var;
 	}

Added: trunk/src/parsing/Parse_buffer.cpp
==============================================================================
--- (empty file)
+++ trunk/src/parsing/Parse_buffer.cpp	Tue Apr  1 04:54:16 2008
@@ -0,0 +1,56 @@
+/*
+ * phc -- the open source PHP compiler
+ * See doc/license/README.license for licensing information
+ *
+ * Provide convenience operators for parsing code
+ */
+
+#include "Parse_buffer.h"
+#include "process_ast/AST_unparser.h"
+
+Parse_buffer::Parse_buffer () {}
+
+Parse_buffer::Parse_buffer (List<AST::Statement*>* out)
+: out (out)
+{
+}
+
+void Parse_buffer::finish (AST::Node* anchor)
+{
+	out->push_back_all (parse_to_ast (new String (ss.str ()), anchor));
+}
+
+void Parse_buffer::to_pass (String* pass, AST::Node* anchor)
+{
+	out->push_back_all (lower_ast (pass, parse_to_ast (new String (ss.str 
()), anchor)));
+}
+
+/* When given a list of IR statements, use the << operator to parse 
the given
+ * string into it. When a IR::Node is passed, use the appropriate 
unparser to
+ * generate ssource from it. */
+
+// The left-most << in the chain needs to create the buffer
+Parse_buffer::Parse_buffer& operator<<(List<AST::Statement*>& stmts, 
const char* in)
+{
+	Parse_buffer& out = *(new Parse_buffer (&stmts));
+	out.ss << "<?php " << in;
+	return out;
+}
+
+Parse_buffer::Parse_buffer& operator<<(Parse_buffer& out, AST::Node* in)
+{
+	// Use the unparser
+	stringstream ss;
+	in->visit (new AST_unparser (ss, true));
+	out.ss << ss.str ();
+
+	return out;
+}
+
+Parse_buffer::Parse_buffer& operator<<(Parse_buffer& out, const char* in)
+{
+	out.ss << in;
+	return out;
+}
+
+

Added: trunk/src/parsing/Parse_buffer.h
==============================================================================
--- (empty file)
+++ trunk/src/parsing/Parse_buffer.h	Tue Apr  1 04:54:16 2008
@@ -0,0 +1,40 @@
+/*
+ * phc -- the open source PHP compiler
+ * See doc/license/README.license for licensing information
+ *
+ * Provide convenience operators for parsing code
+ */
+
+#include "process_ir/General.h"
+
+#ifndef PHC_PARSE_BUFFER
+#define PHC_PARSE_BUFFER
+
+class Parse_buffer
+{
+
+// TODO protected via friend <<
+public:
+	// provides line numbers
+	List<AST::Statement*>* out;
+	stringstream ss;
+
+public:
+
+	Parse_buffer ();
+	Parse_buffer (List<AST::Statement*>* out);
+
+	void finish (AST::Node* anchor);
+	void to_pass (String* pass, AST::Node* anchor);
+};
+
+/* When given a list of IR statements, use the << operator to parse 
the given
+ * string into it. When a IR::Node is passed, use the appropriate 
unparser to
+ * generate ssource from it. */
+
+// Left-most
+Parse_buffer& operator<<(List<AST::Statement*>& stmts, const char* in);
+Parse_buffer& operator<<(Parse_buffer& out, AST::Node* in);
+Parse_buffer& operator<<(Parse_buffer& out, const char* in);
+
+#endif // PHC_PARSE_BUFFER


More information about the phc-internals mailing list