Author Topic: TinyScheme  (Read 63630 times)

0 Members and 3 Guests are viewing this topic.

Charles Pegge

  • Guest
Re: TinyScheme
« Reply #60 on: August 29, 2014, 12:05:36 AM »

To return a string trimmed to the right length, you will have to create an SB string copy of the buffer, to the required length. (excluding null terminator for SB strings)




RobbeK

  • Guest
Re: TinyScheme
« Reply #61 on: August 29, 2014, 12:41:17 AM »
Hi John,

Is there any interest in using TinyScheme embedded in SB or are you happy running it as a dedicated Lisp interpreter?

Well, I try to do everything in Lisp, but whatever they tell , for top speed calculus it's better to use something as Oxygen , FB , C etc...  In BigLoo the variadic nature of arithm. operators can be turned off   , p.e. an addition will then take only two arguments.
But it will turn it off for the complete code , a combination (Lisp + C O² ...)  is best I think --
The main problem I had was when using routines that need pointers (as int* p.e.) , but in CL I set them up with Quicklisp now (the cffi) and bigloo has them onboard.  (without these pointers, i will have to do up to million of calls to Oxygen p.e.  , now both Lisp and pe Oxygen share this address and memory can be allocated by Lisp )

best Rob
Didn't do any timing , the exact calculation in CLisp took about a sec. ,  all the others (except BL) did it immediatly.


JRS

  • Guest
Re: TinyScheme
« Reply #62 on: August 29, 2014, 01:06:51 AM »
Quote
To return a string trimmed to the right length, you will have to create an SB string copy of the buffer, to the required length. (excluding null terminator for SB strings)

Too Lazy.

Code: [Select]
besFUNCTION(TS_load_string) 
  DIM AS scheme PTR sc;
  DIM AS char PTR cmdstr;
  DIM AS char tsbuf[16384];
  besARGUMENTS("iz")
    AT sc, AT cmdstr
  besARGEND
  scheme_set_output_port_string (sc, (char*) tsbuf, (char*) tsbuf + 16383);
  scheme_load_string(sc, cmdstr); 
  besRETURN_STRING(rtrim(tsbuf));
besEND

Code: [Select]
' Tiny Scheme - extension module example

DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"

sc = InitNew()
LoadStr(sc, "(load \"init.scm\")")
PRINT LoadStr(sc, "(load \"mbrot2.scm\")"),"\n"
Deinit sc

Output

Code: [Select]
jrs@laptop:~/sb/sb22/sblisp$ time scriba tsmbrot.sb


Ascii Mandelbrot TinyScheme
---------------------------
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2
1 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 115 4 4 3 3 2 2 2
1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 5 7 9 114 4 3 3 2 2
1 1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 9 118 5 4 4 3 3 3
1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 8 1111116 5 5 4 3 3
1 1 1 1 1 2 3 3 3 3 3 3 3 4 4 4 5 7 8 8 101111119 6 6 5 4 3
1 1 1 1 2 3 3 3 3 3 3 3 4 4 5 5 6 11111111111111111111114 3
1 1 1 1 2 3 3 3 3 3 4 5 5 5 5 6 8 111111111111111111117 5 3
1 1 1 1 3 3 3 3 4 5 7 7 7 7 7 7 11111111111111111111119 5 4
1 1 1 1 3 4 4 4 5 5 7 111111119 1111111111111111111111116 4
1 1 1 1 4 4 4 5 5 6 8 11111111111111111111111111111111115 4
1 1 1 1 4 4 6 6 7 1111111111111111111111111111111111118 5 4
1 1 1 1111111111111111111111111111111111111111111111117 5 4
1 1 1 1 4 4 6 6 7 1111111111111111111111111111111111118 5 4
1 1 1 1 4 4 4 5 5 6 8 11111111111111111111111111111111115 4
1 1 1 1 3 4 4 4 5 5 7 111111119 1111111111111111111111116 4
1 1 1 1 3 3 3 3 4 5 7 7 7 7 7 7 11111111111111111111119 5 4
1 1 1 1 2 3 3 3 3 3 4 5 5 5 5 6 8 111111111111111111117 5 3
1 1 1 1 2 3 3 3 3 3 3 3 4 4 5 5 6 11111111111111111111114 3
1 1 1 1 1 2 3 3 3 3 3 3 3 4 4 4 5 7 8 8 101111119 6 6 5 4 3
1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 8 1111116 5 5 4 3 3
1 1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 9 118 5 4 4 3 3 3
1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 5 7 9 114 4 3 3 2 2
1 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 115 4 4 3 3 2 2 2
1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2

real 0m0.661s
user 0m0.656s
sys 0m0.000s
jrs@laptop:~/sb/sb22/sblisp$

SB embedded TinyScheme features:
  • Single function interface
  • Multi-platform
  • Multi-threaded - Windows via DLLC
  • Embed Sheme scripts in your BASIC code as multi-line strings.
  • Seamless and Fast!

I'm happy!
« Last Edit: August 29, 2014, 02:17:06 AM by John »

JRS

  • Guest
Re: TinyScheme
« Reply #63 on: August 29, 2014, 02:04:15 AM »
Quote from: Rob
Well, I try to do everything in Lisp, but whatever they tell , for top speed calculus it's better to use something as Oxygen , FB , C etc...

Who will be the first to add the 3 needed TinySceme functions to O2 for a BASIC compiler with an embedded Sheme scripting engine?

I thought BASIC was suppose to make stuff easy. Personally I can't type worth a shit so using a language that requires drastically less code and gives me the same results makes sense to me.

RobbeK

  • Guest
Re: TinyScheme
« Reply #64 on: August 29, 2014, 03:05:47 AM »
Hi John,

Yes, but LISP can set up any non-cyclic nested data structures of any dimension - perfect for CAD systems and Graph. Editors like GIMP , it can start mutate itself because it makes no difference between data and code :

from NewLisp :
(println my-program-as-a-string)    ;; prints your code
(eval-string my-program-as-a-string) ;; runs the code

you can write programs mutating themselves , save the mutant and run the mutant -- in the mean time, we can smoke the cigarette and drink the whiskey ...  8)

If you need some teasers for TS -- quickly written :   the reward of the King (story goes when the King asked the inventor of the game of chess about his price ,he answered to start with one grain and double it every next field (rice / corn .. etc ).  The King answered, is that all ?! -- however some calculations learn the amount is immense .....
2 things in the code :   
the named let  --  how to do "pseudo" iterations (the use name "loop" is just another name -- run , do-it etc.. also will do)
how to mark the number is inexact  :  use 1.0 i.o. 1  (otherwise it will run out of capable numbers)

(define (grains x)
   (let loop ((i 1) (j 1.0))
     (display " field ") (display i)
     (display "  number of grains   ")(display j)  (newline)
     (when (< i x) (loop (+ i 1) (* 2 j) ))))

(define (main)
   (display "The reward of the King") (newline)
   (display "----------------------") (newline)
   (newline)
   (grains 64))

------------------------------------------------------------------------------------output

The reward of the King
----------------------

 field 1  number of grains   1
 field 2  number of grains   2
 field 3  number of grains   4
 field 4  number of grains   8
 field 5  number of grains   16
 field 6  number of grains   32
 field 7  number of grains   64
 field 8  number of grains   128
 field 9  number of grains   256
 field 10  number of grains   512
 field 11  number of grains   1024
 field 12  number of grains   2048
 field 13  number of grains   4096
 field 14  number of grains   8192
 field 15  number of grains   16384
 field 16  number of grains   32768
 field 17  number of grains   65536
 field 18  number of grains   131072
 field 19  number of grains   262144
 field 20  number of grains   524288
 field 21  number of grains   1048576
 field 22  number of grains   2097152
 field 23  number of grains   4194304
 field 24  number of grains   8388608
 field 25  number of grains   16777216
 field 26  number of grains   33554432
 field 27  number of grains   67108864
 field 28  number of grains   134217728
 field 29  number of grains   268435456
 field 30  number of grains   536870912
 field 31  number of grains   1073741824
 field 32  number of grains   2147483648
 field 33  number of grains   4294967296
 field 34  number of grains   8589934592
 field 35  number of grains   1.717986918e+010
 field 36  number of grains   3.435973837e+010
 field 37  number of grains   6.871947674e+010
 field 38  number of grains   1.374389535e+011
 field 39  number of grains   2.748779069e+011
 field 40  number of grains   5.497558139e+011
 field 41  number of grains   1.099511628e+012
 field 42  number of grains   2.199023256e+012
 field 43  number of grains   4.398046511e+012
 field 44  number of grains   8.796093022e+012
 field 45  number of grains   1.759218604e+013
 field 46  number of grains   3.518437209e+013
 field 47  number of grains   7.036874418e+013
 field 48  number of grains   1.407374884e+014
 field 49  number of grains   2.814749767e+014
 field 50  number of grains   5.629499534e+014
 field 51  number of grains   1.125899907e+015
 field 52  number of grains   2.251799814e+015
 field 53  number of grains   4.503599627e+015
 field 54  number of grains   9.007199255e+015
 field 55  number of grains   1.801439851e+016
 field 56  number of grains   3.602879702e+016
 field 57  number of grains   7.205759404e+016
 field 58  number of grains   1.441151881e+017
 field 59  number of grains   2.882303762e+017
 field 60  number of grains   5.764607523e+017
 field 61  number of grains   1.152921505e+018
 field 62  number of grains   2.305843009e+018
 field 63  number of grains   4.611686018e+018
 field 64  number of grains   9.223372037e+018

best Rob
« Last Edit: August 29, 2014, 03:13:13 AM by RobbeK »

Mike Lobanovsky

  • Guest
Re: TinyScheme
« Reply #65 on: August 29, 2014, 05:17:25 AM »
Hi Rob,

Quote from: Rob
you can write programs mutating themselves , save the mutant and run the mutant -- in the mean time, we can smoke the cigarette and drink the whiskey ...

Actually this isn't a LISP-only prerogative. Any interpreter that has its own evaluator function, e.g. ExecString() in FBSL, can do exactly the same. This functionality is inherent in the concept of code interpretation. It effectively means that every interpreter developer is a potential alcoholic and tobacco smoker by definition.

 8)

JRS

  • Guest
Re: TinyScheme
« Reply #66 on: August 29, 2014, 04:43:28 PM »
Rob,

I'm assuming at this point the TinyScheme extension module interface for Script BASIC is code complete. Is there anything you can think of I might of missed that needs to be included? I understand that SB & the TS ext. mod. might not be enough for the type of stuff you do in Lisp. You really should look at O2 and TS as a possible solution. I don't see that connection taking much effort.

John

JRS

  • Guest
Re: TinyScheme
« Reply #67 on: August 29, 2014, 05:34:30 PM »
 I thought I would try your Kings Reward example using the new TinyScheme extension module.

Code: [Select]
' King's Reward
 
IMPORT ts.inc
 
scm = TS_New()
TS_Cmd scm, "(load \"init.scm\")"
reward = """
(define (grains x)
  (let loop ((i 1) (j 1.0))
    (display " field ") (display i)
    (display "  number of grains   ")(display j)  (newline)
    (when (< i x) (loop (+ i 1) (* 2 j) ))))
 
(define (main)
  (display "The reward of the King") (newline)
  (display "----------------------") (newline)
  (newline)
  (grains 64))
(main) 
""" 
PRINT TS_Cmd(scm, reward)
TS_Close scm

Output

jrs@laptop:~/sb/sb22/sblisp/Rob$ time scriba king.sb
The reward of the King
----------------------

 field 1  number of grains   1.0
 field 2  number of grains   2.0
 field 3  number of grains   4.0
...
 field 62  number of grains   2.305843009e+18
 field 63  number of grains   4.611686018e+18
 field 64  number of grains   9.223372037e+18

real   0m0.039s
user   0m0.028s
sys   0m0.008s
jrs@laptop:~/sb/sb22/sblisp/Rob$
« Last Edit: August 31, 2014, 07:59:38 AM by John »

JRS

  • Guest
Re: TinyScheme
« Reply #68 on: August 29, 2014, 10:19:55 PM »
Here is the source to the Script BASIC TinyScheme extension module. (using C BASIC)

interface.c
Code: [Select]
// Tiny Scheme - Script BASIC extension module

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include "../../basext.h"
#include "cbasic.h"

#define USE_INTERFACE 1

#include "scheme.h"
#include "scheme-private.h"


/****************************
 Extension Module Functions
****************************/

besVERSION_NEGOTIATE
  RETURN_FUNCTION((int)INTERFACE_VERSION);
besEND

besSUB_START
  DIM AS long PTR p;
  besMODULEPOINTER = besALLOC(sizeof(long));
  IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  p = (long PTR)besMODULEPOINTER;
  RETURN_FUNCTION(0);
besEND

besSUB_FINISH
  DIM AS long PTR p;
  p = (long PTR)besMODULEPOINTER;
  IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  RETURN_FUNCTION(0);
besEND


/***********************
 Tiny Scheme Functions
***********************/

besFUNCTION(TS_init_new)
  DIM AS scheme PTR sc;
  sc = scheme_init_new();
  besRETURN_LONG(sc);
besEND

besFUNCTION(TS_deinit)
  DIM AS scheme PTR sc;
  besARGUMENTS("i")
    AT sc
  besARGEND
  scheme_deinit(sc);
  besRETURNVALUE = NULL;
besEND

besFUNCTION(TS_load_string)
  DIM AS scheme PTR sc;
  DIM AS char PTR cmdstr;
  DIM AS char tsbuf[16384];
  besARGUMENTS("iz")
    AT sc, AT cmdstr
  besARGEND
  scheme_set_output_port_string (sc, (char PTR) tsbuf, (char PTR) tsbuf + 16383);
  scheme_load_string(sc, cmdstr);
  besRETURN_STRING(RTRIM(tsbuf));
besEND

ts.inc
Code: [Select]
' TinyScheme

DECLARE SUB TS_New ALIAS "TS_init_new" LIB "ts"
DECLARE SUB TS_Close ALIAS "TS_deinit" LIB "ts"
DECLARE SUB TS_Cmd ALIAS "TS_load_string" LIB "ts"
« Last Edit: August 29, 2014, 10:39:03 PM by John »

JRS

  • Guest
Re: TinyScheme
« Reply #69 on: August 29, 2014, 11:24:41 PM »
Here is The Kings Reward in SB. Call me stupid but the SB code seems to make more sense to me. At least I'm not slow as well.

Code: [Select]
' Kings Reward

grains = 1
PRINT "The reward of the King\n----------------------\n\n"
FOR field = 1 to 64
  PRINT FORMAT("field %g number of grains %d\n", field, grains)
  grains = grains * 2
NEXT

Output

jrs@laptop:~/sb/sb22/sblisp/Rob$ time scriba sbking
The reward of the King
----------------------

field 1 number of grains 1
field 2 number of grains 2
field 3 number of grains 4
...
field 62 number of grains 2305843009213693952
field 63 number of grains 4611686018427387904
field 64 number of grains -9223372036854775808

real   0m0.006s
user   0m0.000s
sys   0m0.004s
jrs@laptop:~/sb/sb22/sblisp/Rob$
« Last Edit: August 31, 2014, 08:00:29 AM by John »

JRS

  • Guest
Re: TinyScheme
« Reply #70 on: August 29, 2014, 11:45:12 PM »
Quote from: Rob
how to mark the number is inexact  :  use 1.0 i.o. 1  (otherwise it will run out of capable numbers)

Without Rob's 32 bit hack.

Code: [Select]
' King's Reward

DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"

sc = InitNew()
LoadStr(sc, "(load \"init.scm\")")
reward = """
(define (grains x)
   (let loop ((i 1) (j 1))
     (display " field ") (display i)
     (display "  number of grains   ")(display j)  (newline)
     (when (< i x) (loop (+ i 1) (* 2 j) ))))

(define (main)
   (display "The reward of the King") (newline)
   (display "----------------------") (newline)
   (newline)
   (grains 64))
(main)   
"""   
PRINT LoadStr(sc, reward)
Deinit sc

Output

jrs@laptop:~/sb/sb22/sblisp/Rob$ time scriba king.sb
The reward of the King
----------------------

 field 1  number of grains   1
 field 2  number of grains   2
 field 3  number of grains   4
...
 field 62  number of grains   2305843009213693952
 field 63  number of grains   4611686018427387904
 field 64  number of grains   -9223372036854775808

real   0m0.042s
user   0m0.040s
sys   0m0.000s
jrs@laptop:~/sb/sb22/sblisp/Rob$

@Rob - Would it be worth my time to generate a 64 bit version of this (TinyScheme) for Windows 64 bit?
« Last Edit: August 31, 2014, 08:01:21 AM by John »

RobbeK

  • Guest
Re: TinyScheme
« Reply #71 on: August 30, 2014, 02:13:32 AM »
Hi John,

64 bit, yes I think so -- is it a lot of labour ??

the universal Julia , including Mandelbrot

---------------------------------------------------

(define (julia-primitive i x y rc ic maxit orb)
   (if (or (= i maxit) (> orb 4.0))
       (list i orb)
       (julia-primitive (+ i 1)
                        (+ rc (- (* x x) (* y y)))
                        (+ ic (* 2 x y))
                        rc ic maxit (+ (* x x) (* y y)))))

(define (julia x y rc ic maxit)
  (car (julia-primitive 0 x y rc ic maxit 0)))

(define (mandelbrot x y maxit)
  (car (julia-primitive 0 x y x y maxit 0)))

 
(define (juliaM x y rc ic maxit)
  (julia-primitive 0 x y rc ic maxit 0))

(define (mandelbrotM x y maxit)
  (julia-primitive 0 x y x y maxit 0))

-------------------------------------------------------------

be careful , this does more than those found in other languages :
the JuliaM and MandbrotM give back both the iteration level and the orbit in a list (iteration orb).
p.e.
(define result (mandelbrotM x y maxit))
then
(car result) -> iteration level
(cadr result) -> orbit level

this gives more possibilities for research (color schemes etc ...  )

the (Julia ... )  and (Mandelbrot ... ) just give back the iteration level, which is a number

best Rob

JRS

  • Guest
Re: TinyScheme
« Reply #72 on: August 30, 2014, 08:26:44 AM »
Thanks Rob for the iterator +.

I'll give the TinyScheme Windows 64 bit library compile a try. I already have SB 64 bit Windows working. (for some time now)

I would like to work with you on using SDL GFX and the TS engine to create some examples. 

Quote
A worker in southern China was left hanging from 100 feet up the side of a high-rise apartment building when a 10-year-old boy, apparently annoyed at the construction racket outside his window, decided to cut the safety line on the man's rappelling apparatus.

Xinhua says the boy was watching cartoons in his eighth-floor apartment in Guizhou province as the worker was outside installing lighting. So, the boy took a knife and sliced through the rope that allows the worker to move up and down.

According to an English translation of the Xinhua article on the Shanghaiist website, the worker was left dangling midair. He yelled down to a co-worker, who called firemen; he was rescued about 40 minutes later.

I also take funny seriously.

« Last Edit: August 30, 2014, 03:04:34 PM by John »

JRS

  • Guest
Re: TinyScheme
« Reply #73 on: August 30, 2014, 02:44:56 PM »
No luck on the 64 bit TinyScheme for Windows. I tried both MinGW and VC12 (VS2013) on Windows 7 64 bit. I'll keep chipping away at it as time permits.

Any interest in using Linux 64 bit?  8)

« Last Edit: August 30, 2014, 04:06:10 PM by John »

JRS

  • Guest
Re: TinyScheme
« Reply #74 on: August 30, 2014, 04:45:41 PM »
Rob,

I gave your Mandelbrot iterator a try and after about 15 min. of waiting, I killed the task.  :-\

I guess creating a 16K transfer buffer than  trimming it back to a couple characters per iteration was a bit much. I'm thinking of adding a 3rd optional argument to TS_Cmd() to declare the buffer size you want to use.

Not a good day on the TS front.  :-[

@Anyone: I'm trying to define a character array in C conditionally based on user input. C doesn't seem to like conditional declarations. Any ideas how this might be done?

« Last Edit: August 30, 2014, 06:14:15 PM by John »