HOME  NEWS  FORUM  DOWNLOAD  LINK
OpenCOBOL - an open-source COBOL compiler
Main Menu
Download
Documentation
Development
Who's Online
9 user(s) are online (5 user(s) are browsing Forum)

Members: 0
Guests: 9

more...
Powered by
SourceForge

Xoops

Creative Commons

OpenCOBOL Forum Index
   OpenCOBOL
     Guile extension revisited (well, re-read) and name dropping
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
btiffin
Posted on: 2010/11/10 4:17
Webmaster
Joined: 2008/6/7
From: CANADA
Posts: 1196
Guile extension revisited (well, re-read) and name dropping
I was poking around this evening, a little daunted by the ROOT/CINT docs and the best way to be lazy and get it working, so that means it was time to wander off.

I was looking at the Guile in OpenCOBOL sample; and then decided to read up some more. See if building OpenCOBOL with

./configure --with-guile is actually feasible.

Guess who's name popped up?

Our good Keisuke Nishida. He is mentioned in the Guile FAQ...

http://www.gnu.org/software/guile/docs/faq/guile-faq.html#Is-there-a-Scheme-code-profiler-that-works-with-Guile_003f

In reference to a post from 2000
Quote:

Keisuke Nishida asks and answers his own question, supplying a patch to implement such a profiler. Hmm, how would that look on a call-graph? (smile)


As a fan-boy...that's like, incentive.

Cheers,
Brian
btiffin
Posted on: 2011/7/4 6:15
Webmaster
Joined: 2008/6/7
From: CANADA
Posts: 1196
Re: Guile extension revisited (well, re-read) and name dropping
I just installed Guile 2.0.2. I'm keen again. When I asked a question on #guile Freenode IRC today, the first response back was "Is that the cobol Keisuke Nishida worked on?" So he's a known entity in that circle for sure.

Anyway, here's a useless but fun call using libguile-2.0. Please excuse some of the language. bf is one of the newly supported scripting engines in Guile 2, except it's not really called "bf".

callbf.cob
OCOBOL >>SOURCE FORMAT IS FREE
      *> ***************************************************************
      *> Author:    Brian Tiffin 
      *> Date:      20110704
      *> Purpose:   Demonstrate libguile bf interactions 
      *> Tectonics: cobc -x -debug -lguile-2.0 callbf.cob
      *> ***************************************************************
       identification division.
       program-id. callbf.

       data division.
       working-storage section.
       77 scm-result usage pointer.

      *> ***************************************************************
       procedure division.

       display "OC: initialize libguile" end-display
       call "scm_init_guile"
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       display "OC: compile and load Hello bf code" end-display
       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote & "hello.bf" & quote & "#:from 'brainfuck))" & x"00"
           returning scm-result
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       goback.
       end program callbf.

hello.bf (from http://en.wikipedia.org/wiki/Brainfuck)
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

with a run sample of
$ cobc -x callbf.cob -lguile-2.0
$ ./callbf
OC: initialize libguile
OC: compile and load Hello bf code
Hello World!

One of the most convoluted Hello World samples I've ever encountered. Sweet.

Oh, the new Guile 2 does ECMAScript too.
OCOBOL >>SOURCE FORMAT IS FREE
      *> ***************************************************************
      *> Author:    Brian Tiffin 
      *> Date:      20110704
      *> Purpose:   Demonstrate libguile bf interactions 
      *> Tectonics: cobc -x -lguile-2.0 callbf.cob
      *> ***************************************************************
       identification division.
       program-id. callbf.

       data division.
       working-storage section.
       77 scm-result usage pointer.

      *> ***************************************************************
       procedure division.

       display "OC: initialize libguile" end-display
       call "scm_init_guile"
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       display "OC: compile and load Hello bf code" end-display
       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote & "hello.bf" & quote & "#:from 'brainfuck))" & x"00"
           returning scm-result
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       display "OC: compile and load some ECMAScript" end-display
       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote & "some.js" & quote & "#:from 'ecmascript))" & x"00"
           returning scm-result
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call
       goback.
       end program callbf.

some.js
write([0,1,2,3,4,5].length * 7);

giving
$ cobc -x -debug callbf.cob -lguile-2.0
$ ./callbf
OC: initialize libguile
OC: compile and load Hello bf code
Hello World!
OC: compile and load some ECMAScript
42


And, just to demonstrate the EXCEPTION-LOCATION
OCOBOL >>SOURCE FORMAT IS FREE
      *> ***************************************************************
      *> Author:    Brian Tiffin 
      *> Date:      20110704
      *> Purpose:   Demonstrate libguile bf interactions 
      *> Tectonics: cobc -x -debug -lguile-2.0 callbf.cob
      *> ***************************************************************
       identification division.
       program-id. callbf.

       data division.
       working-storage section.
       77 scm-result usage pointer.

      *> ***************************************************************
       procedure division.

       display "OC: initialize libguile" end-display
       call "scm_init_guile"
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       display "OC: compile and load Hello bf code" end-display
       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote & "hello.bf" & quote & "#:from 'brainfuck))" & x"00"
           returning scm-result
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       display "OC: compile and load some ECMAScript" end-display
       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote & "some.js" & quote & "#:from 'ecmascript))" & x"00"
           returning scm-result
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call.

       demonstrate-error section.
       display x"0a" "OC: compile and load some ECMAScript, and screw up the CALL" end-display
       call "scm_c_eval_stringggggggg"
           using "(load-compiled (compile-file " & quote & "some.js" & quote & "#:from 'ecmascript))" & x"00"
           returning scm-result
           on exception
               display "OC: guile linkage problem: " function exception-location end-display
               stop run
       end-call
       goback.
       end program callbf.

which runs as
$ cobc -x -debug callbf.cob -lguile-2.0
$ ./callbf
OC: initialize libguile
OC: compile and load Hello bf code
Hello World!
OC: compile and load some ECMAScript
42
OC: compile and load some ECMAScript, and screw up the CALL
OC: guile linkage problem: callbf; demonstrate-error; 41
as I misspelled the CALL as scm_c_eval_stringgggg on line 41, inside the demonstrate-error section. The tectonics require -debug for FUNCTION EXCEPTION-LOCATION to have enough information to make it useful.

And for the keen eyed, note how I cheated on the newline after the ECMAScript write of 42. It's 2am and I didn't feel like fighting with escaping a \n escape thingy in the some.js script file

Fun.

And just to un-obfuscate the actual Guile Scheme code in use here... from a Guile REPL prompt...
(load-compiled (compile-file "some.js" #:from 'ecmascript))
42

Again, 2am and I didn't feel like fighting OpenCOBOL quoted strings that include both " and '.

Lots and lots of fun. Really.

Cheers,
Brian
btiffin
Posted on: 2011/7/9 14:22
Webmaster
Joined: 2008/6/7
From: CANADA
Posts: 1196
Re: Guile extension revisited (well, re-read) and name dropping
Bumping my own post, as it is really is a lot of useless fun as I come to grips with the work that may become ./configure --with-guile.

From http://esoteric.sange.fi/brainfuck/bf-source/src-bf/

Prime number display, up to a user entered value

===================================================================
======================== OUTPUT STRING ============================
===================================================================
>++++++++[<++++++++>-]<++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++++.[-]
>++++++++++[<++++++++++>-]<+++++.[-]
>++++++++++[<++++++++++>-]<+++++++++.[-]
>++++++++++[<++++++++++>-]<+.[-]
>++++++++++[<++++++++++>-]<+++++++++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]
>++++++++++[<++++++++++>-]<+++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<+++++++++++.[-]
>+++++++[<+++++++>-]<+++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]

===================================================================
======================== INPUT NUMBER  ============================
===================================================================
+                          cont=1
[
 -                         cont=0
 >,
 ======SUB10======
 ----------
 
 [                         not 10
  <+>                      cont=1
  =====SUB38======
  ----------
  ----------
  ----------
  --------

  >
  =====MUL10=======
  [>+>+<<-]>>[<<+>>-]<     dup

  >>>+++++++++
  [
   <<<
   [>+>+<<-]>>[<<+>>-]<    dup
   [<<+>>-]
   >>-
  ]
  <<<[-]<
  ======RMOVE1======
  <
  [>+<-]
 ]
 <
]
>>[<<+>>-]<<

===================================================================
======================= PROCESS NUMBER  ===========================
===================================================================

==== ==== ==== ====
numd numu teid teiu
==== ==== ==== ====

>+<-
[
 >+
 ======DUP======
 [>+>+<<-]>>[<<+>>-]<

 >+<--

 >>>>>>>>+<<<<<<<<   isprime=1

 [
  >+

  <-

  =====DUP3=====
  <[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<<<

  =====DUP2=====
  >[>>+>+<<<-]>>>[<<<+>>>-]<<< <


  >>>


  ====DIVIDES=======
  [>+>+<<-]>>[<<+>>-]<   DUP i=div
  
  <<
  [
    >>>>>+               bool=1
    <<<
    [>+>+<<-]>>[<<+>>-]< DUP
    [>>[-]<<-]           IF i THEN bool=0
    >>
    [                    IF i=0
      <<<<
      [>+>+<<-]>>[<<+>>-]< i=div
      >>>
      -                  bool=0
    ]
    <<<
    -                    DEC i
    <<
    -
  ]
  
  +>>[<<[-]>>-]<<          
  >[-]<                  CLR div
  =====END DIVIDES====


  [>>>>>>[-]<<<<<<-]     if divides then isprime=0


  <<

  >>[-]>[-]<<<
 ]

 >>>>>>>>
 [
  -
  <<<<<<<[-]<<

  [>>+>+<<<-]>>>[<<<+>>>-]<<<

  >>




  ===================================================================
  ======================== OUTPUT NUMBER  ===========================
  ===================================================================
  [>+<-]>
 
  [
   ======DUP======
   [>+>+<<-]>>[<<+>>-]<
  
  
   ======MOD10====
   >+++++++++<
   [
    >>>+<<              bool= 1
    [>+>[-]<<-]         bool= ten==0
    >[<+>-]             ten = tmp
    >[<<++++++++++>>-]  if ten=0 ten=10
    <<-                 dec ten     
    <-                  dec num
   ]
   +++++++++            num=9
   >[<->-]<             dec num by ten
  
   =======RROT======
      [>+<-]
   <  [>+<-]
   <  [>+<-]
   >>>[<<<+>>>-]
   <
  
   =======DIV10========
   >+++++++++<
   [
    >>>+<<                bool= 1
    [>+>[-]<<-]           bool= ten==0
    >[<+>-]               ten = tmp
    >[<<++++++++++>>>+<-] if ten=0 ten=10  inc div
    <<-                   dec ten     
    <-                    dec num
   ]
   >>>>[<<<<+>>>>-]<<<<   copy div to num
   >[-]<                  clear ten
  
   =======INC1=========
   <+>
  ]
  
  <
  [
   =======MOVER=========
   [>+<-]
  
   =======ADD48========
   +++++++[<+++++++>-]<->
  
   =======PUTC=======
   <.[-]>
  
   ======MOVEL2========
   >[<<+>>-]<
  
   <-
  ]
 
  >++++[<++++++++>-]<.[-]
 
  ===================================================================
  =========================== END FOR ===============================
  ===================================================================


  >>>>>>>
 ]
 <<<<<<<<



 >[-]<
  [-]
 <<-
]
 
======LF========
 
++++++++++.[-]

and called with

OCOBOL >>SOURCE FORMAT IS FREE
      *> ***************************************************************
      *> Author:    Brian Tiffin 
      *> Date:      20110704
      *> Purpose:   Demonstrate libguile bf interactions 
      *> Tectonics: cobc -x -lguile-2.0 callbf.cob
      *> ***************************************************************
       identification division.
       program-id. callbf.

       data division.
       working-storage section.
       77 scm-result usage pointer.

      *> ***************************************************************
       procedure division.

       display "OC: initialize libguile" end-display
       call "scm_init_guile"
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       display "OC: compile and load Hello bf code" end-display
       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote & "hello.bf" & quote & "#:from 'brainfuck))" & x"00"
           returning scm-result
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       display "OC: compile and load prime bf code" end-display
       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote & "prime.bf" & quote & "#:from 'brainfuck))" & x"00"
           returning scm-result
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       goback.
       end program callbf.

with a run sample of (I typed the '50')
$ cobc -x -lguile-2.0 -debug callbf.cob
$ ./callbf
OC: initialize libguile
OC: compile and load Hello bf code
Hello World!
OC: compile and load prime bf code
Primes up to: 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

and a timing exercise with
$ echo '99' | time ./callbf
OC: initialize libguile
OC: compile and load Hello bf code
Hello World!
OC: compile and load prime bf code
Primes up to: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
7.25user 0.01system 0:07.29elapsed 99%CPU (0avgtext+0avgdata 18072maxresident)k
0inputs+40outputs (0major+4657minor)pagefaults 0swaps

I wouldn't want to go much past 99 on my little Acer, as it really started to spin the CPU fan at about 71, busy little bf. ;)

Ok, fine, here's the run to 199 as I was typing this (with the output split to keep the post from going too wide).
$ echo '199' | time -p ./callbf
OC: initialize libguile
OC: compile and load Hello bf code
Hello World!
OC: compile and load prime bf code
Primes up to: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 
real 104.18
user 103.91
sys 0.02
You can't beat waiting 104 seconds to KNOW that 199 is a prime.

And then hey, why not modify hello.bf to include a more appropriate greeting. (with some comments this time, as it really is hard on the brain)
helloOC.bf (modified from the sample at http://en.wikipedia.org/wiki/Brainfuck)
+++++ +++++             initialize counter (cell #0) to 10
[                       use loop to set the next four cells to 70/100/30/10
    > +++++ ++              add  7 to cell #1
    > +++++ +++++           add 10 to cell #2 
    > +++                   add  3 to cell #3
    > +                     add  1 to cell #4
    <<<< -                  decrement counter (cell #0)
]                   
> ++ .                  print 'H'
> + .                   print 'e'
+++++ ++ .              print 'l'
.                       print 'l'
+++ .                   print 'o'
> ++ .                  print ' '
<< +++++ ++ .           print 'O'
> + .                   print 'p'
----- ----- - .         print 'e'
+++++ ++++ .            print 'n'
< ----- ----- -- .      print 'C'
+++++ +++++ ++ .        print 'O'
----- ----- --- .       print 'B'
+++++ +++++ +++ .       print 'O'
--- .                   print 'L'
>> + .                  print '!'
> .                     print '\n'

the OpenCOBOL changes to
OCOBOL >>SOURCE FORMAT IS FREE
      *> ***************************************************************
      *> Author:    Brian Tiffin 
      *> Date:      20110709
      *> Purpose:   Demonstrate libguile bf interactions 
      *> Tectonics: cobc -x -lguile-2.0 callbf.cob
      *> ***************************************************************
       identification division.
       program-id. callbf.

       data division.
       working-storage section.
       77 scm-result usage pointer.

      *> ***************************************************************
       procedure division.

       display "OC: initialize libguile" end-display
       call "scm_init_guile"
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call


       display "OC: compile and load HelloOC bf code" end-display
       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote & "helloOC.bf" & quote & "#:from 'brainfuck))" & x"00"
           returning scm-result
           on exception
               display "guile linkage problem: " function exception-location end-display
               stop run
       end-call

       goback.
       end program callbf.

and then, tada!
$ cobc -x -debug -lguile-2.0 callbf.cob 
$ ./callbf
OC: initialize libguile
OC: compile and load HelloOC bf code
Hello OpenCOBOL!


and because it's hard on the brain, (so worth doing).
+++++ +++++             initialize counter (cell #0) to 10
[                       use loop to set the next six cells to 70/80/100/110/30/10
    > +++++ ++              add  7 to cell #1
    > +++++ +++             add  8 to cell #2
    > +++++ +++++           add 10 to cell #3 
    > +++++ +++++ +         add 11 to cell #4 
    > +++                   add  3 to cell #5
    > +                     add  1 to cell #6
    <<<<<< -                decrement counter (cell #0)
]                   
> ++ .                  print 'H'
>> + .                  print 'e'
> -- .                  print 'l'
.                       print 'l'
+++ .                   print 'o'
> ++ .                  print ' '
<<< - .                 print 'O'
>> + .                  print 'p'
< .                     print 'e'
> -- .                  print 'n'
<<< ----- .             print 'C'
> .                     print 'O'
< - .                   print 'B'
> .                     print 'O'
--- .                   print 'L'
>>> + .                 print '!'
> .                     print '\n'

Which keeps the runtime math (after initializing the first few memory cells) to +/- 5.

And to make it harder on the brain, call a one-liner.
OCOBOL >>SOURCE FORMAT IS FIXED
      *> ***************************************************************
      *> Author:    Brian Tiffin 
      *> Date:      20110709
      *> Purpose:   Demonstrate libguile bf interactions 
      *> Tectonics: cobc -x -lguile-2.0 ocone.cob
      *> ***************************************************************
       identification division.
       program-id. ocone.

      *> ***************************************************************
       procedure division.

       call "scm_init_guile" end-call

       call "scm_c_eval_string"
           using "(load-compiled (compile-file " & quote &
      -          "ocone.bf" & quote & "#:from 'brainfuck))" & x"00"
       end-call

       goback.
       end program ocone.
and ocone.bf
++++++++++[>+++++++>++++++++>++++++++++>+++++++++++>+++>+<<<<<<-]>++.>>+.>--..+++.>++.<<<-.>>+.<.>--.<<<-----.>.<-.>.---.>>>+.>.

as
$ ./ocone
Hello OpenCOBOL!


And here is one for a co-worker.
OCOBOL >>SOURCE FORMAT IS FIXED
      *> ***************************************************************
      *> Author:    Brian Tiffin 
      *> Date:      20110720
      *> Purpose:   Demonstrate libguile bf interactions 
      *> Tectonics: cobc -x -lguile-2.0 derps.cob
      *> ***************************************************************
       identification division.
       program-id. derps.

       data division.
       working-storage section.
       77 derps pic x(124) value
           "+++++ +++++ [ > +++++ ++ > +++++ +++ > + <<< - ] >" &
      -    " -- .  + .  > ++ .  -- .  > .  > , [ <<< - . + .  " &
      -    "> ++ .  -- .  > .  > - ]".

       77 scm-result usage pointer.

      *> ***************************************************************
       procedure division.

       call "scm_init_guile" end-call

      *> eval-string in bf context, res is unspecified
       call "scm_c_eval_string"
           using function concatenate(
               "(use-modules (ice-9 eval-string))"
               '(define derps "' derps '")'
               "(define res (eval-string derps #:lang 'brainfuck))"
               low-value)
           returning scm-result
           on exception display function exception-location end-display
       end-call
     
       goback.
       end program derps.

which, prompts with DERP and prints DERP ASCII value times. (Control-K being 12).
$ ./derps 
DERP
^K
DERP
DERP
DERP
DERP
DERP
DERP
DERP
DERP
DERP
DERP
DERP


Cheers,
Brian

Threaded | Newest First Previous Topic | Next Topic | Top

Register To Post
 
Copyright (C) 2005 The OpenCOBOL Project. All rights reserved.
Powered by Xoops2 | PHP | MySQL | Apache
ocean-net