samedi 9 mai 2015

Implementing the equivalent of c++ cin in javascript?

I am trying to design a small javascript interpreter that interpret the print and read statement. (equivalent of cout and cin in c++).

I have implemented the print statement but now I am having difficulties in implementing the read statement. The problem is that even I have a read statement the interpreter finished with all the print statement and than gives me the opportunity to enter the input text.

So what I think is that i should manage to events. -test button click and -ENTER key click.

I am trying to manage read statement like this:

 var read = lines[i].match(rxRead);            
        if(read !==null){//means there is a read statement
        s += "$('#output').focus();";              
        s+= "myRead=function(ab){
            var keycode = (ab.keyCode ? ab.keyCode : ab.which);
            if(keycode == '13'){
            read_lines = $('#output').val().split('\\n');
            alert(read_lines[read_lines.length-1]);
           }};";

       s+= "$('#output').keypress(function (){ 
            myRead(event);});";

       continue;} 

But this is not solving the problem. Can you give me any hint or any idea how to do this? Where can I start? I have been stuck on this for hours I am I not findin what logic to follow?

Here is my DEMO. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire