trans2(do(Alt), Statement, _, NewPC, PID, Span, In, Out) :-
member(A,Alt),
inst(A,S),
( (S=else; S=d_step(else); S=p_atomic(else))
-> delete(Alt,A,Alt2),
( find_exec(Alt2,PID,In) -> fail; true )
; true ),
trans2(S,Statement,A,NewPC,PID,Span,In,Out).
trans2(if(Alt), Statement, _, NewPC, PID, Span, In, Out) :-
trans2(do(Alt), Statement, _, NewPC, PID, Span, In, Out).
trans2(unless(M,E), Statement, _, NewPC, PID, Span, In, Out) :-
inst(E,Escape),
( trans2(Escape,Statement,E,NewPC,PID,Span,In,Out)
-> true
; (inst(M,Main), trans2(Main,Statement,M,NewPC,PID,Span,In,Out)) ).
trans2(provided(expr(Expr)), Statement, PC, NewPC, PID, Span, In, Out) :-
eval(Expr, PID, In, R),
( true(R)
-> (PC2 is PC+1, inst(PC2,S), trans2(S,Statement,PC2,NewPC,PID,Span,In,Out))
; fail ).
trans2(d_step_start(S), Statement, PC, NewPC, PID, Span, In, Out) :-
trans2(d_step(S), Statement, PC, NewPC, PID, Span, In, Out).
trans2(p_atomic_start(S), Statement, PC, NewPC, PID, Span, In, Out) :-
trans2(p_atomic(S), Statement, PC, NewPC, PID, Span, In, Out).
trans2(d_step(S), Statement, PC, NewPC, PID, Span, In, Out) :-
trans2(S, Statement, PC, PC2, PID, Span, In, In2),
check_jump(PC2, NewPC),
inst(NewPC, S2),
% look, if next step is d_step
( S2 = d_step(_)
-> define(In2, var(sys(mode),d_step), global, Out)
; define(In2, var(sys(mode),normal), global, Out) ),!.
trans2(p_atomic(S), Statement, PC, NewPC, PID, Span, In, Out) :-
trans2(S, Statement, PC, PC2, PID, Span, In, In2),
check_jump(PC2, NewPC),
inst(NewPC, S2),
%print(p_atomic_inst(NewPC,S2)),nl,
% look, if next step is p_atomic and is executable.
( S2 = p_atomic(S3), trans2(S3, _, NewPC, _, PID, _, In2, _)
-> define(In2, var(sys(mode),p_atomic), global, Out)%, print(define(p_atomic)),nl
; define(In2, var(sys(mode),normal), global, Out) ).%, print(define(normal)),nl ).
trans2(destructor, destructor, PC, NewPC, PID, src_span(SRow,SCol,ERow,ECol,_,_), In, Out) :-
% ensure, PID has no child.
In = env(Vars,_,_),
\+ lookup3(child(PID,_), Vars, true),
% remove channels that are not being referenced
remove_chans(PID, In, In2),
remove_vars(PID, In2, In3),
remove_children(PID, In3, Out),
NewPC = dead,
line(PC,SRow,SCol,ERow,ECol),!.
trans2(goto(X), goto(X), PC, NewPC, _, src_span(SRow,SCol,ERow,ECol,_,_), In, In) :-
( number(X) -> NewPC is X; inst(Y,label(X)), NewPC is Y ),
inst(NewPC,S),
% goto into a d_step is not allowed
S \= d_step(_),
line(PC,SRow,SCol,ERow,ECol).
trans2(break(X), break(X), PC, NewPC, _, src_span(SRow,SCol,ERow,ECol,_,_), In, In) :-
NewPC is X,
line(PC,SRow,SCol,ERow,ECol).
trans2(label(_), Statement, PC, NewPC, PID, Span, In, Out) :-
PC2 is PC+1,
inst(PC2,S),
trans2(S, Statement, PC2, NewPC, PID, Span, In, Out).
trans2(printf(TextList), printf(Text), PC, NewPC, _, src_span(SRow,SCol,ERow,ECol,_,_), In, In) :-
% ex.: atom_codes(X,[97]).
% X = a
atom_codes(Text,TextList),
NewPC is PC+1,
line(PC,SRow,SCol,ERow,ECol).
trans2(printf(TextList,VarList), printf(Result), PC, NewPC, PID, src_span(SRow,SCol,ERow,ECol,_,_), In, In) :-
% ex.: format("~s~2f",[a,0.3]).
% a0.30
% format_to_codes("a ~c",[13],R).
% R = [97,32,13] ?
eval_list(VarList, PID, In, ValueList),
atom_codes(Text,TextList),
format_to_codes(Text,ValueList,ResultList),
atom_codes(Result,ResultList),
NewPC is PC+1,
line(PC,SRow,SCol,ERow,ECol).
trans2(printm(expr(Expr)), printm(Result), PC, NewPC, PID, src_span(SRow,SCol,ERow,ECol,_,_), In, In) :-
eval(Expr, PID, In, Result),
NewPC is PC+1,
line(PC,SRow,SCol,ERow,ECol).
trans2(Statement, Statement, PC, NewPC, PID, src_span(SRow,SCol,ERow,ECol,_,_), In, Out) :-
trans3(Statement, PID, In, Out),
NewPC is PC+1,
line(PC,SRow,SCol,ERow,ECol).
trans2(send(Name,Msg), Statement, PC, NewPC, PID, SpanOut, In, Out) :-
lookup(var(Name), PID, In, CID),
%% print(chan_id(CID)),nl,
( lookup(chan(CID), global, In, [])
-> r_send(CID, Name, Msg, Statement, PID, Span2, In, Out), SpanOut = multi_span(SRow,SCol,ERow,ECol,_,_,Span2)
; n_send(CID, Name, Msg, Statement, PID, In, Out), SpanOut = src_span(SRow,SCol,ERow,ECol,_,_) ),
NewPC is PC+1,
line(PC,SRow,SCol,ERow,ECol).
trans2(sorted_send(Name,Msg), Statement, PC, NewPC, PID, SpanOut, In, Out) :-
lookup(var(Name), PID, In, CID),
%% print(chan_id(CID)),nl,
( lookup(chan(CID), global, In, [])
-> r_send(CID, Name, Msg, Statement, PID, Span2, In, Out), SpanOut = multi_span(SRow,SCol,ERow,ECol,_,_,Span2)
; s_send(CID, Name, Msg, Statement, PID, In, Out), SpanOut = src_span(SRow,SCol,ERow,ECol,_,_) ),
NewPC is PC+1,
line(PC,SRow,SCol,ERow,ECol).