User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 23, 2019 3:19 am

I'm running Gitlab CE hosted on a Amazon EC2 instance.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 23, 2019 3:47 am

@Paeryn,

Since you seem to feel comfortable under the ScriptBasic covers, maybe you can give me a hand with COMMANDs.. SB reserves a set of operator pairs to be used by extension modules. I would like to replace the GMP functions with DECLARE COMMAND. Here is an example for GMP2 add.

Code: Select all

 Operators that are undefined by default but can be used by external modules are:


?     !     #     `     @
+^    +<    +>    +?    +=    +*    +/    +%    +!    +#    +&    +\    +`    +'   
[email protected]    -^    -<    ->    -?    -=    -*    -/    -%    -!    -#    -&    -\    -`   
-'    [email protected]    ^^    ^<    ^>    ^?    ^=    ^*    ^/    ^%    ^!    ^#    ^&    ^\   
^`    ^'    ^@    <^    <<    <?    <*    </    <%    <!    <#    <&    <\    <`   
<'    <@    >^    ><    >>    >?    >*    >/    >%    >!    >#    >&    >\    >`   
>'    >@    ?^    ?<    ?>    ??    ?=    ?*    ?/    ?%    ?!    ?#    ?&    ?\   
?`    ?'    [email protected]    =^    =<    =>    =?    ==    =*    =/    =%    =!    =#    =&   
=\    =`    ='    [email protected]    *^    *<    *>    *?    *=    **    */    *%    *!    *#   
*&    *\    *`    *'    *@    /^    /<    />    /?    /=    /*    //    /%    /!   
/#    /&    /\    /`    /'    /@    %^    %<    %>    %?    %=    %*    %/    %%   
%!    %#    %&    %\    %`    %'    %@    !^    !<    !>    !?    !=    !*    !/   
!%    !!    !#    !&    !\    !`    !'    [email protected]    #^    #<    #>    #?    #=    #*   
#/    #%    #!    ##    #&    #\    #`    #'    #@    &^    &<    &>    &?    &=   
&*    &/    &%    &!    &#    &&    &\    &`    &'    &@    \^    \<    \>    \?   


\=    \*    \/    \%    \!    \#    \&    \\    \`    \'    \@    `^    `<    `>   
`?    `=    `*    `/    `%    `!    `#    `&    `\    ``    `'    `@    '^    '<   
'>    '?    '=    '*    '/    '%    '!    '#    '&    '\    '`    ''    '@    @^   
@<    @>    @?    @=    @*    @/    @%    @!    @#    @&    @\    @`    @'    @@
Current:
c = GMP2::add(a, b)

Wishlist:
c = a +! b

+! Is the COMMAND name and a and b are arguments. The IFF COMMAND in the trial extension module is an example of this feature of SB.

Try the wishlist syntax in SB. The message returned Is a clue.

(0): error &H1c:Command is not implemented and no currently loaded extension module defined behaviour for it


I think command.c is a good place to start.

Airr
Posts: 19
Joined: Sun Jun 16, 2019 5:17 pm

Re: ScriptBasic

Sun Jun 23, 2019 5:27 am

IFF syntax in SB is:

Code: Select all

IF a THEN a = IFF(a>1,1,-1)
So I don't think it's a good example of what you want.

Also, what you're asking for isn't as clear (as far as it being an extension module function) as what we currently have.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 23, 2019 6:14 am

Once we figure this gem out, it's going to open up a whole new level of extensions,.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 23, 2019 6:22 am

My test of IFF segfaults.

Code: Select all

INCLUDE trial.bas

a = 5

IF a THEN a = IFF(a>1, 1, -1)

PRINT a,"\n"


[email protected]:~/sb/GMP$ scriba cmdtest.sb
The function bootmodu was started and the requested version is 11
The variation is: STANDARD
We are returning accepted version 11
The function bootmodu was started.
Segmentation fault (core dumped)
[email protected]:~/sb/GMP$ 
This works.

Code: Select all

DECLARE COMMAND  IFF  ALIAS  "iff"  LIB  "trial"

a = 5

a = IFF(a > 1, TRUE, FALSE)

PRINT a,"\n"


[email protected]:~/sb/GMP$ scriba cmdtest.sb
The function bootmodu was started and the requested version is 11
The variation is: STANDARD
We are returning accepted version 11
The function bootmodu was started.
-1
The function finimodu was started.
[email protected]:~/sb/GMP$ 
To get the maximum benefit from COMMANDs one should INCLUDE the DECLARE COMMAND and not make it part of a MODULE.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic Sandbox

Mon Jun 24, 2019 3:48 am

If you would like to participate in the ScriptBasic project from reporting issues to correcting and enhancing source, send me an email to [email protected] with ScriptBasic in the subject and I will setup an account in the sandbox based on your role. A guest role in most cases is sufficient.

Image

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Mon Jun 24, 2019 6:15 am

A project on the table is converting ScriptBasic's build system documentation from source to HTML to creating markdown documentation instead. Another option is to embed markdown docs in source. (easier to parse)

Heater
Posts: 12723
Joined: Tue Jul 17, 2012 3:02 pm

Re: ScriptBasic

Mon Jun 24, 2019 7:11 am

ScriptBasic,

In reply to you questions on the Fibonacci thread:

Once again I present codes and results demonstrating memory leaks in ScriptBasic and the GMP2 extension. See below.
I can call the GMP fibo() function in loop with no memory runaway. (user created buffer)
Please show your code because I cannot. See below.
There is noise on the web about GMP leaking string responses or not being properly cleared.
No, there is not. Unless you can provide a link.
Your C version works so go figure. You have tried running that In a loop, correct?
Yes it does. The following C code was my suggested style to do big numbers with GMP in ScriptBasic. It runs forever in a loop.
Provided ScriptBasic would do all the same inits, clears and frees it would also be memory leak free.

integer_strings.c

Code: Select all

//
// An experiment in doing integer arithmetic using GMP with all numbers represented by strings.
//
// By heater.
// Modified June 11, 2019 to use base 32 strings for intermediate results.
//
#include <gmp.h>
#include <string.h>

#include "integer_strings.h"

// Functions letis, addis, subis and mulis do large integer arithmetic on integers represented by strings.
// WARNING: Not thread safe due to use of global op1, op2, res.

static mpz_t op1;
static mpz_t op2;
static mpz_t res;

char* is_base(const char *s, int base) {
    mpz_set_str (op1, s, IS_BASE);
    char* res_string = mpz_get_str (0, base, op1);
    return res_string;
}

char* is_let(const char* s) {
    return strdup(s);
}

char* is_add(const char* s1, const char* s2) {
    mpz_set_str (op1, s1, IS_BASE);
    mpz_set_str (op2, s2, IS_BASE);
    mpz_add (res, op1, op2);  // result = x * y
    char* res_string = mpz_get_str (0, IS_BASE, res);
    return res_string;
}

char* is_sub(const char* s1, const char* s2) {
    mpz_set_str (op1, s1, IS_BASE);
    mpz_set_str (op2, s2, IS_BASE);
    mpz_sub (res, op1, op2);  // result = x * y
    char* res_string = mpz_get_str (0, IS_BASE, res);
    return res_string;
}

char* is_mul(const char* s1, const char* s2) {
    mpz_set_str (op1, s1, IS_BASE);
    mpz_set_str (op2, s2, IS_BASE);
    mpz_mul (res, op1, op2);  // result = x * y
    char* res_string = mpz_get_str (0, IS_BASE, res);
    return res_string;
}

void is_init() {
    mpz_init(op1);
    mpz_init(op2);
    mpz_init(res);

}

void is_clear() {
    mpz_clear(op1);
    mpz_clear(op2);
    mpz_clear(res);
}
fibo_strings.c

Code: Select all

//
// An experiment in doing integer arithmetic using GMP with all numbers represented by strings.
//
// By heater.
//
#include <stdio.h>
#include <stdlib.h>

#include "integer_strings.h"

char* fibos[3];

// Return the n'th Fibonacci number as a decimal string for integer n
char* fibo (int n) {
    char* res;
    if (n <= 2) {
        return is_let(fibos[n]);
    }

    int k = (n / 2);
    char* fk = fibo(k);
    char* fk1 = fibo(k + 1);
    char* a;
    char* b;
    if ((n % 2) == 0) {
        a = is_add(fk1, fk1);
        b = is_sub(a, fk);
        res = is_mul(fk, b);
    } else {
        a = is_mul(fk, fk);
        b = is_mul(fk1, fk1);
        res = is_add(a, b);
    }
    free(a);
    free(b);
    free(fk);
    free(fk1);
    return res;
}

int main(int argc, char* argv[]) {
    int n = 4784969;               // The first Fibonacci number with a million digits

    if (argc >= 2) {
        n = atoi(argv[1]);
    }

    is_init();

    fibos[0] = is_let("0");
    fibos[1] = is_let("1");
    fibos[2] = is_let("1");

    int count = 0;
    while (1) {
        char* f = fibo(n);
        char* f10 = is_base(f, 10);
        puts(f10);
        printf("%d\n", count);
        free(f10);
        free(f);
        count++;
    }

    free(fibos[0]);
    free(fibos[1]);
    free(fibos[2]);

    is_clear();

    return (0);
}
Result: fibo_strings runs forever on my 8GB memory PC with top only showing 0% memory consumption.

1milfibo.sb

Code: Select all

import gmp2.bas

function fibo(n)
    a = 1
    b = 0
    p = 0
    q = 1

    while n > 0
        if (n % 2) = 0 then
            psq   = gmp2::mul(p, p)
            qsq   = gmp2::mul(q, q)
            twopq = gmp2::mul(p, q)
            twopq = gmp2::mul_si(twopq, 2)
            p     = gmp2::add(psq, qsq)
            q     = gmp2::add(twopq, qsq)
            n = n / 2
        else
            bq    = gmp2::mul(b, q)
            aq    = gmp2::mul(a, q)
            ap    = gmp2::mul(a, p)
            bp    = gmp2::mul(b, p)
            a     = gmp2::add(bq, aq)
            a     = gmp2::add(a, ap)
            b     = gmp2::add(bp, aq)
            fibo  = b
            n = n - 1
        end if
    wend
end function

count = 0
while 1
    f = fibo(4784969)
    count = count + 1
    print f,"\n"
    print "Count: ", count,"\n"
wend
Result: 1milfibo.sb runs for 574 iterations on my 8GB memory PC before segfaulting. Last I checked before the segfault memory use was over 66% according to top.

Code: Select all

$ scriba  1milfibo.sb
...
74615715617623206204585291487968755147324883784916338945806361951766313865956891695455563801462249978567353436540666740580717167763216988216644330074030719891463180149736853685001275152076875379936330930391815964864885353407167474856539211500699706378405156269
Count: 574
Segmentation fault (core dumped)
hfibo.sb

Code: Select all

import gmp2.bas

function hfibo(n)
  local a, b, fk, fk1, k, r
  SPLIT "0,0,0,0,0,0" BY "," TO k, a, b, r, fk, fk1

  if n = 0 then
    r = 0
  else if n <= 2 then
    r = 1
  else
    k = (n / 2) OR 0
    fk = hfibo(k)
    fk1 = hfibo(k + 1)
    if (n and 1) = 0 then
      a = gmp2::add(fk1, fk1)
      b = gmp2::sub(a, fk)
      r = gmp2::mul(fk, b)
    else
      a = gmp2::mul(fk, fk)
      b = gmp2::mul(fk1, fk1)
      r = gmp2::add(a, b)
    end if
  end if
  hfibo = r
end function

count = 0
while 1
    f = hfibo(4784969)
    count = count + 1
    print f,"\n"
    print "Count: ", count,"\n"
wend
Result: hfibo.sb runs for 12 iterations on my 8GB memory PC before segfaulting. Last I checked before the segfault memory use was over 68% according to top.

Code: Select all

...
74615715617623206204585291487968755147324883784916338945806361951766313865956891695455563801462249978567353436540666740580717167763216988216644330074030719891463180149736853685001275152076875379936330930391815964864885353407167474856539211500699706378405156269
Count: 12
Segmentation fault (core dumped)

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Mon Jun 24, 2019 7:22 am

The GMP2 extension module source is in the sandbox. The fibo() function is included.

I would be thrilled if you could get the GMP2 module working like your C version. Call it GMP3. 8-)

Heater
Posts: 12723
Joined: Tue Jul 17, 2012 3:02 pm

Re: ScriptBasic

Mon Jun 24, 2019 7:47 am

ScriptBasic,
The GMP2 extension module source is in the sandbox. The fibo() function is included.
Yes I know. It's horrible.

Code: Select all

...
 res = mpz_get_str(NULL,10,g_Res);
 besSET_RETURN_STRING(res);
 free(res);
...
Looks like strings returned from mpz are being freed correctly.

I have idea what happens to the copy of the string after it has been eaten by besSET_RETURN_STRING. And life is too short to find out.
I would be thrilled if you could get the GMP2 module working like your C version. Call it GMP3.
Not going to happen. Too many other fish frying around here.

In don't think the ScriptBasic big number extension should be called "GMP" anything. It implements big integers and should say that. Who cares how they are implemented under the hood. What happens if you want to change from GMP to some other big number library?

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Mon Jun 24, 2019 8:09 am

What happens if you want to change from GMP to some other big number library?
You would create another BIGINT extension module.
Too many other fish frying around here.
Image

Heater
Posts: 12723
Joined: Tue Jul 17, 2012 3:02 pm

Re: ScriptBasic

Mon Jun 24, 2019 9:19 am

ScriptBasic,
You would create another BIGINT extension module.
No doubt.

But then it would have a different name and perhaps different method names and perhaps not work the same way. To make us of it one would have to rewrite all ones code to use HEATER::PLUS...(say) instead of GMP2::ADD and so on.

Or it would have the same name, but that would be confusing, and at that point you start to realize why a big integer, or any other module, should be named after what it does, not how it's implemented. We are working in a high level language, right?

Weave
Posts: 6
Joined: Mon Jun 24, 2019 4:16 pm

Re: ScriptBasic

Mon Jun 24, 2019 4:24 pm

ScriptBasic wrote:
Thu May 09, 2019 5:26 pm
ScriptBasic is my replacement for bash.
BaCon comes in a version written in bash and a version written in itself. I think @ejolson posted a script showing bash being 30 times slower than ScriptBasic.
I think this is rather misleading, BaCon is a BASIC to C converter, only the conversion happens in bash then you end up with a native executable.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Mon Jun 24, 2019 4:42 pm

You're probably the best person to resolve this GMP issue but it's impossible to get you to engage. (someone else's problem)

Glad to hear ScriptBasic isn't alone on this issue and has bitten you as well.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 26, 2019 5:35 am

When you are ready to distribute your ScriptBasic applications you have the option to compile your scripts to C. The hello executable is 13.5 KB. Requires latest source that fixed the -C output source file. (latest scriba)

hello.sb

Code: Select all

PRINT "Hello World!\n"

Code: Select all

[email protected]:~/sbrt/2c $ scriba -Co hello.c hello.sb
[email protected]:~/sbrt/2c $ gcc -Os hello.c ./stndlone.o ./libscriba.so -o hello
[email protected]:~/sbrt/2c $ ./hello
Hello World!
[email protected]:~/sbrt/2c $ ls -l hello
-rwxr-xr-x 1 pi pi 13528 Jun 25 22:20 hello
[email protected]:~/sbrt/2c $ 

Code: Select all

[email protected]:~/sbrt/2c $ gcc hw.c -o hw
[email protected]:~/sbrt/2c $ ./hw
Hello World!
[email protected]:~/sbrt/2c $ ls -l hw
-rwxr-xr-x 1 pi pi 8144 Jun 25 22:54 hw
[email protected]:~/sbrt/2c $ cat hw.c
#include <stdio.h>
int main()
  {
    printf("Hello World!\n");
    return 0;
  }
[email protected]:~/sbrt/2c $ 

hippy
Posts: 5371
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Wed Jun 26, 2019 9:46 am

ScriptBasic wrote:
Wed Jun 26, 2019 5:35 am
When you are ready to distribute your ScriptBasic applications you have the option to compile your scripts to C.
That "compile your scripts to C" is somewhat misleading. What one can do is combine the ScriptBasic interpreter with a script's virtual object code to create a single executable, and this is achieved through creating a C file which then needs to be compiled.

"Compile to C" would normally be taken to mean "convert every script line to its equivalent C". This is not what ScriptBasic provides for. It is more bundling the interpreter with the script's object code to create a single native executable.

It would be better to say "When you are ready to distribute your ScriptBasic applications you have the option to turn that into a single executable".

User avatar
RichardRussell
Posts: 534
Joined: Thu Jun 21, 2012 10:48 am

Re: ScriptBasic

Wed Jun 26, 2019 10:50 am

hippy wrote:
Wed Jun 26, 2019 9:46 am
this is achieved through creating a C file which then needs to be compiled.
Odd. BBC BASIC (for Windows) makes its 'executables' in the same way: it binds a copy of the run-time-engine and a compressed version of the BASIC program (and any libraries and resources it needs) into a single file. But it does that without any recourse to C or 'compiling' anything, indeed it's pretty much achieved simply by concatenating the various components and adding a simple 'directory' pointing to where they are in the file. Using a C compiler and linker to do the same job sounds like overkill (one can't assume such tools will be available in Windows).

hippy
Posts: 5371
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Wed Jun 26, 2019 12:24 pm

RichardRussell wrote:
Wed Jun 26, 2019 10:50 am
it's pretty much achieved simply by concatenating the various components and adding a simple 'directory' pointing to where they are in the file. Using a C compiler and linker to do the same job sounds like overkill (one can't assume such tools will be available in Windows).
I would guess it side-steps any issues which may arise through simple concatenation, or needing to have a complicated concatenation scheme within the ScriptBasic compiler which may have to differ depending on platform.

It would make sense though to provide concatenation where that works and revert to something else only when that doesn't but, on the other hand, having one scheme is easier than having multiple.

It's a good point about needing a C compiler though; I don't have one on any of my Windows machines.

Converting to an executable isn't really necessary under Linux anyway; one can simply add an appropriate shebang and set the file's execute permission, exactly the same as one can do it for Python and other scripting languages -

Code: Select all

#!/usr/local/bin/scriba
ScriptBasic does ignore the shebang when added and scriba handles ScriptBasic programs without extensions.

On Windows one would normally create a batch file which can invoke an interpreter as that is rarely problematic.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 26, 2019 3:08 pm

If you don't want to mess with a C compiler, you can still create a standalone executable.

scriba -Eo hello.exe hello.sb

This appends a binary form of the script to a copy of scriba with the name given.

Scriba always checks to see if a script is attached or not before processing the command line. You can still pass user command line arguments. Just no - options.

Under Linux, you need to use the full path for scriba for this command to work.
Last edited by ScriptBasic on Wed Jun 26, 2019 6:50 pm, edited 3 times in total.

User avatar
RichardRussell
Posts: 534
Joined: Thu Jun 21, 2012 10:48 am

Re: ScriptBasic

Wed Jun 26, 2019 4:07 pm

ScriptBasic wrote:
Wed Jun 26, 2019 3:08 pm
If you don't want to mess with a C compiler, you can still create a standalone executable.
Oh, right, so when is the 'C' approach preferable?

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 26, 2019 4:10 pm

When you want to run scriba as a common shared object with a bunch of scripts as small footprint executables.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 26, 2019 7:33 pm

@scruss,

I just noticed you're a member of the AllBasic. info forum. Cool! We would enjoy your input and feedback there.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 26, 2019 7:43 pm

Image
Image

Would you like to report issues and submit merge requests to the ScriptBasic development sandbox repository? It's easy with no commitment. Just create a project guest account. This project is where it is today due to talented developers like yourself doing their part to make the language better and more user friendly to a wider audience.

Send an email to [email protected] with your preferred user ID and I will setup a guest membership account for you. It would be helpful due to all the spam I get to put SCRIPTBASIC in the subject title so I don't miss your request.

User avatar
scruss
Posts: 2271
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: ScriptBasic

Wed Jun 26, 2019 11:29 pm

ScriptBasic wrote:
Wed Jun 26, 2019 7:33 pm
I just noticed you're a member of the AllBasic. info forum. Cool! We would enjoy your input and feedback there.
You really, really don't want my code contributions, though …
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Mon Jul 01, 2019 12:15 am

I found a cool open source GPIO data analyzer.

Panalyzer
This is a Logic Analyzer that runs on the RaspberryPi. The basic idea is that it disables interrupts for a period, while sampling the GPIO pins once a microsecond. It then re-enables interupts and displays traces showing what the relevant GPIO pins were doing. Clearly the RaspberryPi is frozen while data is being gathered, because interrupts are disabled, but a timeout is implemented to avoid hanging for ever if a trigger word is not seen. Panalyzer has features such as specifying trigger patterns, buffer depth, a pair of cursors for measuring time periods on the trace, etc. In the display you can click and drag across part of the trace to zoom in on that section.
Attachments
screenshot01.gif
screenshot01.gif (10.82 KiB) Viewed 323 times

Return to “Other programming languages”