# New Ticket Created by  Simon Glover 
# Please include the string:  [perl #17899]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=17899 >



  This patch:

  1) Fixes some minor nits in the assembler documentation (mostly POD
  formatting issues)

  2) Documents the .include macro

  3) Fixes a bug in the code that handles includes: we weren't stripping
  off extraneous whitespace, so macros in included files weren't being
  recognised as such.

  4) Adds two tests for .include to macro.t

 NB Patches attached rather than appended, in an effort to avoid mailer
 mangling

 Simon




-- attachment  1 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/39757/32183/2bd328/ass.patch

-- attachment  2 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/39757/32184/49a883/mac.patch

--- assemble.pl.old     Sun Oct 13 16:19:11 2002
+++ assemble.pl Sun Oct 13 17:59:15 2002
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-=head2 Parrot Assembler
+=head1 Parrot Assembler
 
 The Parrot Assembler's job is to take .pasm (Parrot Assembly) files and assemble
 them into Parrot bytecode. Plenty of references for Parrot assembly syntax
@@ -22,9 +22,9 @@ accumulates the (finally completely nume
 The XS portion takes the constants and bytecode, generates a header, tacks the
 constants and bytecode on, and finally prints out the string.
 
-=head1 Macro
+=head2 Macro
 
-The Parrot assebler's macro layer has now been more-or-less defined, with one
+The Parrot assembler's macro layer has now been more-or-less defined, with one
 or two additions to come. The addition of the '.' preface will hopefully make
 things easier to parse, inasmuch as everything within an assembler file that
 needs to be expanded or processed by the macro engine will have a period ('.')
@@ -132,6 +132,8 @@ use Parrot::PMC qw(%pmc_types);
 
 =head2 Macro class
 
+=over 4
+
 =item new
 
 Create a new Macro instance. Simply take the argument list and treat it as a
@@ -145,7 +147,7 @@ sub new {
   my $self;
   #
   # Read the files, strip leading and trailing whitespace, and put the lines
-  # into an array in $self->{contents}.
+  # into an array in $self->{cur_contents}.
   #
   for(@_) {
     open FILE,"< $_" or
@@ -207,33 +209,54 @@ compilation.
   .constant name {"string constant"}
   .constant name {'string constant'}
 
-    Are removed from the array. Given the line:
+are removed from the array. Given the line:
+
+  '.constant HelloWorld "Hello, World!"'
+
+one can expand HelloWorld via:
+
+  'print .HelloWorld' # Note the period to indicate a thing to expand.
+
+Some predefined constants exist for your convenience, namely:
+
+  .Array
+  .PerlHash
+  .PerlArray
+
+and the other PMC types.
+(This should be generated from include/parrot/pmc.h, but isn't at the moment.)
+
+The contents of external files can be included by use of the C<.include> 
+macro:
 
-    '.constant HelloWorld "Hello, World!"'
+  .include "{filename}" 
 
-    One can expand HelloWorld via:
+The contents of the included file are inserted at the point where the
+C<.include> macro occurs. This means that code like this:
 
-    'print .HelloWorld' # Note the period to indicate a thing to expand.
+  print "Hello "
+  .include "foo.pasm"
+  end
+
+where F<foo.pasm> contains:
 
-    Some predefined constants exist for your convenience, namely:
+  print "World \n"
 
-      .Array
-      .PerlHash
-      .PerlArray
-      and the other PMC types.
+becomes:
 
-    This should be generated from include/parrot/pmc.h, but isn't at the moment.
-    A .include should be added, but currently is awaiting more time and sleep.
+  print "Hello "
+  print "World \n"
+  end
 
-  .include "{file name}" # Not quite ready.
+Attempting to include a non-existent file is a non-fatal error.
 
   .macro name ({arguments?})
   ...
   .endm
 
-    Optional arguments are simply identifiers separated by commas. These
-    arguments are matched to instances inside the macro named '.foo'. A
-    simple example follows:
+Optional arguments are simply identifiers separated by commas. These
+arguments are matched to instances inside the macro named '.foo'. A
+simple example follows:
 
   .macro inc3 (A,BLAM)
     inc .A # Mark the argument to expand with a '.'.
@@ -292,6 +315,7 @@ sub preprocess {
         my @include;
         while(<FOO>) {
           chomp;
+          s/(^\s+|\s+$)//g;    # Need to strip leading & trailing whitespace
           push(@include,$_);
         }
         unshift(@todo,@include);
@@ -381,6 +405,8 @@ sub preprocess {
 Access the C<$self->{contents}> internal array, where the post-processed data
 is stored.
 
+=back
+
 =cut
 
 sub contents {
@@ -404,6 +430,8 @@ use Parrot::Config;
 
 =head2 Assembler class
 
+=over 4
+
 =item new
 
 Create a new Assembler instance.
@@ -908,6 +936,8 @@ internal PC tracking.
 The third pass takes labels and replaces them with the PC offset to the actual
 instruction, and generates bytecode. It returns the bytecode, and we're done.
 
+=back
+
 =cut
 
 sub to_bytecode {
@@ -1070,10 +1100,14 @@ exit;
 
 #------------------------------------------------------------------------------
 
+=over 4
+
 =item process_args
 
-Process the argument list and return the list of arguments and files to process.
-Only legal and sane arguments and files should get past this point.
+Process the argument list and return the list of arguments and files to 
+process. Only legal and sane arguments and files should get past this point.
+
+=back
 
 =cut
 
--- t/op/macro.t.old    Sun Oct 13 17:03:01 2002
+++ t/op/macro.t        Sun Oct 13 18:06:39 2002
@@ -1,6 +1,6 @@
 #! perl -w
 
-use Parrot::Test tests => 13;
+use Parrot::Test tests => 15;
 use Test::More;
 
 output_is( <<'CODE', <<OUTPUT, "macro, zero parameters" );
@@ -147,3 +147,55 @@ output_is(<<'CODE', 'foo', "constant def
   .answer(.FOO)
   end
 CODE
+
+open FOO, ">macro.tempfile";
+print FOO <<'ENDF';
+  set S0, "Betelgeuse\n"
+ENDF
+close FOO;
+
+output_is(<<"CODE", <<OUTPUT, "basic include macro");
+.include "macro.tempfile"
+  print S0
+
+  set S0, "Rigel"
+.include "macro.tempfile"
+  print S0
+  end
+CODE
+Betelgeuse
+Betelgeuse
+OUTPUT
+
+
+open FOO, ">macro.tempfile";   # Clobber previous
+print FOO <<'ENDF';
+  .macro multiply(A,B) 
+    new P0, .PerlNum
+    set P0, .A
+    new P1, .PerlNum
+    set P1, .B
+    new P2, .PerlNum
+    mul P2, P1, P0
+  .endm
+ENDF
+close FOO;
+
+output_is(<<"CODE", <<OUTPUT, "include a file defining a macro");
+.include "macro.tempfile"
+  .multiply(12,13)
+  print P2
+  print "\\n"
+  end
+CODE
+156
+OUTPUT
+
+open FOO, ">macro.tempfile";   # Clobber previous
+close FOO;
+unlink("macro.tempfile");
+1;
+
+
+
+

Reply via email to