Hi!

I need to figure out some basic forth question in the way it deals with
strings. I wrote a rudimentary DXF exporter (see attachment) and
stumbled upon problems with strings. The problem is with 3dface word
with is lengthy and I provide it here in a contracted view to
demonstrate the point:
: 3DFACE ( fid r1[ r2[ r3[ -- )
    { r1[ r2[ r3[ | 
    dup [ s"  0 " 2dup endl ] sliteral dxf-write
    dup [ s" 3DFACE " 2dup endl ] sliteral dxf-write
    dup [ s"  8 " 2dup endl ] sliteral dxf-write 
    dup [ s" shell00 " 2dup endl ] sliteral dxf-write
  
...
    
    dup [ s"  62 " 2dup endl ] sliteral dxf-write
    [ s" 0 " 2dup endl ] sliteral dxf-write } ;

The firs version of this word looked like that:
: 3DFACE ( fid r1[ r2[ r3[ -- )
    { r1[ r2[ r3[ | 
    dup s"  0 " 2dup endl dxf-write
    dup s" 3DFACE " 2dup endl dxf-write
    dup s"  8 " 2dup endl dxf-write 
    dup s" shell00 " 2dup endl dxf-write
  
...
    
    dup s"  62 " 2dup endl dxf-write
    s" 0 " 2dup endl dxf-write } ;

However, this version could only be called once (that is saving just one
face). Second call would always raise "Bus error" message. I concluded
that I do not understand something very basic here. But I needed to get
the job done so I came up with the version that uses sliteral trick.
This version works, but lets me save only one file. Trying to save 2
files during one bigforth session again raises "Bus error" message.

Can somebody look and tell me what am I doing wrong?

Thank you in advance.

--
Sergey
\ *****************************************************************
\                   Loading all necessary modules
\ *****************************************************************

warning off
\needs locals| | include locals.fs
\needs gsl | include gsl.fs
\ functions for working with triangles
get-current get-context
\needs (tri-area) | include triangles.fs
set-context set-current
warning on

\ *****************************************************************
\                        MODULE DEFINITION
\ *****************************************************************
Module dxf
also gsl also float

: [" ['] [ execute ['] s" execute ; immediate
: "] ['] " execute ['] ] execute ['] sliteral execute ; immediate

: endl ( addr u -- )
    1- + 10 swap ! ;

: dxf-write ( fid addr u -- )
    rot write-file throw ;    

: dxf-writeln ( fid addr u -- )
    2dup endl rot write-file throw ;    

: DXF-HEADER ( fid -- )
    dup s"  999 " dxf-writeln
    dup s" this file is written from bigforth " dxf-writeln
    dup s"  0 " dxf-writeln
    dup s" SECTION " dxf-writeln
    dup s"  2 " dxf-writeln
    dup s" HEADER " dxf-writeln
    dup s"  0 " dxf-writeln
    s" ENDSEC " dxf-writeln
;       

: DXF-ENT[ ( fid -- )
    dup s"  0 " dxf-writeln
    dup s" SECTION " dxf-writeln
    dup s"  2 " dxf-writeln
    s" ENTITIES "dxf-writeln
;

: ]DXF-ENT ( fid -- )
    dup s"  0 " dxf-writeln
        s" ENDSEC " dxf-writeln
;

: DXF-EOF ( fid -- )
    dup [ s"  0 " 2dup endl ] sliteral dxf-writeln
    dup [ s" EOF " 2dup endl ] sliteral dxf-write
    close-file throw ;

: DXF-TABLE ( fid -- )
    dup s"  0 " dxf-writeln
    dup s" SECTION " dxf-writeln
    dup s"  2 " dxf-writeln
    dup s" TABLES " dxf-writeln
    dup s"  0 " dxf-writeln    
    dup s" TABLE " dxf-writeln
    dup s"  2 " dxf-writeln
    dup s" LAYER " dxf-writeln
    dup s"  70 " dxf-writeln
    dup s" 153 " dxf-writeln
    dup s"  0 " dxf-writeln
    dup s" LAYER " dxf-writeln
    dup s"  2 " dxf-writeln
    dup s" shell00 " dxf-writeln
    dup s"  70 " dxf-writeln
    dup s" 0 " dxf-writeln
    dup s"  62 " dxf-writeln
    dup s" 15 " dxf-writeln
    dup s" 6 " dxf-writeln
    dup s" CONTINUOUS " dxf-writeln
    dup s"  0 " dxf-writeln
    dup s" ENDTAB " dxf-writeln
    dup s"  0 " dxf-writeln
    s" ENDSEC " dxf-writeln ;

\ 256 allocate throw constant $buffer

: 3DFACE ( fid r1[ r2[ r3[ -- )
    { r1[ r2[ r3[ | 
    dup [ s"  0 " 2dup endl ] sliteral dxf-write
    dup [ s" 3DFACE " 2dup endl ] sliteral dxf-write
    dup [ s"  8 " 2dup endl ] sliteral dxf-write 
    dup [ s" shell00 " 2dup endl ] sliteral dxf-write
    
    dup [ s"  10 " 2dup endl ] sliteral dxf-write
    dup r1[ 0 ]@ f$ dxf-writeln
    dup [ s"  20 " 2dup endl ] sliteral dxf-write
    dup r1[ 1 ]@ f$ dxf-writeln    
    dup [ s"  30 " 2dup endl ] sliteral dxf-write
    dup r1[ 2 ]@ f$ dxf-writeln

    dup [ s"  11 " 2dup endl ] sliteral dxf-write
    dup r2[ 0 ]@ f$ dxf-writeln
    dup [ s"  21 " 2dup endl ] sliteral dxf-write
    dup r2[ 1 ]@ f$ dxf-writeln    
    dup [ s"  31 " 2dup endl ] sliteral dxf-write
    dup r2[ 2 ]@ f$ dxf-writeln

    dup [ s"  12 " 2dup endl ] sliteral dxf-write
    dup r3[ 0 ]@ f$ dxf-writeln
    dup [ s"  22 " 2dup endl ] sliteral dxf-write
    dup r3[ 1 ]@ f$ dxf-writeln    
    dup [ s"  32 " 2dup endl ] sliteral dxf-write
    dup r3[ 2 ]@ f$ dxf-writeln

    dup [ s"  13 " 2dup endl ] sliteral dxf-write
    dup r3[ 0 ]@ f$ dxf-writeln
    dup [ s"  23 " 2dup endl ] sliteral dxf-write
    dup r3[ 1 ]@ f$ dxf-writeln    
    dup [ s"  33 " 2dup endl ] sliteral dxf-write
    dup r3[ 2 ]@ f$ dxf-writeln
    
    dup [ s"  62 " 2dup endl ] sliteral dxf-write
    [ s" 0 " 2dup endl ] sliteral dxf-write } ;

: DXF-FACES ( fid geo{{ mesh[[ -- )
    3 gsl_vector_alloc
    3 gsl_vector_alloc
    3 gsl_vector_alloc    
    { g{{ m[[ x1[ x2[ x3[ |
    g{{ cell- cell- @ 0 do
        g{{ 0 i }} w@ m[[ x1[ (c->vec)
        g{{ 1 i }} w@ m[[ x2[ (c->vec)
        g{{ 2 i }} w@ m[[ x3[ (c->vec)
        dup x1[ x2[ x3[ 3DFACE
    loop drop
    x1[ ]free x2[ ]free x3[ ]free } ;
\ *****************************************************************
\                           END OF MODULE
\ *****************************************************************
Module;

\ get-current get-context
\ include dxf.fs
\ set-context set-current

\ 3 fvector x1[
\ 3 fvector x2[
\ 3 fvector x3[

\ s" /tmp/test.dxf" r/w open-file throw
\ dup dxf-header
\ dup dxf-table
\ dup dxf-ent[

\ dup geo{{ mesh-scalp[[ DXF-FACES

\ dup ]dxf-ent
\ dup dxf-eof 
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to