1
\$\begingroup\$

I just created a language on the spot, and it's called "interpreter". Called as such because the only valid word is "interpreter"! A sample program in "interpreter" looks like this:

interpreter interpreterinterpreter interpreterinterpreterinterpreter interpreterinterpreterinterpreterinterpreter interpreterinterpreterinterpreterinterpreterinterpreter interpreterinterpreterinterpreterinterpreterinterpreterinterpreter

It may look meaningless, but the language works like:

  • interpreter initializes the accumulator to zero.
  • interpreterinterpreter adds one to the accumulator.
  • interpreterinterpreterinterpreter subtracts one from the accumulator.
  • interpreterinterpreterinterpreterinterpreter multiplies the accumulator by 2.
  • interpreterinterpreterinterpreterinterpreterinterpreter divides the accumulator by 2.
  • interpreterinterpreterinterpreterinterpreterinterpreterinterpreter outputs the accumulator.

Rules

  • Standard loopholes are forbidden.
  • Shortest program in bytes wins.
  • Your program should work for all test cases.
  • You may assume that all code entered is valid.
  • The first command will always be interpreter.
  • There will always be a single space between each command.
  • Floating point division, not integer division.
  • Trailing newlines are allowed.

Testcases

interpreter interpreterinterpreter interpreterinterpreterinterpreterinterpreterinterpreterinterpreter -> 1
interpreter interpreterinterpreter interpreterinterpreter interpreterinterpreterinterpreterinterpreter interpreterinterpreterinterpreter interpreterinterpreterinterpreterinterpreterinterpreterinterpreter -> 3
interpreter interpreterinterpreterinterpreterinterpreterinterpreter interpreterinterpreter interpreterinterpreterinterpreterinterpreterinterpreterinterpreter -> 1
interpreter interpreterinterpreter interpreterinterpreter interpreterinterpreterinterpreterinterpreterinterpreterinterpreter interpreterinterpreterinterpreter interpreterinterpreterinterpreterinterpreterinterpreterinterpreter interpreter interpreterinterpreter interpreterinterpreterinterpreterinterpreterinterpreterinterpreter -> outputs 2, then outputs 1, then outputs 1
\$\endgroup\$
17
  • \$\begingroup\$ Should we consider that no invalid code will be submitted to our program? (eg behaviour is not defined for invalid code?) \$\endgroup\$
    – Kaddath
    Commented Feb 26, 2021 at 9:06
  • \$\begingroup\$ You may assume that all code entered will be valid (editing that into rules) \$\endgroup\$
    – user100690
    Commented Feb 26, 2021 at 9:06
  • \$\begingroup\$ You say that interpreter initializes the accumulator to 0. Will there always be one at the start of the program? \$\endgroup\$
    – Oliver Ni
    Commented Feb 26, 2021 at 9:30
  • 1
    \$\begingroup\$ @OliverNi Yes, there will always be one interpreter at the beginning. \$\endgroup\$
    – user100690
    Commented Feb 26, 2021 at 9:32
  • 1
    \$\begingroup\$ @Arnauld float division \$\endgroup\$
    – user100690
    Commented Feb 26, 2021 at 16:36

22 Answers 22

8
\$\begingroup\$

JavaScript (V8), 71 bytes

s=>s.split` `.map(i=>(n=i.length%6)?s=[,s/2,s*2,s-1,s+1,0][n]:print(s))

Try it online!

How?

The length of an interpreter instruction is \$11\times k,\:1\le k \le 6\$, which gives \$[11,22,33,44,55,66]\$. When applying a modulo \$6\$, this maps to \$[5,4,3,2,1,0]\$. This is shorter than dividing by \$11\$ and allows us to easily identify the output instruction (size \$66\$, mapped to \$0\$) which behaves differently from the other ones.


JavaScript (V8), 80 bytes

This version is also based on the length of the instruction modulo \$6\$ but doesn't use any lookup table. It updates the accumulator with a single statement, using arithmetic and bitwise operations.

This is rather pointless in JS but does save a few bytes in C (as opposed to a chain of ternary operators).

s=>s.split` `.map(i=>(n=i.length%6)?A=A*(6&9/n|n<2)/2+(3>>n-3^2)-2:print(A),A=0)

Try it online!

           | n =        | multiply by:  | add:
 operation | length % 6 | (6&9/n|n<2)/2 | (3>>n-3^2)-2
-----------+------------+---------------+--------------
 clear     |      5     |       0       |      0
 increment |      4     |       1       |      1
 decrement |      3     |       1       |     -1
 double    |      2     |       2       |      0
 halve     |      1     |      0.5      |      0
\$\endgroup\$
3
\$\begingroup\$

C (gcc), 113 \$\cdots\$ 113 111 bytes

Saved 3 6 bytes thanks to the man himself Arnauld!!!
Added 6 bytes to accommodate floating-point division. Saved 2 bytes thanks to ceilingcat!!!

float a;c;f(char*s){for(;*s;c?a=a*(6&9/c|c<2)/2+(3>>c-3^2)-2:printf("%f ",a),++s)for(c=1;*++s&&*s-32;c=++c%6);}

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ 110 bytes by using modulos. In C, it might be more efficient to use a formula instead of ternary operators, though. \$\endgroup\$
    – Arnauld
    Commented Feb 26, 2021 at 15:22
  • \$\begingroup\$ The last test case in the original post suggests that it's supposed to be float division. (I've asked the OP to clarify.) \$\endgroup\$
    – Arnauld
    Commented Feb 26, 2021 at 15:28
  • \$\begingroup\$ @Arnauld That's a good one - thank! :D Handling fp division would add +6 (float ) to the byte count. :( \$\endgroup\$
    – Noodle9
    Commented Feb 26, 2021 at 15:50
  • \$\begingroup\$ FP division has been confirmed by the OP. :-/ It doesn't fix the division, but porting the formula of my alternate JS version would save 3 bytes: 107 bytes \$\endgroup\$
    – Arnauld
    Commented Feb 26, 2021 at 16:42
  • \$\begingroup\$ @Arnauld Nice one - thanks! :D l fixed the fp too, thanks for the update. \$\endgroup\$
    – Noodle9
    Commented Feb 26, 2021 at 18:40
3
\$\begingroup\$

05AB1E, 18 17 bytes

#€g"=;·<>0"sè».Võ

Try it online!

+2 bytes thanks to a bug, corrected by Makonede

-1 byte thanks to Command Master!

How it works

#€g"=;·<>0"sè».Võ - Program. Push the input I to the stack
#                 - Split I on spaces
 €g               - Lengths of €ach
   "=;·<>0"       - Push "=;·<>0"
           s      - Swap, moving the lengths to the top of the stack
            è     - Index into string, 0-based and modularly
             »    - Join by newlines
              .V  - Run as 05AB1E code
                õ - Push the empty string

This takes a similar approach to my Jelly answer, translating into 05AB1E then running. This only beats Jelly because the increment commands can go in strings in 05AB1E, but not in Jelly

We don't need to bother with any syntax nonsense to reset the accumulator in 05AB1E. Instead, as all the commands operate on the top value, we just push a 0 to the top of the stack

interpreter command Equivalent 05AB1E command What it does in 05AB1E
interpreter 0 Pushes 0 to the ToS (top of stack)
interpreterinterpreter > Increments the ToS
interpreterinterpreterinterpreter < Decrements the ToS
interpreterinterpreterinterpreterinterpreter · Doubles the ToS
interpreterinterpreterinterpreterinterpreterinterpreter ; Halves the ToS
interpreterinterpreterinterpreterinterpreterinterpreterinterpreter = Prints the ToS without popping it

We push an empty string at the end to prevent implicit output. If nothing has been output by the transpiled interpreter code, 05AB1E would output the ToS. We push the empty string so that if no output has been produced, 05AB1E outputs the empty string instead

\$\endgroup\$
6
  • \$\begingroup\$ This is invalid because 1. there is implicit output and 2. the lack of separation between 0s causes multiple interpreters in a row to push a string of multiple zeros. Both of these issues can be seen when running the program interpreter interpreter. The former can be fixed by prepending õ?, and the latter by replacing 0 with ¾. Try it online! \$\endgroup\$
    – Makonede
    Commented Feb 26, 2021 at 23:00
  • \$\begingroup\$ @Makonede Well, I don't fully understand how that works, but I'll take your word for it. Thanks! \$\endgroup\$ Commented Feb 28, 2021 at 1:52
  • \$\begingroup\$ Well, õ? first outputs an empty string with no trailing newline. This essentially outputs nothing while clearing implicit output; implicit output will only ever trigger if no explicit output was ever triggered before that. So, this triggers explicit output while outputting nothing and simultaneously prevents implicit output. As for ¾, it simply pushes the counter_variable which is by default zero and doesn't get updated in your code. While a streak of multiple ¾'s pushes multiple zeros (example: ¾¾¾ pushes three zeros), a streak of multiple zeros pushes a string of them (cont.) \$\endgroup\$
    – Makonede
    Commented Feb 28, 2021 at 20:05
  • \$\begingroup\$ (cont.) (example: 000 pushes "000"). \$\endgroup\$
    – Makonede
    Commented Feb 28, 2021 at 20:05
  • \$\begingroup\$ can you do #€g"=;·<>¾"sèJ.Võ to disable implicit input? \$\endgroup\$ Commented Mar 1, 2021 at 17:19
3
\$\begingroup\$

Vyxal rDO, 13 bytes

⌈vL`…½d‹›0`İĖ

Try it Online!

Vyxal port of @caird coinheringaahing’s 05AB1E answer.

Explanation:

               # Implicit input
⌈              # Split on " "
 vL            # Length of each command
   `…½d‹›0`    # Push "…½d‹›0" 'D' flag - treat as raw string
           İ   # Index into string
            Ė  # Exec as Vyxal code
               # 'O' flag - disable implicit output


The commands in `…½d‹›0`:
… - Print without popping
½ - Halve
d - Double
‹ - Decrement
› - Increment
0 - Push 0
\$\endgroup\$
1
  • \$\begingroup\$ Very nicely done! \$\endgroup\$
    – lyxal
    Commented May 12, 2021 at 1:41
2
\$\begingroup\$

PHP, 128 105 90 86 bytes

foreach(explode(' ',$argn)as$v)($i=strlen($v)%6)?$c-=[0,$c/2,-$c,1,-1,$c][$i]:print$c;

Try it online!

Actually counting the number of parts split by p rather than counting interpreter, so that any word that contains "p" would work.

EDIT: saved 23 bytes by using an array (inspired by other answers)

EDIT 2: another 15 bytes with @Arnauld the Great's modulo 6. Basically a port of his answer now

EDIT3: 4 bytes less with an array of values to subtract instead of an array to assign

\$\endgroup\$
2
  • \$\begingroup\$ Because you want to avoid using too many $c, it's shorter to do it that way in PHP: 86 bytes \$\endgroup\$
    – Arnauld
    Commented Feb 26, 2021 at 10:33
  • \$\begingroup\$ @Arnauld yeah thats what I was doing ^^ but it was a 87 bytes with a +=. Thanks! \$\endgroup\$
    – Kaddath
    Commented Feb 26, 2021 at 10:35
2
\$\begingroup\$

Bash, 120 71 bytes

o=(. =0 ++ -- *=2 /=2)
for i;do
((x${o[n=${#i}/11]},n-6))||echo $x
done

Try it online!

Credits

  • Saved 4 bytes from both answers thanks to @Neil
  • Saved 49 bytes thanks to @DigitalTrauma
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Your first answer doesn't need x=0 because it's guaranteed to be the first operation. \$\endgroup\$
    – Neil
    Commented Feb 26, 2021 at 11:24
2
\$\begingroup\$

Python 3, 138 bytes

Easy translation of the interpreter language instructions:

for i in[len(o)/11for o in input().split(' ')]:
 if i==1:a=0
 if i==2:a=a+1
 if i==3:a=a-1
 if i==4:a=a*2
 if i==5:a=a/2
 if i==6:print(a)

Try it online!

In the first line:

  1. takes the input
  2. split the instructions separated by a space
  3. divide by 11 (the lenght of the base word interpreter) to get the corresponding operation

The subsequent if execute the operations on the accumulator.

EDIT: theoretical improvements, code not modified (see version 2). Anyway, thanks for the suggestions!

-4 bytes thanks to @expressjs123

-3 bytes thanks to @ElPedro


VERSION 2

Python 3, 97 96 93 bytes

Improved the selection of the instructions simply by using list slicing.

-41 bytes compared to original version.

-1 byte thanks to @ElPedro

-3 bytes thanks to @Zaelin Goodman

a=0
for i in[len(o)//11for o in input().split()]:
 a-=[a,-1,1,-a,a/2,0][i-1]
 if i>5:print(a)

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Using shorthands like +=, -= etc saves bytes \$\endgroup\$
    – user100690
    Commented Feb 26, 2021 at 10:52
  • 1
    \$\begingroup\$ split() splits on space by default for -3 \$\endgroup\$
    – ElPedro
    Commented Feb 26, 2021 at 13:24
  • 1
    \$\begingroup\$ Save another 1 with i>5 instead of i==6 as there will not be more than 6 interpreters in a command \$\endgroup\$
    – ElPedro
    Commented Feb 26, 2021 at 14:30
  • 1
    \$\begingroup\$ 89 bytes by switching to -= rather than = and using modulo rather than division Try it online! \$\endgroup\$ Commented Feb 26, 2021 at 18:49
  • 1
    \$\begingroup\$ @Zaelin Goodman, thank you, updated the list selection, but i prefere to leave the division logic ;) \$\endgroup\$
    – SevC_10
    Commented Mar 1, 2021 at 17:03
2
\$\begingroup\$

K (ngn/k), 37 bytes

0{y@x}/( \;0.5*;2*;-1+;1+;0*)6!#'" "\

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 80 bytes

foreach(explode(' ',$argn)as$v)$a-=[$a/2,-$a,1,-1,$a][strlen($v)%6-1]??!print$a;

Try it online!

Explanation

$a                 // accumulator
-=                 // subtract operation result and assign to accumulator
[$a/2,-$a,1,-1,$a] // array of operations (divide, multiply, subtract, add, reset)
[strlen($v)%6-1]   // modulo 6 of command length, minus 1
                   // this way the print command (-1) will not be present in array
??                 // array key does not exist?
!print$a;          // print accumulator and toggle the boolean value of print's return
                   // toggle is required, because print always returns 1
                   // and we don't want to modify accumulator value, so !1 == 0

Credits

\$\endgroup\$
1
  • \$\begingroup\$ Nice improvement! I'm not used to the ?? operator yet (actually I don't use PHP much nowadays except here).. +1 \$\endgroup\$
    – Kaddath
    Commented Feb 26, 2021 at 16:07
1
\$\begingroup\$

Jelly, 18 bytes

ḲẈị“HḤ“øȮ”j⁾’‘¤Vṛ“

Try it online!

How it works

ḲẈị“HḤ“øȮ”j⁾’‘¤Vṛ“ - Main link. Takes a string I on the left
Ḳ                  - Split I at spaces
 Ẉ                 - Get the length of each section
              ¤    - Group into a nilad:
   “HḤ“øȮ”         -   [["H", "Ḥ"], ["ø", "Ȯ"]]
           ⁾’‘     -   ["’", "‘"]
          j        -   Join; ["H", "Ḥ", "’", "‘", "ø", "Ȯ"]
  ị                - Index into the string, modularly and 1-indexed
               V   - Execute as Jelly code
                 “ - Yield the empty string
                ṛ  - Replace with the empty string to suppress automatic output

This program translates interpreter into Jelly and then runs it as Jelly code. First, only the lengths of each command actually matter. The lengths are [11, 22, 33, 44, 55, 66], which are unique modulo 6. The commands are transliterated as follows:

interpreter command Length Length mod 6 Jelly command
interpreter 11 5 ø
interpreterinterpreter 22 4
interpreterinterpreterinterpreter 33 3
interpreterinterpreterinterpreterinterpreter 44 2
interpreterinterpreterinterpreterinterpreterinterpreter 55 1 H
interpreterinterpreterinterpreterinterpreterinterpreterinterpreter 66 0 Ȯ

As Jelly uses modular indexing, we don't need to bother to modulo the lengths, we can just go ahead and into the command list.

Most of these commands are pretty obvious and are direct translations from the spec (e.g. is Jelly's increment command, is double etc.). However, ø is slightly different (and I think this is the only time I've every used it).

ø is a syntax command that tells Jelly to begin a new niladic chain. This basically tells Jelly to throw away everything before it and to reset, using 0 as the argument for the new command. As we're outputting as we go, its actually perfectly fine to "throw away" the previous commands, because they're no longer relevant.

\$\endgroup\$
0
\$\begingroup\$

Python 3, 84 82 bytes

n=0
for c in input().split():l=len(c)%10;n+=[-n,1,-1,n,-n/2,0][l-1];l==6==print(n)

Try it online!

-2 bytes Oliver Ni

\$\endgroup\$
3
  • \$\begingroup\$ 82 \$\endgroup\$
    – Oliver Ni
    Commented Feb 26, 2021 at 9:55
  • \$\begingroup\$ 81 bytes using Arnauld's suggestion on my answer \$\endgroup\$
    – Kaddath
    Commented Feb 26, 2021 at 10:51
  • \$\begingroup\$ 80 bytes: tio.run/##rU/… \$\endgroup\$ Commented Jul 28 at 19:17
0
\$\begingroup\$

Charcoal, 31 bytes

F⪪S ≡№ιi¹≔⁰θ²≦⊕θ³≦⊖θ⁴≦⊗θ⁵≦⊘θ⟦Iθ

Try it online! Explanation:

F⪪S 

Split the input on spaces and loop over each word.

≡№ιi

Count the number of is in each word.

¹≔⁰θ²≦⊕θ³≦⊖θ⁴≦⊗θ⁵≦⊘θ⟦Iθ

Perform the appropriate operation on the accumulator depending on the number of is.

Note that a switch is shorter than looking up the results in an array as I don't have to initialise the accumulator.

\$\endgroup\$
0
\$\begingroup\$

Batch, 206 bytes

@set/ps=
@set t=@set "s=
%t%%s:nterpreter=%
%t%%s: =&set/an%
%t%%s:set/aniiiiii=call:c%
%t%%s:iiiii=/=2%
%t%%s:iiii=*=2%
%t%%s:iii=-=1%
%t%%s:ii=+=1%
%t%%s:i==0%
@set/an%s%
@exit/b
:c
@echo %n%

Takes input on STDIN. Works by performing substitutions on the input string to produce Batch code which is then executed.

\$\endgroup\$
0
\$\begingroup\$

Bash + GNU utils, 89

Transpiles to dc, then interprets:

sed -r 's/\S{11}/x/g;s/x{6}/p/g;s/x{5}/2\//g;s/x{4}/2*/g;s/xxx/1-/g;s/xx/1+/g;s/x/0/g'|dc

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 83 -> 73 -> NOW 59 bytes

$args|% Le*|%{,$a*!($_%6);$a-=(0,($a/2),-$a,1,-1,$a)[$_%6]}

Try it online!

Thanks to @mazzy and @ZaelinGoodman

\$\endgroup\$
5
  • \$\begingroup\$ This doesn't work with the last test case, as a single 'interpreter' should reset A to 0; the first item in your array should be -$a to make it work properly with this method: Try it online! \$\endgroup\$ Commented Feb 26, 2021 at 16:23
  • 1
    \$\begingroup\$ 63 bytes by shuffling some stuff around :) Try it online! \$\endgroup\$ Commented Feb 26, 2021 at 16:44
  • 1
    \$\begingroup\$ 60 bytes? \$\endgroup\$
    – mazzy
    Commented Feb 26, 2021 at 17:17
  • 1
    \$\begingroup\$ 59 bytes? \$\endgroup\$ Commented Feb 26, 2021 at 19:14
  • \$\begingroup\$ @mazzy and ZaelinGoodman you both are too helpful, thanks a lot \$\endgroup\$
    – Wasif
    Commented Feb 27, 2021 at 6:14
0
\$\begingroup\$

Japt, 29 bytes

¸®=Êu6)?E=[,E/2EÑEÉEÄT]gZ:OpE

Try it

\$\endgroup\$
0
\$\begingroup\$

YaBASIC, 130 bytes

dim c$(1)
n=split(i$,c$())
for j=1 to n
c=mod(len(c$(j)),6)
if c a=a*or(and(6,9/c),c<2)/2+xor(mod(3/2^(c-3),6),2)-2
if !c ?a;
next

A basic interpreter interpreter running in an interpreted Basic interpreter...

Try it online!

Well, thanks to Arnauld's infamous MOD 6 trick, I was able to compress this perfectly readable, easily maintained and expandable 134 byte program:

dim c$(1)
n=split(i$,c$())
for j=1 to n
c=len(c$(j))/11
if c=1 a=0
if c=2 a=a+1
if c=3 a=a-1
if c=4 a=a*2
if c=5 a=a/2
if c=6 ?a;
next

into the 130-byte bit of opaque confusion seen above! 🤣 If I was gonna develop "interpreter" as a product, I know which code-base I'd start with, but really, I'd rather program in DeadFish...

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -a, 66 bytes

eval((0,'$a=0','$a++','$a--','$a*=2','$a/=2','say$a')[y/i//])for@F

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java, 137 129 bytes

s->{var x=0d;int l;for(var w:s.split(" ")){if((l=w.length()/11)==6)System.out.println(x);x-=new double[]{x,-1,1,-x,x/2,0}[l-1];}}

Try it online!

Saved 8 bytes thanks to branboyer.

\$\endgroup\$
4
  • \$\begingroup\$ This challenge requires floating point precision, which could be done by changing the int's to double's like this. 137 bytes \$\endgroup\$
    – branboyer
    Commented Mar 7, 2021 at 13:49
  • \$\begingroup\$ My golfed version of the corrected version 134 bytes -3 by putting l=... in the if statement and x-=... \$\endgroup\$
    – branboyer
    Commented Mar 7, 2021 at 13:52
  • \$\begingroup\$ 129 bytes and a 125 byte solution in there too for if newlines in between prints is allowed. \$\endgroup\$
    – branboyer
    Commented Mar 7, 2021 at 14:11
  • 1
    \$\begingroup\$ @branboyer Thanks for the help. \$\endgroup\$ Commented Mar 7, 2021 at 16:24
0
\$\begingroup\$

Stax, 12 bytes

î╜║µI*▄mß┘r>

Run and debug it

Who needs flags when compression? :P

\$\endgroup\$
0
\$\begingroup\$

Pascal, 190 B

Like other submissions, we count the number of characters between each space. This is a full program in accordance to ISO standard 7185 Standard Pascal. Note the empty statement (;) for the zero length case. There must be a case label for every value of the selector expression; otherwise the program experiences an error at the end of input. The last character read from input is always a space.

program p(input,output);var n,x:real;begin
repeat n:=0;repeat n:=n+1;get(input)until' '=input^;case trunc(n/11)of
0:;1:x:=0;2:x:=x+1;3:x:=x-1;4:x:=x*2;5:x:=x/2;6:writeLn(x)end until EOF end.

And now the legible ungolfed version. Note, this version uses integer for the length counter.

program interpreterInterpreter(input, output);
    var
        characterCount: integer;
        accumulator:    real;
    begin
        repeat
        begin
            characterCount ≔ 0;
            
            repeat
            begin
                characterCount ≔ characterCount + 1;
                get(input)
            end
            until input↑ = ' ';
            
            case characterCount div 11 of
                0: ;
                1: accumulator ≔ 0.0;
                2: accumulator ≔ accumulator + 1;
                3: accumulator ≔ accumulator - 1;
                4: accumulator ≔ accumulator * 2;
                5: accumulator ≔ accumulator ∕ 2;
                6: writeLn(output, accumulator)
            end
        end
        until EOF(input)
    end.
\$\endgroup\$
0
\$\begingroup\$

APL(Dyalog Unicode), 56 bytes SBCS

{⍎∊(↓8 3⍴' 0⊣1+ -1-2× .5×⎕← {} 0')[7,⌽8,10|≢¨' '(≠⊆⊢)⍵]}

Try it on APLgolf!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.