perl.cvs.parrot
[Top] [All Lists]

[svn:parrot] r35056 - trunk/languages/befunge

Subject: [svn:parrot] r35056 - trunk/languages/befunge
From:
Date: Tue, 6 Jan 2009 09:45:26 -0800 PST
Newsgroups: perl.cvs.parrot

Author: jquelin
Date: Tue Jan  6 09:45:25 2009
New Revision: 35056

Added:
   trunk/languages/befunge/load.pir
      - copied, changed from r35014, /trunk/languages/befunge/load.pasm
Removed:
   trunk/languages/befunge/load.pasm
Modified:
   trunk/languages/befunge/befunge.pir

Log:
loading befunge program now working


Modified: trunk/languages/befunge/befunge.pir
==============================================================================
--- trunk/languages/befunge/befunge.pir (original)
+++ trunk/languages/befunge/befunge.pir Tue Jan  6 09:45:25 2009
@@ -1,11 +1,12 @@
 # $Id$
 
+.include "load.pir"
+
 =pod
 
 .include "debug.pasm"
 .include "flow.pasm"
 .include "io.pasm"
-.include "load.pasm"
 .include "maths.pasm"
 .include "stack.pasm"
 
@@ -15,7 +16,8 @@
     .param pmc argv
 
     .local int    i, debug
-    .local string arg, char
+    .local string arg, char, file
+    .local pmc    playfield
 
     print "befunge being ported to a working state...\n"
 
@@ -23,6 +25,7 @@
     #getstdout stdout
     #pioctl I10, P10, 3, 0
 
+
     i     = 0
     debug = 0
 
@@ -40,6 +43,8 @@
     branch ARGV_NEXT
 
 ARGV_DONE:
+    file      = argv[i]
+    playfield = load(file)
 
 =pod
 

Copied: trunk/languages/befunge/load.pir (from r35014, 
/trunk/languages/befunge/load.pasm)
==============================================================================
--- /trunk/languages/befunge/load.pasm  (original)
+++ trunk/languages/befunge/load.pir    Tue Jan  6 09:45:25 2009
@@ -1,70 +1,113 @@
 # $Id$
 
-# Load a file given as parameter.
-# Parrot stack:
-#   before:     ... filename
-#   after:      ... ResizablePMCArray
-# The perlarray is a perlarray of perlarrays filled with the
+#
+# playfield = load( file )
+#
+# Load a file given as parameter
+# The return value is a perlarray of perlarrays filled with the
 # ordinal values of the content of the file, 80x25.
-LOAD:
-        restore S0              # Fetch the filename
-        open P0, S0, 'r'
-        set S1, ""              # S1 = accumulator
-
-# Read the file.
-LOAD_READ:
-        read S2, P0, 256
-        length I0, S2
-        le I0, 0, LOAD_EOF
-        concat S1, S2
-        branch LOAD_READ
-
-# Split the buffer around its newlines.
-LOAD_EOF:
-        close P0
-        concat S1, "\n"         # Add a trailing newline if needed.
-        length I0, S1           # I0 = length of the buffer
-        set I1, -1              # I1 = character offset in the file
-        new P1, .ResizablePMCArray      # P1 = the playing field
-        new P2, .ResizablePMCArray      # P2 = the current line (array of ints)
-
-LOAD_PARSE_BUFFER:
-        inc I1
-        ge I1, I0, LOAD_END_BUFFER
-        substr S2, S1, I1, 1    # S2 = S1[I1]
-        eq S2, "\n", LOAD_FILL_LINE
-        ord I4, S2
-        push P2, I4
-        branch LOAD_PARSE_BUFFER
-LOAD_FILL_LINE:
-        set I10, P2             # I10 = length of current line
-        ge I10, 80, LOAD_TRUNCATE_LINE
-        push P2, 32
-        branch LOAD_FILL_LINE
-LOAD_TRUNCATE_LINE:
-        set P2, 80              # Truncate the line.
-        push P1, P2             # Store the line.
-        new P2, .ResizablePMCArray      # Create a new line.
-        branch LOAD_PARSE_BUFFER
-
-# Fill playfield to 25 rows.
-LOAD_END_BUFFER:
-        set I15, P1
-        ge I15, 25, LOAD_COMPLETE
-        new P2, .ResizablePMCArray
-LOAD_FILL_EMPTY_LINE:
-        set I10, P2
-        ge I10, 80, LOAD_STORE_EMPTY_LINE
-        push P2, 32
-        branch LOAD_FILL_EMPTY_LINE
-LOAD_STORE_EMPTY_LINE:
-        push P1, P2
-        new P2, .ResizablePMCArray
-        branch LOAD_END_BUFFER
-
-# Truncate playfield to 25 rows.
-LOAD_COMPLETE:
-        set P1, 25              # Truncate at 25 lines.
-        save P1                 # Return the playfield
-        ret
+#
+.sub "load"
+    .param string file
+
+    .local int    len, missing, noline
+    .local string line
+    .local pmc    fh, playline, playfield
+
+    playfield = new 'ResizablePMCArray'
+
+
+    # read the befunge program
+    fh        = open file, 'r'
+    noline    = 0
+  LOAD__READ_NEXT_LINE:
+    inc noline
+    if noline > 25 goto LOAD__COMPLETE
+
+    line = readline fh
+    len  = length line
+    if len <= 0 goto LOAD__EOF
+
+    line     = _load__fit_str_to_80(line)
+    playline = _load__str_to_array(line)
+    push playfield, playline
+    goto LOAD__READ_NEXT_LINE
+
+    # end of file, but not enough lines
+  LOAD__EOF:
+    missing = 24 - noline
+        
+  LOAD__FILL_EMPTY_LINE:
+    line     = _load__fit_str_to_80('')
+    playline = _load__str_to_array(line)
+    push playfield, playline
+    dec missing
+    if missing > 0 goto LOAD__FILL_EMPTY_LINE
+    
+    # file loaded, return the playfield
+  LOAD__COMPLETE:
+    close fh
+    .return(playfield)
+
+.end
+
+
+#
+# str = _load__fit_str_to_80( str )
+#
+# return a string equal to str, but truncated at 80 chars, or filled up
+# with spaces to have 80 chars.
+#
+.sub "_load__fit_str_to_80"
+    .param string str
+
+    # padding with spaces to 80 chars if needed
+    .local int i, len
+    len = length str
+    i   = 80 - len
+  _LOAD__FIT_STR_TO_80__PADDING:
+    if i <= 0 goto _LOAD__FIT_STR_TO_80__PADDED
+    dec i
+    concat str, ' '
+    goto _LOAD__FIT_STR_TO_80__PADDING
+  _LOAD__FIT_STR_TO_80__PADDED:
+  
+    # truncate to 80 chars
+    substr str, 80, -1, ''
+  
+    .return(str)
+.end
+
+
+#
+# array = _load__str_to_array( str )
+#
+# return a pmc array whose values are ordinal values of each of the chars
+# of str.
+#
+.sub "_load__str_to_array"
+    .param string str
+    
+    .local int i, len, val
+    .local string char
+    .local pmc array
+   
+    i     = 0
+    len   = length str
+    dec len
+    array = new 'ResizablePMCArray'
+  _LOAD__STR_TO_ARRAY__NEXT_CHAR:
+    if i == len goto _LOAD__STR_TO_ARRAY__DONE
+    char = substr str, i, 1
+    val  = ord char
+    push array, val
+    inc i
+    goto _LOAD__STR_TO_ARRAY__NEXT_CHAR
+    
+  _LOAD__STR_TO_ARRAY__DONE:
+    .return(array)
+.end
+
+
+
 

<Prev in Thread] Current Thread [Next in Thread>
  • [svn:parrot] r35056 - trunk/languages/befunge, jquelin <=