|
| 1 | +// pscweb.js : support library for Pseudocode Compiler web interface |
| 2 | + |
| 3 | +var input_index = 0; |
| 4 | + |
| 5 | +function readline() { |
| 6 | + let input = document.getElementById('input').elements['user_input'].value; |
| 7 | + var input_read = ''; |
| 8 | + var more = input.indexOf(',', input_index); |
| 9 | + if (more != -1) { |
| 10 | + let begin = input_index; |
| 11 | + input_index = more + 1; |
| 12 | + input_read = input.substring(begin, more); |
| 13 | + } |
| 14 | + else { |
| 15 | + input_read = input.substring(input_index); |
| 16 | + } |
| 17 | + print(input_read); |
| 18 | + return input_read; |
| 19 | +} |
| 20 | + |
| 21 | +function print(line) { |
| 22 | + let max_lines = 28, max_length = 60; |
| 23 | + let lines = document.getElementById('program_output').innerHTML.split('\n').length; |
| 24 | + if (lines > max_lines) { |
| 25 | + alert('Too many lines of output. Please clear the output window!'); |
| 26 | + throw new Error('Window full'); |
| 27 | + } |
| 28 | + while(line.length > max_length) { |
| 29 | + document.getElementById('program_output').innerHTML += line.substring(0, max_length) + '\n'; |
| 30 | + line = line.substring(max_length); |
| 31 | + } |
| 32 | + document.getElementById('program_output').innerHTML += line + '\n'; |
| 33 | +} |
| 34 | + |
| 35 | +function clear_output() { |
| 36 | + document.getElementById('program_output').innerHTML = ''; |
| 37 | +} |
| 38 | + |
| 39 | +function base64enc(rawutf8) { |
| 40 | + let encweb = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_='; |
| 41 | + var enc = '', pos = 0, len = rawutf8.length; |
| 42 | + while ((len - pos) > 2) { |
| 43 | + enc += encweb[(rawutf8.charCodeAt(pos) >> 2) & 0x3f]; |
| 44 | + enc += encweb[((rawutf8.charCodeAt(pos) << 4) | (rawutf8.charCodeAt(pos + 1) >> 4)) & 0x3f]; |
| 45 | + enc += encweb[((rawutf8.charCodeAt(pos + 1) << 2) | (rawutf8.charCodeAt(pos + 2) >> 6)) &0x3f]; |
| 46 | + enc += encweb[rawutf8.charCodeAt(pos + 2) & 0x3f]; |
| 47 | + pos += 3; |
| 48 | + } |
| 49 | + if ((len - pos) == 2) { |
| 50 | + enc += encweb[(rawutf8.charCodeAt(pos) >> 2) & 0x3f]; |
| 51 | + enc += encweb[((rawutf8.charCodeAt(pos) << 4) | (rawutf8.charCodeAt(pos + 1) >> 4)) & 0x3f]; |
| 52 | + enc += encweb[(rawutf8.charCodeAt(pos + 1) << 2) &0x3f]; |
| 53 | + enc += encweb[64]; |
| 54 | + } |
| 55 | + if ((len - pos) == 1) { |
| 56 | + enc += encweb[(rawutf8.charCodeAt(pos) >> 2) & 0x3f]; |
| 57 | + enc += encweb[(rawutf8.charCodeAt(pos) << 4) & 0x3f]; |
| 58 | + enc += encweb[64]; |
| 59 | + enc += encweb[64]; |
| 60 | + } |
| 61 | + return enc; |
| 62 | +} |
| 63 | + |
| 64 | +function run() { |
| 65 | + input_index = 0; |
| 66 | + let script = document.createElement('script'); |
| 67 | + let query_string = base64enc(document.getElementById('input').elements['program_source'].value); |
| 68 | + script.setAttribute('src', '/cgi-bin/psc?' + query_string); |
| 69 | + document.body.appendChild(script); |
| 70 | +} |
| 71 | + |
0 commit comments