Author Topic: TinyScheme Extension Module  (Read 18706 times)

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
TinyScheme Extension Module
« on: August 27, 2014, 11:38:58 PM »
I thought I would post an update to where things are with the Lisp in BASIC adventure. Mike has taken over with the QB converted Lisp in BASIC and is primarily developing it for FBSL and possibly OxygenBasic. My interest in the project was to see how compatible Script BASIC was with QB 4.5 and as a possible teaching aid. It seemed to be too much work to convert it to C when there are already more complete and faster implementations of Scheme. I'm using TinyScheme which is the scripting language for GIMP and easily embedded in SB. I have a C BASIC TinyScheme extension working on Ubuntu 64 and decided to use DLLC under Windows to take advantage of the multi-threading feature of the DLLC interface. The following example loads/calls a ASCII Mandelbrot Scheme script Rob wrote on the O2 forum and returns the results in a SB predefine buffer string.

Teach Yourself Scheme in Fixnum Days

Code: Scheme
  1. (newline)
  2. (newline)
  3. (display "Ascii Mandelbrot TinyScheme") (newline)
  4. (display "---------------------------") (newline)
  5.  
  6. (define sq
  7.    (lambda (x) (* x x)))
  8.  
  9. (define (1+ x) (+ x 1))
  10. (define (1- x) (- x 1))
  11.  
  12. (define level
  13.   (lambda (i x y rc ic it orb)
  14.    (if (or (= i it) (> orb 4)) i
  15.     (level (1+ i) (+ rc (- (sq x) (sq y))) (+ ic (* 2 x y)) rc ic it (+ (sq x) (sq y))))))
  16.  
  17. (define mlevel
  18.    (lambda (L)
  19.      (level 0 (cadr L) (car L) (cadr L) (car L) 11 0)))
  20.  
  21. (define (main)
  22.    (let ((cnt 0) (lvl 0) (xo -1.7) (yo -2.3) (dz 0.1) )
  23.      (do ((i 0 (1+ i)))
  24.          ((= i 30))
  25.         (do ((j 0 (1+ j)))
  26.             ((= 30 j))
  27.               (set! lvl (mlevel (list (+ xo (* i dz)) (+ yo (* j dz)) )))
  28.               (if (< lvl 10)
  29.                    (begin (display lvl) (display " "))
  30.                    (display lvl))
  31.               (set! cnt (1+ cnt))
  32.               (when (= 30 cnt)
  33.                  (set! cnt 0)
  34.                  (newline))
  35. ))))
  36.  
  37. (main)
  38. (quit)
  39.  

Code: Script BASIC
  1. ' TinyScheme Script BASIC DLLC Example
  2.  
  3. DECLARE SUB dllfile ALIAS "dllfile" LIB "dllc"
  4. DECLARE SUB dllproc ALIAS "dllproc" LIB "dllc"
  5. DECLARE SUB dllcall ALIAS "dllcall" LIB "dllc"
  6. DECLARE SUB dllsptr ALIAS "dllsptr" LIB "dllc"
  7.  
  8. ts = dllfile("libtinyscheme.dll")
  9. InitNew = dllproc(ts, "scheme_init_new i = ()")
  10. RtnStr = dllproc(ts, "scheme_set_output_port_string (i sc, i startptr, i endptr)")
  11. Deinit = dllproc(ts, "scheme_deinit (i sc)")
  12. LoadStr = dllproc(ts, "scheme_load_string (i sc, c *cmd)")
  13.  
  14. sc = dllcall(InitNew)
  15. lispbuf = STRING(4096,CHR(0))
  16. lispstrptr = dllsptr(lispbuf)
  17. lispstrend = lispstrptr + 4096 - 1
  18. dllcall(RtnStr,sc, lispstrptr, lispstrend)
  19. dllcall(LoadStr, sc, "(load \"init.scm\")")
  20. dllcall(LoadStr, sc, "(load \"mbrot2.scm\")")
  21. strlen = INSTR(lispbuf, CHR(0))
  22. PRINT LEFT(lispbuf, strlen - 1),"\n"
  23. dllcall(Deinit, sc)
  24.  
  25. dllfile()
  26.  

Output

Code: [Select]
C:\SB22\TS>ptime scriba dllchellots.sb

ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>

=== scriba dllchellots.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


Execution time: 1.086 s

C:\SB22\TS>
« Last Edit: August 29, 2014, 09:57:28 PM by support »
Script BASIC Project Manager

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: TinyScheme Extension Module
« Reply #1 on: August 28, 2014, 01:35:41 AM »
Here is a primes < 5000  example of passing a Scheme script as a string rather than loading it from a file.  8)

Code: Script BASIC
  1. ' TinyScheme Script BASIC DLLC Example
  2.  
  3. DECLARE SUB dllfile ALIAS "dllfile" LIB "dllc"
  4. DECLARE SUB dllproc ALIAS "dllproc" LIB "dllc"
  5. DECLARE SUB dllcall ALIAS "dllcall" LIB "dllc"
  6. DECLARE SUB dllsptr ALIAS "dllsptr" LIB "dllc"
  7.  
  8. ts = dllfile("libtinyscheme.dll")
  9. InitNew = dllproc(ts, "scheme_init_new i = ()")
  10. RtnStr = dllproc(ts, "scheme_set_output_port_string (i sc, i startptr, i endptr)")
  11. Deinit = dllproc(ts, "scheme_deinit (i sc)")
  12. LoadStr = dllproc(ts, "scheme_load_string (i sc, c *cmd)")
  13.  
  14. sc = dllcall(InitNew)
  15. lispbuf = string(4096,CHR(0))
  16. lispstrptr = dllsptr(lispbuf)
  17. lispstrend = lispstrptr + 4096 - 1
  18. dllcall(RtnStr,sc, lispstrptr, lispstrend)
  19. dllcall(LoadStr, sc, "(load \"init.scm\")")
  20. tscm = """
  21. (newline)
  22. (display "start")
  23. (newline)
  24.  
  25.  
  26. (define (seq x)
  27.   (let ((M '()))
  28.     (do ((i 0 (+ i 1)))
  29.         ((> i x))
  30.         (set! M (cons i M))) M ))
  31.  
  32. (define numbers (reverse (seq 4999)))
  33.  
  34.  
  35.  
  36. (define (sieve L)
  37.   (let ( ( len (- (length L) 2)) (vec (list->vector L)) )
  38.     (do ((i 2 (+ i 1)))
  39.         ((> i (floor (/ len 2))))
  40.        
  41.         (when (> (vector-ref vec i) 0)
  42.            (do ((j 2 (+ j 1)))
  43.                ((> (* i j) len))
  44.              
  45.                (vector-set! vec (* i j) 0)))) vec ))
  46.  
  47. (define (print-vec V)
  48.   (let ((len (vector-length V)) (cnt 0) )
  49.     (do (( i 2 (+ 1 i)))
  50.         (( = i (- len 1)))
  51.         (let (( item (vector-ref V i)))
  52.           (when (not (= item 0))
  53.              (set! cnt (+ 1 cnt))
  54.              (display item)
  55.              (display " ")))) cnt ))
  56.  
  57. (define (main)
  58.  (let ((cnt 0))
  59.   (newline)
  60.   (display "calculating prime-numbers < 5000")  (newline)
  61.   (display "--------------------------------")  (newline)
  62.   (set! cnt (print-vec (sieve numbers)))
  63.   (newline)
  64.   (display "that's it")
  65.   (newline)
  66.    (display cnt) (display "  prime numbers found") ))
  67.  
  68.  
  69. (main)  
  70. (quit)
  71. """
  72.  
  73. dllcall(LoadStr, sc, tscm)
  74. strlen = INSTR(lispbuf, CHR(0))
  75. PRINT LEFT(lispbuf, strlen - 1),"\n"
  76. dllcall(Deinit, sc)
  77.  
  78. dllfile()
  79.  

Output

C:\SB22\TS>ptime scriba tsprimes.sb

ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>

=== scriba tsprimes.sb ===

start

calculating prime-numbers < 5000
--------------------------------
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 16
7 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349
 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547
557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 7
51 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 97
1 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 11
51 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 13
19 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 15
11 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 16
97 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 18
89 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 20
87 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 22
87 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 24
67 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 26
87 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 28
57 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 30
79 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 33
07 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 35
11 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 36
91 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 39
07 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 40
99 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 42
97 4327 4337 4339 4349 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513 4517 4519 45
23 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 47
51 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 49
69 4973 4987 4993
that's it
668  prime numbers found

Execution time: 4.855 s

C:\SB22\TS>

Script BASIC Project Manager

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
TinyScheme - King's Reward
« Reply #2 on: August 29, 2014, 07:15:25 PM »
Quote from: Rob
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)

Script BASIC and the TinyScheme extension module are 64 bit in my example. Rob's 32 bit overflow hack he used on his PC code was removed.

Code: Script BASIC
  1. ' King's Reward
  2.  
  3. DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
  4. DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
  5. DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"
  6.  
  7. sc = InitNew()
  8. LoadStr(sc, "(load \"init.scm\")")
  9. reward = """
  10. (define (grains x)
  11.   (let loop ((i 1) (j 1))
  12.     (display " field ") (display i)
  13.     (display "  number of grains   ")(display j)  (newline)
  14.     (when (< i x) (loop (+ i 1) (* 2 j) ))))
  15.  
  16. (define (main)
  17.   (display "The reward of the King") (newline)
  18.   (display "----------------------") (newline)
  19.   (newline)
  20.   (grains 64))
  21. (main)  
  22. """  
  23. PRINT LoadStr(sc, reward)
  24. Deinit sc
  25.  

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 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   17179869184
 field 36  number of grains   34359738368
 field 37  number of grains   68719476736
 field 38  number of grains   137438953472
 field 39  number of grains   274877906944
 field 40  number of grains   549755813888
 field 41  number of grains   1099511627776
 field 42  number of grains   2199023255552
 field 43  number of grains   4398046511104
 field 44  number of grains   8796093022208
 field 45  number of grains   17592186044416
 field 46  number of grains   35184372088832
 field 47  number of grains   70368744177664
 field 48  number of grains   140737488355328
 field 49  number of grains   281474976710656
 field 50  number of grains   562949953421312
 field 51  number of grains   1125899906842624
 field 52  number of grains   2251799813685248
 field 53  number of grains   4503599627370496
 field 54  number of grains   9007199254740992
 field 55  number of grains   18014398509481984
 field 56  number of grains   36028797018963968
 field 57  number of grains   72057594037927936
 field 58  number of grains   144115188075855872
 field 59  number of grains   288230376151711744
 field 60  number of grains   576460752303423488
 field 61  number of grains   1152921504606846976
 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$
« Last Edit: August 30, 2014, 02:16:38 AM by support »
Script BASIC Project Manager

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: TinyScheme Extension Module
« Reply #3 on: August 29, 2014, 11:29:52 PM »
Here is the source to the Script BASIC TinyScheme extension module written in C BASIC. (my BASIC wrapper for C)

interface.c
Code: C
  1. // Tiny Scheme - Script BASIC extension module
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <math.h>
  8. #include <time.h>
  9. #include "../../basext.h"
  10. #include "cbasic.h"
  11.  
  12. #define USE_INTERFACE 1
  13.  
  14. #include "scheme.h"
  15. #include "scheme-private.h"
  16.  
  17.  
  18. /****************************
  19.  Extension Module Functions
  20. ****************************/
  21.  
  22. besVERSION_NEGOTIATE
  23.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  24. besEND
  25.  
  26. besSUB_START
  27.   DIM AS long PTR p;
  28.   besMODULEPOINTER = besALLOC(sizeof(long));
  29.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  30.   p = (long PTR)besMODULEPOINTER;
  31.   RETURN_FUNCTION(0);
  32. besEND
  33.  
  34. besSUB_FINISH
  35.   DIM AS long PTR p;
  36.   p = (long PTR)besMODULEPOINTER;
  37.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  38.   RETURN_FUNCTION(0);
  39. besEND
  40.  
  41.  
  42. /***********************
  43.  Tiny Scheme Functions
  44. ***********************/
  45.  
  46. besFUNCTION(TS_init_new)
  47.   DIM AS scheme PTR sc;
  48.   sc = scheme_init_new();
  49.   besRETURN_LONG(sc);
  50. besEND
  51.  
  52. besFUNCTION(TS_deinit)
  53.   DIM AS scheme PTR sc;
  54.   besARGUMENTS("i")
  55.     AT sc
  56.   besARGEND
  57.   scheme_deinit(sc);
  58.   besRETURNVALUE = NULL;
  59. besEND
  60.  
  61. besFUNCTION(TS_load_string)
  62.   DIM AS scheme PTR sc;
  63.   DIM AS char PTR cmdstr;
  64.   DIM AS char tsbuf[16384];
  65.   besARGUMENTS("iz")
  66.     AT sc, AT cmdstr
  67.   besARGEND
  68.   scheme_set_output_port_string (sc, (char PTR) tsbuf, (char PTR) tsbuf + 16383);
  69.   scheme_load_string(sc, cmdstr);
  70.   besRETURN_STRING(RTRIM(tsbuf));
  71. besEND
  72.  

ts.inc
Code: Script BASIC
  1. ' TinyScheme
  2.  
  3. DECLARE SUB TS_New ALIAS "TS_init_new" LIB "ts"
  4. DECLARE SUB TS_Close ALIAS "TS_deinit" LIB "ts"
  5. DECLARE SUB TS_Cmd ALIAS "TS_load_string" LIB "ts"
  6.  
« Last Edit: September 01, 2014, 07:24:12 PM by support »
Script BASIC Project Manager

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: TinyScheme Extension Module
« Reply #4 on: September 01, 2014, 07:23:08 PM »
I found a github repository for TinyScheme (fork) that has fixed some issues and included the RE (regular expression) and TSX (TinySchmeme Extensions) extension modules. I couldn't get RE to work so it isn't included in the attached zip. If you could give this a test, that would be great. The tinyscheme.exe was compiled with VS2008 (VC9) and the schme.exe and the libtinyscheme.dll were compiled with TDM-GCC-32. The Script BASIC TinyScheme extension module for Windows 32 seems to work fine with the new libtinyscheme.dll.

  • based on TinyScheme 1.41
  • build with MS Visual C++ on Windows
  • continuations support without using 'SCHEME STACK' (#undef USE_SCHEME_STACK)
  • embedding 'init.scm' into tinyscheme.exe (or tinyscheme.lib)
  • fixed crash in GC after getting read-char 'EOF'
  • implement 'string->uninterned-symbol' and 'gensym' gets new uninterned-symbol
  • include TinyScheme Extensions (TSX)
  • include TinyScheme RE extension

Code: Scheme
  1. ts> (load-extension "tsx")
  2. #t
  3. ts> (time)
  4. (114 8 1 17 59 44)
  5. ts> (gettimeofday)
  6. (1409619622 865397)
  7. ts> (file-size "init.scm")
  8. 24511
  9. ts> (system "ls -l init.scm")
  10. -rw-rw-r-- 1 jrs jrs 24511 Sep  1 16:45 init.scm
  11. 0
  12. ts>
  13.  

Code: [Select]
TinyScheme Extensions (TSX) 1.1  [September, 2002]
(c) 2002 Manuel Heras-Gilsanz (manuel@heras-gilsanz.com)

This software is subject to the license terms contained in the
LICENSE file.


TSX FUNCTIONS

TSX incorporates the following functions:

*Sockets (included if HAVE_SOCKETS is defined in tsx.h)

(make-client-socket host port)
        host: string (IP address or host name)
        port: integer number

        Returns a socket which is already connected to the
        specified host and port, or #f if the connection could
        not be performed.

(make-server-socket port)
        port: integer number

        Returns a socket which is bound to the specified port on
        the local machine, and ready to accept connections. If the
        socket could not be created (e.g., because the port is
        already in use, or it is a privileged port and the user has
        no permissions on it), #f is returned.

(recv! sock buff)
        sock: socket obtained with make-client-socket or accept
        buff: string

        Waits for received data through the specified socket, and
        stores it on the buffer. The return value indicates the
        number of received bytes. This call blocks until some data
        is received, but does not guarantee that buff gets
        completely filled. If an error occurs (e.g., the other
        peer disconnects) then #f is returned.

(recv-new-string sock)
        sock: socket obtained with make-client-socket or accept

        Waits for received data through the specified socket, and
        returns it in a new string. This call blocks until some
        data is received. If an error occurs, then #f is returned.

(send sock buff)
        sock: socket obtained with make-client-socket or accept
        buff: string

        Sends the data contained in the string through the socket.
        It returns the number of transmitted bytes (could be
        different than the size of the string!), or #f if an error
        occured (e.g., the other peer disconnected).

(accept server-sock)
        server-sock: socket obtained with make-server-socket

        Waits until a connection is received on the specified
        server socket, and returns the connected socket. If an
        error occurs (e.g., the network interface shuts down), it
        returns #f instead.

(close-socket sock)
        sock: socket obtained with make-server-socket,
              make-client-socket or accept

The socket is closed. No further calls should be performed
        on this socket.

(sock-is-data-ready? sock)
        sock: socket obtained with make-server-socket,
              make-client-socket or accept

This function allows non-blocking operation with sockets.
It returns #t if data is available for reception on this
socket, and #f if no data has been received.

(sock-peek sock)
        sock: socket obtained with make-server-socket,
              make-client-socket or accept

This function returns (as a newly created string) the
data received in this socket. The information is not
removed from the input queue.

*File system (included if HAVE_FILESYSTEM is defined in tsx.h)

Scheme already defines functions to read and write files. These
functions allow access to the filesystem to check if a certain
file exists, to get its size, etc.

(file-size filename)
        filename: string

        This function returns the size (in bytes) of the
        indicated file, or #f if the file does not exists or
        is not accessible to the requesting user.

(file-exists? filename)
        filename: string

        This function returns #t if the indicated file exists, and
        #f if it does not exists or it is not accessible to the
        requesting user.

(delete-file filename)
        filename: string

        Removes the specified file. It returns #t if the operation
        succeeds, or #f otherwise (e.g., because the file is
        read-only, or because the file does not exist).

(open-dir-stream path)
        path: string

        Opens a "directory stream" on the provided directory path.
        This stream will provide all the files within the directory,
        using the function read-dir-entry. The stream should be closed
        at the end with close-dir-stream.

(read-dir-entry dirstream)
        dirstream: directory stream, obtained with open-dir-stream.

        It returns the name of the following directory entry, or eof
        if all the entries were provided. Check the return value with
        with eof-object?.

(close-dir-stream dirstream)
        dirstream: directory stream, obtained with open-dir-stream.

        Close directory stream. No further calls to read-dir-entry should
        be performed.


*Time (available if HAVE_TIME is defined in tsx.h)

(time)
        Returns the current local time, as a list of integer
        containing:
          (year month day-of-month hour min sec millisec)
        The year is expressed as an offsett from 1900.

(gettimeofday)
        Returns a list containing the number of seconds from
        the beginning of the day, and microseconds within the
        current second.

(usleep microsec)
        microsec: integer

        Suspends execution of the calling thread during the
        specified number of microseconds.


*Miscellaneous functions (available if HAVE_MISC is defined)

(getenv varname)
        varname: string

        Returns a string with the content of the specified
        environment variable, or #f if the variable is not
        defined.

(system command)
        command: string

        Executes a command on the /bin/sh shell. Returns #f if
        it is unable to run /bin/sh or another error occurs,
        or an integer return code which is the value returned
        by the command to the shell.
Script BASIC Project Manager

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: TinyScheme Extension Module
« Reply #5 on: April 21, 2015, 07:12:46 PM »
Here is a Prime Number example done in TinyScheme by Mike (FBSL author)

Code: Script BASIC
  1. IMPORT ts.inc
  2.  
  3. sc = TS_New()
  4. TS_Cmd sc, "(load \"init.scm\")"
  5. ts_src = """
  6. (define (prime? n)
  7.  (if (< n 4) (> n 1)
  8.      (and (odd? n)
  9.           (let loop ((k 3))
  10.             (or (> (* k k) n)
  11.                 (and (positive? (remainder n k))
  12.                      (loop (+ k 2))))))))
  13.  
  14. (define (main)
  15.  (do ((i 3 (+ i 2)))
  16.    ((> i 4999) #t)
  17.      (if (prime? i) (begin (display i)(display " ")) "")
  18.  )
  19.  (newline)
  20. )
  21.  
  22. (main)
  23. """
  24. PRINT TS_Cmd(sc, ts_src),"\n"
  25. TS_Close sc
  26.  

Output

jrs@laptop:~/sb/sb22/TS$ time scriba tspnmike.sb
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 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973 4987 4993 4999

real   0m0.394s
user   0m0.392s
sys   0m0.000s
jrs@laptop:~/sb/sb22/TS$

Script BASIC Project Manager