This is the fourth day of my participation in the August Text Challenge.More challenges in August
AviatorScript supports common types such as numbers, booleans, strings, and so on, as well as large integers, BigDecimal, and regular expressions as basic types.
digital
Numbers include integers and floating-point numbers, and AviatorScript reduces and extends Java’s types while maintaining consistent operator rules.
Integers and arithmetic operations
Integers such as -99, 0, 1, 2, 100…… Wait, the corresponding type is the Java long type. AviatorScript does not have byte, short, int and other types. The unified integer type is long, and the range supported is the same as the Java language: -9223372036854774808~9223372036854774807.
Integers can also be expressed in hexadecimal, starting with 0x or 0x, such as 0xFF(255), 0xAB(171), and so on.
Integers can perform all kinds of arithmetic operations, such as addition, subtraction, multiplication, division, and modulo.
let a = 99;
let b = 0xFF;
let c = -99;
println(a + b);
println(a / b);
println(a- b + c);
println(a + b * c);
println(a- (b - c));
println(a/b * b + a % b);
Copy the code
The operators for addition, subtraction, multiplication, and division are +,-,*, and /. The modulo operator is %. The rules and syntax are the same as in Java.
Note that the result of dividing an integer is still an integer, such as the result of a/b in the example, which is 0, following Java’s integer arithmetic rules.
The precedence between operators is as follows:
- Unary operator
-
Take a negative number *, /
The +, -
The entire rule is also consistent with Java operator priorities. You can use parentheses to enforce precedence, such as a-(b-c) in this example, which forces b-c to be executed first and then subtracted by A.
In general, complex arithmetic expressions, for code readability and robustness, recommend the use of parentheses to enforce precedence.
Large integer (BigInt)
AviatorScript also specifically provides support for large integer types for integers greater than long, corresponding to the java.math.BigINTEGER class. Any integer literal outside the long range is automatically promoted to BigInt, and any number ending in N is automatically changed to BigInt:
## examples/bigint.av
let a = 10223372036854774807; ## Literal BigInteger
let b = 1000N; ## BigInteger
let c = 1000; ## long type
println(a);
println(a + a);
println(a * a);
println(a + b + c);
Copy the code
10223372036854774807 is a much larger number than long returns. B is also of type BigInt because it ends with N. BigInt has the same arithmetic operators and rules as a normal integer
10223372036854774807
20446744073709549614
104517335803944147014652834074681887249
10223372036854776807
Copy the code
Note that the default long is not automatically promoted to BigInt if it overflows, but when BigInt and long participate in the arithmetic operation together, the result is BigInt. More on the rules for type conversions later.
Floating point Numbers
Numbers in addition to integers, AviatorScript also supports floating point numbers, but only those of type double, that is, double 64-bit, floating point numbers that conform to IEEE754 specification. The incoming Java float is also converted to a double. All floating-point numbers are considered double. Floating-point numbers are represented in two ways:
- A decimal number with a decimal point, for example
1.34159265
,0.33333
And so on. - Scientific notation, such as
1e-2
,2E3
Wait, upper and lower casee
All can.
Let’s do a simple example, Newton’s method for square roots
## examples/square_root.av let a = 2; let err = 1e-15; let root = a; While math. Abs (a - root * root) > err {root = (a/root + root) / 2.0; } println("square root of 2 is: " + root);Copy the code
This example is a little more complicated because we use the while loop (see condition), which we’ll get to later, but the logic is simpler. Take the square root of 2, and we keep counting (a/root + root)/2.0 to see if it’s within the error range specified by err. If not, continue iterating, otherwise break out of the loop and print the result:
Square root of 2 is: 1.414213562373095Copy the code
Floating-point operators support addition, subtraction, multiplication and division as well as the same precedence as integers. Floating-point and floating-point arithmetic operations result in floating-point numbers, and floating-point and integer operations result in floating-point numbers.
High Precision Computing (Decimal)
Floating-point numbers cannot be used for exact operations such as currency operations or physical formula operations, in which case it is generally recommended to use BigDecimal in Java, calling its add/sub methods for arithmetic operations.
AviatorScript supports BigDecimal as a basic type (hereinafter referred to as decimal type), recognizing deicmal as long as the floating-point M ends, such as 1.34m, 0.333m, scientific notation 2e-3m, and so on.
Decimal also uses +,-,*,/ for arithmetic operations, and AviatorScript overrides these operators to automatically convert them to the various operations of the BigDecimal class. Let’s change the square root example to decimal
## examples/bigdecimal.av let a = 2M; let err = 1e-15M; let root = a; While math. Abs (a - root * root) > err {root = (a/root + root) / 2.0m; } println("square root of 2M is: " + root);Copy the code
The result of the operation root is also of type decimal. Numeric types other than double are evaluated with decimal, resulting in decimal. Any operation that involves a double results in a double.
The default accuracy is mathContext.decimal128, which you can change by modifying the engine configuration option options.math_context.
If you are having trouble adding the M suffix to floating-point numbers and want all floating-point numbers to be resolved to decimal, you can enable the options. ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL option.
Numeric type conversion
When numeric types are evaluated, they follow certain conversion rules:
- Operations involving a single type that still result in that type. For example, an integer divided by an integer still results in an integer, and double and double still result in a double.
- Operations involving multiple types, in the following order:
long -> bigint -> decimal -> double
Automatic promotion, such as long and BigInt to BigINT, long and decimal to decimal, and any type combined with double to double
You can force a number to long with long(x), losing precision in the process, or use double(x) to force a number to double.
## examples/double.av
let a = 1;
let b = 2;
println("a/b is " + a/b);
println("a/double(b) is " + a/double(b));
Copy the code
A and b are long, and their division is still an integer, and 1/2 is 0, but when we cast b to double, both are floating point numbers:
A /b is 0 a/b is 0Copy the code
string
In any language, strings are the most basic type, such as String in Java. Strings are also supported in AviatorScript, as long as consecutive characters enclosed in single or double quotation marks are a complete string object, for example:
"hello world"
'hello world'
"a"
or'a'
The string can be printed directly through the println function.
The length of a string can be obtained using the string.length function:
## examples/string.av let a = "hello world"; println(a); println(string.length(a)); ` ` print:Copy the code
hello world 11
String concatenation can be done with a '+' sign (another operator overload) :Copy the code
examples/string.av
let a = “hello world”; let b = ‘AviatorScript’;
println(a); println(string.length(a)); println(a + ‘,’ + b + 5);
The string concatenation 'a + ',' + b + 5' contains the number 5 and the string ','. Any type and string added together will be concatenated into a string, as Java rules allow. So the last line above will print 'Hello World,AviatorScript5'. Strings also contain other functions, such as intercepting the string 'substring' under the namespace 'string', See [function library list] (https://www.yuque.com/boyan-avfmj/aviatorscript/ashevw). Again, just like any other language, strings in AviatorScript also support escaping characters when they encounter special characters. Just like in Java, a character can be escaped by ' '. For example, if we want to represent a string with single quotes, if we continue to use single quotes to represent strings, This is where the escape character is needed:Copy the code
examples/escape_string.av
println(‘Dennis’s car’); println(‘AviatorScript is great.\r\nLet’s try it! ‘);
Special characters such as' \r ', '\n', '\t', etc. are also supported. In the example above we use the newline '\r\n', which will print:Copy the code
Dennis’s car AviatorScript is great. Let’s try it!
Of course, in the case of quotes, you can simply use double quotes to represent strings and avoid escaping:Copy the code
println(“Dennis ‘s car”);
String Interpolation can be added, for exampleCopy the code
let name = “aviator”; let s = “hello,” + name;
The concatenated string s is' Hello aviator '. The '+' addition is specially optimized for string concatenation, which is automatically converted internally to 'StringBuilder' for concatenation. But for more complex scenarios, the syntax for string concatenation is still too ugly and cumbersome, so since 5.1.0, AviatorScript has supported string interpolation, an example:Copy the code
examples/string_interpolation.av
let name = “aviator”; let a = 1; let b = 2; let s = “hello, #{name}, #{a} + #{b} = #{a + b}”; p(s);
Expressions enclosed in '#{}' will be evaluated automatically in the current context and then inserted into the final result string. The above example will print:Copy the code
hello, aviator, 1 + 2 = 3
AviatorScript has done a lot of internal optimization to make it faster to reuse Expression in compilation mode than to use additive concatenation strings. Boolean types are used to represent truth and false. they have only two values, 'true' and 'false', respectively. Comparison operations such as greater than and less than can produce booleans:Copy the code
examples/boolean.av
println(“3 > 1 is ” + (3 > 1)); println(“3 >= 1 is ” + (3 >= 1)); println(“3 >= 3 is ” + (3 >= 3)); println(“3 < 1 is ” + (3 < 1)); println(“3 <= 1 is ” + (3 <= 1)); println(“3 <= 3 is ” + (3 <= 3)); println(“3 == 1 is ” + (3 == 1)); println(“3 ! = 1 is ” + (3 ! = 1));
Output:Copy the code
3 > 1 is true 3 >= 1 is true 3 >= 3 is true 3 < 1 is false 3 <= 1 is false 3 <= 3 is true 3 == 1 is false 3 ! = 1 is true
All logical operators are demonstrated above: - '>' greater than - '>=' greater than or equal to <! ----> - '<' less than - '<=' less than or equal to <! ----> - == == == -! = ` is not equal toCopy the code