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

Re: ScriptBasic

Mon May 20, 2019 6:51 pm

ScriptBasic wrote:
Mon May 20, 2019 3:41 pm
After looking at the code, can I assume your target is Linux 64 bit?
No, completely generic; one doesn't need a 64-bit OS to handle 64-bit data.

Python's PyArg_ParseTuple() equivalent of SB's besARGUMENTS() seems quite capable in determining if what's being passed in will fit in the parameter type used and throwing an error if not, and similarly in passing a return value back; creates a Python object type big enough to hold whatever the return result is.

It's therefore quite easy for Python. One simply decides what the smallest type an extension module will take in, for example int for a 0-31 pin number, and what it's going to give back, for example, uint32 for a 32-bit read of the GPLEV0 port, or int64 if it's big number, float if non-integer. Python then sorts out what's what simply from the character code for what the C type is.

I might have to layer-on some fudging for ScriptBasic. If SB doesn't support int64 or uint64, they can't go in, can't come out, so those can be ignored.

Having got the code generation tools mostly sorted, I suppose it's time to get down to what should be in the extension module ! That's mostly sorted but I've rationalised and refactored things, but haven't tested that.

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

Re: ScriptBasic

Mon May 20, 2019 7:21 pm

ScriptBasic supports long, real, string (terminated or not) arrays and undef are also types. You can include whatever type.h you need and cast the passed variables for use with your library functions.

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

Re: ScriptBasic

Tue May 21, 2019 1:42 pm

Thanks. I think I've got casting and everything else working though not actually tested.

One question though on optional parameters ...

Code: Select all

char * arg_string;
long   arg_long   = 0;
double arg_double = 0.0;
besARGUMENTS("s[ir]")
  &arg_string, &arg_long, &arg_double
besARGEND
Should that besARGUMENTS() parameter be "s [ i r ]" or "s [ i ] [ r ]" ?

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

Re: ScriptBasic

Tue May 21, 2019 2:13 pm

I believe it's  "s [ i ] [ r ]".

Great to hear you're making progress!

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

Re: ScriptBasic

Tue May 21, 2019 2:21 pm

It's great to be transitioning from a language / open source project promoter to a support resource. ScriptBasic had a lot of catching up to do. :o

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

Re: ScriptBasic

Wed May 22, 2019 12:16 am

I've got my tools creating Python 2, 3 and ScriptBasic extensions on my Pi though the SB files may need a bit of tweaking. Python 2 and 3 modules build, import and test okay but I am stuck on SB being unfamiliar with the extension build system.

This is what's I've generated into my ~/sb-dev-cleanup/extensions/gpio directory. The sb-*.* file names have to stay as they are for now so it's the makefile etc which probably needs adjusting to suit.
sb-gpio-linux.zip
(8.53 KiB) Downloaded 15 times

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

Re: ScriptBasic

Wed May 22, 2019 12:41 am

./setup --module=gpio

Should do it for you.

All that should be in your GPIO extension directory is interface.c and any .c or .h you are using before the build.

You may need to create the ext. name directory in bin/mod/obj. This is normally done when doing a fresh build.

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

Re: ScriptBasic

Wed May 22, 2019 2:01 am

@hippy,

I tried to compile your gpio extension on my Ubuntu 18.10 64 bit laptop.

Curious why you didn't put all your code in interface.c? Is your GPIO code generic to both Python and ScriptBasic?

1. Change sb-gpio-interface.c to interface.c
2. Removed #include "../../basext.h" from sb-gpio.h
3. Created a gpio directory in extensions
4. Created a gpio directory in bin/mod/obj
5. Copied the modified interface.c, sb-gpio.c and sb-gpio.h to to the extensions/gpio directory
6. in source root I ran ./setup --module=gpio

This is the results of the compile. (uint seems to be the problem)

Code: Select all

[email protected]:~/sb/source$ ./setup --module=gpio
This is unix cwd=/home/jrs/sb/source/
executing headerer for the C files
 extracting header from interface.c
 extracting header from sb-gpio.c
creating the module object directory
Creating typical makefile.jam for the module
Searching for libraries
file: interface.c
file: sb-gpio.c
determining dependency from extensions/gpio/interface.c
determining dependency from extensions/gpio/sb-gpio.c
Processing jamal files creating makefile
make: Warning: File 'interface.c' has modification time 13432 s in the future
In file included from interface.c:27:
sb-gpio.h:22:1: error: unknown type name ‘uint’; did you mean ‘int’?
 uint   peek(uint offset);
 ^~~~
 int
sb-gpio.h:22:13: error: unknown type name ‘uint’; did you mean ‘int’?
 uint   peek(uint offset);
             ^~~~
             int
sb-gpio.h:23:13: error: unknown type name ‘uint’; did you mean ‘int’?
 void   poke(uint offset, uint value);
             ^~~~
             int
sb-gpio.h:23:26: error: unknown type name ‘uint’; did you mean ‘int’?
 void   poke(uint offset, uint value);
                          ^~~~
                          int
sb-gpio.h:31:1: error: unknown type name ‘uint’; did you mean ‘int’?
 uint   getPins(void);
 ^~~~
 int
sb-gpio.h:35:1: error: unknown type name ‘uint’; did you mean ‘int’?
 uint   getTyp(int alt);
 ^~~~
 int
make: *** [makefile:19: /home/jrs/sb/source/bin/mod/obj/gpio/s_interface.o] Error 1
ERROR: The module did not compile
[email protected]:~/sb/source$ 
Note: The gpio.so would go in your modules directory which isn't in your system path so there shouldn't be a conflict with your Python module if given the same name.

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

Re: ScriptBasic

Wed May 22, 2019 10:44 am

ScriptBasic wrote:
Wed May 22, 2019 2:01 am
Curious why you didn't put all your code in interface.c?
Mostly as "good practice", but also as a consequence of my automated workflow.

Python extensions are meant to be "_module.c" ( SB's interface.c ), "module.h" and "module.c". That keeps the Python-to-C interface and the native C separate; keeps clutter out of the way when editing the native C. The same would be recommended for SB use.

I do generate that for Python, but I can also generate a monolithic, all in one file version, which is easier to share.
Is your GPIO code generic to both Python and ScriptBasic?
Pretty much. The SB files are effectively a copy of the non-monolithic Python files but with a few changes applied along the way.

My monolithic Python is not great for automatically creating an SB version; it's easier to just generate the recommended non-monolithic version and adjust the three files and that was the path of 'least resistance' which I took.
1. Change sb-gpio-interface.c to interface.c
It does look like I'm going to have to be more compliant with what SB expects than trying to coerce SB into using what I have.

I am on the way to doing that. Having filenames reflect the module name does seem the key to getting the SB stuff to work.
2. Removed #include "../../basext.h" from sb-gpio.h
I believe that might have been my hack to get around the "uint" issue.

I was working on the principle that; if something solved the "uint" issue it didn't much matter what that was or if it were defining stuff which was otherwise unnecessary to define.

My effort has basically followed the doctrine of "get it working, optimise it later, or leave it as is - if it isn't broken; don't fix it".
4. Created a gpio directory in bin/mod/obj
That was one of the steps I had missed earlier, but did eventually figure that out.

I currently have a case-sensitive issue requiring "GPIO" rather than "gpio" but should be able to figure out where that is and fix it.
6. in source root I ran ./setup --module=gpio
That was my main mistake; expecting to need to be in the module's source directory to build the module.

What I currently have does actually seem to be building -

Code: Select all

[email protected]:~/sb-dev-cleanup $ ./setup --module=gpio
This is unix cwd=/home/pi/sb-dev-cleanup/
executing headerer for the C files
 extracting header from interface.c
 extracting header from gpio.c
creating the module object directory
Processing jamal files creating makefile
Module was compiled fine

Code: Select all

[email protected]:~/sb-dev-cleanup $ ls -l extensions/gpio
total 72
-rw-r--r-- 1 pi pi  3132 May 22 02:49 gpio.bas
-rw-r--r-- 1 pi pi 24009 May 22 02:49 gpio.c
-rw-r--r-- 1 pi pi  3841 May 22 02:49 gpio.h
-rw-r--r-- 1 pi pi  1691 May 22 02:49 install.sb
-rw-r--r-- 1 pi pi 17319 May 22 02:49 interface.c
-rw-r--r-- 1 pi pi    68 May 22 11:38 libraries.jim
-rwxr-xr-x 1 pi pi  1180 May 22 11:38 makefile
-rw-r--r-- 1 pi pi   767 May 22 02:49 makefile.jam
-rw-r--r-- 1 pi pi   117 May 22 02:49 manual.texi.jam

Code: Select all

[email protected]:~/sb-dev-cleanup $ ls -l bin/mod/obj/GPIO
total 16
-rw-r--r-- 1 pi pi 7340 May 22 11:38 interface.o
-rw-r--r-- 1 pi pi 7340 May 22 11:38 s_interface.o

Code: Select all

[email protected]:~/sb-dev-cleanup $ ls -l bin/mod/dll/gpio.*
-rwxr-xr-x 1 pi pi 11568 May 22 11:38 bin/mod/dll/gpio.so
So it looks like we're on the way to a winner. Thanks for your help.

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

Re: ScriptBasic

Wed May 22, 2019 1:23 pm

Congrats!

You're now a SB master module builder. 8-)

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

Re: ScriptBasic

Wed May 22, 2019 1:55 pm

ScriptBasic wrote:
Wed May 22, 2019 1:23 pm
You're now a SB master module builder. 8-)
Not quite, not yet ...

Code: Select all

[email protected]:~/extensions $ cat test-sb-gpio.sb
IMPORT gpio.bas
PRINT gpio::version(), "\n"
[email protected]:~/extensions $ scriba test-sb-gpio.sb
(0): error &H68:The user function "gpio::version" is used but is not defined.
This seems to be a compile time error, rather than run-time, but odd there's a "0" line number. - more a 'prior to run-time' run-time error. It throws this &H68 error even when there's a preceding PRINT which it doesn't execute and show.

It's obviously finding "gpio.bas" as it gives a "not found" compile error if I make that something else. It runs if I have the IMPORT but gpio::version() removed.

The version routine is defined in the gpio.bas file ...

Code: Select all

DECLARE SUB ::version    ALIAS "GPIO_version"    LIB "gpio"
And it's also in the interface.c file, and is not dependent on the gpio.c file ...

Code: Select all

// .--------------------------------------------------------------------------.
// |  string = GPIO::version()                                                |
// |--------------------------------------------------------------------------|
// |  Return version details                                                  |
// `--------------------------------------------------------------------------'

besFUNCTION(GPIO_version)
  besRETURN_STRING("Version " VERSION " (#" RELEASE ") - " TOUCHED )
besEND
Is there any 'reflection' or 'intraspection' support in ScriptBasic ; being able to print what an extension module implements, like Python's "import gpio; print(dir(gpio))" ?

Or any tools you can recommend to see what's in the gpio.so file, which might show where the disconnect is ?
Last edited by hippy on Wed May 22, 2019 2:08 pm, edited 1 time in total.

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

Re: ScriptBasic

Wed May 22, 2019 2:07 pm

Run your script with the -d option to debug your extension module interface.

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

Re: ScriptBasic

Wed May 22, 2019 2:10 pm

Code: Select all

[email protected]:~/extensions $ cat test-sb-gpio.sb
PRINT "Started\n"
IMPORT gpio.bas
PRINT "Imported\n"
PRINT gpio::version(), "\n"
PRINT "Finished\n"

Code: Select all

[email protected]:~/extensions $ scriba -d test-sb-gpio.sb
Searching installed module header file 'gpio.bas' ...
Checking installed module header file location '/usr/local/include/scriba/gpio.bas' Result=OK
Including file '/usr/local/include/scriba/gpio.bas'
(0): error &H68:The user function "gpio::version" is used but is not defined.
Aha .... nano /usr/local/include/scriba/gpio.bas ...

Code: Select all

'GPIO Extension Mondule

GLOBAL CONST SETUP_OK           = 0
GLOBAL CONST SETUP_DEVMEM_FAIL  = 1
That looks like that's your gpio.bas not mine.

Am I missing some step in getting mine used ?

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

Re: ScriptBasic

Wed May 22, 2019 2:21 pm

ScriptBasic builds a gpio.bas but feel free to use your version. For SB to find it with IMPORT gpio.bas it needs to be in your /user/loca/include/scriba directory.

What does scriba -D say your module and include paths are?

I'm assuming your gpio.bas has a MODULE GPIO / END MODULE encapsulating your definitions?

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

Re: ScriptBasic

Wed May 22, 2019 2:42 pm

Code: Select all

[email protected]:~/extensions $ scriba -D
dll ".so"
module "/usr/local/lib/scriba/"
include "/usr/local/include/scriba/"
maxstep 0
maxlocalstep 0
maxlevel 29666
maxmem 0
So I copied the gpio.so and gpio.def over ...

Code: Select all

[email protected]:~/sb-dev-cleanup/extensions/gpio $ cp  ../../bin/mod/dll/gpio.so /usr/local/lib/scriba/gpio.so
[email protected]:~/sb-dev-cleanup/extensions/gpio $ cp gpio.bas /usr/local/include/scriba/gpio.bas
Which got us a little further along ...

Code: Select all

[email protected]:~/extensions $ scriba -d test-sb-gpio.sb
Searching installed module header file 'gpio.bas' ...
Checking installed module header file location '/usr/local/include/scriba/gpio.bas' Result=OK
Including file '/usr/local/include/scriba/gpio.bas'
/usr/local/include/scriba/gpio.bas(16): error &H77:syntax error during checking the line and also noting that the syntax error so serious that none of the other syntax defintions can match the current line
/usr/local/include/scriba/gpio.bas(17): error &H77:syntax error during checking the line and also noting that the syntax error so serious that none of the other syntax defintions can match the current line
Not exactly what the problem is there with line 16 and 17 when line 18 is okay - One of the problems I mentioned about ScriptBasic providing less than useful error messages through its 'it works or it doesn't' approach to syntax checking. And, BTW, "defintions" has a typo ...

Code: Select all

16: GLOBAL CONST INPUT      = 1
17: GLOBAL CONST OUTPUT     = 0
18: GLOBAL CONST PULLUP     = 1
Still I commented out 16 and 17 and, bingo! ...

Code: Select all

[email protected]:~/extensions $ scriba -d test-sb-gpio.sb
Searching installed module header file 'gpio.bas' ...
Checking installed module header file location '/usr/local/include/scriba/gpio.bas' Result=OK
Including file '/usr/local/include/scriba/gpio.bas'
Started
Imported
Version 0.00 (#0008) - 2019-05-22 14:38:30
Finished

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

Re: ScriptBasic

Wed May 22, 2019 2:46 pm

INPUT and OUTPUT are SB keywords so using them as constants won't do.

Hint: Once I get a module to build, any minor fixes are recompiled in the extension directory with a make -B. This guarantees everything is rebuilt by gcc.

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

Re: ScriptBasic

Wed May 22, 2019 4:13 pm

ScriptBasic wrote:
Wed May 22, 2019 2:46 pm
INPUT and OUTPUT are SB keywords so using them as constants won't do.
So technically a semantic error rather than a syntax error :?

I've said it before; but this is one of the reasons I cannot recommend ScriptBasic as a general purpose programming language. It insists one gets it right, knows the language, rather than telling a programmer what it is they got wrong when they don't. That can be acceptable for an application language, but IMO is not for general purpose use.

Take those "unknown type name 'uint'" errors for example; imagine how much more difficult to figure out what was wrong if gcc had simply said "Nope; you got line 22 wrong".
Hint: Once I get a module to build, any minor fixes are recompiled in the extension directory with a make -B. This guarantees everything is rebuilt by gcc.
I'm okay there; I delete all the extension files before regenerating them. That ensures there can be no mismatches in anything.

My workflow is to stay in the 'extension creating directory', only edit my *.def and *.c files to define what the extension is and does, then generate everything for Python and SB from scratch. So, if anything changes, it gets changed only in the creating directory. Changes elsewhere will be over-written. Hence the "do not edit" banners.

Code: Select all

                             Python 2/3       ScriptBasic
.-------.                                   .-------------.
| *.def |--.                            .-->| *.bas       |
`-------'  |    ________      .------.  |   |-------------|
           `-->|        |---->| _*.c |--^-->| interface.c |
.-------.      |        |     |------|      |-------------|
| *.lib |----->| CREATE |---->|  *.h |----->| *.h         |
`-------'      |        |     |------|      |-------------|
           .-->|________|---->|  *.c |--.-->| *.c         |
           |                  `------'  |   `-------------'
           `----------------------------'
I had to abandon my previous MicroPython extension efforts as that, in hindsight, was poorly thought out. Doing it how I've done it with ScriptBasic is a better approach, so it's a good stepping stone towards that.

I'm Python-centric so it's what suits me. But there's no reason not to create an initial extension skeleton and then remove "ScriptBasic" from the .def, and then go on with editing stuff in the SB extension directory.

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

Re: ScriptBasic

Wed May 22, 2019 4:21 pm

So technically a semantic error rather than a syntax error 
Nope. It's a syntax error. You were trying combine two statements on a line. I posted a reserved keyword list for ScriptBasic on the RaspberryBASIC.org forum.

ScriptBasic really needs a cheat sheet for building extension modules. I think this exercise helped flush out things that aren't obvious. Once again, ScriptBasic is an API and not a Lego snap it together kit.
I cannot recommend ScriptBasic as a general purpose programming language.
If by general purpose you mean a BASIC to teach programming, probably not. As I said when I first joined this forum, ScriptBasic is glueware and a high level wrapper for C. Where ScriptBasic shines is allowing the developer to control execution at the statement level and modifying its environment / functionality to meet the needs of the project.

Maybe Peter Verhas shoud have called it APIBASIC. ;)

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

Re: ScriptBasic

Wed May 22, 2019 7:39 pm

Still not there. gpio::version() works, but not =gpio::enter(), nor gpio::leave() if the enter stuff is commented out ...

Code: Select all

[email protected]:~/extensions $ cat test-sb-gpio.sb
PRINT  "Started\n"
IMPORT gpio.bas
PRINT  "Imported\n"
PRINT  gpio::version(), "\n"
result = gpio::enter()
IF result = 0 THEN
  PRINT "Initialised : ", result, " - Okay\n"
ELSE
  PRINT "Initialised : ", result, " - FAILED\n"
END IF
gpio::leave()
PRINT  "Finished\n"

Code: Select all

[email protected]:~/extensions $ scriba -d test-sb-gpio.sb
Searching installed module header file 'gpio.bas' ...
Checking installed module header file location '/usr/local/include/scriba/gpio.bas' Result=OK
Including file '/usr/local/include/scriba/gpio.bas'
Started
Imported
Version 0.00 (#0011) - 2019-05-22 20:34:29
scriba: symbol lookup error: /usr/local/lib/scriba/gpio.so: undefined symbol: enter

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

Re: ScriptBasic

Wed May 22, 2019 7:47 pm

Can post the gpio.bas you're using? For grins, try changing the function name for ENTER.

Does nm show enter as an exported function?

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

Re: ScriptBasic

Wed May 22, 2019 7:56 pm

Solved that one. Despite the "./setup --module=gpio" extracting headers from "interface.c" and "gpio.c", it doesn't actually do anything else with "gpio.c", so it builds the "gpio.so" without links to the routines in "gpio.c".

Code: Select all

[email protected]:~/extensions $ ./test-sb
Searching installed module header file 'gpio.bas' ...
Checking installed module header file location '/usr/local/include/scriba/gpio.bas' Result=OK
Including file '/usr/local/include/scriba/gpio.bas'
Started
Imported
Version 0.00 (#0011) - 2019-05-22 20:34:29
Initialised : 0 - Okay
Finished
Next test is to see if I can peek the "aux0" text.

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

Re: ScriptBasic

Wed May 22, 2019 8:01 pm

Cool!

Why I suggested make -B. I have made changes to a module and compiled it with the ./setup --module= method and it didn't generate a new .so. You said you delete everything except the .c and .h file so that shouldn't happen. Maybe it's not creating a new interface.obj. Mystery to me.

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

Re: ScriptBasic

Thu May 23, 2019 2:36 am

I would be happy to setup an account for you in the SciptBasic Extensions sandbox if you would like to share your SB GPIO extension module project adventure with others.

Is the py.gpio extension I did useable and worth putting any more effort in? Is there anything there you can use with your existing project?

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

Re: ScriptBasic

Thu May 23, 2019 10:20 am

I am certainly happy to publish and share what I have when done. I just need to check it all actually works first. There's also that issue of read and write barriers which got lost in all the asides and distractions in this thread. I'll post separately on that.

I think your library and others will help with what I'm doing. Once I can put extension generation behind me I can get back to figuring out how the ideal extension should be.

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

Re: ScriptBasic

Thu May 23, 2019 1:13 pm

Sounds like a good plan!

I had to smile when gcc said I was compiling code from the future. The byproduct of working with others globally.

Return to “Other programming languages”