⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tinyscript-manual.tex

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 TEX
📖 第 1 页 / 共 2 页
字号:
\documentclass[12pt]{article}\usepackage{graphicx}\usepackage{graphics}\usepackage{multicol}\usepackage{epsfig,amsmath,amsfonts}\usepackage{xspace}\usepackage{subfigure}\makeatletter                                   % Make '@' accessible. \oddsidemargin=0in                              % Left margin minus 1 inch.\evensidemargin=0in                             % Same for even-numbered pages.\marginparsep=0pt                               % Space between margin & text\renewcommand{\baselinestretch}{1.2}              % Double spacing\textwidth=6.5in                                % Text width (8.5in - margins).\textheight=9in                                 % Body height (incl. footnotes)\topmargin=0in                                  % Top margin minus 1 inch.\headsep=0.0in                                  % Distance from header to body.\skip\footins=4ex                               % Space above first footnote.\hbadness=10000                                 % No "underfull hbox" messages.\makeatother                                    % Make '@' special again.\newcommand{\mate}{Mat\'{e}\xspace}\newcommand{\bomb}{Bombilla\xspace}\newcommand{\kw}[1]{{\tt #1}}\newcommand{\grammarshift}{\vspace*{-.7cm}}\newcommand{\grammarindent}{\hspace*{2cm}\= \\ \kill}\newcommand{\opt}{$_{OPT}$}\title{The TinyScript Language}\author{Philip Levis\\pal@cs.berkeley.edu}\date{}\begin{document}%\fontfamily{cmss}                               % Make text sans serif.%\fontseries{m}                                  % Medium spacing.%\fontshape{n}                                   % Normal: not bold, etc.%\fontsize{10}{10}                               % 10pt font, 10pt line spacing \maketitle\vspace{2in}\begin{center}Version 1.0\\July 12, 2004\end{center}\fontfamily{cmr}                                % Make text Roman (serif).\fontseries{m}                                  % Medium spacing.\fontshape{n}                                   % Normal: not bold, etc.\fontsize{10}{10}                               % 10pt font, 10pt line spacing\thispagestyle{empty}\newpage\tableofcontents\newpage\section{Introduction}This document describes TinyScript, a language that the \mate VMframework supports for programming sensor networks. TinyScript is animperative, BASIC-like language with dynamic typing and basic controlstructures such as conditionals and loops. \section{Script Structure}\label{sec:tinyscript}This is an example TinyScript program that increments a counter:\begin{quotation}\begin{verbatim}! Define a shared variable, countershared counter;! Increment itcounter = counter + 1;\end{verbatim}\end{quotation}In TinyScript programs, all variables must be declared before anyprogram statements. For example, the following program is invalid (andwill throw a compilation error):\begin{quotation}\begin{verbatim}! Define a shared variable, countershared counter;! Increment itcounter = counter + 1;! Define a shared variable, index: ERRORshared index;\end{verbatim}\end{quotation}Statements generally end with a semicolon. {\tt !} declares the startof a comment, which extends to the end of a line. For example,\begin{quotation}\begin{verbatim}shared counter; ! Define a shared variable, countercounter = counter + 1; ! Increment it\end{verbatim}\end{quotation}is valid TinyScript code.TinyScript function and variable identifiers are case insensitive:{\tt var} is the same as {\tt VAR} or {\tt Var}. Identifiers arecomposed of alphanumeric characters and the underscore ('\_'), but thefirst character of an identifier must be a letter or theunderscore. The following are all valid identifiers:\begin{verbatim}tempa51_bTEMPtemp7buffer_Index_3\end{verbatim}The following are invalid identifiers:\begin{verbatim}@a4bsd?a 5\end{verbatim}To be precise, identifiers must follow the pattern {\tt[A-Za-z\_][A-Za-z0-9\_]*}.Certain words are TinyScript keywords, and cannot be used in programsto name variables or functions. In the above scripts, {\tt shared} isa keyword, declaring {\tt counter} to be a shared variable. The fulllist of keywords is:\begin{verbatim}not     and     or      xor     eqv     impfor     to      next    step    until   whileend     private shared  buffer  if       thenelse\end{verbatim}TinyScript function and variable identifiers are case insensitive:{\tt var} is the same as {\tt VAR} or {\tt Var}. Keywords exist asboth their uppercase and lowercase versions: {\tt shared} can also bewritten {\tt SHARED}, but cannot be written {\tt sHarEd}.\section{Variables}\label{sec:variables}TinyScript programs have two basic variable types: scalars andbuffers. Scalars represent a single data item, such as an integer or asensor reading. Buffers are small collections of values. In the aboveprograms, the keyword {\tt shared} declared the variables to bescalars. Handlers can declare two kinds of scalars: {\tt private} and{\tt shared}. Private variables are local to that handler; thestatement {\tt private a;} in two different handlers refers to twodifferent variables. In contrast, {\tt shared a;} in two differenthandlers refer to the same variable. Using a shared variable, handerscan pass data to one another. Buffers, declared with {\tt buffer}, areimplicitly shared variables.Both values and buffers are dynamically typed. That is, the variablesthemselves have no explicit type in a program; instead, their typeis determined dynamically as a program runs. In this program,\begin{quotation}\begin{verbatim}shared counter; shared sensor;counter = random();sensor = light();\end{verbatim}\end{quotation}the variable {\tt counter} takes the type integer ({\tt rand()}returns an integer) while {\tt sensor} takes the type light ({\ttlight()} returns a light value).Types constrain how values can be modified, and how buffers can beaccessed. There is one basic scalar type, integer (16-bit, signed).Additionally, every sensor has its own type. Integers can be modifiedfreely, through arithmetic, assignment, and othertransformation. Sensor readings, however, are immutable. You cannotadd two sensor readings, even if from the same sensor.The idea is that sensor readings should only be what is actually readfrom a sensor. Transformations on these readings should bedistinguishable from actual readings.  To modify sensor readings, theymust be cast to an integer with the {\tt int()} function. For example,this program computes an exponentially weighted moving average of thelight sensor:\begin{quotation}\begin{verbatim}shared sensor; shared aggregate;sensor = light();aggregate = (aggregate / 2) + (int(sensor) / 2); ! Cast light reading to integer\end{verbatim}\end{quotation}Buffers also have a type, which defines what values can be placed init. A cleared buffer has no type, and takes the type of the firstvalue placed in it. In the following program, {\tt aggBuffer} iscleared, which clears its type. An integer ({\tt aggregate}) is addedto the buffer, making the buffer of the type integer.\begin{quotation}\begin{verbatim}shared sensor;shared aggregate;buffer aggBuffer;sensor = light();aggregate = aggregate + int(sensor); ! Cast light reading to integeraggregate = aggregate / 2;bclear(aggBuffer); ! Clear all buffer entries and typeaggBuffer[] = aggregate; ! Append aggregate value to buffer                         ! Buffer is now of type integer\end{verbatim}\end{quotation}Buffers have a fixed maximum size of ten values. The function {\ttbfull()} can be used to see if a buffer is full, while {\tt bsize()}indicates how many entries it currently has. Individual buffer valuescan be accessed by indexing into a buffer. The following programobtains the median value stored in a buffer:\begin{quotation}\begin{verbatim}shared size;shared median;buffer aggBuffer; bsorta(aggBuffer);        ! Sort buffer entries in ascending ordersize = bsize(aggBuffer);  ! Number of entries in buffermedian = aggBuffer[size / 2];  ! Return median value\end{verbatim}\end{quotation}An empty index value implies the tail (last value) of a buffer onaccess, 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[] = light();    ! Append a new light value to the buffer\end{verbatim}\end{quotation}\section{Functions}The above code examples used several functions, such as {\tt light()},{\tt bsorta()}, and {\tt int()}. Functions take a fixed number (zeroor more) of parameters. For example, {\tt bclear()} takes a singleparameter, a buffer to clear, and {\tt rand()} takes noparameters. Some functions return values (e.g., {\tt rand()}), whileothers do not (e.g., {\tt bsorta()}).Function parameters may have type requirements. However, as TinyScriptis dynamically typed, these types are not checked at compile time. Forexample, {\tt bclear()} takes a single parameter, a buffer. Passing itan integer will cause a run-time error.Return values of function calls may be ignored. For example,\begin{quotation}\begin{verbatim}rand();\end{verbatim}\end{quotation}is a valid program.The return values of functions can be directly used as values orparameters to functions:\begin{quotation}\begin{verbatim}buffer aggBuf;shared val;val = aggBuf[rand() \% bsize(aggBuff)]; ! Hope size isn't zeroval = sqrt(bsize(aggBuff)) + 2;         \end{verbatim}\end{quotation}Note that assigning to a buffer is different than assigning to anelement of a buffer. Assigning a scalar to a buffer appends it. If ascalar is assigned to an element of a buffer, the buffer isdynamically sized to include that element if need be. Buffers can beassigned to one another (which results in a copy), but a buffer cannotbe assigned to be the element of another buffer.\begin{quotation}\begin{verbatim}buffer aggBuf;buffer aggBuf2shared val;aggBuf2 = aggBuf;aggBuf[] = val;aggBuf2[val \% 8] = val;aggBuf[1] = aggBuf2;  ! ERROR\end{verbatim}\end{quotation}Currently, TinyScript does not support scripting functions.\section{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 \\ \hline

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -