11// pscweb.js : support library for Pseudocode Compiler web interface
22
3- var input_index = 0 ;
3+ var input_index = 0 , pseudocode_examples ;
44
55function readline ( ) {
6- let input = document . getElementById ( 'input' ) . elements [ ' user_input'] . value ;
6+ let input = document . getElementById ( 'user_input' ) . value ;
77 var input_read = '' ;
88 var more = input . indexOf ( ',' , input_index ) ;
99 if ( more != - 1 ) {
@@ -14,15 +14,20 @@ function readline() {
1414 else {
1515 input_read = input . substring ( input_index ) ;
1616 }
17- print ( input_read ) ;
17+ if ( input_read != '' ) {
18+ print ( input_read ) ;
19+ }
20+ else {
21+ print ( '#Empty input#' ) ;
22+ }
1823 return input_read ;
1924}
2025
2126function print ( line ) {
22- let max_lines = 28 , max_length = 60 ;
23- let lines = document . getElementById ( 'program_output' ) . innerHTML . split ( '\n' ) . length ;
27+ let max_lines = 26 , max_length = 60 ;
28+ let lines = document . getElementById ( 'program_output' ) . innerHTML . split ( '\n' ) . length + line . length / max_length ;
2429 if ( lines > max_lines ) {
25- alert ( 'Too many lines of output. Please clear the output window! ' ) ;
30+ alert ( 'Too many lines of output! Please clear the output window. ' ) ;
2631 throw new Error ( 'Window full' ) ;
2732 }
2833 while ( line . length > max_length ) {
@@ -36,6 +41,10 @@ function clear_output() {
3641 document . getElementById ( 'program_output' ) . innerHTML = '' ;
3742}
3843
44+ function clear_input ( ) {
45+ document . getElementById ( 'input' ) . elements [ 'program_source' ] . value = '' ;
46+ }
47+
3948function toutf8 ( str ) {
4049 var urifmt = '' + encodeURIComponent ( str ) , x = function ( s ) {
4150 return String . fromCharCode ( parseInt ( s . substring ( 1 ) , 16 ) ) ;
@@ -77,3 +86,28 @@ function run() {
7786 document . body . appendChild ( script ) ;
7887}
7988
89+ function createmenu ( ) {
90+ var container = document . getElementById ( 'examples' ) ;
91+ if ( pseudocode_examples == undefined ) {
92+ container . innerHTML = '<p onclick="alert(\'Could not load pscexamples.js\');">No examples</p>' ;
93+ return ;
94+ }
95+ for ( var e = 0 ; e != pseudocode_examples . length ; e ++ ) {
96+ container . innerHTML += '<p onclick="menuload(\'' + pseudocode_examples [ e ] . name + '\');">' + pseudocode_examples [ e ] . name + '</p>\n' ;
97+ }
98+ }
99+
100+ function menuload ( program ) {
101+ var container = document . getElementById ( 'input' ) . elements [ 'program_source' ] ;
102+ if ( container . value != '' ) {
103+ alert ( 'Please clear input window first!' ) ;
104+ return ;
105+ }
106+ for ( var e = 0 ; e != pseudocode_examples . length ; e ++ ) {
107+ if ( ( pseudocode_examples [ e ] . name == program ) && ( pseudocode_examples [ e ] . code != undefined ) ) {
108+ container . value = pseudocode_examples [ e ] . code ;
109+ return ;
110+ }
111+ }
112+ alert ( 'Could not load program ' + program ) ;
113+ }
0 commit comments