Poll

What method would you prefer to translate BASIC to a cross platform binary executable.

Daniel's uCalc Transform --> C++
1 (33.3%)
ScriptBasic --> Nimrod --> C (project abandoned by John)
0 (0%)
Nimrod --> C
0 (0%)
Python --> Nimrod --> C
1 (33.3%)
C BASIC (Charles Pegge's BASIC wrapper for C)
1 (33.3%)
Other (post your idea)
0 (0%)

Total Members Voted: 2

Author Topic: BAS2NIM  (Read 45970 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
BAS2NIM
« Reply #75 on: October 18, 2013, 10:08:30 AM »
I have merged all BAS2NIM threads into one. The project / post thread idea  didn't work out and became too confusing. As this progresses, I might create separate sub boards for each of the BAS2NIM translators. (SB, Python, uCalc, ...)

Please post all working code concepts here on All BASIC. The C9 IDE BAS2NIM site is not viewable to non-developer members.

 
« Last Edit: October 18, 2013, 10:24:33 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: BAS2NIM
« Reply #76 on: October 18, 2013, 10:39:23 AM »
Kent merged his BASIC to Nimrod translation maps into the ScriptBasic proof of concept example I posted. This example is to show how common BASIC syntax converts to Nimrod and not an example of a real BASIC program. (BASIC functions are part of a directive not standalone)

BASIC code example
Code: [Select]
LET A = "Hello BAS2NIM"
PRINT A,"\n"
ASC   "Z"
VAL   "123"
STR   321
MID   "One Two Three Four",  1, 3
MID   "One Two Three Four",  5, 3
MID   "One Two Three Four",  9, 5
MID   "One Two Three Four", 15, 4
LEFT  "One Two Three Four", 3
INSTR "One Two Three Four", "One"
INSTR "One Two Three Four", "Two"
INSTR "One Two Three Four", "Three"
INSTR "One Two Three Four", "Four"
UCASE "aLl mIxEd cAsE"
LCASE "aLl mIxEd cAsE"

ScriptBasic translator
Code: [Select]
'b2nV2.sb
'renamed don't want to mess up the original copy
PRINT "# output from command line: scriba b2nV2.sb test2.bas > test2.nim \n"
PRINT "# compile from command line: nimrod c test2.nim \n"
PRINT "# execute from command line: ./test2 \n"
PRINT "import strutils \n"


' BAS2NIM - ScriptBasic
' Last Update: 2013-10-15
' Contributors: John Spikowski & Kent Sarikaya

' BASIC keyword dictionary

keywords{"DIM"}[0]=ADDRESS(Convert_DIM())
keywords{"FOR"}[0]=ADDRESS(Convert_FOR())
keywords{"NEXT"}[0]=ADDRESS(Convert_NEXT())
keywords{"IF"}[0]=ADDRESS(Convert_IF())
keywords{"WHILE"}[0]=ADDRESS(Convert_WHILE())
keywords{"WEND"}[0]=ADDRESS(Convert_WEND())
keywords{"LET"}[0]=ADDRESS(Convert_LET())
' keywords{"GOTO"}[0]=ADDRESS(Convert_GOTO())
' keywords{"GOSUB"}[0]=ADDRESS(Convert_GOSUB())
' keywords{"RETURN"}[0]=ADDRESS(Convert_RETURN())
keywords{"PRINT"}[0]=ADDRESS(Convert_PRINT())
keywords{"END"}[0]=ADDRESS(Convert_END())
keywords{"ASC"}[0]=ADDRESS(Convert_ASC())
keywords{"VAL"}[0]=ADDRESS(Convert_VAL())
keywords{"STR"}[0]=ADDRESS(Convert_STR())
keywords{"MID"}[0]=ADDRESS(Convert_MID())
keywords{"LEFT"}[0]=ADDRESS(Convert_LEFT())
keywords{"INSTR"}[0]=ADDRESS(Convert_INSTR())
keywords{"UCASE"}[0]=ADDRESS(Convert_UCASE())
keywords{"LCASE"}[0]=ADDRESS(Convert_LCASE())
keywords{"UBOUND"}[0]=ADDRESS(Convert_UBOUND())
keywords{"LBOUND"}[0]=ADDRESS(Convert_LBOUND())


' BASIC Directives

SUB Convert_DIM
END SUB

SUB Convert_FOR
  IF BASIC LIKE "FOR * = * TO *" THEN
    expr[1] = JOKER(1)
    expr[2] = JOKER(2)
    expr[3] = JOKER(3)
    ' Translate
    PRINT Nimrod
  ELSE IF BASIC LIKE "FOR * = * TO * STEP *" THEN
    expr[1] = JOKER(1)
    expr[2] = JOKER(2)
    expr[3] = JOKER(3)
    expr[4] = JOKER(4)
    ' Translate
    PRINT Nimrod
  ELSE
    END
  END IF 
END SUB

SUB Convert_NEXT
'no NEXT in nimrod
END SUB

SUB Convert_IF
END SUB

SUB Convert_WHILE
END SUB

SUB Convert_WEND
'no WEND in nimrod
END SUB

SUB Convert_LET
  IF BASIC LIKE "LET *" THEN
    PRINT "var ", JOKER(1), "\n"
  ELSE
    END
  END IF
END SUB

' SUB Convert_GOTO
'no GOTO in nimrod
' END SUB

' SUB Convert_GOSUB
' END SUB

' SUB Convert_RETURN
' END SUB

SUB Convert_PRINT
  IF BASIC LIKE "PRINT *" THEN
    PRINT "stdout.write(", JOKER(1), ")\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_END
END SUB


' BASIC Functions

SUB Convert_ASC
  IF BASIC LIKE "ASC *" THEN
     S = MID(JOKER(1), 2, LEN(JOKER(1))-2)
     PRINT "echo(", "ord('", S, "'))\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_VAL
  IF BASIC LIKE "VAL *" THEN
    PRINT "echo(", "parseInt(", JOKER(1), "))\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_STR
  IF BASIC LIKE "STR *" THEN
    PRINT "echo(", "$", JOKER(1), ")\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_MID
  IF BASIC LIKE "MID *, *, *" THEN
    PRINT "echo(", JOKER(1),"[",JOKER(2)-1,"..",JOKER(2)+JOKER(3)-2,"])\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_LEFT
  IF BASIC LIKE "LEFT *, *" THEN
    PRINT "echo(", JOKER(1),"[0..",JOKER(2) - 1,"])\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_INSTR
  IF BASIC LIKE "INSTR *, *" THEN
    PRINT "echo(find(", JOKER(1), ",", JOKER(2), ")+1)\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_UCASE
  IF BASIC LIKE "UCASE *" THEN
    PRINT "echo(", JOKER(1),".toUpper())\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_LCASE
  IF BASIC LIKE "LCASE *" THEN
    PRINT "echo(", JOKER(1),".toLower())\n"
  ELSE
    END
  END IF
END SUB

SUB Convert_UBOUND
END SUB

SUB Convert_LBOUND
END SUB


' MAIN

filename_argument = COMMAND()
OPEN filename_argument FOR INPUT AS #1
WHILE NOT(EOF(1))
  LINE INPUT #1, BASIC
  BASIC = CHOMP(BASIC)
  sp = INSTR(BASIC, " ")
  test_keyword = LEFT(BASIC, sp -1)
  IF keywords{test_keyword}[0] <> undef THEN
    success = ICALL(keywords{test_keyword}[0])
  ELSE
    END
  END IF
WEND

Output

scriptbasic@nimrod:~/639761/projects/John $ scriba b2nV2.sb test2.bas
# output from command line: scriba b2nV2.sb test2.bas > test2.nim
# compile from command line: nimrod c test2.nim
# execute from command line: ./test2
import strutils
var A = "Hello BAS2NIM"
stdout.write(A,"\n")
echo(ord('Z'))
echo(parseInt("123"))
echo($321)
echo("One Two Three Four"[0..2])
echo("One Two Three Four"[4..6])
echo("One Two Three Four"[8..12])
echo("One Two Three Four"[14..17])
echo("One Two Three Four"[0..2])
echo(find("One Two Three Four","One")+1)
echo(find("One Two Three Four","Two")+1)
echo(find("One Two Three Four","Three")+1)
echo(find("One Two Three Four","Four")+1)
echo("aLl mIxEd cAsE".toUpper())
echo("aLl mIxEd cAsE".toLower())
scriptbasic@nimrod:~/639761/projects/John $ scriba b2nV2.sb test2.bas > test2.nim
scriptbasic@nimrod:~/639761/projects/John $ nimrod c test2.nim
config/nimrod.cfg(36, 2) Hint: added path: '/var/lib/stickshift/525387a75973caafd40003a0/app-root/data/.babel/pkgs/babel-0.1.0' [Path]
config/nimrod.cfg(36, 2) Hint: added path: '/var/lib/stickshift/525387a75973caafd40003a0/app-root/data/.babel/pkgs/' [Path]
Hint: used config file '/var/lib/stickshift/525387a75973caafd40003a0/app-root/data/639761/Nimrod/config/nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: test2 [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: operation successful (9446 lines compiled; 1.955 sec total; 16.163MB) [SuccessX]
scriptbasic@nimrod:~/639761/projects/John $ ./test2
Hello BAS2NIM
90
123
321
One
Two
Three
Four
One
1
5
9
15
ALL MIXED CASE
all mixed case
scriptbasic@nimrod:~/639761/projects/John $

kryton9

  • Guest
Re: BAS2NIM
« Reply #77 on: October 18, 2013, 12:15:06 PM »
Thanks John, for posting the code.
I tried and thought it was too long with all three projects in one. So I just attached the zip file.

Is there a way to turn on syntax highlighting as you and Charles did on the OxygenBasic Forums?
http://www.oxygenbasic.org/forum/index.php?topic=200.0
The you just specify like this with BBCODE:
Code: Text
  1.  code here
Code: Text
  1.  code here
Code: Python
  1.  code here
« Last Edit: October 18, 2013, 12:23:05 PM by kryton9 »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: BAS2NIM
« Reply #78 on: October 18, 2013, 12:48:34 PM »
There are too many versions of BASIC and other LIKE languages posted here to try and create keyword maps for. It would be a full time job.  I would like to get Nimrod and SB supported on C9 IDE for the ACE editor but I don't know if they will allow additions to what they already offer. Researching it.



kryton9

  • Guest
Re: BAS2NIM
« Reply #79 on: October 18, 2013, 11:30:46 PM »
There is an older cloud9 running on the beaglebone black. I played with it. So you could probably download cloud9 and put it on your server and then do what you want with it.

About syntax highlighting... in one of my posts, I had gotten (font)(size) -using () instead of square brackets to not confuse it--  to work, but it was just one time, all the others it failed.
I had courier 8 point and you could fit a lot of code and it kept the space nicely since monospaced.

Let me try it here without the (code) block to see if that was messing it up.

Without code block:
                  1                  2                  3
123456789012345678901234567890


With code block:
Code: [Select]
[font=andale mono][size=8pt]                  1                  2                  3
123456789012345678901234567890[/size][/font]

So the code block is messing it up. I will just remember to not use it from now on. Courier didn't work as well as andale mono tonight, so switched to that.

Hey, the voting changed,  I will see if I can change my vote too :)
« Last Edit: October 18, 2013, 11:42:33 PM by kryton9 »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Nimsic BASIC Language Translator(s)
« Reply #80 on: October 22, 2013, 03:06:43 AM »
AIR and I have been chipping away at creating a BASIC to Nimrod translator in Python and in ScriptBasic. Daniel has joined in the effort with his uCalc Transform set of tools. My hope is that Kent will join forces with Daniel and get that project off the ground.

I think the goal of the BAS2NIM project should be to create a BASIC like language syntax that is traditional enough that BASIC programmers will feel at home with it but with the goal of a smooth and easy translation to Nimrod code. If the goal was to target any one BASIC language then only a small group would benefit. The real end goal is to make BASIC programmers efficient Nimrod programmers without the pain of abruptly having to learn a new language. Think of BAS2NIM as training wheels for Nimrod.

Nimrod doesn't support GOTO / GOSUB so it isn't going to be part of Nimsic. :o  Armando is forging the path and I'm trying to follow his lead with SB. As the core language jells we will post some examples here to play with.

I would also like to see a USE Nimrod, C and ASM feature in the BASIC syntax as a pass-through using the translator. The hope again is the user becomes more comfortable with Nimrod using less BASIC wrapper code as time goes on.
« Last Edit: October 22, 2013, 03:14:12 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Shot down
« Reply #81 on: October 22, 2013, 02:16:20 PM »
Armando doesn't like the Nimsic name idea so the name is not going to be used. It sounded good at the time.  :)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Nimsic BASIC Language Translator(s)
« Reply #82 on: October 22, 2013, 03:50:44 PM »
The real end goal is to make BASIC programmers efficient Nimrod programmers without the pain of abruptly having to learn a new language. Think of BAS2NIM as training wheels for Nimrod.

I think it's just better to learn Nimrod.  It's a very powerful and flexible language.

Quote
I would also like to see a USE Nimrod, C and ASM feature in the BASIC syntax as a pass-through using the translator. The hope again is the user becomes more comfortable with Nimrod using less BASIC wrapper code as time goes on.

You're gonna have to write that part.  ;D

Nimrod's syntax is not difficult to pick up.  You can code it in a more Basic-like way [LEN(mystring)] or in a more OOP-like style [mystring.len()].

What I find really cool about it is if you create a function like:

Code: [Select]
proc stuff(a: string): string =
    return "This is your string: " & a

You can call it both ways:

Code: [Select]
echo stuff("Nimrod")

echo "Nimrod".stuff()

It's a trivial example, but it illustrates one of the things I like about this language.

So you can take Kent's basic.nim provided "left()" command, and instead of doing

Code: [Select]
left(mystring,5)
You can also do

Code: [Select]
mystring.left(5)
Which looks clearer, to me anyway.

A.

kryton9

  • Guest
Re: BAS2NIM
« Reply #83 on: October 23, 2013, 12:25:21 AM »
I agree just the little bit I worked with it. I don't think it is too hard for any basic programmer to come over to it.

There is lots of documentation, enough to get you started, but many more examples with commented code or articles would really help.
Once I get into it, I hope to document with examples as AIR just showed above. Seeing something is a lot easier to understand than reading about it
in depth.