cvsuser     03/07/06 15:35:22

  Modified:    .        MANIFEST
               config/gen/makefiles bf.in
  Added:       languages/bf bfc.imc
  Removed:     languages/bf bfc.pasm
  Log:
  Move the BF compiler from PASM to IMCC.
  It's much better code too.
  
  Revision  Changes    Path
  1.367     +1 -1      parrot/MANIFEST
  
  Index: MANIFEST
  ===================================================================
  RCS file: /cvs/public/parrot/MANIFEST,v
  retrieving revision 1.366
  retrieving revision 1.367
  diff -u -w -r1.366 -r1.367
  --- MANIFEST  6 Jul 2003 15:22:33 -0000       1.366
  +++ MANIFEST  6 Jul 2003 22:35:18 -0000       1.367
  @@ -1387,7 +1387,7 @@
   languages/befunge/test.bef                        [befunge]
   languages/bf/README                               [bf]
   languages/bf/bf.pasm                              [bf]
  -languages/bf/bfc.pasm                             [bf]
  +languages/bf/bfc.imc                              [bf]
   languages/bf/countdown.bf                         [bf]
   languages/bf/helloworld.bf                        [bf]
   languages/bf/test.bf                              [bf]
  
  
  
  1.4       +3 -5      parrot/config/gen/makefiles/bf.in
  
  Index: bf.in
  ===================================================================
  RCS file: /cvs/public/parrot/config/gen/makefiles/bf.in,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -w -r1.3 -r1.4
  --- bf.in     30 May 2003 17:17:02 -0000      1.3
  +++ bf.in     6 Jul 2003 22:35:20 -0000       1.4
  @@ -1,19 +1,17 @@
   RM_F = ${rm_f}
  -PERL = ${perl}
   
  -ASSEMBLE=$(PERL) ../../assemble.pl
   PARROT=../../parrot
   IMCC=../imcc/imcc
   
   all: build
   
   test: build
  -     $(PARROT) bf.pbc test.bf
  +     $(IMCC) -r bf.pbc test.bf
        $(IMCC) -r bfc.pbc test.bf
   
   build: bf.pasm
  -     $(ASSEMBLE) -o bf.pbc bf.pasm
  -     $(IMCC) -o bfc.pbc bfc.pasm
  +     $(IMCC) -o bf.pbc bf.pasm
  +     $(IMCC) -o bfc.pbc bfc.imc
   
   clean:
        $(RM_F) core *.pbc *~
  
  
  
  1.1                  parrot/languages/bf/bfc.imc
  
  Index: bfc.imc
  ===================================================================
  # $Id: bfc.imc,v 1.1 2003/07/06 22:35:22 acme Exp $
  # A Brainfuck compiler
  # By Leon Brocard <[EMAIL PROTECTED]>
  # 
  # See http://www.catseye.mb.ca/esoteric/bf/
  # for more information on this silly language
  
    .local int pc
    .local int maxpc
    .local int label
    .local string labelstr
    .local string code
    .local string filename
    .local string file
    .local string line
    .local string program
    .local string char
  
    # Get the filename
    filename = P0[1]
    if filename goto SOURCE
    program = P0[0]
    print "usage: ../imcc/imcc -r "
    print program
    print " file.bf\n"
    end
  
    # Read the file into S1
  SOURCE:
    open I0, filename
  SOURCE_LOOP:
    readline line, I0
    file = file . line
    if line goto SOURCE_LOOP
    close I0
  
    length maxpc, file
  
    # Initialise
    code =      "set I0, 0          # pc\n"
    code = code . "new P0, .PerlArray # memory\n"
    code = code . "set I1, 0          # pointer\n"
  
    pc    = 0    # pc
    label = 0    # label count
  
    # The main interpreter loop
  INTERP:
    char = file[pc]
    code = code . "\nSTEP"
    labelstr = pc
    code = code . labelstr
    code = code . ": # "
    code = code . char
    code = code . "\n"
  
    if char != "+" goto NOTPLUS
    code = code . "set I2, P0[I1]\n"
    code = code . "inc I2\n"
    code = code . "set P0[I1], I2\n"
    goto NEXT
  
  NOTPLUS:
    if char != "-" goto NOTMINUS
    code = code . "set I2, P0[I1]\n"
    code = code . "dec I2\n"
    code = code . "set P0[I1], I2\n"
    goto NEXT
  
  NOTMINUS:
    if char != ">" goto NOTGT
    code = code . "inc I1\n"
    goto NEXT
  
  NOTGT:
    if char != "<" goto NOTLT
    code = code . "dec I1\n"
    goto NEXT
  
  NOTLT:
    if char != "[" goto NOTOPEN
  
    I2 = 0 # "depth"
  
    label = pc
  OPEN_LOOP:
    inc label
    substr S2, file, label, 1
    if S2 != "[" goto OPEN_NOTOPEN
    inc I2
    goto OPEN_LOOP
  OPEN_NOTOPEN:
    if S2 != "]" goto OPEN_LOOP
    if I2 == 0 goto OPEN_NEXT
    dec I2
    goto OPEN_LOOP
  OPEN_NEXT:
    inc label
    labelstr = label
    code = code . "set I2, P0[I1]\n"
    code = code . "unless I2, STEP"
    code = code . labelstr
    code = code . "\n"
  
    goto NEXT
  
  NOTOPEN:
    if char != "]" goto NOTCLOSE
  
    label = pc
    I2 = 0 # "height"
  
  CLOSE_LOOP:
    dec label
    substr S2, file, label, 1
    if S2 != "]" goto CLOSE_NOTCLOSE
    inc I2
    goto CLOSE_LOOP
  CLOSE_NOTCLOSE:
    if S2 != "[" goto CLOSE_LOOP
    if I2 == 0 goto CLOSE_NEXT
    dec I2
    goto CLOSE_LOOP
  
  CLOSE_NEXT:
    labelstr = label
    code = code . "branch STEP"
    code = code . labelstr
    code = code . "\n"
  
    goto NEXT
  
  NOTCLOSE:
    if char != "." goto NOTDOT
    code = code . "set I2, P0[I1]\n"
    code = code . "chr S31, I2\n"
    code = code . "print S31\n"
    goto NEXT
  
  NOTDOT:
    if char != "," goto NEXT
    code = code . "readline S31, 0\n"
    code = code . "ord I2, S31\n"
    code = code . "set P0[I1], I2\n"
    goto NEXT
  
  NEXT:
    inc pc
  
    if pc < maxpc goto INTERP
    code = code . "end\n"
  
  #  print code
  #  print "\n"
  
    # Now actually run it
    compreg P1, "PASM"
    compile P0, P1, code
    invoke
    end
  
  
  

Reply via email to