String Manipulation Interpreter

11

3

Summary

A new string manipulation language has been made, using only the characters $+#-!*|@>! Your task is to implement an interpreter for it in as few bytes as possible.

Input

A string, which is a single line of this language. This can be taken in any reasonable way (stdin, function parameter, command line argument etc.), or as a predefined variable. If the program asks for user input, accept all user input it asks for from stdin and nothing more, see below. You may assume it is a valid program.

Output

Whatever the language would output, specifications below. You must output a string, in any reasonable way (stdout, function output, etc.), or a variable value. When the language outputs explicitly, this must go to stdout. Standard loopholes are banned.

Language Specifications

Processing and Syntax

The language has a very simple form of processing as it does only string manipulation: it starts with an empty string (""), and changes it with each term. A term is made up of one or two parts: a function (below) followed by possibly a parameter(below), which edits its behaviour. Terms are separated by pipes (|). You may assume it will not be an empty program, and no term will be empty. You should output the value at the end of the program.

Functions

The language has just 6 functions, as shown below. Each function either accepts one or zero parameters.

  • + concatenate strings (takes one string parameter, concatenates it to the current value)
  • ! reverse the character order of the current value (no parameter)
  • * repeat the string (takes one integer parameter, repeats the current value that many times)
  • - removes all occurrences of a value (takes one string parameter, removes all occurrences of it from the current value)
  • $ [pseudo-]randomly shuffles the current value (no parameter)
  • < output the current value to stdout (no parameters)

Values

These are the values that may be passed to functions, represented by regex that would match them:

  • @[^|]* a string literal, including any character other than pipes. It may be empty.
  • #[0-9]+ an integer literal
  • > the next line of stdin. If used with *, convert to integer.

Test Cases

╔════════════════════════╤═════════════╤══════════════╗
║code                    │input        │output        ║
╟────────────────────────┼─────────────┼──────────────╢
║+>|!|+@hello|*>         │13           │31hello31hello║
║                        │2            │              ║
╟────────────────────────┼─────────────┼──────────────╢
║+>|+@abcdefg|$          │hello        │hcloeebafdlg  ║
╟────────────────────────┼─────────────┼──────────────╢
║+@how areyou|-@o|->     │w            │h areyu       ║
╟────────────────────────┼─────────────┼──────────────╢
║+@out|<|*#3             │             │out           ║
║                        │             │outoutout     ║
╟────────────────────────┼─────────────┼──────────────╢
║+>                      │what ever 345│what ever 345 ║
╟────────────────────────┼─────────────┼──────────────╢
║+@$pe<i@l|<|-@$pe<i@l|+>│A|$o $pe<!@| │$pe<i@l       ║
║                        │             │A|$o $pe<!@|  ║
╟────────────────────────┼─────────────┼──────────────╢
║<|+>|!|<                │input text   |              ║
║                        │             │txet tupni    ║ 
║                        │             │txet tupni    ║
╟────────────────────────┼─────────────┼──────────────╢
║+@>#                    │             |>#            ║
╚════════════════════════╧═════════════╧══════════════╝

Note that test case 2 is random, so any permutation of the characters in it is valid. Also, the outputs in the table are seperated by newlines, but your program doesn't have to do the same. The last value in each case the the final output.

Example (Un-golfed) python interpreter

Try it online! IMO better if you run it through IDLE or whatever you use. (I golfed it down to 424 bytes after, but I'm sure you lot can do better).

Artemis still doesn't trust SE

Posted 2019-04-06T17:29:29.970

Reputation: 525

2Allowing input to already be in a variable is non-standard, as is allowing output to be in one. – Jonathan Allan – 2019-04-06T19:10:29.373

Your examples seem to print a newline everytime < is encountered. Is this mandatory? – Embodiment of Ignorance – 2019-04-06T20:14:41.553

Will the program have newlines in it? Because if it can, it invalidates Chas Brown's answer – Embodiment of Ignorance – 2019-04-06T23:56:13.000

2

For your future questions, please consider avoiding cumbersome I/O formats. Limiting input to stdin costs extra bytes in some languages and doesn't bring much to the challenge.

– Arnauld – 2019-04-07T08:08:41.803

@EmbodimentofIgnorance No – Artemis still doesn't trust SE – 2019-04-07T08:38:54.210

@ArtemisFowl FYI, the minimum recommended time to Sandbox a challenge for is 7 days. – Shaggy – 2019-04-07T11:07:08.900

@Shaggy Thought I read 72 hrs somewhere. Oh well. – Artemis still doesn't trust SE – 2019-04-07T11:17:32.720

Suggested test case : +@foo> – digEmAll – 2019-04-07T14:02:30.570

@ArtemisFowl: because > can be either an input command (but always following another command like +) or inside a literal; while < can be inside a literal but when indended as command it will be alone in between pipes. I suggested this example because I had problem managing this difference ;) – digEmAll – 2019-04-07T18:25:27.620

1@digEmAll How's the one I just added, +@>#? I used # aswell. – Artemis still doesn't trust SE – 2019-04-07T18:30:55.197

@ArtemisFowl: yep, perfect ! – digEmAll – 2019-04-07T19:20:49.367

Answers

3

Ruby -palF\|, 146 142 bytes

r='';$F.map{|i|x=i[1]!=?>?i[2..-1]:gets.chomp;eval %w[r.reverse! r*=x.to_i 0 $><<r r=r.chars.shuffle*'' r.gsub!x,'' r+=x][i[0].ord*5%11]};$_=r

Try it online!

Port of Chas Brown's Python answer. Does not print newlines after output.

As usual, Ruby 2.6 version will be 2 bytes shorter with endless range indexing (i[2..]).

Kirill L.

Posted 2019-04-06T17:29:29.970

Reputation: 6 693

6

R, 287 286 273 269 bytes

function(C,x='',`[`=gsub,`!`=intToUtf8,`?`=utf8ToInt){for(k in el(strsplit(C,'\\|'))){B=eval(parse(t='^.'['','(?<=.)>$'['readLines(,1)','[@#](.+)'['"\\1"',k],,T]]));x=switch((?substr(k,1,1))%%13-2,strrep(x,B),paste0(x,B),,B['',x,f=T],!rev(?x),print(x),,!sample(?x))};x}

Try it online!

  • -1 thanks to @Kirill L.
  • -4 thanks to @Giuseppe

Unrolled code and explanation :

function(C){                                      # C is the string manipulation expression
  x = ''                                          # initialize x = ''
  tokens = el(strsplit(C,'\\|'))                  # split C by pipe '|'
  for(k in tokens){                               # for each token k
    arg2 = k
    arg2 = gsub('[@#](.+)','"\\1"',k)             # replace @X or #X with "X" (in quotes)
    arg2 = gsub('(?<=.)>$','"readLines(,1)"',
                 arg2,perl=T)                     # replace > with readLines(,1)
    arg2 = gsub('^.','',arg2)                     # remove the first character
    B = eval(parse(t=arg2))                       # evaluate the string : this will be our 
                                                  # second argument B
    A = substr(k,1,1)                             # take the first character : 
                                                  # i.e. the main command (+,-,! etc)
    x = switch(A,                                 # switch on the main command, execute the 
            '+'=paste0(x,B),                      # corresponding expression and 
            '!'=intToUtf8(rev(utf8ToInt(x))),     # store the result into x
            '*'=strrep(x,B),                      # Note: in the actual code we switch on
            '-'=B['',x,f=T],                      # the utf8 value MOD 13-2 of the command
            '$'=intToUtf8(sample(utf8ToInt(x))),
            '<'=print(x)
        )
    }
    x                                             # return x (and print it implicitly)
}

digEmAll

Posted 2019-04-06T17:29:29.970

Reputation: 4 599

3

Python 2, 215 219 209 208 bytes

from random import*
I=raw_input;o=''
for t in I().split('|'):p=t[1:]=='>'and I()or t[2:];exec"o=o[::-1] o*=int(p) 0 print(o) o=''.join(sample(o,len(o))) o=o.replace(p,'') o+=p".split()[ord(t[0])*5%11]
print o

Try it online!

-4 because raw_input is required.

9 bytes thanks to Embodiment of Ignorance; 1 byte from Ascii-only.

Chas Brown

Posted 2019-04-06T17:29:29.970

Reputation: 8 959

Input other than the program must be from stdin, as specified in the question. – Artemis still doesn't trust SE – 2019-04-06T21:38:47.393

I use Python 3, but as far as I was aware, that usage of input requires raw_input. Correct me if I am wrong.. – Artemis still doesn't trust SE – 2019-04-06T21:42:02.590

According to Py 2.7 docs: input([prompt]) Equivalent to eval(raw_input(prompt)). This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. – Artemis still doesn't trust SE – 2019-04-06T21:48:43.250

So, the issue you're raising is something like here, where the input strings would need to be quoted - rather than unquoted as in a 'true' stdin situation. Again, usually the I/O rules are a bit lax; but I will modify.

– Chas Brown – 2019-04-06T21:59:34.260

Thanks for changing. You could save a few bytes by changing to Python 3 and using your old code + 3 bytes for brackets, but... +1 anyways – Artemis still doesn't trust SE – 2019-04-06T22:03:04.147

This, as a standalone program, does not seem to be able to handle multiline programs, because by using stdin, you don't know when the program part ends and the input part starts – Embodiment of Ignorance – 2019-04-06T23:50:35.703

@Embodiment of Ignorance: See OP's TIO Implementation. Uses the same approach: program is the first line of input.

– Chas Brown – 2019-04-06T23:57:27.420

208 – ASCII-only – 2019-04-07T03:34:39.693

2

C# (Visual C# Interactive Compiler), 305 bytes

a=>{string s="",d,g;foreach(var c in a.Split('|')){g=$"{c,2}";d=g[1]==62?ReadLine():g.Substring(2);var z=c[0]%14;s=z<1?string.Concat(Enumerable.Repeat(s,int.Parse(d))):z<2?s+d:z<4?s.Replace(d,""):z<5?s:z<6?string.Concat(s.Reverse()):string.Concat(s.OrderBy(_=>Guid.NewGuid()));Write(z==4?s:"");}return s;}

Try it online!

Embodiment of Ignorance

Posted 2019-04-06T17:29:29.970

Reputation: 7 014

1

Javascript, 292 267 bytes

f=(p)=>{c='';p.split`|`.map(l=>{v=l.substr(2);v=l[1]=='#'?parseInt(v):l[1]=='>'?prompt():v;c={'+':_=>c+v,'-':_=>c.split(v).join``,'*':_=>c.repeat(v),'$':_=>[...c].sort(_=>.5-Math.random()).join``,'!':_=>[...c].reverse().join``,'<':_=>alert(c)||c}[l[0]]();});return c}

JSFiddle

Johan du Toit

Posted 2019-04-06T17:29:29.970

Reputation: 1 524

Test case 6 doesn't quite work... – Artemis still doesn't trust SE – 2019-04-25T11:33:49.187

1@ArtemisFowl, Thanks, the regex was not working correctly and switching to split..join saved a couple of bytes. – Johan du Toit – 2019-04-25T14:34:32.447

1

Perl 5 -MList::Util=shuffle -pF/\|/, 220 217 210 183 bytes

map{$,=s/..//r;$\=reverse$\if/^!/;$,ne""||chomp($,=<>),$\=~s/\Q$,//g if/^-/;say$\if/^</;$\=join"",shuffle$\=~/./g if/^\$/;$\.=$,eq""?<>=~s/\n//r:$,if/^\+/;$\x=$,eq""?<>:$,if/^\*/}@F}{

Try it online!

Xcali

Posted 2019-04-06T17:29:29.970

Reputation: 7 671