Author Topic: Construct ?  (Read 3596 times)

0 Members and 1 Guest are viewing this topic.

Aurel

  • Guest
Construct ?
« on: May 08, 2018, 08:50:19 AM »
Hi Charles

is this contruct valid in o2:
it is from C file i would like to translate to o2

Function GetNextChar()
 {
 while ((NextChar = getchar()) = " " )
 }
End Function




Mike Lobanovsky

  • Guest
Re: Construct ?
« Reply #1 on: May 08, 2018, 12:35:49 PM »
Assuming NextChar is a global char variable and getchar() is a C function that gets the next character from stdin,

Sub GetNextChar()
    NextChar = Chr(getchar())
    While NextChar = " "
        NextChar = Chr(getchar())
    Wend
End Sub


The loop runs until the character that getchar() fetches appears any other than a space.
« Last Edit: May 08, 2018, 04:57:37 PM by Mike Lobanovsky »

Aurel

  • Guest
Re: Construct ?
« Reply #2 on: May 08, 2018, 01:02:44 PM »
Yes i think in same direction but original C code is as is presented

Mike Lobanovsky

  • Guest
Re: Construct ?
« Reply #3 on: May 08, 2018, 01:08:53 PM »
No, it should have been

while ((NextChar = getchar()) == ' '); // there's a space in between the single quotes

to be valid in C.
« Last Edit: May 08, 2018, 04:55:36 PM by Mike Lobanovsky »

Charles Pegge

  • Guest
Re: Construct ?
« Reply #4 on: May 08, 2018, 02:17:47 PM »
I would do it like this:

Code: [Select]
function GetNextChar() as string
string nextchar
do
  nextchar=getchar()
  if nextchar<>" " then exit do
loop
return nextchar
end function

But I suspect GetChar returns bytes:

Code: [Select]
function GetNextChar() as byte
byte nextchar
do
  nextchar=getchar()
  if nextchar<>32 then exit do
loop
return nextchar
end function

Another construct, recently introduced, compares favourably with C for brevity, without twisting the brain :)

do { nextchar=getchar() } while nextchar=32

Code: [Select]
function GetNextChar() as byte
byte nextchar
do { nextchar=getchar() } while nextchar=32
return nextchar
end function
« Last Edit: May 08, 2018, 02:35:11 PM by Charles Pegge »

Mike Lobanovsky

  • Guest
Re: Construct ?
« Reply #5 on: May 08, 2018, 03:12:26 PM »
Ah yes, in fact C language getchar() (lowercase!) returns an int that can be reduced to an unsigned char. NextChar is obviously global in Aurel's C code, and the procedure needs not be dressed as a BASIC function because the rest of his parser is apparently going to use this global NextChar without any special assignment.

So, if we abstract from Aurel's literal while, then one of many possible versions might be

Sub GetNextChar()
    Do
        NextChar = Chr(getchar())
        Break When NextChar <> " " ' 8) :D
    Loop
End Sub


If NextChar is a byte rather than char or string, then just drop Chr() and compare against 32.

[UPD]

Haha yessssss! :D

Another construct, recently introduced, compares favourably with C for brevity ...

do { nextchar=getchar() } while nextchar=32

...

It also compares favorably with every other advanced BASIC (cf. Do [While|Until]/Loop [While|Until]) but has been somehow overlooked in Oxygen's many looping options until now. :)

So,

Sub GetNextChar()
    Do { _ // it probably won't work w/o line continuation
        NextChar = Chr(getchar()) _ // ditto
    } While NextChar = " "
End Sub


is my vote (even if I consider those curly braces and the associated line continuations out of place in a BASIC context) with my note about the NextChar declaration and byte/char/string/32 still holding true.

_____________________________________

(Charles, what about normal BASIC Do [While|Until]/Loop [While|Until] instead of all the existing verbosity?)
« Last Edit: May 08, 2018, 05:03:22 PM by Mike Lobanovsky »

Aurel

  • Guest
Re: Construct ?
« Reply #6 on: May 08, 2018, 08:50:46 PM »
Hi guys and thank you both for replys.
This old C code is from old PC Magazine 1993
and i try to translate to o2 ,
I don't have code than just picture of code..so i type  ::)
I ask because i see that this while loop is empty what looks confusing.
And yes NextChar is defined as INT.
More morbid thing i for/loop with construct for  ( ; ; )
what is that ?
for each ..or for any
ok here is i think whole code :

Code: [Select]
'EVAL.C expression evaluator
$ filename "EVALC.exe"
include "rtl32.inc"
#lookahead
'******************************************
' EXPRESSION EVALUATOR - PC Magazine 1993
'******************************************

def false 0
def true 1

int NextChar
int contin = true

! Factor()     as float
! Expression() as float
! Term()       as float

'************************************************************
'  GetNextChar - Reads chars until a non-space char is found
'************************************************************
Function GetNextChar()
 {
 while ((NextChar = getchar()) = " " )
 }
End Function

'**************************************************************
' Expression - A Recursive routine that evaluates an expression
' EXPRESSION = <EXPRESSION> + TERM | <EXPRESSION> - <TERM>
'**************************************************************

Function Expression() as float
float value
value = Term()

for (;;) {
select NextChar
case " "
GetNextChar()
break
case "+"
GetNextChar()
value = value + Term()
                    continue
case "-"
GetNextChar()
value = value - Term()
                    continue
case else
return value
end select
}

'****************************************************************
' Term - Handles Multiplication and division
' <TERM> = <TERM> * <FACTOR> | <TERM> div <FACTOR> | <FACTOR>
'****************************************************************

Function Term() as float
float value, divisor
value = Factor()
for (;;) {
select NextChar
case " "
GetNextChar()
exit for
case "*"
GetNextChar()
value = value * Factor()
                    continue
case "^"
GetNextChar()
value = value - Factor()
                    continue
case "/"
GetNextChar()
if ((divisor = Factor()) <> 0
   value = value / divisor
else
print " DIVISION BY ZERO !"
Exit Function
end if
                    continue

case else
return value
end select
}
End Function

'*********************************************************
' Factor - Handles numbers,minus signs and parens
' <FACTOR> = <EXPRESSION> | <VARIABLE> | <CONSTANT>
'*********************************************************

Function Factor() as float
float value = 0
int count = 0
int i
int d_point = false

if (( NextChar <= "9") AND (NextChar >= "0"))
while ((NextChar <= "9") AND (NextChar >= "0"))
value = value * 10 + NextChar - "0"
NextChar = getchar()
if (d_point)
count++
if (NextChar = ".")
NextChar = getchar()
d_point = true
end if
end if
wend
for i = 0 To (i < count)
value = value / 10
return value
    next
else
select NextChar
case "-"
GetNextChar()
return -1 * Factor()
case "("
GetNextChar()
value = Expression()
if (NextChar <> ")"
print " MISMATCHED PARENTHES !"
Exit Function
else
NextChar = getchar()
return value
end if
case "."
d_point = true

case else
contin = false
end select

return 0
End Function

'*******************************************************
' Main - Program entry point
'*******************************************************

Aurel

  • Guest
Re: Construct ?
« Reply #7 on: May 08, 2018, 09:30:45 PM »
One more thing
Is exit(0) should be exit function ?
and
break should be exit for ?
right?

Charles Pegge

  • Guest
Re: Construct ?
« Reply #8 on: May 08, 2018, 10:25:50 PM »
Mike,

A few more single-liner variations:

do : nextchar=getchar() : loop while nextchar=32

do : nextchar=getchar() : loop until nextchar<>32

do : nextchar=getchar() : exit when nextchar<>32 : loop

do : nextchar=getchar() : exit if not nextchar=32 : loop


Aurel,

for ( ; ; ) Looks like a do loop

Code: [Select]
do
   ... 'cases with continue do
   exit do 'instead of exit for
loop
 

exit(0) 'could be return 0 its not in your code
« Last Edit: May 08, 2018, 10:43:47 PM by Charles Pegge »

Mike Lobanovsky

  • Guest
Re: Construct ?
« Reply #9 on: May 09, 2018, 01:49:41 AM »
Hi Aurel,

Re. C loops

1. In C, "empty" while (........); is used to set up a loop whose entire code can be expressed in between the condition statement parentheses and thus won't need additional code lines in the "body" of the loop proper.

Thus

while ((NextChar = getchar()) == ' ');

means
  • get a char (in fact, not a char-long string but rather a numeric byte) from stdin
  • assign it to NextChar
  • check if the assignment returns literal 32 (unlike BASIC, matching C parentheses embracing an assignment define the scope that returns the value of this assignment) where = denotes assignment, == denotes equality, and ' ' with a space in between denotes numerical 32 and is equivalent to BASIC Asc(" ")
  • loop back if the value of assignment is equal to 32, otherwise break (i.e. stop looping)
2. Conversely, for (;;) ........ ; is often used to set up a loop in which everything occurs within the loop body rather than in its declaration, e.g.

int i = 0;
for (;;) {
    if (++i == 5) break;
}


whereby the loop is going to break (it means the code will exit from the loop) on its 5th iteration.

3. Summing up, C language "empty" for (;;);, while(1); and do{}while(1); would all mean "loop forever". Exactly which "infinite loop" the programmer is going to use in their code depends entirely on their coding habits.


Re. C sources

No problem, please post the C picture and we will try to turn it into usable O2 code. I suppose you'd also like to use it for parsing a memory string rather than the console buffer, wouldn't you? And no, the code isn't complete as it lacks the executable's main() function or its O2 global level code equivalent. But we can manage to write it ourselves if we know exactly what you're going to parse -- the console's keyboard input buffer or a memory string.
« Last Edit: May 09, 2018, 03:19:59 AM by Mike Lobanovsky »

Mike Lobanovsky

  • Guest
Re: Construct ?
« Reply #10 on: May 09, 2018, 01:57:08 AM »
Hi Charles,

A few more single-liner variations:

do : nextchar=getchar() : loop while nextchar=32

do : nextchar=getchar() : loop until nextchar<>32

do : nextchar=getchar() : exit when nextchar<>32 : loop

do : nextchar=getchar() : exit if not nextchar=32 : loop

Wow! That's exactly what I've been after all this time! :D

And I suppose all of them can also be used as multi-liners, can't they? By the looks of them, there's nothing that can cause syntactically unresolvable ambiguity for the compiler even if they would contain other multiply nested loops.

Charles Pegge

  • Guest
Re: Construct ?
« Reply #11 on: May 09, 2018, 02:48:55 AM »
Hi Mike,

I implemented this syntax about 3 months ago. They all use the same nestable blocks system as the prior loops and conditionals in o2tran.bas

Here are some more in multiline format:

http://www.oxygenbasic.org/forum/index.php?topic=1548.0

I also considered extending this syntax for goto and gosub but I am uncertain  it would enhance the language: Do you like your imperatives to come at the beginning of a sentence? :)

gosub xx when a>b
« Last Edit: May 09, 2018, 02:58:30 AM by Charles Pegge »

Mike Lobanovsky

  • Guest
Re: Construct ?
« Reply #12 on: May 09, 2018, 02:59:56 AM »
http://www.oxygenbasic.org/forum/index.php?topic=1548.0

Oh, I must have overlooked that thread for some reason among the others, probably due to no follow-up...

Quote
... I am uncertain  it would add clarity to the language:

gosub xx when a>b

Why not? The keyword is already there and looks clear enough syntactically IMO.

Quote
Do you like your imperatives to come at the beginning of a sentence? :)

Anyway we already have them in some of the loops and in your Do{}While one-liner. :)

Charles Pegge

  • Guest
Re: Construct ?
« Reply #13 on: May 09, 2018, 03:59:11 AM »

Okay, it's only 11 lines. It will be available later today :)

Aurel

  • Guest
Re: Construct ?
« Reply #14 on: May 10, 2018, 11:47:59 AM »
Thanks guys code is let say simple with stupid C constructs
in fact it is recursive descent parser but in some places should be int not string
ok here is image:

so break should break loop like exit while or exit for ..right?