Difference between revisions of "SIPL"

From Software Inc.
Jump to navigation Jump to search
Line 30: Line 30:
 
==Built-in functions==
 
==Built-in functions==
 
===Math functions===
 
===Math functions===
Abs(x) – Returns the absolute value of x
+
* Abs(x) – Returns the absolute value of x
Pow(x, y) – Raises x to the power of y
+
* Pow(x, y) – Raises x to the power of y
Sqrt(x) – Returns the square root of x
+
* Sqrt(x) – Returns the square root of x
Log(x) – Returns the natural logarithm of x
+
* Log(x) – Returns the natural logarithm of x
Log10(x) – Returns the base 10 logarithm of x
+
* Log10(x) – Returns the base 10 logarithm of x
Round(x) – Rounds x to the nearest integer
+
* Round(x) – Rounds x to the nearest integer
Ceil(x) – Rounds x up to the nearest integer
+
* Ceil(x) – Rounds x up to the nearest integer
Floor(x) – Rounds x down to the nearest integer
+
* Floor(x) – Rounds x down to the nearest integer
Min(x, y) – Returns the smallest number between x and y
+
* Min(x, y) – Returns the smallest number between x and y
Max(x, y) – Returns the largest number between x and y
+
* Max(x, y) – Returns the largest number between x and y
Sign(x) – Returns -1, 0, 1 based on the sign of x
+
* Sign(x) – Returns -1, 0, 1 based on the sign of x
Sin(x) – Returns the sine of x
+
* Sin(x) – Returns the sine of x
Cos(x) – Returns the cosine of x
+
* Cos(x) – Returns the cosine of x
Lerp(a, b, t) – Interpolates between a and b using t
+
* Lerp(a, b, t) – Interpolates between a and b using t
Clamp(x, a, b) – Clamps x between a and b
+
* Clamp(x, a, b) – Clamps x between a and b
Clamp01(x) – Clamps x between 0 and 1
+
* Clamp01(x) – Clamps x between 0 and 1
 
===Enumerable functions===
 
===Enumerable functions===
 
These functions can be used on any C# enumerable, e.g. arrays, lists, dictionaries, sets, etc. You simply use the function on the enumerable, and pass an anonymous function as a parameter, using x to refer to the elements of the enumerable, e.g. employees.Where(x.Salary > 1000).OrderBy(x.Age).Select(x.Name), will return the names of all employees that make more than $1000 ordered by their age. They work exactly like LINQ extension methods, basically.
 
These functions can be used on any C# enumerable, e.g. arrays, lists, dictionaries, sets, etc. You simply use the function on the enumerable, and pass an anonymous function as a parameter, using x to refer to the elements of the enumerable, e.g. employees.Where(x.Salary > 1000).OrderBy(x.Age).Select(x.Name), will return the names of all employees that make more than $1000 ordered by their age. They work exactly like LINQ extension methods, basically.
  
Any(func) – Returns whether any elements satisfy func(x)
+
* Any(func) – Returns whether any elements satisfy func(x)
All(func) – Returns whether all elements satisfy func(x)
+
* All(func) – Returns whether all elements satisfy func(x)
None(func) - Returns whether no elements satisfy func(x)
+
* None(func) - Returns whether no elements satisfy func(x)
ForEach(func) – Runs func(x) on all elements
+
* ForEach(func) – Runs func(x) on all elements
Select(func) – Runs func(x) on all elements and returns the result
+
* Select(func) – Runs func(x) on all elements and returns the result
SelectMany(func) – Assuming input is an enumerable of enumerables, runs func(x) on elements in each sub enumerable and returns it as a flat enumerable
+
* SelectMany(func) – Assuming input is an enumerable of enumerables, runs func(x) on elements in each sub enumerable and returns it as a flat enumerable
Count(func) – Returns how many elements satisfy func(x)
+
* Count(func) – Returns how many elements satisfy func(x)
Where(func) – Returns elements that satisfy func(x)
+
* Where(func) – Returns elements that satisfy func(x)
FindFirst(func) – Returns the first element that satisfies func(x)
+
* FindFirst(func) – Returns the first element that satisfies func(x)
OrderBy(func) – Returns elements sorted by func(x) in ascending order
+
* OrderBy(func) – Returns elements sorted by func(x) in ascending order
OrderByDescending(func) – Pretty obvious
+
* OrderByDescending(func) – Pretty obvious
Distinct() – Returns only unique elements in the enumerable
+
* Distinct() – Returns only unique elements in the enumerable
Sum(func) – Returns the sum of running func(x) on all elements
+
* Sum(func) – Returns the sum of running func(x) on all elements
Average(func) – Returns the sum of running func(x) on all elements divided by the amount of elements
+
* Average(func) – Returns the sum of running func(x) on all elements divided by the amount of elements
Size() – Returns how many elements there are in the enumerable
+
* Size() – Returns how many elements there are in the enumerable
GetRandomElement() – Returns a random element from the enumerable
+
* GetRandomElement() – Returns a random element from the enumerable
 +
 
 
===Other functions===
 
===Other functions===
String(x) – Converts x to a string, using C# .ToString()
+
* String(x) – Converts x to a string, using C# .ToString()
FormatString(x, y) – Converts x to a string with a format string y using C# .Tostring(format)
+
* FormatString(x, y) – Converts x to a string with a format string y using C# .Tostring(format)
Debug(x) – Writes x to the log using Unity’s log system
+
* Debug(x) – Writes x to the log using Unity’s log system
Random() – Returns a random decimal number between 0 and 1
+
* Random() – Returns a random decimal number between 0 and 1
RandomRange(x, y) – Returns a random decimal number between x and y
+
* RandomRange(x, y) – Returns a random decimal number between x and y
RandomInteger(x, y) – Returns a random integer between x and y(exclusive)
+
* RandomInteger(x, y) – Returns a random integer between x and y(exclusive)

Revision as of 17:54, 2 February 2020

The Software Inc. Programming Language consists of a lexer, parser and interpreter written specifically for scripting in Software Inc.

Performance considerations

SIPL is an interpreted language and relies heavily on the .NET reflection library to interact with C# types, which makes it somewhat slow. Since scripts aren’t executed very often, it won’t be much of a problem, just keep the complexity of your scripts in mind.

Syntax

SIPL was written to resemble C# as much as possible, while still remaining a simple scripting language, so the simplest way to explain the syntax is how it varies from C#:

  • You cannot define namespaces, classes or functions
  • Number operations in SIPL convert all numbers to doubles behind the scenes
  • You can use the var keyword to declare temporary variables, but you cannot specify their type
  • You cannot use bitwise operations. The ^ symbol raises a number to a power
  • You cannot increment or use operations together with assignment, i.e. += or ++. You must write i = i + 1
  • SIPL does not support the new keyword, but you can create arrays by writing ~[1, 2, 3]
  • For loops are not supported, however you can write foreach (element in list)
  • Comparisons can be chained, e.g. if (10 < x < 20)
  • Any enumerable can be indexed into with a number (except for dictionaries), including sets.
  • Single quotation marks (') do nothing
  • Multiline comments are not supported

Example

var words = ~["hello", " ", "world"]; //Create an array
if (5 < 10 < 20) //Always true
	{
	var result = "";
	foreach (word in words)
		{
		result = result + word; //Concatenate the strings from the array
		}
	Debug(result); //Log the result
	}

Built-in functions

Math functions

  • Abs(x) – Returns the absolute value of x
  • Pow(x, y) – Raises x to the power of y
  • Sqrt(x) – Returns the square root of x
  • Log(x) – Returns the natural logarithm of x
  • Log10(x) – Returns the base 10 logarithm of x
  • Round(x) – Rounds x to the nearest integer
  • Ceil(x) – Rounds x up to the nearest integer
  • Floor(x) – Rounds x down to the nearest integer
  • Min(x, y) – Returns the smallest number between x and y
  • Max(x, y) – Returns the largest number between x and y
  • Sign(x) – Returns -1, 0, 1 based on the sign of x
  • Sin(x) – Returns the sine of x
  • Cos(x) – Returns the cosine of x
  • Lerp(a, b, t) – Interpolates between a and b using t
  • Clamp(x, a, b) – Clamps x between a and b
  • Clamp01(x) – Clamps x between 0 and 1

Enumerable functions

These functions can be used on any C# enumerable, e.g. arrays, lists, dictionaries, sets, etc. You simply use the function on the enumerable, and pass an anonymous function as a parameter, using x to refer to the elements of the enumerable, e.g. employees.Where(x.Salary > 1000).OrderBy(x.Age).Select(x.Name), will return the names of all employees that make more than $1000 ordered by their age. They work exactly like LINQ extension methods, basically.

  • Any(func) – Returns whether any elements satisfy func(x)
  • All(func) – Returns whether all elements satisfy func(x)
  • None(func) - Returns whether no elements satisfy func(x)
  • ForEach(func) – Runs func(x) on all elements
  • Select(func) – Runs func(x) on all elements and returns the result
  • SelectMany(func) – Assuming input is an enumerable of enumerables, runs func(x) on elements in each sub enumerable and returns it as a flat enumerable
  • Count(func) – Returns how many elements satisfy func(x)
  • Where(func) – Returns elements that satisfy func(x)
  • FindFirst(func) – Returns the first element that satisfies func(x)
  • OrderBy(func) – Returns elements sorted by func(x) in ascending order
  • OrderByDescending(func) – Pretty obvious
  • Distinct() – Returns only unique elements in the enumerable
  • Sum(func) – Returns the sum of running func(x) on all elements
  • Average(func) – Returns the sum of running func(x) on all elements divided by the amount of elements
  • Size() – Returns how many elements there are in the enumerable
  • GetRandomElement() – Returns a random element from the enumerable

Other functions

  • String(x) – Converts x to a string, using C# .ToString()
  • FormatString(x, y) – Converts x to a string with a format string y using C# .Tostring(format)
  • Debug(x) – Writes x to the log using Unity’s log system
  • Random() – Returns a random decimal number between 0 and 1
  • RandomRange(x, y) – Returns a random decimal number between x and y
  • RandomInteger(x, y) – Returns a random integer between x and y(exclusive)