Author Topic: New BASIC, for fun  (Read 13331 times)

Marcus

  • Guest
New BASIC, for fun
« on: April 25, 2012, 04:12:05 AM »
For the fun of it, I've started writing a BASIC compiler in naalaa, targeting the naalaa virtual machine of course. My first idea was to write an yabasic compiler, but after reading the documentation of yabasic I cried "NO NO!!!" and had to drink three beers to calm down. Anyhow, I will try to make this language more BASIC-like than naalaa (even if it means less orthogonality), and the source code will be open. Numbers will be typeless (no floats or ints), but I will still separate numbers from strings.

The language is useless, so far. You can assign the value of an expression to a global variable, no more. The resulting value of every assignment is written to the output window.

For example:

Code: [Select]
foo = (3+17)/2 + 5
bar = 3 + foo

Outputs:
15
18

If you want to compile any other program than "test.bas" (by clicking on compile.exe), you should run compile.exe from the command prompt (yes, Windows only) and pass the name of the file to compile and run like:

Code: [Select]
compile my_test.bas
No executable is created at the moment. The compiler only creates a naalaa bytecode file (temp.nvm) and runs it.

The sourcecode of the compiler is in "compile.txt."

EDIT: When you compile a program, the bytecode is written to the window. You then have to press any key to run the actual program.
« Last Edit: April 30, 2012, 09:53:01 AM by Marcus »

SteveA

  • Guest
Re: New BASIC, for fun
« Reply #1 on: April 25, 2012, 06:53:52 AM »
Hey Marcus,

This is great!
I'm glad to see you doing this.
I love working with byte code compilers.

You've been working on this..., this didn't happen just over night.
Very interesting.

Marcus

  • Guest
Re: New BASIC, for fun
« Reply #2 on: April 25, 2012, 10:09:46 AM »
I glanced at the naalaa compiler while writing this, of course. But naalaa is a real mess (those who say yabasic is haven't seen sh*t) so I'll try not to look too much at it.

Offline John

  • Forum Support / SB Dev
  • Posts: 3004
    • ScriptBasic Open Source Project
Re: New BASIC, for fun - OT
« Reply #3 on: April 25, 2012, 03:46:46 PM »
Marcus,

[Your Sponsor]
I don't mean to hijack your thread but if you lose interest in this for fun project, I have a proposal for you.

ScriptBasic already has a preprocessor as a debugger. (single step, breakpoint, view variables, ...) It would be really cool if you could create an interactive version of ScriptBasic. (like the original QuickBasic version) I would say 90% plus of the code is already done and working for years.

Back to the MarcusBasic show.
[/Your Sponsor]

I'm just happy that a programmer of your skill level would hang with us Basic dudes.


Marcus

  • Guest
Re: New BASIC, for fun
« Reply #4 on: April 27, 2012, 02:58:23 AM »
I was given some spare time today, so I added a couple of things to the compiler (the attachment in the first post has been updated). You can now use floating point constants, not just integer constants. The floating point constants are cached and saved into the binary (a requirement of the vm, as constant floating point values can't be part of an instruction). The numeric factor function also handles some basic system calls for cos, sin, tan etc. And I also added string expressions and string constants. As before, every assignment causes an ouput. This is the example program:

Code: [Select]
a$ = "nink"
b$ = "Hegonkllo wogonkrld" - (("go") + (a$ - "ni")) + "!"
dummy = 13 + |-50 + sin(90.0)*13|

This is the output:
  nink
  Hello world!
  50.0

The thing I like the most is the use of a pair of | characters for absolute values, like |<expr>|, instead of abs(<expr>). That's the way you type it in the universal mathematical language :) But I guess that also means I just stepped off the BASIC-like bus.

I hope this doesn't turn into a spam-like thread. I'll try to keep a low update frequency.

@JRS
I'm not sure if your words are ironic or not (I'm a swede). But I truely know very little about compiler design and I have a REAL problem with understanding other peoples code (that's why I'm not the open source kind of toad). So I don't think I'd do any good on a project like that :)  But thanks for the proposal.

I created naalaa because I wanted to be able to write retro games quickly. Now I just want to reuse the virtual machine for another, very experimental, language. For example, I'll try to implement the subroutine calling convention that I got so eager about on bp.org.
« Last Edit: April 27, 2012, 03:42:51 AM by Marcus »

Marcus

  • Guest
Re: New BASIC, for fun
« Reply #5 on: April 28, 2012, 11:13:04 AM »
I added logic expressions and IF-statements today (updated the zip in the first post once more). I'll go for SELECT/CASE next as naalaa doesn't have it - I'll see if it's possible to generate jump tables for this :)

Programs may behave strange as I haven't cared for line endings yet:

Code: [Select]
foo
=5
IF
foo = 5 THEN bar =
3
END
IF

is a valid program. Sorry.
« Last Edit: April 28, 2012, 11:21:36 AM by Marcus »

Offline John

  • Forum Support / SB Dev
  • Posts: 3004
    • ScriptBasic Open Source Project
Re: New BASIC, for fun
« Reply #6 on: April 28, 2012, 11:20:18 AM »
Is your current IF/THEN/ELSE structure nestable?

Is this going to be a Windows only Basic?

Marcus

  • Guest
Re: New BASIC, for fun
« Reply #7 on: April 28, 2012, 11:23:43 AM »
Is your current IF/THEN/ELSE structure nestable?

Is this going to be a Windows only Basic?

Yes they can be nested. As naalaa works on linux (well, very slow on some systems, haven't figured that one out yet) this will work there too.

EDIT:
I also just added commands for text input and output, ex:

Code: [Select]
INPUT "Give me a value: ", foo, "And another value: ", bar, "And your name: ", name$
PRINT
PRINT "The first value plus the second value equals ", foo + bar, "."
PRINT "And I know that your name is ", name$, "."
IF name$ = "Marcus" OR name$ = "marcus" THEN
PRINT "... What a lovely name!"
END IF
PRINT
PRINT "Press any key to exit ..."

This was fun, because in naalaa there is no INPUT statement (atleast not in this form). And line endings are now treated correctly (you can also use ; for a virtual line ending).
« Last Edit: April 29, 2012, 04:38:17 AM by Marcus »

Marcus

  • Guest
Re: New BASIC, for fun
« Reply #8 on: April 30, 2012, 10:00:53 AM »
When I came home from work today I implemented all but one of the loop structures that I found on some page about qbasic:

while <expr> ... wend
do ... loop
do while <expr> ... loop
do until <expr> ... loop
do ... loop while <expr>
do ... loop until <expr>

(I've once again updated the attachment in the first post.)

What I'm wondering about right now is how the for <assignment> to <expr> [step <???>] ... next works in "standard" basic? In naalaa I have no 'step' feature, instead you can use 'to' and 'downto' for 1 and -1 increments. My question is if the step value should be an expression or limited to a constant? It makes quite a damn difference :)

Iteration example program:

Code: [Select]
a = 0
PRINT "while a <= 4 / loop"
WHILE a < 4
PRINT a
a = a + 1
WEND

a = 0
PRINT "do while a < 4 / loop"
DO WHILE a < 4
PRINT a
a = a + 1
WEND

a = 0
PRINT "do until a = 4 / loop"
DO UNTIL a = 5
PRINT a
a = a + 1
LOOP

a = 0
PRINT "do / until a >= 5"
DO
PRINT a
a = a + 1
LOOP UNTIL a >= 5

a = 0
PRINT "do / while a < 4"
DO
PRINT a
a = a + 1
LOOP WHILE a < 5

PRINT
PRINT "Press any key to exit ..."

Every loop oviously outputs 0..4.

EDIT: Is it possible to write a stack clean version of for/to/step/next that needn't special treatment on a break in a subroutine?
« Last Edit: April 30, 2012, 11:27:29 AM by Marcus »

SteveA

  • Guest
Re: New BASIC, for fun
« Reply #9 on: April 30, 2012, 11:47:04 AM »
What I'm wondering about right now is how the for <assignment> to <expr> [step <???>] ... next works in "standard" basic?

If I understand the question...,
that is simple to illustrate:

You would assume the default STEP value to be "1", as in:
Code: [Select]
FOR indx=1 TO 100 STEP 1
....
NEXT indx

however, you can't assume that the incrementation is always going to be by a factor of "1".
As in "C":
Code: [Select]
for(indx=0; indx <= 100; indx++)
{
   ....
}

what about:
Code: [Select]
for(indx=0; indx <= 100; indx += 2) ?

In the above, you have:
for(indx=0; indx <= 100; indx += 2)
   -FOR-  /     -TO-     /   -STEP-


So, the STEP value is any value (or evaluation) other that "1".

If the STEP value is "1", then the step value need not be stated, as the default is "1".

Code: [Select]
FOR indx = 1 TO 100
    ....
NEXT indx


The STEP value would not be a constant.
It has to be the a value given, a variable, the result of an expression, or the default if none is stated.

FOR x = 1 TO 100    (default "1")
FOR x = 1 TO 100  STEP 3
FOR x = 1 TO 100  STEP MyVar
FOR x = 1 TO 100  STEP [expression]


NOTE:
once the entire FOR/NEXT expression is evaluated, prior to the first pass, the STEP value cannot change.
"x" is the only value that is allowed to change during the loop process.

As in:
FOR x = val1 TO val2  STEP val3
« Last Edit: April 30, 2012, 12:02:31 PM by SteveA »

Offline John

  • Forum Support / SB Dev
  • Posts: 3004
    • ScriptBasic Open Source Project
Re: New BASIC, for fun
« Reply #10 on: April 30, 2012, 11:56:04 AM »
Code: [Select]
a = 5

FOR x = 1 to a
  IF x = 4 THEN a = 4
  PRINT x,":",a,"\n"
NEXT

In your new Basic, would you see 5 iterations or 4?


Marcus

  • Guest
Re: New BASIC, for fun
« Reply #11 on: April 30, 2012, 01:09:37 PM »
NOTE:
once the entire FOR/NEXT expression is evaluated, prior to the first pass, the STEP value cannot change.
"x" is the only value that is allowed to change during the loop process.

Okay, that is the standard?

This is how it works in naalaa:

Code: [Select]
for a = b to/downto c
 ...
next

b is evaluated once and c is evaluated before each branch.

So in this case:

Code: [Select]
for a = b to c step d
  ...
next

, you're telling me that the d should only be evaluated the first time? I know it's good for the stack and would make execution faster. But do you think it could harm backward compability to evaluate both expression c and d before each branch?

I want to make this language fully compatible with qbasic and ... well, yabasic, but I still want it to be better and more flexible :)
« Last Edit: April 30, 2012, 01:30:54 PM by Marcus »

SteveA

  • Guest
Re: New BASIC, for fun
« Reply #12 on: April 30, 2012, 02:03:45 PM »
..., you're telling me that the d should only be evaluated the first time?

I want to make this language fully compatible with qbasic and ... well, yabasic, but I still want it to be better and more flexible :)
Yes.
However, Yabasic is very non-standard (and peculiar) in many things it does.
I wouldn't base much of anything on what Yabasic does.

Marcus

  • Guest
Re: New BASIC, for fun
« Reply #13 on: April 30, 2012, 03:15:49 PM »
Yes.
However, Yabasic is very non-standard (and peculiar) in many things it does.
I wouldn't base much of anything on what Yabasic does.


But yabasic is obviously so ... "Lollipop, lollipop, oh lolli lolli lolli lollipop!"

SteveA

  • Guest
Re: New BASIC, for fun
« Reply #14 on: April 30, 2012, 03:46:15 PM »
I just checked my Basic Reference Book: "The Basic Handbook",
in every case and example STEP remains constant and is not re-evaluated.