builtin.jc
来自「The JILRunOnly project is a simple comma」· JC 代码 · 共 229 行
JC
229 行
/*
* builtin.jc
*
* The built-in data types long and float are "simple data types", which means
* they are not treated as classes by the language, much like it is in C/C++.
* Since version 0.6 of the JewelScript compiler, the types string and array
* are built-in classes and feature a set of useful methods.
* This file will give a general insight on using simple data types.
*/
import stdlib;
/*
* long
*/
function type_long()
{
// A long is a 32-bit integer data type on 32-bit processors
long a;
// like in C/C++, you can specify a long in decimal, octal or hexadecimal
a = 1023;
a = 01777;
a = 0x3ff;
// you can also specify a long as a character constant with up to 4 digits
a = 'abcd';
// a long auto-converts to a float
float f = a;
}
/*
* float
*/
function type_float()
{
// By default, a float in JewelScript is a 64-bit floating point data type.
// However, you can compile the library for 32-bit floats, if desired.
// Just change the typedef "JILFloat" from "double" to "float".
float a;
// in C/C++ you need to specify float literals with a trailing "." or ".0" to
// avoid conversion warnings from integer to float.
// Here, you can omit the trailing "." and the literal is still recognized as
// a float number, if the receiving L-Value is of type float.
a = 0;
a = 0.;
a = 0.0;
// currently, a float auto-converts to a long, even though you lose precision
// and the value might be truncated from 64 to 32 bits. This is currently under
// evaluation and may be removed in the future if it proves to be too critical.
long l = a;
}
/*
* string
*/
function type_string()
{
// The built-in string is a dynamically allocated string object, and is not
// limited in size (up to 2 GB theoretically).
// There are a number of out-of-the-box string manipulation functions that
// are built-in to the string class.
string a;
// To initialize a string, you can use a string-literal in double-quotes. The
// compiler interpretes all ANSI escape sequences in string and character literals.
// Additionally, the parser will combine multiple string or character literals,
// if there is only whitespace between them.
a = "M\x61rvin says:"
"\t"
"\"Hello world!\""
"\n";
// In certain cases it might be unwanted that ANSI escape sequences are interpreted.
// For these cases JewelScript supports un-escaped string literals. This is
// especially useful for regular expressions or (windows-) path names.
a = /"This is an "unescaped" string literal. We can use the \ normally here. "/
/"We can also use the "double quotes" normally. "/
"And we can combine them with \"escaped\" string literals, \n"
/"and switch between them to our heart's content! ;)"/ "\n";
// To append a string to a string, using the "+" or "+=" operators is legal.
string b = "Variable 'a' contains: " + a;
a += "How are you?\n";
// To get the length of a string in characters, you can use the "length" accessor
long c = a.length;
// if a class or native class has a convertor function to a string, or a
// constructor for a string, auto-conversion can be performed between a
// string and that class. This is of course true for all of the data types.
stdlib::Print( a );
stdlib::Print( b );
}
/*
* array
*/
function type_array()
{
// Similar to the string, the built-in array object is a dynamically allocated
// object, not limited in size (except for available memory).
// The array is one-dimensional and can dynamically grow, depending on the
// index you use to access elements from it.
// To create multi-dimensional arrays, you can put an array into an array
// into an array, and so forth.
// Note that the data type of the arrays elements is "var", meaning it is
// typeless and you can put anything into it.
// It is also important to know that the reference type has a double meaning
// for arrays: If an array variable is a reference, then it's elements are
// treated as references as well, meaning values and objects are moved by
// reference into the array.
// If an array variable is a non-reference, then it's elements are also treated
// as non-references, meaning values and objects are COPIED into the array!
// array a = {}; // an array of copies!
array& a = new array; // an array of references
// To initialize an array, you can use the curly braces:
// Note that this is a runtime operation, so you are not limited to specifying
// literals! You can also place function calls and variables into the initializer.
a = { "Hello", 3.141, "World", 71 };
// To add a new element, using the "+" or "+=" operators is legal.
array b = a + "element";
a += "element";
// To get the number of elements in an array, you can use the "length" accessor
long c = a.length;
// To convert an array into a formatted string, you can use the ToString method
string s = a.ToString("Array contains: %s, %g, %s, %d\n");
stdlib::Print( s );
// An array is also useful to make functions with variable number of arguments
MultiOut({ "MultiOut: ", c, s, 3.3, "Another string", 4, 5, "\n" });
}
/*
* MultiOut
*/
function MultiOut( const array& args )
{
for( long i = 0; i < args.length; i++ )
stdlib::Print( args[i] );
}
/*
* simple_types
*
* Simple data types, references and operator "new"
*/
function simple_types()
{
// declaring / initializing multiple variables at once
long z1 = 10, z2 = z1 + 5, z3 = z2 + 10;
// multiple constants
const long kConst1 = 10, kConst2, kConst3 = 30;
// multiple references (you must initialize all immediately)
const long& kRef1 = kConst1, kRef2 = kRef1;
// you cannot mix references / non-references in one declaration:
// const long kConst4, &kRef3 = kConst1;
// you also cannot mix const / non-const
// long z4 = 5, const kConst5 = 10;
// the problem: can't take non-const reference from constant
// long& lRef = 10;
// the solution:
long& lRef = new long(10);
// the problem: must init references immediately
// string& sRef;
// the solution:
string& sRef = new string;
// these 3 equate to: long a = 0;
long l = new long;
long m = new long();
long n = new long(0);
// these 3 equate to: float a = 0;
float f = new float;
float g = new float();
float h = new float(0);
// these 3 equate to: string a = "";
string s = new string;
string t = new string();
string u = new string("");
// these 3 equate to: array a = {};
array a = new array;
array b = new array[];
array c = new array[0];
}
/*
* main
*
* call'em all
*/
function string main()
{
type_long();
type_float();
type_string();
type_array();
simple_types();
return "";
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?