Author Topic: MY-BASIC  (Read 24319 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 1641
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #195 on: March 14, 2016, 01:41:47 AM »
It's a bug in Script BASIC I need to fix.

I have known about it for years.  ???

Offline wangrenxin

  • BASIC Developer
  • Posts: 87
Re: MY-BASIC
« Reply #196 on: March 17, 2016, 04:16:23 AM »
Hi,

I found a way to simulate SELECT CASE in MY-BASIC without adding new keywords. Implemented as a select function as follow, which accepts a variable argument list with a boolean expression, a lambda expression, another boolean expression, and another lambda... The advantage is it accepts not only one constant equivalence case condition, but also a boolean expression with more sub conditions for one case; the disadvantage is it requires a very ugly single line code calling the select function. Maybe it's necessary to add multi-line code linking to MB now, just like what the underline _ does in VB.

Code: MyBASIC
  1. default = true ' For the default case
  2. def select(...)
  3.         l = len(...)
  4.         if l mod 2 <> 0 then
  5.                 print "Parameters not matched.";
  6.                 return
  7.         endif
  8.         for i = 0 to l - 1 step 2
  9.                 if ... then ' Evaluates a condition
  10.                         _ = ... ' Gets a lambda and executes it
  11.                         _()
  12.                         return
  13.                 else
  14.                         _ = ... ' Ignores a lambda argument
  15.                 endif
  16.         next
  17. enddef
  18. a = 85
  19. select(a >= 85, lambda () (print "A";), a >= 75, lambda () (print "B";), a >= 60, lambda () (print "C";), default, lambda () (print "D";))
  20.  

Offline John

  • Forum Support / SB Dev
  • Posts: 1641
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #197 on: March 17, 2016, 09:11:19 PM »
Script BASIC doesn't have a SELECT/CASE structure either. It does have a flexible IF that works just as well. Actually better as it doesn't have the traditional CASE compare / range / type restrictions.

Code: Script BASIC
  1. a = 85
  2. IF a >= 85 THEN
  3.   PRINT "A\n"
  4. ELSE IF a >= 75 THEN
  5.   PRINT "B\n"
  6. ELSE IF a >= 60 THEN
  7.   PRINT "C\n"
  8. ELSE
  9.   PRINT "D\n"
  10. END IF
  11.  


jrs@laptop:~/sb/sb22/test$ scriba simcase.sb
A
jrs@laptop:~/sb/sb22/test$


Code: Script BASIC
  1. DECLARE SUB mb_init ALIAS "mbas_init" LIB "mb"
  2. DECLARE SUB mb_dispose ALIAS "mbas_dispose" LIB "mb"
  3. DECLARE SUB mb_open ALIAS "mbas_open" LIB "mb"
  4. DECLARE SUB mb_close ALIAS "mbas_close" LIB "mb"
  5. DECLARE SUB mb_load_str ALIAS "mbas_load_str" LIB "mb"
  6. DECLARE SUB mb_run ALIAS "mbas_run" LIB "mb"
  7.  
  8. mb_code = """
  9.    default = true ' For the default case
  10.    def select(...)
  11.            l = len(...)
  12.            if l mod 2 <> 0 then
  13.                    print "Parameters not matched.";
  14.                    return
  15.            endif
  16.            for i = 0 to l - 1 step 2
  17.                    if ... then ' Evaluates a condition
  18.                            _ = ... ' Gets a lambda and executes it
  19.                            _()
  20.                            return
  21.                    else
  22.                            _ = ... ' Ignores a lambda argument
  23.                    endif
  24.            next
  25.    enddef
  26.    a = 85
  27.    select(a >= 85, lambda () (print "A";), a >= 75, lambda () (print "B";), a >= 60, lambda () (print "C";), default, lambda () (print "D";))
  28. """
  29. mb_init()
  30. mb_open()
  31. mb_load_str(mb_code)
  32. mb_run()
  33. mb_close()
  34. mb_dispose()
  35.  


jrs@laptop:~/sb/sb22/mybasic$ scriba mbcase.sb
A
jrs@laptop:~/sb/sb22/mybasic$


« Last Edit: March 17, 2016, 11:35:04 PM by John »

Offline wangrenxin

  • BASIC Developer
  • Posts: 87
Re: MY-BASIC
« Reply #198 on: March 17, 2016, 11:49:22 PM »
It does have a flexible IF that works just as well. Actually better as it doesn't have the traditional CASE compare / range / type restrictions.

Yes, I can't agree more. IF has done it well.

John, does SB support separating a single statement into multi-lines? I'm a little bit refusing to add these features; but not quite sure.

Offline John

  • Forum Support / SB Dev
  • Posts: 1641
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #199 on: March 18, 2016, 12:45:33 AM »
Quote
does SB support separating a single statement into multi-lines?

Yes it does with the _ character.

Code: Script BASIC
  1. FUNCTION test (arg1, _
  2.                arg2, _
  3.                arg3)
  4.  

Offline wangrenxin

  • BASIC Developer
  • Posts: 87
Re: MY-BASIC
« Reply #200 on: April 22, 2016, 04:54:05 AM »
Finally, I added Unicode token support to MY-BASIC! For instance I can use Chinese variable now:

Code: MyBASIC
  1. &#21464;&#37327; = 22
  2. &#21478;&#19968;&#20010;&#21464;&#37327; = 7
  3. print &#21464;&#37327; / &#21478;&#19968;&#20010;&#21464;&#37327;;
  4.  

Offline wangrenxin

  • BASIC Developer
  • Posts: 87
Re: MY-BASIC
« Reply #201 on: April 22, 2016, 04:55:50 AM »
Finally, I added Unicode token support to MY-BASIC! For instance I can use Chinese variable now:

Code: MyBASIC
  1. &#21464;&#37327; = 22
  2. &#21478;&#19968;&#20010;&#21464;&#37327; = 7
  3. print &#21464;&#37327; / &#21478;&#19968;&#20010;&#21464;&#37327;;
  4.  

Well, I meant this:

Offline wangrenxin

  • BASIC Developer
  • Posts: 87
Re: MY-BASIC
« Reply #202 on: December 06, 2016, 07:28:57 AM »
Added an inline data sequence module, now it has a simple equivalent to retro DATA, READ, RESTORE statements :)

Offline John

  • Forum Support / SB Dev
  • Posts: 1641
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #203 on: December 08, 2016, 05:45:08 PM »
Great to see you still plugging away at the project. This is a nice enhancement for retro style BASIC compatibility.

Thanks for keeping us in the loop here on All BASIC.

Offline wangrenxin

  • BASIC Developer
  • Posts: 87
Re: MY-BASIC
« Reply #204 on: December 08, 2016, 06:52:37 PM »
Yep, more stuff to add, although the procedure is very slow ;D
BTW. it also supports REM statement now for comment.