cvsuser     03/03/16 08:06:50

  Modified:    .        MANIFEST
  Added:       examples/assembly uniq.pasm
  Log:
  A PASM version of Uniq. Not as fast as GNU's uniq, but a nice enough
  example anyway.
  
  Courtesy of Leon Brocard <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.327     +1 -0      parrot/MANIFEST
  
  Index: MANIFEST
  ===================================================================
  RCS file: /cvs/public/parrot/MANIFEST,v
  retrieving revision 1.326
  retrieving revision 1.327
  diff -u -w -r1.326 -r1.327
  --- MANIFEST  15 Mar 2003 01:42:26 -0000      1.326
  +++ MANIFEST  16 Mar 2003 16:06:48 -0000      1.327
  @@ -196,6 +196,7 @@
   examples/assembly/sub.pasm
   examples/assembly/substr.pasm
   examples/assembly/trace.pasm
  +examples/assembly/uniq.pasm
   examples/assembly/xml_parser.pasm
   examples/benchmarks/bench_newp.pasm
   examples/benchmarks/gc_alloc_new.pasm
  
  
  
  1.1                  parrot/examples/assembly/uniq.pasm
  
  Index: uniq.pasm
  ===================================================================
  # $Id: uniq.pasm,v 1.1 2003/03/16 16:06:50 dan Exp $
  # uniq - Remove duplicate lines from a sorted file
  #
  #   % ./assemble.pl uniq.pasm -o uniq.pbc
  #   % ./parrot uniq.pbc data.txt
  #   % ./parrot uniq.pbc -c data.txt
  #
  # Takes options and a filename as argument:
  #
  # -c
  #   Precede each output line with the count of the number of times the
  #   line occurred in the input, followed by a single space
  #
  # -d
  #   Don't output lines that are not repeated in the input
  #
  # -u
  #   Don't output lines that are repeated in the input
  #
  # By Leon Brocard <[EMAIL PROTECTED]>
  
    set S0, P0[1]
    if S0, SOURCE
    set S0, P0[0]
    print "usage: parrot "
    print S0
    print " [-cdu] filename\n"
    end
  
  SOURCE:
    # do some simple option parsing
  
    ne S0, "-c", NOTC
    set I10, 1 # count mode
    set S0, P0[2]
  
  NOTC:
    ne S0, "-d", NOTD
    set I11, 1 # duplicate mode
    set S0, P0[2]
  
  NOTD:
    ne S0, "-u", GO
    set I12, 1 # unique mode
    set S0, P0[2]
  
  GO:
    # S2 is the previous line
  
    set I1, 1 # count
    # Read the file into S1
    open I0, S0
    readline S2, I0
  
  SOURCE_LOOP:
    readline S1, I0
  
    eq S1, S2, MATCH
  
    # different line
  
    unless I10, NOTC2
    # count mode
    # we go to some lengths to make the count pretty
    set S3, I1
    length I2, S3
    sub I2, 7, I2
    set S3, " "
    repeat S3, S3, I2
    print S3
    print I1
    print " "
    print S2
    branch RESET
  
  NOTC2:
    unless I11, NOTD2
  
    # show duplicates mode
    eq 1, I1, RESET
    print S2
    branch RESET
  
  NOTD2:
    unless I12, NOTU2
  
    # don't show lines that are duplicated mode
    ne 1, I1, RESET
    print S2
    branch RESET
  
  NOTU2:
  
    # default mode
    print S2
    branch RESET
  
  RESET:
    set I1, 1
    branch LOOP
  
  MATCH:
    inc I1
    # fall through
  
  LOOP:
    set S2, S1
    if S1, SOURCE_LOOP
    close I0
  
    end
  
  
  

Reply via email to