Skip to content

OSL ‐ Operators and Expressions

Mistium edited this page Mar 15, 2024 · 14 revisions

Mathematical Equations in OSL

In originOS scripting, mathematical equations play a crucial role in creating dynamic systems and performing calculations. These equations involve operators and comparisons that allow you to manipulate data and make decisions based on conditions. Here's a comprehensive guide to mathematical equations in OSL:

How They Function

Mathematical expressions are evaluated based on the given operators and data. For example:

if true and false (

)

When parsed, it becomes:

if false (

)

The and statement replaces true and false with false, demonstrating how expressions can be dynamically altered.

Syntax

The general syntax for mathematical equations in OSL is:

data (operator) data2

Operators

1. +

  • (If both data are integers) Adds the number on the right and left together.
  • (If either data is not an integer) Joins the right and left with a space in-between.
  • (If data on the left is an array) Appends the right data on the right to the array.
  • (If data on the right is an array) Appends the left data on the left to the array. (v4.6.9 or later)

Example:

result = 5 + 3    // result is 8
text_result = "hello" + "world"    // text_result is "hello world"
array_result = ["apple"] + "orange"    // array_result is ["apple", "orange"]
array_result = "orange" + ["apple"] // array_result is ["orange","apple"]

2. ++

  • (If both data are arrays) Concatenates the two arrays together.
  • (Else) Joins the two sides together with no spaces.

Example:

array_result = ["hello"] ++ ["world"]    // array_result is ["hello", "world"]
text_result = "hello" ++ "world"    // text_result is "helloworld"

3. -

  • Takes the number on the right away from the number on the left.
  • (If the data on the left is an array) Removes all instances of the specified string from the array.

Example:

result = 8 - 3    // result is 5
array_result1 = ["hello"] - "hello"    // array_result1 is []
array_result2 = ["hello"] - 1    // array_result2 is []
text_result = "hello" - "llo"    // text_result is "he"

4. /

  • Divides the number on the left by the number on the right.

Example:

result = 10 / 2    // result is 5

5. *

  • (If the data on the left is a number) Multiplies the number on the left and right together.
  • (If the data on the left is text) Repeats the text by the number on the right.

Example:

result = 5 * 3    // result is 15
text_result = "hello" * 3    // text_result is "hellohellohello"

6. %

  • Runs modulus on the left number using the right number.

Example:

result = 10 % 3    // result is 1

7. ^

  • Runs an exponential on the left number.

Example:

result = 2 ^ 3    // result is 8

8. //

  • Divides the number on the left by the number on the right and then rounds it down to the biggest whole number.

Example:

(floor divide):
result = 10 // 3    

^ result is 3

(normal divide):
result = 10 / 3    

^ result is 3.3333333

Comparisons

1. ==

  • True if both sides are the same (non-case-sensitive).

Example:

if "apple" == "Apple" (
   // This block will be executed because the comparison is not case-sensitive.
)

2. !=

  • True if both sides aren't the same (non-case-sensitive).

Example:

if "apple" != "orange" (
   // This block will be executed because the strings are not the same.
)

3. ===

  • True if both sides are the same (case-sensitive).

Example:

if "apple" === "apple" (
   // This block will be executed because the strings are the same in a case-sensitive manner.
)

4. >

  • True if the number on the left is bigger than the one on the right.

Example:

if 10 > 5 (
   // This block will be executed because 10 is greater than 5.
)

5. <

  • True if the number on the right is bigger than the one on the left.

Example:

if 5 < 10 (
   // This block will be executed because 5 is less than 10.
)

6. >=

  • True if the number on the left is bigger than or equal to the one on the right.

Example:

if 10 >= 10 (
   // This block will be executed because 10 is equal to 10.
)

7. <=

  • True

if the number on the right is bigger than or equal to the one on the left.

Example:

if 5 <= 10 (
   // This block will be executed because 5 is less that 10.
)

8. !==

  • True if both sides aren't the same (case-sensitive).

Example:

if "apple" !== "apple" (
   // This block will not be executed because the strings are the same in a case-sensitive manner.
)

9. in

  • True if the right data contains the left data

Example:

if "apple" in "apples are great" (
    // This block will be executed because the string "apple" is in the string "apples are great"
)

Boolean Expressions

1. and

  • True if both sides are true.

Example:

if true and false (
   // This block won't be executed because the condition is false.
)

2. or

  • True if at least one side is true.

Example:

if true or false (
   // This block will be executed because the condition is true.
)

3. xor

  • True if exactly one side is true.

Example:

if true xor false (
   // This block will be executed because only one side is true.
)

4. nor

  • True if both sides are false.

Example:

if false nor false (
   // This block will be executed because both sides are false.
)

5. nand

  • True if both sides aren't true.

Example:

if false nand false (
   // This block will be executed because both sides aren't true.
)

Advanced

1. ?

  • If the boolean before the ? is true it returns the first string on the right else it returns the second string

Example:

say false ? "hello" "world"

^ this will output "world"

say true ? "hello" "world"

^ this will output "hello"

Example Use Cases

Use Case 1: Mathematical Operation.

number_result = 5 + 3    // number_result is 8
text_result = "hello" * 3    // text_result is "hellohellohello"

Use Case 2: Data Manipulation

array_result = ["apple"] + "orange"    // array_result is ["apple", "orange"]
text_result = "hello" - "llo"    // text_result is "he"

Use Case 3: String Comparison

if "apple" == "Apple" (
   // This block will be executed because the comparison is not case-sensitive.
)

Use Case 4: Boolean Expressions

if true and false (
   // This block won't be executed because the condition is false.
)

These examples showcase the versatility of mathematical equations and boolean expressions in originOS scripting, providing you with powerful tools to manipulate data and make logical decisions in your applications.

originOS Wiki

Wiki Views: :views


OSL | OTS | ICN | OASM

Clone this wiki locally