Skip to content

8. JSON In Line Math Expressions

CAS_ual_TY edited this page Jul 5, 2023 · 1 revision

JSON In-Line Math Expressions

Still under construction


Quick Language Explanation

There are terms that might be confusing. So here are some synonyms and explanations:

  • "returns" / "yields" / "results in": The result of an operation or function.
  • "parameter" / "argument": The values you pass to an operation or function.

Variables

  • Names may not start with digits (0-9)
  • Names may only contain letters (A-Z and a-z), digits (0-9) and underscores (_)

Types

  • int: Integers, eg. 0, 7, -87.
  • double: Floating point numbers, eg. 0.0, 1.7, -43.467, 2.
  • vec3: 3-dimensional vector made of 3 double elements representing any position or motion in a world.
  • boolean: Boolean (logical) values, eg. either true or false.
  • tag: NBT compound tags (see Minecraft wiki).
  • string: A sequence of characters, eg. 'hello', 'ABCdef123_', 'Let us also do some Spaces!!'.

Type Conversions

These are the possible type conversions (eg. whenever a double is required you can also put in a value of type int, but not the other way around):

  • int -> double
  • int -> string
  • double -> string
  • boolean -> string

Type Literals

E.g. how to make type values with text:

  • int: Just type the integer, eg. 0, 7, -87.
  • double: Type the floating point number using a ., eg. 0.0, 1.7, -43.467, 2.
  • vec3: Use the vec3 function mentioned below, eg. vec3(2, 4, 6).
  • boolean: Type the logical values, eg. either true or false.
  • tag: To make a new compound tag use the new_tag supplier function mentioned below and populate that using the put_nbt_* functions, eg. put_nbt_int(new_tag(), 'my_key', 7) makes a new tag and attaches the integer 7 with the key my_key resulting in {"my_key": 7}.
  • string: Write the sequence of characters wrapped by ' characters, eg. 'hello', 'ABCdef123_', 'Let us also do some Spaces!!'.

Operations

These tables represent all binary operations defined by types. If two operants have different types the resulting type is looked up and the non-matching operant is converted according to the type conversions definition. Eg. Adding a double and a int together will convert the later into a double first and then sum these two up.

Any operation between 2 types not being defined here means that it is also not defined in the system. Attempting such an operation will yield no result.


Addition (+) / Subtraction (-)

+ / - int double
int int double
double double double
+ / - vec3
vec3 vec3

The + operation also works with strings. Whenever a string is added together with another string, these are concatenated, eg. 'abc' + 'def' results in the string 'abcdef'.


Multiplication (*) / Division (/)

* / / int double vec3
int int double vec3
double double double vec3
vec3 vec3 vec3

Dividing by 0 yields no result.


Remainder (%)

Works only with the int type. Remainder by 0 yields no result.


Comparison (==, !=, >, >=, <, <=)

All of these comparison operators allow you to compare 2 values. They always yield a boolean as result depending on whether the comparison is true or false (eg. 0 <= 1 is obviously true, 'AB' == 'CD' is false because they are not equal). The comparison operators are available for the following types:

  • == (equality): int, double, vec3, boolean, string
  • != (inequality): int, double, vec3, boolean, string
  • > (greater than): int, double
  • >= (greater than or equal): int, double
  • < (less than): int, double
  • <= (less than or equal): int, double

Boolean AND (&&)

&& false true
false false false
true false true

Boolean OR (||)

&& false true
false false true
true true true

Boolean NOT (!)

This is a unary operation where you write the ! symbol in front of the operant.

  • !true -> false
  • !false -> true

Conditional (?:)

This is a ternary operation (3 operants) which yields the 2nd or 3rd operant depending on if the 1st operant is true or not:

  • condition ? value_if_true : value_if_false

Examples:

  • a > b ? a : b will always return the bigger value of a or b (this is the same as the max function).
  • flag ? var : 12 will return var if flag is true, otherwise returns 12.

You can also stack these:

  • a ? b : (c ? d : e)

Functions

All functions are explained here. This is to be read as function_name(argument_types) -> result_type:

  • function_name: Name of the function
  • argument_types: The amount of arguments and their types
  • result_type: Every function yields a single result with a defined type

Supplier Functions (no arguments)

  • pi() -> double: The value of PI.
    • pi() -> 3.14159265359...
  • tag() -> tag: Create a new empty nbt compound tag.
  • sqrt2() -> double: The value of the square root of 2.
    • sqrt2() -> 1.41421356237...
  • random_int() -> int: Any possible integer x with -2147483648 <= x <= 2147483647 (2 to the power of 32 different values).
    • random_int() -> -67
  • random_double() -> int: Any possible double x with 0 <= x < 1.
    • random_double() -> 0.78125
  • random_uuid() -> string: A random UUID.
    • random_uuid() -> '139AA4B1-D4EC-4A01-9BA0-18E5C32BFCF2'
  • new_tag() -> tag: A new and empty NBT compound tag.

Unary Functions (1 argument)

  • round(double) -> int: Rounds the double number towards the closest int number with .5 being rounded up.
    • round(1.2) -> 1
    • round(2.5) -> 2
    • round(3.7) -> 4
    • round(-5.5) -> -5
    • round(-1.9) -> -2
    • round(6.0) -> 6
  • floor(double) -> int: Rounds down the double number to an int number.
    • floor(1.2) -> 1
    • floor(2.5) -> 2
    • floor(3.7) -> 3
    • floor(-5.5) -> -6
    • floor(-1.9) -> -2
    • floor(6.0) -> 6
  • ceil(double) -> int: Rounds up the double number to an int number.
    • ceil(1.2) -> 2
    • ceil(2.5) -> 3
    • ceil(3.7) -> 4
    • ceil(-5.5) -> -5
    • ceil(-1.9) -> -1
    • ceil(6.0) -> 6
  • sqrt(double) -> double: Calculates the square root of a double number. This number must be positive.
    • sqrt(9.0) -> 3.0
    • sqrt(2) -> 1.41421356237...
  • get_x(vec3) -> double: The x-coordinate of the vec3 vector.
  • get_y(vec3) -> double: The y-coordinate of the vec3 vector.
  • get_z(vec3) -> double: The z-coordinate of the vec3 vector.
  • length(vec3) -> double: Calculates the length of the vec3 vector.
  • normalize(vec3) -> vec3: Normalizes the vec3 vector (yields the argument vector divided by its length).
  • sin(double) -> double: Calculates the sinus of the double angle, in radians.
  • cos(double) -> double: Calculates the cosinus of the double angle, in radians.
  • asin(double) -> double: Calculates the asinus (inverse sinus, or sin-1) of the double angle, in radians.
  • acos(double) -> double: Calculates the acosinus (inverse cosinus, or cos-1) of the double angle, in radians.
  • to_radians(double) -> double: Converts the double angle from radians to degrees.
  • to_degrees(double) -> double: Converts the double angle from degrees to radians. . uuid_from_string(string) -> string: Takes any string as seed for the random generator to generate a new UUID from. Calling this method any amount of times with the same parameter always yields the same UUID as result.
  • next_int(int) -> int: Generates a random positive integer up to but excluding the value of the parameter, eg. if x is the parameter and y is the result then 0 <= y < x.
    • next_int(10) -> 3

Binary Functions (2 arguments)

  • min(int, int) -> int / min(double, double) -> double: Gives you the lower value of the 2 arguments.
    • min(1, 2) -> 1
    • min(2, 1) -> 1
    • min(-5, -2) -> -5
    • min(-7, -7) -> -7
  • max(int, int) -> int / max(double, double) -> double: Gives you the higher value of the 2 arguments.
    • max(1, 2) -> 2
    • max(2, 1) -> 2
    • max(-5, -2) -> -2
    • max(-7, -7) -> -7
  • nbt_contains(tag, string) -> boolean: Returns true if the given tag contains a value of the given string name, otherwise returns false.
  • get_nbt_int(tag, string) -> int: Returns the int that is on the given tag with the given string name.
  • get_nbt_double(tag, string) -> int: Returns the double that is on the given tag with the given string name.
  • get_nbt_boolean(tag, string) -> boolean: Returns the boolean that is on the given tag with the given string name.
  • get_nbt_compound_tag(tag, string) -> tag: Returns the tag that is on the given tag with the given string name.
  • get_nbt_string(tag, string) -> string: Returns the string that is on the given tag with the given string name.
  • get_nbt_uuid(tag, string) -> string: Returns the string that is on the given tag with the given string name in form of a UUID.
  • get_nbt_vec3(tag, string) -> vec3: Returns the vec3 that is on the given tag with the given string name in form of a list of type double.

Ternary Functions (3 arguments)

  • vec3(double, double, double) -> vec3: Constructs a vec3 made of the 3 double arguments, with the first double becoming the x-coordinate, the second double becoming the y-coordinate, and the third double becoming the z-coordinate.
  • put_nbt_int(tag, string, int) -> tag: Makes a copy of the given tag and puts the given int on it under the given string name then returns this new tag.
  • put_nbt_double(tag, string, double) -> tag: Makes a copy of the given tag and puts the given double on it under the given string name then returns this new tag.
  • put_nbt_boolean(tag, string, boolean) -> tag: Makes a copy of the given tag and puts the given boolean on it under the given string name then returns this new tag.
  • put_nbt_compound_tag(tag, string, boolean) -> tag: Makes a copy of the given tag (1st parameter) and puts the given tag (2nd parameter) on it under the given string name then returns this new tag.
  • put_nbt_string(tag, string, string) -> tag: Makes a copy of the given tag and puts the given string (2nd parameter) on it under the given string name (3rd parameter) then returns this new tag.
  • put_nbt_uuid(tag, string, string) -> tag: Makes a copy of the given tag and puts the given string (2nd parameter) on it under the given string name (3rd parameter) in form of a UUID then returns this new tag.
  • put_nbt_vec3(tag, string, vec3) -> tag: Makes a copy of the given tag and puts the given vec3 on it under the given string name in form of a list of type double then returns this new tag.

Compiler Grammar

More specifics about this compiler.


Operator Precedence

  • Binds strongest
  • ()
  • - / !
  • * / / / %
  • + / -
  • > / >= / < / <=
  • == / !=
  • &&
  • ||
  • ?:
  • Binds weakest

Compiler Definition

This is the exact definition of the compiler, for those interested in it.

-> expression

variable      := ( letter | "_" ) { letter | digit | "_" } .
value         := digit { digit } [ "." { digit } ] .
function_call := letter { letter | digit | "_" } "(" [ expression { "," expression } ] ")" .

expression    := conditional .

conditional   := disjunction [ "?" disjunction ":" disjunction ] .

disjunction   := conjunction { "||" conjunction } .
conjunction   := comparison { "&&" comparison } .

comparison    := relation { ( "==" | "!=" ) relation } .
relation      := sum { ( ">" | ">=" | "<" | "<=" ) sum } .

sum           := product { ( "+" | "-" ) product } .
product       := factor { ( "*" | "/" | "%" ) factor } .

factor        := [ "-" | "!" ] ( variable | value | "(" expression ")" | function_call ) | "'" string "'" .