Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 2.35 KB | None | 0 0
  1. %% ?- one_instruction(state(1, 0, [101, 102, 101], [100] ,[6, 7, 8, 9], 0), X).
  2. %
  3. %% X = state(103, 1, [101, 102, 101], [100], [6, 7, 8, 9], 0).
  4. %
  5. %
  6. one_instruction(state(Acc, Pc, Mem, In, Out, Flag),
  7.                 state(NewAcc, NewPc, NewMem, NewIn, NewOut, Flag)):-
  8.                 nth0(Pc,Mem,Cell),
  9.                 Op is div(Cell,100),
  10.                 Address is mod(Cell,100),
  11.                 operator(Op,Address,Mem,Acc,NewAcc,Pc,NewPc, NewMem, In, NewIn,Out,NewOut).
  12. %add
  13. operator(1,Address,Mem,Acc,NewAcc,Pc,NewPc,Mem,In,In,Out,Out):- pc_update(Pc,NewPc),
  14.                 nth0(Address,Mem,Value),NewAcc is Acc+Value.
  15. %sub
  16. operator(2,Address,Mem,Acc,NewAcc,Pc,NewPc,Mem,In,In,Out,Out):- pc_update(Pc,NewPc),
  17.                 nth0(Address,Mem,Value),NewAcc is Acc-Value.
  18.  
  19. %store
  20. operator(3,Address,Mem,Acc,Acc,Pc,NewPc,NewMem,In,In,Out,Out):- pc_update(Pc,NewPc), fun(Mem, Address, Acc, _, NewMem),!.
  21.  
  22. %load
  23. operator(5,Address,Mem,_,NewAcc,Pc,NewPc,Mem,In,In,Out,Out):- pc_update(Pc,NewPc), nth0(Address, Mem, NewAcc).
  24.  
  25.  
  26. %branch
  27. operator(6,Address,Mem,Acc,Acc,_,Address,Mem,In,In,Out,Out):- Address < 100.
  28.  
  29. %branch if zero
  30. operator(7,Address,Mem,Acc,Acc,_,Address,Mem,In,In,Out,Out):- Acc = 0.
  31. operator(7,_,Mem,Acc,Acc,Pc,NewPc,Mem,In,In,Out,Out):- pc_update(Pc,NewPc).
  32.  
  33.  
  34.  
  35. %input:
  36. operator(9, 01,Mem,_,NewAcc,Pc,NewPc,Mem, In, NewIn,Out,Out):- pc_update(Pc,NewPc), input(In, NewAcc), nth0(0, In, _, NewIn).
  37.  
  38. %output:
  39. operator(9, 02, Mem,Acc,Acc,Pc,NewPc,Mem,In,In,Out,NewOut):- pc_update(Pc,NewPc), append(Out,Acc, NewOut).
  40.  
  41.  
  42.  
  43. %fine
  44. operator(0,_,_,_,_,_,_,_,_,_,_,_):- halt(0).
  45. operator(4,_,_,_,_,_,_,_,_,_,_,_):- halt(4).
  46.  
  47.  
  48.  
  49.  
  50. %Qui sotto lo schifo. Poi creiamo un secondo file di moduli magari
  51. %
  52. %
  53. %
  54.  
  55.  
  56. %aggiunge in posizione indice
  57. fun(List, Indice, Val, NewList, X):- nth0(Indice, NewList, Val, List), NewIndice is Indice+1, fun2(NewList, NewIndice, _, X),!.
  58. %%toglie da indice +1
  59. fun2(List, Indice,_, NewList):- nth0(Indice, List, _, NewList).
  60.  
  61. %aggiunge in testa
  62. input([H], H).
  63. input([H | _], H).
  64.  
  65. %non serve, ma la memoria dovra' avere 100 posizioni
  66. mem(Index,[0|T]):- Index < 100, NewIndex is 1+Index, mem(NewIndex,T),!.
  67. mem(Index,[0]):- Index is 99.
  68.  
  69.  
  70.  
  71. pc_update(Pc,NewPc):-Pc<99,NewPc is Pc+1,!.
  72. %Pc=99 non serve ad un cazzo, è già maggiore di 99 per conoscenza
  73. %pregressa.
  74. pc_update(Pc,NewPc):-Pc=99,NewPc is 0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement