Guest User

Untitled

a guest
Feb 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. line ::= inc_line
  2. | dec_line
  3. inc_line ::= label ' : ' reg_name ' + ' state_name
  4. dec_line ::= label ' : ' reg_name ' - ' state_name ' ' state_name
  5. state_name ::= label
  6. | '"' message '"'
  7. label ::= identifier
  8. reg_name ::= identifier
  9.  
  10. s0 : a - s1 "Ok"
  11. s1 : b + s0
  12. a=3 b=4
  13.  
  14. Ok
  15. a=0 b=7
  16.  
  17. init : t - init d0
  18. d0 : a - d1 a0
  19. d1 : b + d2
  20. d2 : t + d0
  21. a0 : t - a1 "Ok"
  22. a1 : a + a0
  23. a=3 b=4
  24.  
  25. Ok
  26. a=3 b=7 t=0
  27.  
  28. s0 : t - s0 s1
  29. s1 : t + "t is 1"
  30. t=17
  31.  
  32. t is 1
  33. t=1
  34.  
  35. s0 : t - "t is nonzero" "t is zero"
  36. t=1
  37.  
  38. t is nonzero
  39. t=0
  40.  
  41. init0 : k - init1 init3
  42. init1 : r + init2
  43. init2 : t + init0
  44. init3 : t - init4 init5
  45. init4 : k + init3
  46. init5 : r - init6 "ERROR k is 0"
  47. init6 : i + init7
  48. init7 : n - loop0 "ERROR n is 0"
  49. loop0 : n - loop1 "Ok"
  50. loop1 : i + loop2
  51. loop2 : k - loop3 loop5
  52. loop3 : r + loop4
  53. loop4 : t + loop2
  54. loop5 : t - loop6 loop7
  55. loop6 : k + loop5
  56. loop7 : i - loop8 loopa
  57. loop8 : r - loop9 loopc
  58. loop9 : t + loop7
  59. loopa : t - loopb loop7
  60. loopb : i + loopa
  61. loopc : t - loopd loopf
  62. loopd : i + loope
  63. loope : r + loopc
  64. loopf : i + loop0
  65. n=40 k=3
  66.  
  67. Ok
  68. i=40 k=3 n=0 r=27 t=0
  69.  
  70. @p=<>;/=/,$_{$`}=$' for split$",pop@p;$o='w+';(map{($r
  71. ,$o,$,,$b)=$'=~/".*?"|S+/g if/^$o :/}@p),$_=$o=($_{$r}
  72. +=','cmp$o)<0?do{$_{$r}=0;$b}:$,until/"/;say for eval,%_
  73.  
  74. import sys,os,shlex
  75. G=shlex.shlex(sys.stdin).get_token
  76. A=B=''
  77. C='_:'
  78. V={}
  79. J=lambda x:'goto '+x+';'if'"'!=x[0]else'{puts('+x+');goto _;}'
  80. while 1:
  81. L,c=G(),G()
  82. if''==c:break
  83. if':'==c:
  84. v,d=G(),G()
  85. V[v]=1;B+=L+c+v+d+d+';'
  86. if'+'==d:B+=J(G())
  87. else:B+='if('+v+'>=0)'+J(G())+'else{'+v+'=0;'+J(G())+'}'
  88. else:A+=L+c+G()+';'
  89. for v in V:C+='printf("'+v+'=%d\n",'+v+');'
  90. open('C.c','w').write('int '+','.join(V)+';main(){'+A+B+C+'}')
  91. os.system('gcc -w C.c;./a.out')
  92.  
  93. (w%f)(u@(s,v):z)|s==w=(s,f+v):z|t=u:(w%f)z
  94. (w%f)[]=[(w,f)]
  95. p#(a:z)|j==a=w p++[j]&z|t=(p++[a])#z;p#[]=w p
  96. p&(a:z)|j==a=p:""#z|t=(p++[a])&z
  97. c x=q(m!!0)$map(((s,_:n)->(s,read n)).break(=='=')).w$last x where
  98. m=map(""#)$init x
  99. q[_,_,r,"+",s]d=n s$r%1$d
  100. q[_,_,r,_,s,z]d|maybe t(==0)(lookup r d)=n z d|t=n s$r%(-1)$d
  101. n('"':s)d=unlines[s,d>>=((r,v)->r++'=':shows v" ")]
  102. n s d=q(filter((==s).head)m!!0)d
  103. main=interact$c.lines
  104. t=1<3;j='"';w=words
  105.  
  106. *s,k=*$<
  107. a,=s
  108. b=Hash.new 0
  109. eval k.gsub /(w+)=/,';b["1"]='
  110. loop{x,y,r,o,t,f=a.scan /".*?"|S+/
  111. l=(b[r]-=o<=>?,)<0?(b[r]=0;f):t
  112. l[?"]&&puts(eval(l),b)&exit
  113. a,=s.grep /^#{l} /}
  114.  
  115. uses SysUtils,Generics.Collections;type P=array[0..99]of string;Y=TDictionary<string,P>;Z=TDictionary<string,Int32>;var t:Y;l,i:string;j,k:Int32;q:P;u:Z;v:TPair<string,Int32>;begin t:=Y.Create;repeat if i=''then i:=q[0];t.Add(q[0],q);ReadLn(l);for j:=0to 6do begin k:=Pos(' ',l+' ');q[j]:=Copy(l,1,k-1);Delete(l,1,k)end;until q[1]<>':';u:=Z.Create;j:=0;repeat k:=Pos('=',q[j]);u.Add(Copy(q[j],1,k-1),StrToInt(Copy(q[j],k+1,99)));Inc(j)until q[j]='';repeat q:=t[i];i:=q[4];u.TryGetValue(q[2],j);if q[3]='+'then Inc(j)else if j=0then i:=q[5]else Dec(j);u.AddOrSetValue(q[2],j)until i[1]='"';WriteLn(i);for v in u do Write(v.Key,'=',v.Value,' ')end.
  116.  
  117. uses SysUtils,Generics.Collections;
  118. type
  119. // P is a declaration line, offsets:
  120. // 0 = label
  121. // 1 = ':'
  122. // 2 = register
  123. // 3 = operation ('-' or '+')
  124. // 4 = 1st state (or message)
  125. // 5 = 2nd state (or message)
  126. P=array[0..99]of string;
  127. // T is a dictionary of all state lines :
  128. Y=TDictionary<string,P>;
  129. // Z is a dictionary of all registers :
  130. Z=TDictionary<string,Int32>;
  131. var
  132. t:Y;
  133. l,
  134. i:string;
  135. j,
  136. k:Int32;
  137. q:P;
  138. u:Z;
  139. v:TPair<string,Int32>;
  140. begin
  141. // Read all input lines :
  142. t:=Y.Create;
  143. repeat
  144. // Put all lines into a record
  145. if i=''then i:=q[0];
  146. t.Add(q[0],q);
  147. // Split up each input line on spaces :
  148. ReadLn(l);
  149. for j:=0to 6do
  150. begin
  151. k:=Pos(' ',l+' ');
  152. q[j]:=Copy(l,1,k-1);
  153. Delete(l,1,k)
  154. end;
  155. // Stop when there are no more state transitions :
  156. until q[1]<>':';
  157. // Scan initial registers :
  158. u:=Z.Create;
  159. j:=0;
  160. repeat
  161. k:=Pos('=',q[j]);
  162. // Add each name=value pair to a dictionary :
  163. u.Add(Copy(q[j],1,k-1),StrToInt(Copy(q[j],k+1,99)));
  164. Inc(j)
  165. until q[j]='';
  166. // Execute the state machine :
  167. repeat
  168. q:=t[i];
  169. i:=q[4];
  170. u.TryGetValue(q[2],j);
  171. if q[3]='+'then
  172. Inc(j)
  173. else
  174. if j=0then
  175. i:=q[5]
  176. else
  177. Dec(j);
  178. u.AddOrSetValue(q[2],j)
  179. until i[1]='"';
  180. WriteLn(i);
  181. for v in u do
  182. Write(v.Key,'=',v.Value,' ')
  183. end.
  184.  
  185. <?$t=trim;$e=explode;while($l=$t(fgets(STDIN))){if(strpos($l,"=")){foreach($e(" ",$l)as$b){list($k,$c)=$e("=",$b);$v[$k]=$c;}break;}list($k,$d)=$e(":",$l);$r[$z=$t($k)]=$t($d);$c=$c?:$z;}while($d=$e(" ",$r[$c],4)){$c=$v[$a=$d[0]]||!$d[3]?$d[2]:$d[3];if(!$r[$c]){eval("echo $c.'n';");foreach($v as$k=>$c)echo$k."=".$c." ";die;}if(!$d[3]&&++$v[$a]||$v[$a]&&--$v[$a]);}
  186.  
  187. <?php
  188.  
  189. $register = array();
  190. $values = array();
  191.  
  192. while($line = trim(fgets(STDIN))){
  193.  
  194. if(strpos($line, "=")){
  195.  
  196. // Set each value and then continue to the calculations
  197.  
  198. foreach(explode(" ", $line) as $var){
  199. list($key, $val) = explode("=", $var);
  200.  
  201. $values[$key] = $val;
  202. }
  203.  
  204. break;
  205. }
  206.  
  207. list($key, $data) = explode(":", $line);
  208.  
  209. // Add data to the register
  210.  
  211. $register[$z = trim($key)] = trim($data);
  212.  
  213. // Set the first register
  214.  
  215. $current = $current?:$z;
  216. }
  217.  
  218. while($data = explode(" ", $register[$current], 4)){
  219.  
  220. // Determine next register and current register
  221.  
  222. $current = $values[$target = $data[0]] || !$data[3]? $data[2] : $data[3];
  223.  
  224. // Will return true if the register does not exist (Messages wont have a register)
  225.  
  226. if(!$register[$current]){
  227.  
  228. // No need to strip the quotes this way
  229.  
  230. eval("echo$current.'n';");
  231.  
  232. // Print all values in the right formatting
  233.  
  234. foreach($values as $key => $val)
  235. echo $key."=".$val." ";
  236.  
  237. die();
  238. }
  239.  
  240. // Only subtraction has a third index
  241. // Only positive values return true
  242.  
  243. // If there is no third index, then increase the value
  244. // If there is a third index, increment the decrease the value if it is positive
  245.  
  246. // Uses PHP's short-circuit operators
  247.  
  248. if(!$data[3] && ++$values[$target] || $values[$target] && --$values[$target]);
  249. }
  250.  
  251. m={s=r=[:];z=[:]
  252. it.eachLine{e->((e==~/w+=.*/)?{(e=~/((w+)=(d+))+/).each{r[it[2]]=it[3] as int}}:{f=(e=~/(w+) : (.*)/)[0];s=s?:f[1];z[f[1]]=f[2];})()}
  253. while(s[0]!='"'){p=(z[s]=~/(w+) (.) (w+|(?:".*?")) ?(.*)?/)[0];s=p[3];a=r[p[1]]?:0;r[p[1]]=p[2]=='-'?a?a-1:{s=p[4];0}():a+1}
  254. println s[1..-2]+"n"+r.collect{k,v->"$k=$v"}.join(' ')}
  255.  
  256.  
  257. ['''s0 : a - s1 "Ok"
  258. s1 : b + s0
  259. a=3 b=4''':'''Ok
  260. a=0 b=7''',
  261. '''init : t - init d0
  262. d0 : a - d1 a0
  263. d1 : b + d2
  264. d2 : t + d0
  265. a0 : t - a1 "Ok"
  266. a1 : a + a0
  267. a=3 b=4''':'''Ok
  268. a=3 b=7 t=0''',
  269. '''s0 : t - s0 s1
  270. s1 : t + "t is 1"
  271. t=17''':'''t is 1
  272. t=1''',
  273. '''s0 : t - "t is nonzero" "t is zero"
  274. t=1''':'''t is nonzero
  275. t=0''',
  276. '''init0 : k - init1 init3
  277. init1 : r + init2
  278. init2 : t + init0
  279. init3 : t - init4 init5
  280. init4 : k + init3
  281. init5 : r - init6 "ERROR k is 0"
  282. init6 : i + init7
  283. init7 : n - loop0 "ERROR n is 0"
  284. loop0 : n - loop1 "Ok"
  285. loop1 : i + loop2
  286. loop2 : k - loop3 loop5
  287. loop3 : r + loop4
  288. loop4 : t + loop2
  289. loop5 : t - loop6 loop7
  290. loop6 : k + loop5
  291. loop7 : i - loop8 loopa
  292. loop8 : r - loop9 loopc
  293. loop9 : t + loop7
  294. loopa : t - loopb loop7
  295. loopb : i + loopa
  296. loopc : t - loopd loopf
  297. loopd : i + loope
  298. loope : r + loopc
  299. loopf : i + loop0
  300. n=40 k=3''':'''Ok
  301. i=40 k=3 n=0 r=27 t=0'''].collect {input,expectedOutput->
  302. def actualOutput = m(input)
  303. actualOutput == expectedOutput
  304. }
  305.  
  306. (let[i(apply str(butlast(slurp *in*)))]
  307. (loop[s(read-string i)p(->> i(replace(zipmap":n=""[] "))(apply str)(format"{%s}")read-string)]
  308. (let[c(p s)](cond(string? s)(println s"n"(filter #(number?(% 1))p))
  309. (=(c 1)'-)(let[z(=(get p(c 0)0)0)](recur(c(if z 3 2))(if z p(update-in p[(c 0)]dec))))
  310. 1(recur(c 2)(update-in p[(c 0)]#(if %(inc %)1)))))))
  311.  
  312. errordict/undefined{& " * 34 eq{.()= !{& " .(=). load " .( ).}forall ^()=
  313. stop}{^ ^ " 0 @ : 0}ifelse}put<</^{pop}/&{dup}/:{def}/#{exch}/*{& 0
  314. get}/.{print}/~{1 index}/"{=string cvs}/`{cvn # ^ #}/+={~ load add :}/++{1
  315. ~ length 1 sub getinterval}/S{/I where{^}{/I ~ cvx :}ifelse}/D{/? # :/_ #
  316. cvlit :}/+{D S({//_ 1 +=//?})$ ^ :}/-{/| # : D S({//_ load 0 ne{//_ -1
  317. +=//?}{//|}ifelse})$ ^ :}/![]/@{~/! #[# cvn ! aload length & 1 add #
  318. roll]:}/;{(=)search ^ # ^ # cvi @ :}/${* 32 eq{++}if * 34 eq{& ++(")search
  319. ^ length 2 add 4 3 roll # 0 # getinterval cvx `}{token ^
  320. #}ifelse}>>begin{currentfile =string readline ^( : )search{`( + )search{`
  321. $ ^ +}{( - )search ^ ` $ $ ^ -}ifelse}{( ){search{;}{; I}ifelse}loop}ifelse}loop
  322.  
  323. %!
  324. %Minsky Register Machine Simulation
  325. errordict/undefined{ %replace the handler for the /undefined error
  326. & " * 34 eq{ % if, after conversion to string, it begins with '"',
  327. .()= !{ % print it, print newline, iterate through the register list
  328. & " .(=). load " .( ). % print regname=value
  329. }forall ^()= stop % print newline, END PROGRAM
  330. }{ % if it doesn't begin with '"', it's an uninitialized register
  331. ^ ^ " 0 @ : 0 %initialize register to zero, return zero
  332. }ifelse
  333. }put
  334. <<
  335. /^{pop}
  336. /&{dup}
  337. /:{def} % cf FORTH
  338. /#{exch}
  339. /*{& 0 get} % cf C
  340. /.{print} % cf BF
  341.  
  342. % these fragments were repeated several times
  343. /~{1 index}
  344. /"{=string cvs} % convert to string
  345. /`{cvn # ^ #} % convert to name, exch, pop, exch
  346. /+={~ load add :} % add a value to a variable
  347. /++{1 ~ length 1 sub getinterval} % increment a "string pointer"
  348.  
  349. /S{/I where{^}{/I ~ cvx :}ifelse} %setINIT define initial state unless already done
  350. /D{/? # :/_ # cvlit :} %sr define state and register for generated procedure
  351. /+{D S({//_ 1 +=//?})$ ^ :} % generate an increment state and define
  352. /-{/| # : D S({//_ load 0 ne{//_ -1 +=//?}{//|}ifelse})$ ^ :} % decrement state
  353. /![] %REGS list of registers
  354. /@{~/! #[# cvn ! aload length & 1 add # roll]:} %addreg append to REGS
  355. /;{(=)search ^ # ^ # cvi @ :} %regline process a register assignment
  356. /${ %tpe extract the next token or "string"
  357. * 32 eq{++}if %skip ahead if space
  358. * 34 eq{ %if quote, find the end-quote and snag both
  359. & ++(")search ^ length 2 add 4 3 roll # 0 # getinterval cvx `
  360. }{
  361. token ^ # %not a quote: pull a token, exch, pop
  362. }ifelse
  363. }
  364. >>begin
  365.  
  366. {
  367. currentfile =string readline ^
  368. ( : )search{ % if it's a state line
  369. `( + )search{ % if it's an increment
  370. ` $ ^ + %parse it
  371. }{
  372. ( - )search ^ ` $ $ ^ - %it's a decrement. Parse it
  373. }ifelse
  374. }{ % not a state, do register assignments, and call initial state
  375. ( ){search{;}{; I}ifelse}loop %Look Ma, no `exit`!
  376. }ifelse
  377. }loop
  378. init0 : k - init1 init3
  379. init1 : r + init2
  380. init2 : t + init0
  381. init3 : t - init4 init5
  382. init4 : k + init3
  383. init5 : r - init6 "ERROR k is 0"
  384. init6 : i + init7
  385. init7 : n - loop0 "ERROR n is 0"
  386. loop0 : n - loop1 "Ok"
  387. loop1 : i + loop2
  388. loop2 : k - loop3 loop5
  389. loop3 : r + loop4
  390. loop4 : t + loop2
  391. loop5 : t - loop6 loop7
  392. loop6 : k + loop5
  393. loop7 : i - loop8 loopa
  394. loop8 : r - loop9 loopc
  395. loop9 : t + loop7
  396. loopa : t - loopb loop7
  397. loopb : i + loopa
  398. loopc : t - loopd loopf
  399. loopd : i + loope
  400. loope : r + loopc
  401. loopf : i + loop0
  402. n=40 k=3
  403.  
  404. BEGIN{FS=":"}NF<2{split($1,x," ");for(y in x){split(x[y],q,"=");
  405. g[q[1]]=int(q[2])}}NF>1{w=$1;l=$2;gsub(/ /,"",w);if(!a)a=w;for(i=0;;)
  406. {sub(/^ +/,"",l);if(l=="")break;if(substr(l,1,1)=="""){l=substr(l,2);
  407. z=index(l,""")}else{z=index(l," ");z||z=length(l)+1}d[w,i++]=
  408. substr(l,1,z-1);l=substr(l,z+1)}}END{for(;;){if(!((a,0)in d))break;h=d[a,0];
  409. if(d[a,1]~/+/){g[h]++;a=d[a,2]}else{a=g[h]?d[a,2]:d[a,3];g[h]&&g[h]--}}
  410. print a;for(r in g)print r"="g[r]}
  411.  
  412. % cat | awk -f mrm1.awk
  413. s0 : a - s1 "Ok"
  414. s1 : b + s0
  415. a=3 b=4
  416. ^D
  417. Ok
  418. a=0
  419. b=7
Add Comment
Please, Sign In to add comment