lkesteloot / turbopascal

Web-based Turbo Pascal compiler.
https://www.teamten.com/lawrence/projects/turbo_pascal_compiler/
BSD 2-Clause "Simplified" License
480 stars 54 forks source link

readln #6

Open lucianolorenti opened 6 years ago

lucianolorenti commented 6 years ago

Hello. First of all I want to congratulate you on the code. It is really clear and well written.

I am writing beacuse I want to implement some blocking input functions (like read and readln). I have tried some things but I wanted to know If you could give me a hint about how you think it is the best way to do it. Thank you

lkesteloot commented 6 years ago

Do you want to read general strings or just numbers? The compiler uses the UCSD Pascal opcodes, and they unfortunately don't have good string support. (E.g., adding two strings together.) So if you want to do more with strings, that might be a non-trivial amount of work. If you just want to input a number, let me know and I can look into that.

lkesteloot commented 6 years ago

I've checked in a commit that adds ReadLn. It's not the original ReadLn, which takes parameters to write into. It's a function that returns a string (the line that was read). If you want the original, either submit a sample Pascal program for it, or modify mine to do what you want. The hard part was suspending the execution of the program while getting user input. Modifying it to put the line into its parameters shouldn't be too hard. Mostly changes to builtin.js. Instead of pushing the result, write it into a parameter that was passed in by reference. (See Inc in that same file.)

Sample program:

Program StringTest;

var s : String;

begin
    WriteLn('What is your name?');
    s := ReadLn();
    WriteLn('Hello', s);
end.
lucianolorenti commented 6 years ago

@lkesteloot Thank you very much! I will check out your code.