Interpret +p code

Inspired by the recent craze over another two character language, ;#

Intro

According to community consensus, acceptable answers on this site must use programming languages that, at minimum:

1. Can determine if a natural number is prime
2. Can add two natural numbers together
3. Can represent a list/tuple of numbers, as well as a single number

For purposes of this challenge, we will ignore #3. Therefore, the simplest language that could be used on this site (ignoring #3) would have exactly two commands, isPrime and add. For ease of interpretation and byte count, let's assign isPrime to p and add to +. Thus, we have our language, +p. Your challenge is to interpret some +p code.

Behavior

• + the add instruction takes two numbers, adds them, and outputs the result
• p the isPrime instruction takes a single number, and outputs 1 if it is prime, and 0 if it is not

Rules

• You must write a program/function which, given a string of characters, interprets that string as +p code. You may assume well-formed input (only + and p characters).
• Input is flexible. You may take in the program as a string, character array, integer array of codepoints, etc. Input for the program being interpreted is also flexible. You may take in an integer array, and use up entries as the program executes, or each instruction (+ and p) may individually request input. You may assume there will be enough input for every instruction. Input is guaranteed to consist of numbers between 0 and 200 (but your algorithms should theoretically work for any positive integer input).
• Output is also flexible. You may print the results, return them as a list, return a string that contains all the results, etc. If printed or returned as a string, output must be separated by some non-digit, consistent separator, such as a new line, tab, space, or , character. You may have a trailing separator or some trailing whitespace. Also, p's output may be any truthy or falsey value, as defined by the language you are working in, rather than 1 or 0.
• The interpreter may or may not terminate (if it is a full program), but it must stop printing after all instructions are interpreted. (It cannot continue printing the separator forever, or a null character, etc.).
• These standard loopholes are forbidden by default
• This is , the answer with the least bytes wins

Test Cases

Program: +
Input: [56, 50]
Output: 106
----------------------------------
Program: p
Input: 
Output: 0
----------------------------------
Program: p
Input: 
Output: 1
----------------------------------
Program: ++
Input: [172, 120, 33, 58]
Output: 292 91
----------------------------------
Program: p
Input: 
Output: 1
----------------------------------
Program: pp
Input: [176, 12]
Output: 0 0
----------------------------------
Program: ++++p
Input: [32, 16, 69, 197, 73, 171, 21, 178, 72]
Output: 48 266 244 199 0
----------------------------------
Program: pp+++p+pp+
Input: [151, 27, 119, 189, 198, 107, 174, 15, 166, 106, 134, 108, 169, 55, 42]
Output: 1 0 308 305 189 0 240 0 0 97
----------------------------------
Program: p+p+++++++pp+p
Input: [143, 67, 30, 149, 178, 52, 112, 122, 55, 122, 142, 199, 20, 175, 138, 80, 116, 180, 50, 116, 15, 92, 74]
Output: 0 97 1 230 234 177 341 195 218 296 0 0 107 0
----------------------------------
Program: ++p++p+pp+++++p+p+pp++
Input: [120, 177, 23, 116, 163, 52, 65, 98, 177, 16, 96, 131, 160, 48, 153, 0, 139, 33, 62, 49, 129, 86, 99, 135, 187, 80, 137, 130, 113, 136, 0, 1, 186, 100, 38, 153]
Output: 297 139 1 117 275 0 227 0 0 153 172 111 215 234 0 217 0 249 0 0 286 191
----------------------------------
Program: ++p+++++p+p+++++++
Input: [181, 169, 6, 84, 68, 171, 129, 107, 106, 114, 197, 58, 11, 88, 156, 169, 43, 77, 49, 43, 102, 78, 93, 51, 91, 37, 64, 93, 82, 126, 181, 81, 44]
Output: 350 90 0 300 213 311 69 244 0 120 0 145 171 142 101 175 307 125
----------------------------------
Program: ++p+
Input: [131, 127, 115, 40, 113, 196, 83]
Output: 258 155 1 279
----------------------------------
Program: +ppp++p+ppp+p++++++++p+p+++pp+ppp++
Input: [6, 9, 187, 168, 96, 167, 178, 139, 86, 148, 99, 103, 166, 18, 119, 15, 132, 77, 16, 88, 139, 34, 58, 90, 43, 69, 68, 152, 59, 106, 134, 49, 155, 100, 52, 55, 27, 188, 41, 77, 23, 49, 171, 23, 193, 84, 111, 165, 80, 18, 63, 23, 116, 112, 119]
Output: 15 0 0 0 345 225 0 202 0 0 0 147 0 104 173 148 112 220 165 183 255 0 82 0 118 72 194 1 0 276 0 0 0 139 231
----------------------------------
Program: ++++++++p++++++++++++
Input: [156, 5, 34, 25, 117, 98, 139, 131, 88, 82, 191, 13, 1, 170, 51, 116, 144, 85, 92, 170, 25, 94, 149, 131, 19, 161, 115, 160, 8, 6, 195, 101, 11, 185, 87, 50, 33, 140, 188, 135, 164]
Output: 161 59 215 270 170 204 171 167 0 177 195 243 150 276 168 201 112 272 83 328 299
----------------------------------

Many, many, very long test cases

The java code used to generate test cases

Example

Below is an ungolfed java function which will interpret +p:

public static void interpret(String program, int[] input) {
int index = 0;
for (char inst : program.toCharArray()) {
switch (inst) {
case '+':
System.out.print((input[index++] + input[index++]) + " ");
break;
case 'p':
int n = input[index++];
System.out.print((isPrime(n) ? 1 : 0) + " ");
break;
}
}
}

public static boolean isPrime(long n) { //Taken from https://stackoverflow.com/a/2385999/4484294
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) return false;
}
return true;
}

Note: Using the search query prime AND add AND interpret is:question, there do not appear to be any duplicates to this question. If there is one, sorry.

• In your outputs the ps' results are concatenated without separator, is this intended? – Gábor Fekete May 25 '17 at 14:48
• Can we use a heuristic prime test? i.e. isprime in julia. – Rɪᴋᴇʀ May 25 '17 at 14:58
• I started that craze! But... what has it done? The robots... no! – caird coinheringaahing May 25 '17 at 15:19
• Interestingly enough, I made an exact opposite to the challenge – caird coinheringaahing May 25 '17 at 15:22
• @GáborFekete They are? They look fine to me... – Socratic Phoenix May 25 '17 at 15:29

05AB1E, 5 bytes

vy.V,

Try it online!

Explanation

This challenge fit 05AB1E like a glove :)

vy      # for each instruction in the program
.V    # execute as 05AB1E code
,   # print
• Definitely the right tool for the job. – Erik the Outgolfer May 25 '17 at 14:49
• This looks cheaty... I mean really. – Christopher May 25 '17 at 14:51
• @Christopher: Luckily + and p means add and isPrime in 05AB1E :) – Emigna May 25 '17 at 14:53
• @Emigna I've never used 05AB1E, so I had no idea! Clever answer :) – Socratic Phoenix May 25 '17 at 15:35
• @Emigna Wait were you Enigma? – Christopher May 25 '17 at 16:40

Python 2, 135 133 bytes

l,p=input()
i=j=0
while len(l)-i:print(int(all(l[i]%k for k in range(2,l[i])))if p[j]=='p'else l[i]+l[i+1]);i+=1+'p+'.find(p[j]);j+=1

-2 bytes thanks to kundor

• i,j=0,0 is redundant, right? Couldn't it be i,j=0 ? – Pavel May 26 '17 at 15:58
• @Phoenix: No, that won't work. You can do i=j=0 though. – Nick Matteo May 26 '17 at 21:01

Python 2, 91 bytes

p,i=input()
for c in p:n=i.pop(0);print all(n%k for k in range(2,n))if c>'+'else n+i.pop(0)

Try it online!

Haskell, 88 79 bytes

('+':r)!(a:b:c)=a+b:r!c
('p':r)!(a:c)=min(foldr((*).mod a)1[2..a-1])1:r!c
_!e=e

"commands" ! [args] for use.

• Saved 9 bytes thanks to @Laikoni (#56433)

I'm still learning Haskell; golfing tips appreciated!

• This tip to use infix notation for functions can save you some bytes. Also the base case i _[]=[] can be moved to be the last pattern matching rule and then be shortened to i _ e=e, or something like _!e=e after you switched to infix notation. – Laikoni May 26 '17 at 6:15
• (min$product ... can be min(product .... – Laikoni May 26 '17 at 6:17 • product$map(mod a) can be shortened to foldr((*).mod a)1. – Laikoni May 26 '17 at 6:30

Ruby 2.4, 77+7 = 84 bytes

Uses the -rprime flag.

->g,i{g.chars.map{|c|c==?p?i.shift.prime?? 1:0: c==?+?i.shift(2).sum: p}-[p]}

Perl 6, 70 bytes

{@^b.rotor($^a.comb.map(1+(*ne'p'))).map({$_-2??..is-prime!!.sum})}

First the rotor method is used to split the input list into chunks of size 1 or 2 depending on whether the next character of the program is p or not. Then that chunked list is mapped over; chunks of size 2 are summed, and chunks of size 1 have their sole element tested for primality.

Jelly, 18 bytes

ÆP,+/ị@L
OḂ‘Bṁ@Ç€K

Try it online!

Alternate solution, 18 bytes

⁴Ḣ
¢+¢µ¢ÆPµḂ?
OÇ€K

Try this one online!

C#, 130 129 bytes

p=>d=>{var i=0;p.Any(c=>{Console.Write((c==43?d[i++]+d[i]:Enumerable.Range(2,d[i]-2).Any(x=>d[i]%x==0)?0:1)+" ");return++i<0;});}

Try it online!

• Saved 1 byte by currying the function (thanks to Cyoce)
• not sure how C# works, but could you change (p,d)=> to p=>d=> to save a byte and make a curried function? – Cyoce May 26 '17 at 15:29
• Right, thanks. (It is debatable how much of the required C# boilerplate should be included in the byte count, but yeah, you can write that. (See the linked TIO.)) – Mormegil May 27 '17 at 19:24

PowerShell 3+, 151 121 Bytes

$r,$v=$args;$p={0-notin((2..(($n=0+"$args")-1)|%{$n%$_}))};$i=0;$r|%{if($_-eq"p"){&p$v[$i]}else{$v[$i]+$v[($i+=1)]}$i++}

PowerShell has no prime built-ins so I had to roll my own. My first version was terrible and I took from most of the other ones that test for 0 among modulus results which saves a lot. Also sabed a few bytes by using -notin instead of -notcontains but it mean PowerShell v2 is out.

Comment based explanation

# $r is the program code. Assumed char array #$v is the remaining variables in an assumed integer array.
$r,$v=$args # Anonymous function to determine if a number is a prime or not. # Test all potential factors. Check if any 0 modulus remainders are present$p={0-notin((2..(($n=0+"$args")-1)|%{$n%$_}))}
# $i is an index for tracking location in$v
$i=0 # Cycle each of the instructions$r|%{if($_-eq"p"){ # Call the prime checking anonymous function on this number &p$v[$i] }else{ # Add the next two numbers. Adjust the index accordingly.$v[$i]+$v[($i+=1)] } # Next number in list.$i++
}
# Next number in list.
$i++ } F#, 130 bytes let rec r=function|[],_->()|'+'::t,f::s::u->printf"%i "(f+s);r(t,u)|_::t,n::u->printf"%b "(List.exists((%)n>>(=)0)[2..n-1]);r(t,u) Try it online! QBasic, 122 bytes INPUT p$
FOR i=1TO LEN(p$) INPUT x IF"+"=MID$(p\$,i,1)THEN INPUT y:?x+y:ELSE f=0:FOR j=2TO x:f=f-(x MOD j=0):NEXT:?f=1
NEXT

Takes the code as a line of input, then takes each input number on its own line. The outputs are interspersed with the inputs because they're printed as soon as they're calculated. The truthy value is -1; falsey is 0.