write an interpreter understanding nested commands

1

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.

Thorsten Staerk

Posted 2014-01-05T08:42:16.837

Reputation: 261

Question was closed 2014-01-05T17:58:09.077

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 – 2014-01-05T09:23:53.620

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 – 2014-01-05T09:44:58.710

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 – 2014-01-05T12:00:11.503

The last two sentences in particular need a much clearer explanation. – Peter Taylor – 2014-01-05T16:25:25.163

This puzzle needs a grammar to answer questions such as: is input("What is your " + "name? ") valid? – Wayne Conrad – 2014-01-05T17:38:20.760

Answers

2

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
>>>

Konrad Borowski

Posted 2014-01-05T08:42:16.837

Reputation: 11 185

The challenge says "write an interpreter", not "use your language's built-in interpreter". – nitro2k01 – 2014-01-05T11:12:05.787

1

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.

Doorknob

Posted 2014-01-05T08:42:16.837

Reputation: 68 138

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 – 2014-01-05T17:49:17.523

-1

Javascript console: 25 bytes

This is too easy.

output=alert;input=prompt

r3mainer

Posted 2014-01-05T08:42:16.837

Reputation: 19 135

Where is the interpreter? I'm almost sure you want to run eval(), but I don't see it here. – Konrad Borowski – 2014-01-05T10:05:00.373

I was using the Javascript console. I've edited the answer to reflect this. – r3mainer – 2014-01-05T10:06:49.837

The challenge says "write an interpreter", not "use your language's built-in interpreter". – nitro2k01 – 2014-01-05T11:10:54.563