k-legacy icon indicating copy to clipboard operation
k-legacy copied to clipboard

I/O does not seem to work properly

Open grosu opened this issue 9 years ago • 6 comments

Somehow, the output buffer does not seem to be properly flushed to the screen:

D:\github\grosu\k\k-distribution\tutorial\1_k\4_imp++\lesson_4>kompile imp.k
D:\github\grosu\k\k-distribution\tutorial\1_k\4_imp++\lesson_4>krun ..\lesson_1\tests\io.imp
10
Input two numbers: 20
Their sum is: 30
<T> <k> . </k> <env> x |-> 1 y |-> 2 </env> <store> 1 |-> 10 2 |-> 20 </store> <
in> ListItem ( #buffer ( "" ) ) ListItem ( "on" ) ListItem ( #istream ( 0 ) ) </
in> <out> ListItem ( #ostream ( 1 ) ) ListItem ( "on" ) ListItem ( #buffer ( ""
) ) </out> </T>
D:\github\grosu\k\k-distribution\tutorial\1_k\4_imp++\lesson_4>krun ..\lesson_1\
tests\sum-io.imp


10
Add numbers up to (<= 0 to quit)? 100
Sum = 55
Add numbers up to (<= 0 to quit)? 1000
Sum = 5050
Add numbers up to (<= 0 to quit)? 0
Sum = 500500
Add numbers up to (<= 0 to quit)? <T> <k> halt ; ~> while ( true ) { print ( "Ad
d numbers up to (<= 0 to quit)? " , .Ids ) ; n = read ( ) ; if ( n <= 0 ) { halt
 ; } else { s = 0 ; while ( ! ( n <= 0 ) ) { s = s + n ; n = n + -1 ; } print (
"Sum = " , ( s , ( "\n" , .Ids ) ) ) ; } } </k> <env> s |-> 2 n |-> 1 </env> <st
ore> 1 |-> 0 2 |-> 500500 </store> <in> ListItem ( #buffer ( "" ) ) ListItem ( "
on" ) ListItem ( #istream ( 0 ) ) </in> <out> ListItem ( #ostream ( 1 ) ) ListIt
em ( "on" ) ListItem ( #buffer ( "" ) ) </out> </T>
D:\github\grosu\k\k-distribution\tutorial\1_k\4_imp++\lesson_4>

I'm using Windows 10.

┆Issue is synchronized with this Asana task

grosu avatar Jan 16 '16 18:01 grosu

It works for my machine, Mac OS X 10.9. I suspect you didn't wait enough until it prints the outputs. I had to wait a few (1 or 2) seconds to see the prompt: Input two numbers:. It may be due to the recent slowdown of the java backend. Let me know if you still have the problem.

fix:lesson_4 daejunpark$ krun ../lesson_1/tests/io.imp
Input two numbers: 10 20
Their sum is: 30
<T> <k> . </k> <env> x |-> 1 y |-> 2 </env> <store> 1 |-> 10 2 |-> 20 </store> <in> ListItem ( #buffer ( "" ) ) ListItem ( "on" ) ListItem ( #istream ( 0 ) ) </in> <out> ListItem ( #ostream ( 1 ) ) ListItem ( "on" ) ListItem ( #buffer ( "" ) ) </out> </T>
fix:lesson_4 daejunpark$ krun ../lesson_1/tests/sum-io.imp
Add numbers up to (<= 0 to quit)? 10
Sum = 55
Add numbers up to (<= 0 to quit)? 100
Sum = 5050
Add numbers up to (<= 0 to quit)? 0
<T> <k> halt ; ~> while ( true ) { print ( "Add numbers up to (<= 0 to quit)? " , .Ids ) ; n = read ( ) ; if ( n <= 0 ) { halt ; } else { s = 0 ; while ( ! ( n <= 0 ) ) { s = s + n ; n = n + -1 ; } print ( "Sum = " , ( s , ( "\n" , .Ids ) ) ) ; } } </k> <env> s |-> 2 n |-> 1 </env> <store> 1 |-> 0 2 |-> 5050 </store> <in> ListItem ( #buffer ( "" ) ) ListItem ( "on" ) ListItem ( #istream ( 0 ) ) </in> <out> ListItem ( #ostream ( 1 ) ) ListItem ( "on" ) ListItem ( #buffer ( "" ) ) </out> </T>

daejunpark avatar Jan 17 '16 02:01 daejunpark

That's not the problem. I waited 1 min for io.imp. Then I pushed a few ENTER. Then I typed in a 10 followed by ENTER. Then the text which was supposed to be printed at the beginning was printed immediately as soon as I hit ENTER. Then I typed the second number. Then the result was printed immediately:

D:\github\grosu\k\k-distribution\tutorial\1_k\4_imp++\lesson_4>krun ..\lesson_1\
tests\io.imp




10
Input two numbers: 20
Their sum is: 30
<T> <k> . </k> <env> x |-> 1 y |-> 2 </env> <store> 1 |-> 10 2 |-> 20 </store> <
in> ListItem ( #buffer ( "" ) ) ListItem ( "on" ) ListItem ( #istream ( 0 ) ) </
in> <out> ListItem ( #ostream ( 1 ) ) ListItem ( "on" ) ListItem ( #buffer ( ""
) ) </out> </T>
D:\github\grosu\k\k-distribution\tutorial\1_k\4_imp++\lesson_4>

grosu avatar Jan 17 '16 03:01 grosu

In the VM which was assigned to us for CS427, this bug is not reproducible. In the Ubuntu Linux, this is a bug and the fix is to replace the line 87 in imp.k "context print(HOLE:AExp, _);" with "context print(HOLE:AExp, AEs:AExps); In Mac OSX, this is also a bug but the fix above does not work for OSX. The fix is adding a "[read]" at the end of the line 55. We suspect this bug may relate to the underlying operating system.

yeahyy avatar Dec 01 '16 17:12 yeahyy

@yeahyy, could you explain why it was failing before and why your changed fixed the issues?

cos avatar Dec 01 '16 23:12 cos

Actually, I don't why it was failing and why the fix works since I have very limited knowledge on K and the bug may be caused by operating system. What I did is to compare this .k file with other working .k files(other IO .k files work, only this one fails.) and find out what difference could be the possible reason cause this failure.

yeahyy avatar Dec 02 '16 00:12 yeahyy

Yes, this bug seems so deep with some problem with the underlying system. So what we have done is to format the imp.k file so that the bug won't appear in OSX system and Ubuntu system

JoeSun940303 avatar Dec 11 '16 02:12 JoeSun940303