Page 1 of 1

IF

Posted: Mon Mar 01, 2010 4:54 pm
by izidor
I am working on my programing language and it would help if I'd know how to implement IF-END IF in it. So are there any examples that someone is willing to show?

Posted: Mon Mar 01, 2010 5:06 pm
by burger2227
IF you are unfamiliar with IF...END IF, you should not be creating another programming language. You should USE another programming language! :roll:

LOL

Posted: Mon Mar 01, 2010 11:17 pm
by bongomeno
An IF statement can be pain in the ass to create. As can LET (or no LET :lol: ).

the most easy form of IF THEN to create is like this:
IF something=something THEN linenumber
that jumps to a line number if the condition is true

If you want to create END IF, then you will need to count the number of IF statements on a stack (variable) and decrement it as they are completed. So as you go through each IF like so...

If a=b then
If b=c then
If c=d then
print "COOL!"
Endif
Endif
Endif

...You will just check the condition. If false, then move on to the nearest Endif without executing any code. Then do the next one.

If you dont plan of "stacking" IFs and having them work properly, then you could just jump to the nearest END IF.

That probably was not the BEST description, but it should give you an idea...

Posted: Tue Mar 02, 2010 11:50 am
by izidor
@burger2227 If anyone ever said that you are funny I would bet that he had brain damage.

@bongomeno Thanks for the idea!

Posted: Tue Mar 02, 2010 12:53 pm
by izidor
I found a way how to make IF command with 2 variables just like told me, but i have trouble with GOTO. It would work like this:

Code: Select all

start:
variable1 = 2
variable2 = 3
IF,variable1<variable2,THEN,GOTO,start:
But I don't know how to make GOTO because the program would need to search the code for the "start:" and then execute the code beneath it. And that i don't really know how to do. Any help bongomeno?

Posted: Tue Mar 02, 2010 1:16 pm
by BigBadKing
some help is available. first, include these three lines at the start
of your program:

Code: Select all

 dim shared Labels$(1000)
 dim shared LineNum(1000)
 GOTOs = 0
the third line is used to say how many labels we defined. insert the
following snippet of code before where you begin to translate commands
and do what they say:

Code: Select all

 for i = 0 to TotalLineNumbers
  ' if at the end of the line is a colon, then make a label
  if right$(Line$(i), 1) = ":" then
   GOTOs = GOTOs + 1
   LineNum(GOTOs) = i
   Labels$(GOTOs) = Line$(i)
  end if
 next i
replace TotalLineNumbers with the variable which you use to store
how many lines the program is. and also replace Line with the string
array on which you use to store the program. now, whenever you
need to jump to a specific label. use the following code:

Code: Select all

 for i = 1 to GOTOs
  if Labels$(GOTOs) = Line$(count+1) then count = LineNum(GOTOs)
 next i
replace count with the variable you use to loop through the program.

if anything is not clear then just ask!

Posted: Tue Mar 02, 2010 1:53 pm
by izidor
Ok, I understand most of it. When my program reads a line that starts with "IF" it stores and checks variables and other things, it stores the name of the label (witch in my code example is "start:") in Label$. And my question is how to link (connect) Label$ with third part of your code. Thanks!

Posted: Tue Mar 02, 2010 8:40 pm
by bongomeno
You dont need a goto statement or a label. You could just follow classic BASIC syntax like this:

10 LET X=X+1
20 PRINT X
30 IF X<10 THEN 10

That program would count to 10, but the IF statement only jumps to the line.

So all you would have to do is parse out the line number.

If your code array is a max of 100 lines, then it would be 0-99.
So you take the line number in the IF statement and subtract 1 and move your line pointer to that number.

For an actual code example, you can check my site for my TINY BASIC interpreter in the downloads, it might answer a few of your questions.
All code written in it can be run under Qbasic without any modification.

Posted: Wed Mar 03, 2010 7:01 am
by izidor
Link for TinyBASIC doesn't work :(

Posted: Wed Mar 03, 2010 9:50 am
by bongomeno
I had the same problem, you have to right click and do Save As for some wierd reason.

Posted: Wed Mar 03, 2010 12:59 pm
by izidor
@bongomeno, can i e-mail you my code so you can see what is my problem?

Posted: Wed Mar 03, 2010 8:34 pm
by bongomeno
sure, I will be glad to take a look at it!

Posted: Thu Mar 04, 2010 8:37 am
by BigBadKing
i think a working example will do it:

Code: Select all

 dim Labels$(1000)
 dim LineNum(1000)
 dim Line$(1000) 
 GOTOs = 0
 LengthOfFile = 0

 open command$ for input as #1
  while not eof(1)
   input #1, Line$(LengthOfFile)
   Line$(LengthOfFile) = ucase$(Line$(LengthOfFile))
   LengthOfFile = LengthOfFile + 1
  wend
 close #1

 for count = 0 to TotalLineNumbers 
  ' if at the end of the line is a colon, then make a label 
  if right$(Line$(count), 1) = ":" then 
   GOTOs = GOTOs + 1 
   LineNum(GOTOs) = count
   Labels$(GOTOs) = Line$(count) 
  end if 
 next count

 for count = 0 to LengthOfFile
  select case Line$(count)
   case "GOTO"
    ' GOTO code is here  
     for i = 1 to GOTOs 
     if Labels$(GOTOs) = Line$(count+1) then count = LineNum(GOTOs) 
    next i 
  ' Anything else goes here
  end select
 next count
i hope this explains something, to make it work. compile it
and make a file named on whatever you like, then make the
contents of the file like this:
test:
goto,test:
warning: this enters into an infinite loop, to stop it. press the
ALT+ENTER.

i hope this is clear.

Posted: Thu Mar 04, 2010 9:52 am
by bongomeno
You ould just press CTRL+BREAK to return to the code. Doesnt ALT+ENTER exit?

Posted: Thu Mar 04, 2010 10:59 am
by BigBadKing
Bongomeno, you didnt notice that i said COMPILE the example
i gave. CTRL+BREAK works only in the QB environment.

Posted: Thu Mar 04, 2010 11:58 am
by izidor
BigBadKing, your code works perfectly, but when I add

Code: Select all

CASE "beep"
BEEP

after GOTO part it doesn't work. Why?

Posted: Fri Mar 05, 2010 3:37 am
by BigBadKing
i tried Beep personally, it doesnt work for me either. just
BEEP doesnt work, i tried everything else and they all worked.
if you want a print command:

Code: Select all

 case "PRINT"
  if Line$(count+1) <> "" then print Line$(count+1) else print
any questions?
btw, i think BEEP doesnt work if the PC doesnt support PC
SPEAKER. mine doesnt anyway. :(

Posted: Fri Mar 05, 2010 9:28 am
by izidor
But what i have more inputs in a command like this:

Code: Select all

sound,2,400