8
3. Textual languages The textual languages defined in this standard are IL (Instruction List) and ST (Structured Text). The sequential function chart (SFC) elements can be used in conjunction with either of these languages. 3.1 Common elements See ‘Common Elements’. 3.2 Language IL (Instruction List) see part on IL 3.3 Language ST (Structured Text This subclause defines the semantics of the ST (Structured Text) language whose syntax is defined in B.3. In this language, the end of a textual line shall be treated the same as a space (SP) character. 3.3.1 Expressions An expression is a construct which, when evaluated, yields a value corresponding to one of the data types. Expressions are composed of operators and operands. An operand shall be a, a variable, a function invocation, or another expression. The operators of the ST language are summarized in table 55. The evaluation of an expression consists of applying the operators to the operands in a sequence defined by the operator precedence shown in table 55. The operator with highest precedence in an expression shall be applied first, followed by the operator of next lower precedence, etc., until evaluation is complete. Operators of equal precedence shall be applied as written in the expression from left to right. For example, if A, B, C, and D are of type INT with values 1, 2, 3, and 4, respectively, then A+B-C*ABS(D) shall evaluate to -9, and (A+B-C)*ABS(D) shall evaluate to 0 . When an operator has two operands, the leftmost operand shall be evaluated first. For example, in the expression SIN(A)*COS(B) the expression SIN(A) shall be evaluated first, followed by COS(B), followed by evaluation of the product. Boolean expressions may be evaluated only to the extent necessary to determine the resultant value. For instance, if A<=B, then only the

Structured Text Textual

Embed Size (px)

DESCRIPTION

Structured Text Basics

Citation preview

3.Textual languagesThe textual languages defined in this standard are IL (Instruction List) and ST (Structured Text).The sequential function chart (SFC) elements can be used in conjunction with either of these languages.3.1Common elementsSee Common !lements".3.2Language IL (Instruction List)see #art on IL3.3Language ST (Structured TextThis subclause defines the semantics of the ST (Structured Text) language whose s$ntax is defined in %.&.In this language' the end of a textual line shall be treated the same as a s#ace (S() character.3.3.1Expressions )n expression is a construct which' when e*aluated' $ields a *alue corres#onding to one of the data t$#es.!x#ressions are com#osed of o#erators and o#erands.)n operand shall be a' a *ariable' a function in*ocation' or another ex#ression.The operators of the ST language are summari+ed in table ,,.The e*aluation of an ex#ression consists of a##l$ing the o#erators to the o#erands in a sequence defined b$ the o#erator #recedence shown in table ,,.The o#erator with highest #recedence in an ex#ression shall be a##lied first' followed b$ the o#erator of next lower #recedence' etc.' until e*aluation is com#lete.-#erators of equal #recedence shall be a##lied as written in the ex#ression from left to right.For exam#le' if )' %' C' and . are of t$#e I/T with *alues 0' 1' &' and 2' res#ecti*el$' then)3%4C5)%S(.)shall e*aluate to 46' and()3%4C)5)%S(.)shall e*aluate to 7 .8hen an o#erator has two o#erands' the leftmost o#erand shall be e*aluated first.For exam#le' in the ex#ressionSI/())5C-S(%) the ex#ression SI/()) shall be e*aluated first' followed b$ C-S(%)' followed b$ e*aluation of the #roduct.%oolean ex#ressions ma$ be e*aluated onl$ to the extent necessar$ to determine the resultant *alue.For instance' if )9:%' then onl$ the ex#ression ();%) would be e*aluated to determine that the *alue of the ex#ression ();%) < (C9.)is %oolean +ero.Functions shall be in*o=ed as elements of ex#ressions consisting of the function name followed b$ a #arenthesi+ed list of arguments.8hen an o#erator in an ex#ression can be re#resented as one of the o*erloaded functions' con*ersion of o#erands and results shall follow the rule and exam#les gi*en in the standard at 1.,.0.2.TABLE 55 - Operators of the ST languageNo. Operation Symbol Preceence0 (arenthesi+ation (ex#ression) >I?>!ST1 Function e*aluation identifier (argument list)E!amples@ L/())' A)B(B'C)' etc.& !x#onentiation (/ote 1) 552 /egation 4, Com#lement /-TD Aulti#l$ 5E .i*ide FG Aodulo A-.6 )dd 307 Subtract 400 Com#arison 9 ' ; ' 9: ' ;:01 !qualit$ :0& Inequalit$ 9;02 %oolean )/. !/ /H--TS @: 7 I!LSIF . : 7.7 T>!//H--TS @: 0 IB0 @: 4 %F(1.75)) I!LS!/H--TS @: 1 IB0 @: (4 % 3 SNHT(.))F(1.75)) IB1 @: (4 % 4 SNHT(.))F(1.75)) I!/.KIF I, C)S! (&.&.1.&) T8 @: %C.KT-KI/T(T>OA%8>!!L)IT8K!HH-H @: 7IC)S! T8 -F0',@.IS(L)C @: -J!/KT!A([email protected](L)C @: A-T-HKS(!!.I&@.IS(L)C @: ?H-SS 4 T)H!I2'D..07@ .IS(L)C @: ST)TOS(T8 4 2)I!LS!.IS(L)C @: 7 IT8K!HH-H @: 0I!/.KC)S!IN8077 @: I/TKT-K%C.(.IS(L)C)ID F-H (&.&.1.2) P @: 070 IF-H I @: 0 T- 077 %C 1 .-IF 8-H.SQIR : ST!CS T>!/P @: I I!BIT I!/.KIF I!/.KF-H IE 8>IL! (&.&.1.2) P @: 0I8>IL! P 9: 077 < 8-H.SQPR 9; ST!CS .-P @: P31 I!/.K8>IL! IG H!(!)T (&.&.1.2) P @: 40 IH!(!)TP @: P31 IO/TIL P : 070 -H 8-H.SQPR : ST!CS!/.KH!(!)T I6 !BIT (&.&.1.2) !BIT I07 !m#t$ Statement I/-T! 4 If the !BIT statement (6) is su##orted' then it shall be su##orted for all of the iteration statements (F-H' 8>IL!' H!(!)T) which are su##orted in the im#lementation.3.3.2.1Assignment statements The assignment statement re#laces the current *alue of a single or multi4element *ariable b$ the result of e*aluating an ex#ression.)n assignment statement shall consist of a *ariable reference on the left4hand side' followed b$ the assignment operator U@:U' followed b$ the ex#ression to be e*aluated.For instance' the statement) @: % Iwould be used to re#lace the single data *alue of *ariable ) b$ the current *alue of *ariable % if both were of t$#e I/T.>owe*er' if both ) and % were of t$#e )/)L-?KC>)//!LKC-/FI?OH)TI-/ as described in table 01' then the *alues of all the elements of the structured *ariable ) would be re#laced b$ the current *alues of the corres#onding elements of *ariable %.)s illustrated in figure D' the assignment statement shall also be used to assign the *alue to be returned b$ a function' b$ #lacing the function name to the left of an assignment o#erator in the bod$ of the function declaration.The *alue returned b$ the function shall be the result of the most recent e*aluation of such an assignment.It is an error to return from the e*aluation of a function with the U !/-U out#ut non4+ero unless at least one such assignment has been made.3.3.2.2Function and function bloc control statements. Function and function bloc= control statements consist of the mechanisms for in*o=ing function bloc=s andfor returning control to the in*o=ing entit$ before the #h$sical end of a function or function bloc=.Function e*aluation shall be in*o=ed as #art of ex#ression e*aluation' as s#ecified in &.&.0.Function bloc=s shall be in*o=ed b$ a statement consisting of the name of the function bloc= followed b$ a #arenthesi+ed list of named in#ut #arameter *alue assignments' as illustrated in table,D.The order in which in#ut #arameters are listed in a function bloc= in*ocation shall not be significant.It is not required that all in#ut #arameters be assigned *alues in e*er$ in*ocation of a function bloc=.If a #articular #arameter is not assigned a *alue in a function bloc= in*ocation' the #re*iousl$ assigned *alue (or the initial *alue' if no #re*ious assignment has been made) shall a##l$.The H!TOH/ statement shall #ro*ide earl$ exit from a function ' function bloc= or #rogram (e.g.' as the result of the e*aluation of an IF statement).3.3.2.3Selection statementsSelection statements include the IF and C)S! statements.) selection statement selects one (or a grou#) of its com#onent statements for execution' based on a s#ecified condition.!xam#les of selection statements are gi*en in table ,D.The IF statement s#ecifies that a grou# of statements is to be executed onl$ if the associated %oolean ex#ression e*aluates to the *alue 0 (true).If the condition is false' then either no statement is to be executed' or the statement grou# following the !LS! =e$word (or the !LSIF =e$word if its associated %oolean condition is true) is to be executed.The C)S! statement consists of an ex#ression which shall e*aluate to a *ariable of t$#e I/T (the UselectorU)'and a list of statement grou#s' each grou# being labelled b$ one or more integers or ranges of integer *alues.It s#ecifies that the first grou# of statements' one of whose ranges contains the com#uted *alue of the selector' shall be executed .If the *alue of the selector does not occur in a range of an$ case' the statement sequence following the =e$word !LS! (if it occurs in the C)S! statement) shall be executed.-therwise' none of the statement sequences shall be executed.3.3.2.4Iteration statementsIteration statements s#ecif$ that the grou# of associated statements shall be executed re#eatedl$.The F-Hstatement is used if the number of iterations can be determined in ad*anceI otherwise' the 8>IL! or H!(!)T constructs are used.The !BIT statement shall be used to terminate iterations before the termination condition is satisfied.8hen the !BIT statement is located within nested iterati*e constructs' exit shall be from the innermost loo# in which the !BIT is located' that is' control shall #ass to the next statement after the first loo# terminator (!/.KF-H' !/.K8>IL!' or !/.KH!(!)T) following the !BIT statement. For instance' after executing the statements shown in figure 11' the *alue of the *ariable SOA shall be 0, if the *alue of the %oolean *ariable FL)? is 7' and D if FL)?:0.SUM := 0 ;FOR I := 1 TO 3 DOFOR J := 1 TO 2 DOIF FLAG THEN EXIT ; END_IFSUM := SUM + J ;END_FOR ;SUM := SUM + I ;END_FOR ;%igure && - E'(T statement e!ampleThe F-H statement indicates that a statement sequence shall be re#eatedl$ executed' u# to the !/.KF-H =e$word' while a #rogression of *alues is assigned to the F-H loo# control *ariable.The control *ariable'initial *alue' and final *alue shall be ex#ressions of the same integer t$#e (SI/T' I/T' or .I/T) and shall not be altered b$ an$ of the re#eated statements.The F-H statement increments the control *ariable u# ordown from an initial *alue to a final *alue in increments determined b$ the *alue of an ex#ressionI this *alue defaults to 0.The test for the termination condition is made at the beginning of each iteration' so that the statement sequence is not executed if the initial *alue exceeds the final *alue.The *alue of the control *ariable after com#letion of the F-H loo# is im#lementation4de#endent.)n exam#le of the usage of the F-H statement is gi*en in feature D of table ,D.In this exam#le' the F-H loo# is used to determine the index P of the first occurrence (if an$) of the string ST!CS in the odd4numbered elements of an arra$ of strings 8-H.S with a subscri#t range of (0..077). If no occurrence is found' P will ha*e the *alue 070.The 8>IL! statement causes the sequence of statements u# to the !/.K8>IL! =e$word to be executedre#eatedl$ until the associated %oolean ex#ression is false.If the ex#ression is initiall$ false' then the grou# of statements is not executed at all.For instance' the F-H...!/.KF-H exam#le gi*en in table ,D can be rewritten using the 8>IL!...!/.K8>IL! construction shown in table ,D.The H!(!)T statement causes the sequence of statements u# to the O/TIL =e$word to be executed re#eatedl$ (and at least once) until the associated %oolean condition is true.For instance' the 8>IL!...!/.K8>IL! exam#le gi*en in table ,D can be rewritten using the H!(!)T...!/.KH!(!)T construction shown in table ,D.The 8>IL! and H!(!)T statements shall not be used to achie*e inter#rocess s$nchroni+ation' for exam#le as a Uwait loo# with an externall$ determined termination condition.The SFC elements defined in1.D shall be used for this #ur#ose.It shall be an error in the sense of 0.,.0 if a 8>IL! or H!(!)T statement is used in an algorithm for which satisfaction of the loo# termination condition or execution of an !BIT statement cannot be guaranteed.