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:
interpreterinitializes the accumulator to zero.interpreterinterpreteradds one to the accumulator.interpreterinterpreterinterpretersubtracts one from the accumulator.interpreterinterpreterinterpreterinterpretermultiplies the accumulator by 2.interpreterinterpreterinterpreterinterpreterinterpreterdivides the accumulator by 2.interpreterinterpreterinterpreterinterpreterinterpreterinterpreteroutputs 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
22 Answers 22
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))
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)
| 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
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);}
-
-
\$\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– Arnauld2021年02月26日 15:28:05 +00:00Commented 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– Noodle92021年02月26日 15:50:31 +00:00Commented Feb 26, 2021 at 15:50 -
-
\$\begingroup\$ @Arnauld Nice one - thanks! :D l fixed the fp too, thanks for the update. \$\endgroup\$Noodle9– Noodle92021年02月26日 18:40:33 +00:00Commented Feb 26, 2021 at 18:40
05AB1E, (削除) 18 (削除ここまで) 17 bytes
#€g"=;·<>0"sè».Võ
+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
-
\$\begingroup\$ This is invalid because 1. there is implicit output and 2. the lack of separation between
0s causes multipleinterpreters in a row to push a string of multiple zeros. Both of these issues can be seen when running the programinterpreter interpreter. The former can be fixed by prependingõ?, and the latter by replacing0with¾. Try it online! \$\endgroup\$Makonede– Makonede2021年02月26日 23:00:12 +00:00Commented 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\$2021年02月28日 01:52:56 +00:00Commented 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 thecounter_variablewhich 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– Makonede2021年02月28日 20:05:15 +00:00Commented Feb 28, 2021 at 20:05 -
\$\begingroup\$ (cont.) (example:
000pushes"000"). \$\endgroup\$Makonede– Makonede2021年02月28日 20:05:32 +00:00Commented Feb 28, 2021 at 20:05 -
\$\begingroup\$ can you do
#€g"=;·<>¾"sèJ.Võto disable implicit input? \$\endgroup\$Command Master– Command Master2021年03月01日 17:19:43 +00:00Commented Mar 1, 2021 at 17:19
Vyxal rDO, 13 bytes
⌈vL`...1⁄2d‹›0`İĖ
Vyxal port of @caird coinheringaahing’s 05AB1E answer.
Explanation:
# Implicit input
⌈ # Split on " "
vL # Length of each command
`...1⁄2d‹›0` # Push "...1⁄2d‹›0" 'D' flag - treat as raw string
İ # Index into string
Ė # Exec as Vyxal code
# 'O' flag - disable implicit output
The commands in `...1⁄2d‹›0`:
... - Print without popping
1⁄2 - Halve
d - Double
‹ - Decrement
› - Increment
0 - Push 0
-
\$\begingroup\$ Very nicely done! \$\endgroup\$2021年05月12日 01:41:58 +00:00Commented May 12, 2021 at 1:41
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;
(削除) 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
Bash, (削除) 120 (削除ここまで) 71 bytes
o=(. =0 ++ -- *=2 /=2)
for i;do
((x${o[n=${#i}/11]},n-6))||echo $x
done
Credits
- Saved 4 bytes from both answers thanks to @Neil
- Saved 49 bytes thanks to @DigitalTrauma
-
1\$\begingroup\$ Your first answer doesn't need
x=0because it's guaranteed to be the first operation. \$\endgroup\$Neil– Neil2021年02月26日 11:24:37 +00:00Commented Feb 26, 2021 at 11:24
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)
In the first line:
- takes the input
- split the instructions separated by a space
- 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)
-
1\$\begingroup\$ Using shorthands like
+=,-=etc saves bytes \$\endgroup\$user100690– user1006902021年02月26日 10:52:08 +00:00Commented Feb 26, 2021 at 10:52 -
1\$\begingroup\$
split()splits on space by default for -3 \$\endgroup\$ElPedro– ElPedro2021年02月26日 13:24:16 +00:00Commented Feb 26, 2021 at 13:24 -
1\$\begingroup\$ Save another 1 with
i>5instead ofi==6as there will not be more than 6interpreters in a command \$\endgroup\$ElPedro– ElPedro2021年02月26日 14:30:17 +00:00Commented 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\$Zaelin Goodman– Zaelin Goodman2021年02月26日 18:49:03 +00:00Commented 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– SevC_102021年03月01日 17:03:08 +00:00Commented Mar 1, 2021 at 17:03
PHP, 80 bytes
foreach(explode(' ',$argn)as$v)$a-=[$a/2,-$a,1,-1,$a][strlen($v)%6-1]??!print$a;
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
- Main idea for performing commands from Kaddath's answer
- Modulo operation from Arnauld's answer
-
\$\begingroup\$ Nice improvement! I'm not used to the
??operator yet (actually I don't use PHP much nowadays except here).. +1 \$\endgroup\$Kaddath– Kaddath2021年02月26日 16:07:49 +00:00Commented Feb 26, 2021 at 16:07
Jelly, 18 bytes
ḲẈị"HḤ"øȮ"j)’‘¤Vṛ"
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.
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)
-2 bytes Oliver Ni
-
-
-
\$\begingroup\$ 80 bytes: tio.run/##rU/… \$\endgroup\$squareroot12621– squareroot126212024年07月28日 19:17:34 +00:00Commented Jul 28, 2024 at 19:17
Charcoal, 31 bytes
F⪪S ≡Noιi1≔0θ2≦⊕θ3≦⊖θ4≦⊗θ5≦⊘θ⟦Iθ
Try it online! Explanation:
F⪪S
Split the input on spaces and loop over each word.
≡Noιi
Count the number of is in each word.
1≔0θ2≦⊕θ3≦⊖θ4≦⊗θ5≦⊘θ⟦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.
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.
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
PowerShell, 83 -> 73 -> NOW 59 bytes
$args|% Le*|%{,$a*!($_%6);$a-=(0,($a/2),-$a,1,-1,$a)[$_%6]}
Thanks to @mazzy and @ZaelinGoodman
-
\$\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\$Zaelin Goodman– Zaelin Goodman2021年02月26日 16:23:47 +00:00Commented Feb 26, 2021 at 16:23
-
1\$\begingroup\$ 63 bytes by shuffling some stuff around :) Try it online! \$\endgroup\$Zaelin Goodman– Zaelin Goodman2021年02月26日 16:44:01 +00:00Commented Feb 26, 2021 at 16:44
-
1
-
1\$\begingroup\$ 59 bytes? \$\endgroup\$Zaelin Goodman– Zaelin Goodman2021年02月26日 19:14:58 +00:00Commented Feb 26, 2021 at 19:14
-
\$\begingroup\$ @mazzy and ZaelinGoodman you both are too helpful, thanks a lot \$\endgroup\$avarice– avarice2021年02月27日 06:14:58 +00:00Commented Feb 27, 2021 at 6:14
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...
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...
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];}}
Saved 8 bytes thanks to branboyer.
-
\$\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– branboyer2021年03月07日 13:49:31 +00:00Commented Mar 7, 2021 at 13:49
-
\$\begingroup\$ My golfed version of the corrected version 134 bytes -3 by putting
l=...in the if statement andx-=...\$\endgroup\$branboyer– branboyer2021年03月07日 13:52:20 +00:00Commented Mar 7, 2021 at 13:52 -
-
1\$\begingroup\$ @branboyer Thanks for the help. \$\endgroup\$Unmitigated– Unmitigated2021年03月07日 16:24:59 +00:00Commented Mar 7, 2021 at 16:24
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.
APL(Dyalog Unicode), (削除) (削除ここまで)56 bytes SBCS
{⍎∊(↓8 3⍴' 0⊣1+ ×ばつ ×ばつ⎕← {} 0')[7,⌽8,10|≢ ̈' '(≠⊆⊢)⍵]}
interpreterinitializes the accumulator to 0. Will there always be one at the start of the program? \$\endgroup\$interpreterat the beginning. \$\endgroup\$