24
3
3var is a variant of deadfish which uses three variables called A, B and R. A and B are accumulators, while R is used as a result variable.
In this code-golf challenge, you need to make an interpreter for a stripped-down version of this language.
Required Commands:
i Increments A
d Decrements A
s Squares A
p Prints A
P Prints the ASCII character of A
> Sets A to R
a Increments B
k Decrements B
m Squares B
o Prints B
O Prints the ASCII character of B
< Sets B to R
+ Adds A and B, stores in R
- Subtracts B from A, stores in R
* Multiplies A and B, stores in R
/ Divides A by B, stores in R
w Prints R
@ Resets A to 0
# Resets B to 0
e Resets R to 0
Everything else (including whitespaces) is ignored.
Clarifications
o
andp
should output without anything after it.- Division is integer division.
- Numbers above 255 and numbers below 0 are supported.
- 'w' should output a space or a newline after R
- Division by 0 halts without errors. (No output to STDERR)
- A, B and R are initially 0
Rules
- This is code-golf so the shortest answer wins.
- In the event of a tie, the oldest answer wins.
- The file will be provided through command line arguments or STDIN.
- Any language is allowed.
- Eval is allowed.
Test Cases
Hello world!
(taken from Esolangs)
iisssaa/>e
maa->e#
aamam->e#
dddddddddddddddddddddddddPiiiiiiiiiiiiiiiiiiiiiiiii
iiiiPiiiiiiiPPiiiP
@#e
iis
aamaaaa
*>P
@#e
iisssaa/>e
maa->e#
aamam->e#
iiiiiiiiiiiiiiiiiiiiiiP
ddddddddP
iiiP
ddddddP
ddddddddP
@#e
iiss
aa*>
iP
Outputs 20spooky22me
:
iipois+iis<-<aaaO<OkOOkkkkOP@#iippisa+<m+>PaO
Outputs 9P-1420 100 3Q-1 162 0
:
iiispsdPkokmo/w<+w#aaaommO-w@ii*wew
Leaderboards
Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
# Language Name, N bytes
where N
is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
# Ruby, <s>104</s> <s>101</s> 96 bytes
If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:
# Perl, 43 + 2 (-p flag) = 45 bytes
You can also make the language name a link which will then show up in the leaderboard snippet:
# [><>](http://esolangs.org/wiki/Fish), 121 bytes
var QUESTION_ID=63008,OVERRIDE_USER=45220;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"http://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>
Should the output of
o
be followed by a newline? – LegionMammal978 – 2015-11-05T13:20:21.530@LegionMammal978 No, it outputs the ascii value stored in B as a number. – DJgamer98 – 2015-11-05T13:21:37.100
That would be
O
. – LegionMammal978 – 2015-11-05T13:22:36.777@LegionMammal978 Yeah, sorry.
o
prints it as a number,O
prints it as a character. – DJgamer98 – 2015-11-05T13:23:25.6601@Sp3000 Speaking of it, I recently helped fix a nested loop bug in the interpreter. – LegionMammal978 – 2015-11-05T13:33:34.980
2Additional clarifications for people familiar with Deadfish:
dddddp
results in -5 andiiiissp
results in 256 as you'd expect, rather than 0. – Sp3000 – 2015-11-05T13:35:04.2001In the test Case What dose
r
do? its not included on your list of valid commands – JimmyJazzx – 2015-11-05T14:17:01.967I have an answer but fail with your example using
r
command – edc65 – 2015-11-05T14:17:58.990@Sp3000 Thanks for the extra test case – DJgamer98 – 2015-11-05T15:35:42.253
@edc65 Sp3000 edited the test case so it should work now. – DJgamer98 – 2015-11-05T15:36:00.880
1@JimmyJazzx The testcase is now edited, but in the full 3var language r resets everything. It's the same as
@#e
. – DJgamer98 – 2015-11-05T15:36:44.047Added clarifications to the post. – DJgamer98 – 2015-11-05T15:44:14.520
@DJgamer98 Deleted my comments - would you mind adding another test case:
iiispsdPkokmo/w<+w#aaaommO-w@ii*wew
? I also see you added that division by 0 halts without errors - does this mean no output to STDERR, exit code 0 or...? – Sp3000 – 2015-11-05T15:48:53.010@Sp3000 No output to STDERR. – DJgamer98 – 2015-11-05T15:51:50.260
(er... test case means posting the expected output/s along with it) – Sp3000 – 2015-11-05T15:56:40.603
@Sp3000 Whoops. I'm currently on mobile, i'll post it when i get home – DJgamer98 – 2015-11-05T16:09:28.673
Prints A
I assume this means print the numerical value of A, right? – Digital Trauma – 2015-11-05T16:27:46.083@Digital Trauma 1. Prints A means Print the numerical value of A. 2. It's the same thing. I'll edit the post. – DJgamer98 – 2015-11-05T16:31:55.363
You should add test cases for the clarifications – aditsu quit because SE is EVIL – 2015-11-06T00:30:59.350
1Are A, B, and R initialized to 0 when the interpreter starts? – Tony Ennis – 2015-11-06T08:33:00.003
@TonyEnnis Yes. – DJgamer98 – 2015-11-06T08:41:56.840
It's too bad this language doesn't seem to have any means of accepting input - then you could write a 3var interpreter IN 3var. (Probably wouldn't be very golfable, but would still be impressive...) – Darrel Hoffman – 2015-11-06T15:29:15.987
@DarrelHoffman The full language has input.
– DJgamer98 – 2015-11-06T15:31:20.860Which numbers are "about 255"? – Bakuriu – 2015-11-07T21:18:17.847
@Bakuriu It should have said "above". Edited the post. – DJgamer98 – 2015-11-08T12:30:08.813