\documentclass[tikz]{standalone}
\usepackage{stmaryrd}
\usepackage{listings}

\usepackage{../preamble/lstlangclean}
\usetikzlibrary{arrows.meta,shapes.symbols,matrix,positioning,calc,fit}
\begin{document}
\begin{tikzpicture}
	\node [draw,label=above:{Clean source code}] (src) {
\begin{lstlisting}[language=Clean,
	basewidth=0.5em,
	basicstyle=\tt\scriptsize,
	breakatwhitespace=false,
	breaklines=true,
	backgroundcolor=,
	commentstyle=\sl,
	frame=,
	keepspaces=true,
	keywordstyle=\bf,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	stringstyle=\it,
	tabsize=2]
someTask :: MTask v Int | ...
someTask =
	sensor1 config1 \sns1->
	sensor2 config2 \sns2->
	   sds      \s1  = initialValue
	In lowerSds \s2  = someiTaskSDS
	In fun      \fun1= ( \(a0, a1)->...)
	In fun      \fun2= ( \a->...)
	In { main = mainexpr }
\end{lstlisting}
	};

	\node (bc) [draw,node distance=4em,right=of src,label=above:{Byte code}] {
			\scriptsize
\begin{tabular}{ll}
	\verb|main:| & \ldots\\
	             & \ldots\\
	\verb|fun0:| & \ldots\\
	             & \ldots\\
	\verb|fun1:| & \ldots\\
	             & \ldots\\
	\verb|sds0:| & \ldots\\
	\verb|sds1:| & \ldots\\
	\verb|periph0:| & \ldots\\
	\verb|periph1:| & \ldots\\
\end{tabular}
};
	\draw [->] (src) to node [midway,above] {compile} (bc);

	\matrix (m) [node distance=4em,right=of bc,matrix of nodes,nodes in empty cells,row sep=1em] {
		Interpreter & Rewriter\\
		\verb|main|\\
		tree & tree\\
		     &     \\
			 & tree\\
		 };
	\draw [->] (m-2-1) -- (m-3-1);
	\draw [->] (m-3-1) -- (m-3-2);
	\draw [->] (m-3-2) -- (m-4-2.north) -- (m-4-1.north) -- (m-4-1.south) -- (m-4-2.south) -- (m-5-2);
	\draw [dashed]($(m-1-1.north)!0.5!(m-1-2.north)$) -- ($(m-5-1.south)!0.5!(m-5-2.south)$);
	\draw [->] (m-5-2) to [out=0,in=0] (m-3-2);

	\node (dev) [draw,fit=(m),label=above:{Device}] {};
	\draw [dashed,->] (bc) to node [midway,above] {send} (dev);
%	\node (int) [block,node distance=5em,right=of bc.north] {Interpreter};
%	\node (rew) [block,node distance=1em,right=of int] {Rewrite engine};
%	\node (main) [block,node distance=.5em,below=of int] {\verb|main|};
%	\node (t1) [block,node distance=.5em,below=of main] {tree};
%	\node (t2) [block,node distance=1em,right=of t1] {tree};
%	\node (t3) [block,node distance=.5em,below=of t2] {};
%	\node (t4) [block,node distance=1em,left=of t3] {};
%	\node (t5) [block,node distance=.5em,below=of t4] {};
%	\node (t6) [block,node distance=1em,right=of t5] {};
%	\node (t7) [block,node distance=.5em,below=of t6] {tree};
%
%	\draw [->] (main) -- (t1);
%	\draw [->] (t1) -- (t2);
%	\draw [->] (t2) -- (t3) -- (t4) -- (t5) -- (t6) -- (t7);
%	%\node (dev) [draw,fit right=of bc,label=above:{Device}] {};
\end{tikzpicture}
\end{document}
