Please write an interpreter that understands the commands input and output. Output shall do the same as echo, print or cout in real-world languages. Input shall do the same as read, input or cin: it shall return the string entered by the user. Your interpreter shall be able to execute

output("Hello "+input("What is your name ? "))

Execution will then look like:

What is your name ? Thorsten
Hello Thorsten

Note that in order to execute the outer command (output) the execution of the nested (inner) command (input) is needed. input returns Thorsten in this case and output prints it. Your take how you deal with parentheses and operators like +. Please make your interpreter extendable so do not scan for "input" and "output" as keywords.

closed as unclear what you're asking by Peter Taylor, Howard, manatwork, ProgramFOX, Timtech Jan 5 '14 at 17:58

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Right now this problem is rather underspecified. input() doesn't just read input, it apparently also does output as well. And it looks like this interpreter also needs to understand + as a concatenation operator. I'm assuming multiple concatenations can be chained together, left-associatively. Can parentheses be used to override that? Does the interpreter need to understand a single top-level command or are multiple commands permitted? – breadbox Jan 5 '14 at 9:23
  • input only inputs and returns a string. output outputs this string. And yes you can allow multiple concatenators and multiple top-level commands. Thanks, will edit my original text. – Thorsten Staerk Jan 5 '14 at 9:44
  • You should give a primary winning criterion and add a tag. Also the definition of the requirements seems to be very vague. If you are unsure about how to specify a puzzle or want to discuss any details there is a questions sandbox available which you should use before posting here. – Howard Jan 5 '14 at 12:00
  • The last two sentences in particular need a much clearer explanation. – Peter Taylor Jan 5 '14 at 16:25
  • This puzzle needs a grammar to answer questions such as: is input("What is your " + "name? ") valid? – Wayne Conrad Jan 5 '14 at 17:38

Python 3 (43 bytes)

I don't think it can be shorter than this. You may consider it to be cheating, but there is nothing in rules against this (at least, there wasn't when I wrote that).

from sys import*
output=print
eval(argv[1])

Sample output:

$ python3 interpreter.py 'output("Hello "+input("What is your name ? "))'
What is your name ? Thorsten
Hello Thorsten

Python 3 REPL (12 bytes)

Alternatively, if you are going to consider squeamish ossifrage's solution to be legit, this is a shorter solution having just 12 bytes. After you run a single line of code, the Python REPL turns into Your Language REPL.

output=print

Sample output:

$ python3
Python 3.3.2 (default, Nov  7 2013, 10:01:05) 
[GCC 4.8.1 20130814 (Red Hat 4.8.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> output=print
>>> output("Hello "+input("What is your name ? "))
What is your name ? Thorsten
Hello Thorsten
>>>
  • The challenge says "write an interpreter", not "use your language's built-in interpreter". – nitro2k01 Jan 5 '14 at 11:12

Ruby

Code golf was not specified, so I didn't golf it.

$funcs = {
    input: ->arg{ print arg; gets.chomp },
    output: ->arg{ puts arg; arg }
}

def pass code
    changed = false
    code.gsub!(/([a-z]+)\("([^"]*)"\)/) { changed = true; "\"#{$funcs[$1.to_sym][$2]}\"" }
    code.gsub!(/"([^"]*)"\+"([^"]*)"/) { changed = true; "\"#{$1 + $2}\"" }
    changed
end

code = 'output("Hi "+input("Enter your name: "))'
nil while pass code

pass is a function that takes the code and scans for either a function call or a string concatenation. If it finds either of those, it returns true, otherwise nothing was changed so it returns false to signify that you should stop passing.

  • Nice. I believe it is correct for all cases of the (currently implied) grammar. The "changed" variable could be elimitated with original_code = code.dup ... code != original_code. – Wayne Conrad Jan 5 '14 at 17:49

Javascript console: 25 bytes

This is too easy.

output=alert;input=prompt
  • Where is the interpreter? I'm almost sure you want to run eval(), but I don't see it here. – Konrad Borowski Jan 5 '14 at 10:05
  • I was using the Javascript console. I've edited the answer to reflect this. – squeamish ossifrage Jan 5 '14 at 10:06
  • The challenge says "write an interpreter", not "use your language's built-in interpreter". – nitro2k01 Jan 5 '14 at 11:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.