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

ScriptBasic to BaCon Converter

Wed Nov 20, 2019 4:07 am

Peter van Eerten (BaCon author) has done an amazing job with his traditional BASIC to C translator. Fortunately BaCon syntax is very compatible with ScriptBasic.

If I were to set the ScriptBasic option to declare variables using LET to notate the declare and use the optional $ reference for strings and their functions, the translation should be rather painless.

It would be nice to see a BaCon user presence here on the forum.
Last edited by John_Spikowski on Wed Nov 20, 2019 5:46 am, edited 1 time in total.

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

Re: ScriptBasic to BaCon Converter

Wed Nov 20, 2019 4:26 am

As an example, I'm converting the ScriptBasic Tatami challenge submission to Bacon. The execution speed should be equivalent to the other C versions.

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

Re: ScriptBasic to BaCon Converter

Wed Nov 20, 2019 5:54 am

The current BaCon GUI on the RPi requires FLTK which seems to build / install fine. I wasn't so lucky with the BaCon build as it complained about its reference to the FLTK library. On a positive note I can still use the BaCon command line translator to generate executable C code.

I've attached the build / install log if Peter or others would like to review it.
Attachments
bacon-3.9.2b3_install_log.txt.zip
(9.58 KiB) Downloaded 3 times

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24129
Joined: Sat Jul 30, 2011 7:41 pm

Re: ScriptBasic to BaCon Converter

Wed Nov 20, 2019 10:06 am

Is this even remotely Pi related?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

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

Re: ScriptBasic to BaCon Converter

Wed Nov 20, 2019 1:31 pm

Both BASIC languages run on the RPi. One is an interpreter and the other a compiler.

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

Re: ScriptBasic to BaCon Converter

Wed Nov 20, 2019 2:06 pm

I converted the tatamix.sb script to BaCon and it compiled without error. After 45 minutes I killed the task. Not sure what the problem is at this time.

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

Re: ScriptBasic to BaCon Converter

Wed Nov 20, 2019 5:24 pm

Here is the converted ScriptBasic script I used with BaCon if anyone wishing to comment on this to help solve the issue of hanging and not returning a result the help would be appreciated. I haven't used BaCon in a while.

Update: I noticed that my find s code overshot the end of the array. I added a couple things I thought that would help. Still no results and hangs.

Code: Select all


GLOBAL nMax, s, i, j, k2, k3, k4 TYPE long

nMax = 100000000

nMaxSqrt = INT(SQR(nMax))

DECLARE v[50000000] TYPE NUMBER
FUNCTION Tatami(s)
  FOR i = 7 TO nMaxSqrt - 1 STEP 2
    k2 = i + 3
    k3 = i + i - 4
    WHILE (k2 <= k3) AND ((i * k2) < nMax)
      k4 = INT(nMax / i)
      IF k3 < k4 THEN
        k4 = k3
      END IF
      FOR j = k2 TO k4 STEP 2
         v[(i * j) / 2] += 1;
      NEXT
      k2 = k2 + (i + 1)
      k3 = k3 + (i - 1)
    WEND
  NEXT
  FOR i = 8 TO nMaxSqrt - 1 STEP 2
    k2 = i + 3
    k3 = i + i - 4
    WHILE (k2 <= k3) AND ((i * k2) < nMax)
        k4 = INT(nMax / i)
        IF k3 < k4 THEN
          k4 = k3
        END IF
        FOR j = k2 TO k4
          v[(i * j) / 2] += 1;
        NEXT
        k2 = k2 + (i + 1)
        k3 = k3 + (i - 1)
    WEND
  NEXT
  FOR i = 0 TO 49999999 
    IF v[i] = s THEN
      RETURN i
      EXIT FUNCTION
    END IF
  NEXT
END FUNCTION

s = 200
PRINT "The smallest room size s for which T(s) = ", s, " is ", Tatami(s)
Compile Output

Code: Select all

[email protected]:~/bacon-dev/examples $ bacon tatami.bac
Converting 'tatami.bac'... done, 55 lines were processed in 0.083 seconds.
Compiling 'tatami.bac'... cc  -c tatami.bac.c
cc -o tatami tatami.bac.o -lbacon -L. -lm  
Done, program 'tatami' ready.
[email protected]:~/bacon-dev/examples $ 

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

Re: ScriptBasic to BaCon Converter

Wed Nov 20, 2019 8:59 pm

I got it to work finally.

tatami.bac

Code: Select all

OPTION BASE 0
GLOBAL nMax, nMaxSqrt TYPE long
GLOBAL v TYPE char ARRAY 50000000

nMax = 100000000
nMaxSqrt = INT(SQR(nMax))

FUNCTION Tatami(s)
  LOCAL i, j, k2, k3, k4 TYPE long
  FOR i = 7 TO nMaxSqrt - 1 STEP 2
    k2 = i + 3
    k3 = i + i - 4
    WHILE (k2 <= k3) AND ((i * k2) < nMax)
      k4 = INT(nMax / i)
      IF k3 < k4 THEN
        k4 = k3
      END IF
      FOR j = k2 TO k4 STEP 2
         INCR v[(i * j) / 2]
      NEXT
      k2 = k2 + (i + 1)
      k3 = k3 + (i - 1)
    WEND
  NEXT
  FOR i = 8 TO nMaxSqrt - 1 STEP 2
    k2 = i + 3
    k3 = i + i - 4
    WHILE (k2 <= k3) AND ((i * k2) < nMax)
        k4 = INT(nMax / i)
        IF k3 < k4 THEN
          k4 = k3
        END IF
        FOR j = k2 TO k4
          INCR v[(i * j) / 2]
        NEXT
        k2 = k2 + (i + 1)
        k3 = k3 + (i - 1)
    WEND
  NEXT
 FOR i = 0 TO 49999999 
    IF v[i] = s THEN
      RETURN i + i
      EXIT FUNCTION
    END IF
  NEXT
END FUNCTION

s = 200
PRINT "The smallest room size s for which T(s) = ", s, " is ", Tatami(s)
Output

Code: Select all

[email protected]:~/bacon-dev/examples $ bacon tatami.bac
Converting 'tatami.bac'... done, 50 lines were processed in 0.078 seconds.
Compiling 'tatami.bac'... cc  -c tatami.bac.c
cc -o tatami tatami.bac.o -lbacon -L. -lm  
Done, program 'tatami' ready.
[email protected]:~/bacon-dev/examples $ /usr/bin/time ./tatami
The smallest room size s for which T(s) = 200 is 85765680
15.66user 0.17system 0:15.86elapsed 99%CPU (0avgtext+0avgdata 50280maxresident)k
0inputs+0outputs (0major+24499minor)pagefaults 0swaps
[email protected]:~/bacon-dev/examples $ 

Return to “General programming discussion”