Difference between revisions of "SCI Programming Language/Primitive Procedures"
(69 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
<div align="center"> | <div align="center"> | ||
Chapter: | Chapter: | ||
− | [[ | + | [[SCI Programming Language/Introduction|1]] | |
− | [[ | + | [[SCI Programming Language/Primitive Procedures|2]] | |
− | [[ | + | [[SCI Programming Language/Primitive_Procedures#Arithmetic_Primitives|3]] | |
− | [[ | + | [[SCI Programming Language/Primitive_Procedures#Boolean_Primitives|4]] | |
− | [[ | + | [[SCI Programming Language/Primitive_Procedures#Assignment_Primitives |5]] | |
− | [[ | + | [[SCI Programming Language/Data Types and Variables|6]] | |
− | [[ | + | [[SCI Programming Language/Definitions|7]] | |
− | [[ | + | [[SCI Programming Language/Control Flow|8]] | |
− | [[ | + | [[SCI Programming Language/Control Flow#Conditionals|9]] | |
− | [[ | + | [[SCI Programming Language/Control Flow#Iteration|10]] | |
− | [[ | + | [[SCI Programming Language/Procedures|11]] | |
− | [[ | + | [[SCI Programming Language/Files|12]] | |
− | [[ | + | [[SCI Programming Language/Compiling SCI|13]] | |
− | [[ | + | [[SCI Programming Language/Index|Index]] |
</div><br /> | </div><br /> | ||
Line 27: | Line 27: | ||
==<br /> Primitive Procedures == | ==<br /> Primitive Procedures == | ||
− | ===<br /> Arithmetic | + | ===<br /> Arithmetic Primitives === |
In the following examples, e1, e2, etc. are arbitrary expressions. Brackets [...] indicate optional entries. Procedures evaluate their parameters from left to right. | In the following examples, e1, e2, etc. are arbitrary expressions. Brackets [...] indicate optional entries. Procedures evaluate their parameters from left to right. | ||
+ | <blockquote> | ||
====<br /> Addition ==== | ====<br /> Addition ==== | ||
− | <blockquote><code>(+ e1 e2 [e3...])</code> | + | <blockquote> |
− | + | <code>(+ e1 e2 [e3...])</code> | |
− | evaluates to: e1 + e2 [+ e3...] | + | {| |
− | + | |- | |
− | example: (+ 7 12 4) evaluates to 23. | + | |evaluates to: ||e1 + e2 [+ e3...] |
+ | |- | ||
+ | |''example:'' ||<code>(+ 7 12 4)</code> evaluates to 23. | ||
+ | |} | ||
+ | </blockquote> | ||
====<br /> Multiplication ==== | ====<br /> Multiplication ==== | ||
− | <blockquote><code>(* e1 e2 [e3...])</code> | + | <blockquote> |
− | + | <code>(* e1 e2 [e3...])</code> | |
− | evaluates to: | + | {| |
− | + | |- | |
− | example: (* 2 10 3) evaluates to 60. | + | |evaluates to: ||e1 (*e2[*e3...] |
+ | |- | ||
+ | |''example:'' ||<code>(* 2 10 3)</code> evaluates to 60. | ||
+ | |} | ||
+ | </blockquote> | ||
====<br /> Subtraction ==== | ====<br /> Subtraction ==== | ||
− | <blockquote><code>(- e1 e2)</code | + | <blockquote> |
− | + | <code>(- e1 e2)</code> | |
− | evaluates to : e1 - e2 | + | {| |
− | + | |- | |
− | example: (- 20 11) evaluates to 9. | + | |evaluates to: ||e1 - e2 |
+ | |- | ||
+ | |''example:'' ||<code>(- 20 11)</code> evaluates to 9. | ||
+ | |} | ||
+ | </blockquote> | ||
====<br /> Division ==== | ====<br /> Division ==== | ||
− | <blockquote><code>(/ e1 e2)</code> | + | <blockquote> |
− | + | <code>(/ e1 e2)</code> | |
− | evaluates to: e1 / e2 | + | {| |
− | + | |- | |
− | example: (/ 24 6) evaluates to 4. < | + | |evaluates to: |e1 / e2 |
+ | |- | ||
+ | |''example:'' ||<code>(/ 24 6)</code> evaluates to 4.* | ||
+ | |} | ||
+ | <nowiki>*</nowiki> Note: does this round when necessary (up, down)? | ||
+ | </blockquote> | ||
====<br /> Remainder ==== | ====<br /> Remainder ==== | ||
− | <blockquote><code>(mod e1 e2)</code> | + | <blockquote> |
− | + | <code>(mod e1 e2)</code> | |
− | evaluates to: the remainder of e1 when divided by e2. | + | {| |
− | + | |- | |
− | example: (mod 17 5) evaluates to 2. | + | |evaluates to: ||the remainder of e1 when divided by e2. |
+ | |- | ||
+ | |''example:'' ||<code>(mod 17 5)</code> evaluates to 2. | ||
+ | |} | ||
+ | </blockquote> | ||
====<br /> Operation Shift Left ==== | ====<br /> Operation Shift Left ==== | ||
− | <blockquote><code>(<< e1 e2)</code> | + | <blockquote><code>(<< e1 e2)</code> |
− | + | {| width="500" | |
− | evaluates to : e1<< e2 where the << operation shifts its left hand side left by the number of bits specified by its right hand side. | + | |- |
− | + | |valign="top" |evaluates to: ||e1<< e2 where the << operation shifts its left hand side left by the number of bits specified by its right hand side. | |
− | example: (<< 7 2 ) evaluates to 28. | + | |- |
− | + | |''example:'' ||<code>(<< 7 2 )</code> evaluates to 28. | |
− | + | |- | |
+ | | ||In binary: 111 << 2 = 11100 | ||
+ | |} | ||
+ | </blockquote> | ||
====<br /> Operation Shift Right ==== | ====<br /> Operation Shift Right ==== | ||
− | <blockquote><code>(>> e1 e2)</code> | + | <blockquote> |
− | + | <code>(>> e1 e2)</code> | |
− | evaluates to: e1 >> e2 (as in << except with a right shift) | + | {| |
− | + | |- | |
− | example: (>> 7 2) evaluates to 1. | + | |evaluates to: ||e1 >> e2 (as in << except with a right shift) |
− | + | |- | |
− | + | |''example:'' ||<code>(>> 7 2)</code> evaluates to 1. | |
+ | |- | ||
+ | | ||In binary: 111 >> 2 = 001 | ||
+ | |} | ||
+ | </blockquote> | ||
====<br /> Bitwise Exclusive OR Operator ==== | ====<br /> Bitwise Exclusive OR Operator ==== | ||
− | <blockquote><code>(^ e1 e2 [e3...])</code> | + | <blockquote> |
− | + | <code>(^ e1 e2 [e3...])</code> | |
− | evaluates to: e1^ e2 [^ e3...] | + | {| |
− | + | |- | |
− | example: (^ 11 26) evaluates to 17. | + | |evaluates to: ||e1^ e2 [^ e3...] |
− | + | |- | |
− | + | |example: ||(^ 11 26) evaluates to 17. | |
+ | |- | ||
+ | | ||In binary: 01011" 11010 = 10001 | ||
+ | |} | ||
+ | </blockquote> | ||
====<br /> Bitwise AND Operator ==== | ====<br /> Bitwise AND Operator ==== | ||
Line 141: | Line 174: | ||
| ||In binary: ~01011 = 1111111111110100 (all the leading 0s in the 16 bit number change to 1s). | | ||In binary: ~01011 = 1111111111110100 (all the leading 0s in the 16 bit number change to 1s). | ||
|} | |} | ||
+ | </blockquote> | ||
</blockquote> | </blockquote> | ||
− | ===<br /> Boolean | + | ===<br /> Boolean Primitives === |
These procedures evaluate their parameters from left to right and terminate the moment the | These procedures evaluate their parameters from left to right and terminate the moment the | ||
Line 150: | Line 184: | ||
indicate optional entries. The compiler predefines FALSE to be 0 and TRUE to be 1. | indicate optional entries. The compiler predefines FALSE to be 0 and TRUE to be 1. | ||
+ | <blockquote> | ||
====<br /> Greater Than ==== | ====<br /> Greater Than ==== | ||
+ | <blockquote> | ||
+ | |<code>(> e1 e2 [e3...])</code> | ||
{| | {| | ||
− | |||
|- | |- | ||
− | |evaluates to: ||TRUE if e1 > e2 [> e3...], else FALSE. | + | |evaluates to: ||TRUE if e1 > e2 [> e3...], else FALSE. |
|- | |- | ||
|''example:'' ||(> 7 4 6) evaluates to FALSE. | |''example:'' ||(> 7 4 6) evaluates to FALSE. | ||
|} | |} | ||
+ | </blockquote> | ||
====<br /> Greater Than or Equals ==== | ====<br /> Greater Than or Equals ==== | ||
+ | <blockquote> | ||
+ | <code>(>= e1 e2 [e3...])</code> | ||
{| | {| | ||
− | |||
|- | |- | ||
− | |evaluates to: ||TRUE if e1 >= e2 [>= e3...], else FALSE. | + | |evaluates to: ||TRUE if e1 >= e2 [>= e3...], else FALSE. |
|- | |- | ||
− | |''example:'' ||(>= 7 4 4) evaluates to TRUE. | + | |''example:'' ||<code>(>= 7 4 4)</code> evaluates to TRUE. |
+ | |} | ||
+ | </blockquote> | ||
====<br /> Less Than ==== | ====<br /> Less Than ==== | ||
− | (< e1 e2 [e3...]) | + | <blockquote> |
− | |- | + | <code>(< e1 e2 [e3...])</code> |
− | |evaluates to: ||TRUE if e1 < e2 [< e3...], else FALSE. | + | {| |
+ | |- | ||
+ | |evaluates to: ||TRUE if e1 < e2 [< e3...], else FALSE. | ||
|- | |- | ||
− | |''example:'' ||(< 2 4 5) evaluates to TRUE. | + | |''example:'' ||<code>(< 2 4 5)</code> evaluates to TRUE. |
|} | |} | ||
+ | </blockquote> | ||
====<br /> Less Than or Equals ==== | ====<br /> Less Than or Equals ==== | ||
+ | <blockquote> | ||
+ | <code>(<= e1 e2 [e3...])</code> | ||
{| | {| | ||
− | |||
|- | |- | ||
− | |evaluates to: ||TRUE if e1 <= e2 [<= e3...], else FALSE. | + | |evaluates to: ||TRUE if e1 <= e2 [<= e3...], else FALSE. |
|- | |- | ||
− | |''example:'' ||(<= 7 8 7) evaluates to FALSE. | + | |''example:'' ||<code>(<= 7 8 7)</code> evaluates to FALSE. |
|} | |} | ||
+ | </blockquote> | ||
====<br /> Is Equal To ==== | ====<br /> Is Equal To ==== | ||
+ | <blockquote> | ||
+ | <code>(== e1 e2 [e3...])</code> | ||
{| | {| | ||
− | |||
|- | |- | ||
− | |evaluates to: ||TRUE if e1 == e2 [== e3...], else FALSE. | + | |evaluates to: ||TRUE if e1 == e2 [== e3...], else FALSE. |
|- | |- | ||
− | |''example:'' ||(== 1 TRUE 1) evaluates to TRUE. | + | |''example:'' ||<code>(== 1 TRUE 1)</code> evaluates to TRUE. |
|} | |} | ||
− | + | </blockquote> | |
− | |||
====<br /> Is Not Equal To ==== | ====<br /> Is Not Equal To ==== | ||
+ | <blockquote> | ||
+ | <code>(!= e1 e2 [e3...])</code> | ||
{| | {| | ||
− | + | |- | |
− | |evaluates to: ||TRUE if e1 != e2 [!= e3...], else FALSE.|- | + | |evaluates to: ||TRUE if e1 != e2 [!= e3...], else FALSE. |
− | |''example:'' ||( ! = 7 4 6) evaluates to TRUE. | + | |- |
+ | |''example:'' ||<code>( ! = 7 4 6)</code> evaluates to TRUE. | ||
|} | |} | ||
+ | </blockquote> | ||
====<br /> AND ==== | ====<br /> AND ==== | ||
+ | <blockquote> | ||
+ | <code>(and e1 e2 [e3...])</code> | ||
{| | {| | ||
− | |||
|- | |- | ||
− | |evaluates to: ||TRUE if all the expressions are non-zero, else FALSE.|- | + | |evaluates to: ||TRUE if all the expressions are non-zero, else FALSE. |
− | |''example:'' ||(and 7 4 6) evaluates to TRUE. | + | |- |
+ | |''example:'' ||<code>(and 7 4 6)</code> evaluates to TRUE. | ||
|} | |} | ||
+ | </blockquote> | ||
====<br /> OR ==== | ====<br /> OR ==== | ||
+ | <blockquote> | ||
+ | <code>(or e1 e2 [e3...])</code> | ||
{| | {| | ||
− | |||
|- | |- | ||
− | |evaluates to: ||TRUE if any of the expressions are non-zero, else FALSE.|- | + | |evaluates to: ||TRUE if any of the expressions are non-zero, else FALSE. |
− | |''example:'' ||(or 3 0 2) evaluates to TRUE. | + | |- |
+ | |''example:'' ||<code>(or 3 0 2)</code> evaluates to TRUE. | ||
|} | |} | ||
+ | </blockquote> | ||
====<br /> NOT ==== | ====<br /> NOT ==== | ||
− | (not el)|- | + | <blockquote> |
− | |evaluates to: ||TRUE if the expression is zero, else FALSE.|- | + | <code>(not el)</code> |
− | |''example:'' ||(not 6) evaluates to FALSE. | + | {| |
+ | |- | ||
+ | |evaluates to: ||TRUE if the expression is zero, else FALSE. | ||
+ | |- | ||
+ | |''example:'' ||<code>(not 6)</code> evaluates to FALSE. | ||
|} | |} | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
− | + | ===<br /> Assignment Primitives === | |
− | ===<br /> Assignment | ||
All assignment procedures store a value in a variable and return that value as the result of the assignment. In the following, v is a variable and e is an expression. | All assignment procedures store a value in a variable and return that value as the result of the assignment. In the following, v is a variable and e is an expression. | ||
+ | <blockquote> | ||
{| | {| | ||
|- | |- | ||
− | |<code>(= v e)</code> ||evaluates to ||v = e | + | |<code>(= v e)</code> ||evaluates to ||v = e |
|- | |- | ||
|<code>(+= v e)</code> ||evaluates to ||v = v + e | |<code>(+= v e)</code> ||evaluates to ||v = v + e | ||
Line 250: | Line 311: | ||
|<code>(/= v e)</code> ||evaluates to ||v = v / e | |<code>(/= v e)</code> ||evaluates to ||v = v / e | ||
|- | |- | ||
− | |<code>(|= v e)</code> ||evaluates to ||v = v | e | + | |<code>(<nowiki>|</nowiki>= v e)</code> ||evaluates to ||v = v <nowiki>|</nowiki> e |
|- | |- | ||
|<code>(&= v e)</code> ||evaluates to ||v = v & e | |<code>(&= v e)</code> ||evaluates to ||v = v & e | ||
Line 264: | Line 325: | ||
|<code>(--v)</code> ||evaluates to ||v = v - 1 | |<code>(--v)</code> ||evaluates to ||v = v - 1 | ||
|} | |} | ||
− | + | </blockquote> | |
− | |||
;Notes | ;Notes | ||
<references /> | <references /> | ||
− | + | [[SCI Programming Language | Table of Contents]] | |
− | |||
− | [[ | ||
− | & | + | <span style="float: left">[[SCI Programming Language/Introduction|< Previous: Introduction]]</span><span style="float: right">[[SCI Programming Language/Data Types and Variables|Next: Data Types and Variables >]]</span> |
− | + | [[Category:SCI Documentation]] | |
+ | [[Category:SCI32]] | ||
+ | [[Category:Scripting]] |
Latest revision as of 21:46, 24 May 2016
Primitive Procedures
Arithmetic Primitives
In the following examples, e1, e2, etc. are arbitrary expressions. Brackets [...] indicate optional entries. Procedures evaluate their parameters from left to right.
Addition
(+ e1 e2 [e3...])
evaluates to: e1 + e2 [+ e3...] example: (+ 7 12 4)
evaluates to 23.
Multiplication
(* e1 e2 [e3...])
evaluates to: e1 (*e2[*e3...] example: (* 2 10 3)
evaluates to 60.
Subtraction
(- e1 e2)
evaluates to: e1 - e2 example: (- 20 11)
evaluates to 9.
Division
(/ e1 e2)
e1 / e2 example: (/ 24 6)
evaluates to 4.** Note: does this round when necessary (up, down)?
Remainder
(mod e1 e2)
evaluates to: the remainder of e1 when divided by e2. example: (mod 17 5)
evaluates to 2.
Operation Shift Left
(<< e1 e2)
evaluates to: e1<< e2 where the << operation shifts its left hand side left by the number of bits specified by its right hand side. example: (<< 7 2 )
evaluates to 28.In binary: 111 << 2 = 11100
Operation Shift Right
(>> e1 e2)
evaluates to: e1 >> e2 (as in << except with a right shift) example: (>> 7 2)
evaluates to 1.In binary: 111 >> 2 = 001
Bitwise Exclusive OR Operator
(^ e1 e2 [e3...])
evaluates to: e1^ e2 [^ e3...] example: (^ 11 26) evaluates to 17. In binary: 01011" 11010 = 10001
Bitwise AND Operator
(& e1 e2 [e3...])
evaluates to: e1 &e2 [& e3...] example: (& 11 26)
evaluates to 10.In binary: 01011 & 11010 = 01010
Bitwise OR Operator
(| e1 e2 [e3...])
evaluates to: e2 [| e3...] example: (| 11 26)
evaluates to 27.In binary: 01011 111010 = 11011
Bitwise NOT
(~ e1)
evaluates to: the bitwise not of e1 (all 1 bits are changed to 0 and all 0 bits are changed to 1). example: (~ 11)
evaluates to -12.In binary: ~01011 = 1111111111110100 (all the leading 0s in the 16 bit number change to 1s).
Boolean Primitives
These procedures evaluate their parameters from left to right and terminate the moment the truth value of the expression is determined. If the truth value of the Boolean expression is determined before an expression is reached, that expression is never evaluated. Brackets [...] indicate optional entries. The compiler predefines FALSE to be 0 and TRUE to be 1.
Greater Than|
(> e1 e2 [e3...])
evaluates to: TRUE if e1 > e2 [> e3...], else FALSE. example: (> 7 4 6) evaluates to FALSE.
Greater Than or Equals
(>= e1 e2 [e3...])
evaluates to: TRUE if e1 >= e2 [>= e3...], else FALSE. example: (>= 7 4 4)
evaluates to TRUE.
Less Than
(< e1 e2 [e3...])
evaluates to: TRUE if e1 < e2 [< e3...], else FALSE. example: (< 2 4 5)
evaluates to TRUE.
Less Than or Equals
(<= e1 e2 [e3...])
evaluates to: TRUE if e1 <= e2 [<= e3...], else FALSE. example: (<= 7 8 7)
evaluates to FALSE.
Is Equal To
(== e1 e2 [e3...])
evaluates to: TRUE if e1 == e2 [== e3...], else FALSE. example: (== 1 TRUE 1)
evaluates to TRUE.
Is Not Equal To
(!= e1 e2 [e3...])
evaluates to: TRUE if e1 != e2 [!= e3...], else FALSE. example: ( ! = 7 4 6)
evaluates to TRUE.
AND
(and e1 e2 [e3...])
evaluates to: TRUE if all the expressions are non-zero, else FALSE. example: (and 7 4 6)
evaluates to TRUE.
OR
(or e1 e2 [e3...])
evaluates to: TRUE if any of the expressions are non-zero, else FALSE. example: (or 3 0 2)
evaluates to TRUE.
NOT
(not el)
evaluates to: TRUE if the expression is zero, else FALSE. example: (not 6)
evaluates to FALSE.
Assignment Primitives
All assignment procedures store a value in a variable and return that value as the result of the assignment. In the following, v is a variable and e is an expression.
(= v e)
evaluates to v = e (+= v e)
evaluates to v = v + e (-= v e)
evaluates to v = v - e (*= v e)
evaluates to v = v * e (/= v e)
evaluates to v = v / e (|= v e)
evaluates to v = v | e (&= v e)
evaluates to v = v & e (^= v e)
evaluates to v = v ^ e (>>= v e)
evaluates to v = v >> e (<<= v e)
evaluates to v = v << e (++ v)
evaluates to v = v + 1 (--v)
evaluates to v = v - 1
- Notes