📄 mate-manual.tex
字号:
access, or after the tail on assignment (append). For example:\begin{quotation}\begin{verbatim}buffer aggBuffer;shared val;val = aggBuffer[]; ! Val is the last value in the bufferaggBuffer[] = call light(); ! Append a new light value to the buffer\end{verbatim}\end{quotation}\subsection{Functions}The above code examples used several functions, such as {\tt light()},{\tt bsorta()}, and {\tt cast()}. Programs invoke functions using the{\tt call} keyword. Functions take a fixed number (zero or more) ofparameters. For example, {\tt bclear()} takes a single parameter, abuffer to clear, and {\tt rand()} takes no parameters. Some functionsreturn values (e.g., {\tt rand()}), while others do not (e.g., {\ttbsorta()}).Function parameters may have type requirements. However, as TinyScriptis a dynamically typed language, these types are not checked atcompile time. For example, {\tt bclear()} takes a single parameter, abuffer. Passing it an integer will cause an error.Return values of function calls may be ignored. For example,\begin{quotation}\begin{verbatim}call rand();\end{verbatim}\end{quotation}is a valid program.Every \mate VM has a set of functions it provides as primitives (theyare built into the VM). Customizing this set of functions is one ofthe ways that \mate programming environments can be tailored tospecific deployments or applications (Section~\ref{sec:building}describes the details of how to do so). A \mate scripting environmentshould provide information on what functions are available, as well aswhat they do, their required parameters, and return values.The return values of functions can be directly used as values orparameters to functions:\begin{quotation}\begin{verbatim}buffer aggBuf;shared val;val = aggBuf[call rand() \% call bsize(aggBuff)]; ! Hope size isn't zeroval = call sqrt(call bsize(aggBuff)) + 2; \end{verbatim}\end{quotation}Note that assigning to a buffer is very different than assigning to anelement of a buffer.\begin{quotation}\begin{verbatim}buffer aggBuf;buffer aggBuf2shared val;aggBuf2 = aggBuf;aggBuf[] = val;\end{verbatim}\end{quotation}Currently, TinyScript does not support user-written functions.\subsection{Arithmetic, Logic, and Conditionals}Figure~\ref{fig:arithmetic} shows the set of arithmetic operationsTinyScript provides, as well as their syntax.\begin{figure}\centering\subfigure[Arithmetic Operations]{\scriptsize\begin{tabular}{|l|c|l|} \hlineName & Operator & Example \\ \hlineAddition & + & val = val + 2;\\ \hlineSubtraction & - & val = a - b; \\ \hlineDivision & / & val = call bsize() / 2; \\ \hlineMultiplication & * & val = 2 * b; \\ \hlineExponentiation & $^{\wedge}$ & val = val $^{\wedge}$ 2 \\ \hline\end{tabular}\label{fig:arithmetic}}\subfigure[Comparison Operations]{\scriptsize\begin{tabular}{|l|c|l|} \hlineName & Operator & Example \\ \hlineLess than & $<$ & val = val $<$ 2;\\ \hlineGreater than & $>$ & val = a $>$ b; \\ \hlineLess than or equal & $<=$ & val = call bsize() $<=$ 8; \\ \hlineGreater than or equal & $>=$ & val = b $>=$ 2; \\ \hlineNot equal & $<>$ & cond = val $<>$ b \\ \hline\end{tabular}\label{fig:comparison}}\caption{TinyScript Computational Primitives}\label{fig:tscriptcomp}\end{figure}All of these operations have a shorthand (similar to the C language)for self-assignment. For example, {\tt val *= 2;} is the same as {\ttval = val * 2;}.TinyScript also supports logical operations, which are show inFigure~\ref{fig:logic}. All of these operations only accept integersas operands. For the boolean operators (e.g., and, not), a value ofzero is considered false; all other values are considered true. Alloperators use 0 as false and 1 as true. So, {\tt 1 and 2} resolves to1, while {\tt 0 and 34} resolves to 0. Figure~\ref{fig:tables}contains the truth tables for the boolean operators.\begin{figure}\centering\scriptsize\subfigure[Logical Operations] {\begin{tabular}{|l|c|l|} \hlineName & Operator & Example \\ \hlineAnd & AND, and & ready = full and idle;\\ \hlineOr & OR, or & ready = full OR idle;\\ \hlineNot & NOT, not & ready = not idle; \\ \hlineExclusive or & XOR, xor & diff = a XOR b; \\ \hlineEquivalent & EQV, eqv & rval = a eqv b; \\ \hlineImplies & IMP, imp & ready = a imp b; \\ \hlineLogical And & \& & bits = packetbits \& mask; \\ \hlineLogical Or & $\mid$ & bits = firstbit $\mid$ secondbi t; \\ \hlineLogical Not & $^{\sim}$ & mask = $^{\sim}$bits; \\ \hline\end{tabular}\label{fig:logic}}\subfigure[Truth Tables] {\begin{tabular}{|c||c|c||c|c||c|c||}\hline&\multicolumn{2}{|c||}{and}&\multicolumn{2}{c||}{or}&\multicolumn{2}{c||}{not} \\ \hline & F & T & F & T & \multicolumn{2}{|c||}{} \\ \hlineF & {\bf F} & {\bf F} & {\bf F} & {\bf T} & \multicolumn{2}{|c||}{{\bf T}} \\ \hlineT & {\bf F} & {\bf T} & {\bf T} & {\bf T} & \multicolumn{2}{|c||}{{\bf F}} \\ \hline \hline&\multicolumn{2}{|c|}{xor}&\multicolumn{2}{|c|}{eqv}&\multicolumn{2}{|c|}{imp} \\ \hline & F & T & F & T & F & T \\ \hlineF & {\bf F} & {\bf T} & {\bf T} & {\bf F} & {\bf T} & {\bf T} \\ \hlineT & {\bf T} & {\bf F} & {\bf F} & {\bf T} & {\bf F} & {\bf T} \\ \hline\end{tabular}}\caption{TinyScript Logical Primitives}\label{fig:tables}\end{figure}The logical operations manipulate integer bit fields. Instead ofmanipulating the integer as a singe value, they operate on each bit,in a manner similar to C operators. For example, {\tt 1 and 2}resolves to 1, while {\tt 1 \& 2} resolves to zero (1 and 2 share nocommon bits), and {\tt 1 $\mid$ 2} resolves to 3.Finally, TinyScript has standard comparison operators, as shown inFigure~\ref{fig:comparison}. They resolve to one if true, zero iffalse.\subsection{Control Structures}TinyScript supports standard language control structures such asconditionals and loops.The first set of control structures, conditionals, take this form:\begin{quotation}\begin{verbatim}if <expression> then if <expression> then <block 1> <block 1>end if else <block 2> end if\end{verbatim}\end{quotation}If expression resolves to true, then block 1 executes. If thestatement has an else clause and expression resolves to false, thenblock 2 executes. There can be nested if-then statements:\begin{quotation}\begin{verbatim}shared idle;buffer buf;if call bfull(buf) then idle = 0; if call rand() & 1 then call send(buf); call bclear(buf); end if idle = 1;end if\end{verbatim}\end{quotation}TinyScript provides loops through the {\tt for} construct. There aretwo basic forms, unconditional and conditional. Unconditional (for-to)loops run a specific number of times; their termination condition whenthe loop variable takes a specific value. Conditional (for-until)loops run until an arbitrary condition becomes true. {\tt next}defines the end of the loop block, and increments the loopvariable. By default, the variable increments by one. However, theincrement step can be set with the {\tt step} keyword. In summary:\begin{quotation}\begin{verbatim}for <x> = <expression> to <to-constant> ...next <x>for <x> = <expression> to <to-constant> step <step-constant> ...next <x>for <x> = <expression> until <until-exp> ...next <x>for <x> = <expression> step <step-constant> until <until-exp> ...next <x>\end{verbatim}\end{quotation}For example, this loop will run one hundred times, blinking the leds,\begin{quotation}\begin{verbatim}private i;for i = 1 to 100 call leds(i & 7)next i\end{verbatim}\end{quotation}while this loop will put the values 1 to 21 in the buffer (when it hasten values, it will be full),\begin{quotation}\begin{verbatim}private i;buffer buf;call bclear(buf);for i = 1 step 1 until i > 10 buf[] = i * 2;next i\end{verbatim}\end{quotation}Standard while loops can be implemented by setting a step ofzero. This loop, for example, will put random values into a bufferuntil it is full:\begin{quotation}\begin{verbatim}private i;buffer buf;for i = 0 step 0 until call bfull(buf) buf[] = call rand();next i\end{verbatim}\end{quotation}Parenthesis pairs can be added to define precedence, or for readability.\begin{quotation}\begin{verbatim}private i;buffer buf;call bclear(buf);for i = 1 step 1 until i > 10 buf[] = ((i * 2) + 1);next ii = (5 + 2 * 2); ! i = 9i = (5 + 2) * 2; ! i = 14i = ((((5)))); ! i = 5\end{verbatim}\end{quotation}\subsection{Error Conditions}Program errors can be divided into two classes: compile-time andrun-time. Compile-time errors cause the compiler to fail; they areoften language mistakes, such as passing too few parameters to afunction. Run-time errors are detected as a program runs on a \mateVM, can cause the VM to stop execution and enter an errorcondition. The error condition involves flashing all of the mote LEDSsimultaneously, and broadcasting an error message, alternating overthe radio and over the UART. Examples of run-time errors include typechecks (e.g., adding a sensor reading to an integer) and bufferoverflow (trying to put more in a buffer than it can hold).Once a mote has entered an error condition, the only way to return itto operation is to reprogram it; hopefully, the new program will nothave the error.\subsection{Event Handlers}All \mate scripts run in response to an event. A given \mate VM has astatic set of events that trigger execution. To program a \matenetwork, one writes scripts for these event handlers. Some events,such as Timer, are of general use. Timer executes periodically (e.g.,every second). Other events have very narrow and specific uses. TheSendR event, for example, is triggered by the {\tt sendr()} functionand allows a program to modify packet headers, which is useful ifimplementing an ad-hoc routing protocol.The Once event is unusual; it only runs once, when new code isinstalled for it. This allows users to manage a network more easilythan with periodic events. For example, one can write a Once handlerthat changes a mote's radio transmit power; this need not run manytimes, and doing so would be wasteful.\subsection{Parallelism}\label{sec:parallelism}\mate VMs can run multiple event handlers can run simultaneously. When it installsnew code, the runtime determines which handlers can run concurrently in a safe manner,and disallows parallelism that could corrupt data.Handlers can declare two kinds of variables: {\tt private} and {\ttshared}. Private variables are local to that handler; the statement{\tt private a;} in two different handlers refers to two differentvariables. In contrast, {\tt shared a;} in two different handlersrefer to the same variable. Using a shared variable, handers can passdata to one another. Buffers are implictly shared variables.For example, imagine a situation in which you want a program tomeasure the maximum time that elapses between receiving packets. Oneway to accomplish this is to use two handlers, Receive and Timer, withtwo shared variables, {\tt current} and {\tt max}. Whenever Timer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -