Compiler | Category | Remarks |
---|---|---|
GCC 9 | 🥇Primary | |
Clang 11 | 🥈Secondary | A number of meaningless warnings. |
I started learning programming from ActionScript since Flash 5 days. It was no sooner (and not strange, either) than I learned JavaScript, because they are so similar. I admit that JavaScript is a fascinating language, but it also has quite a few drawbacks when we look at it today:
- There is no 64-bit integer type. While some data exchange formats (such as Protocol Buffers) do have 64-bit integers, they cannot be stored as
Number
s safely. Some implementations split 64-bit integers into higher and lower parts, which is not very handy, as the lower part suddenly becomes signed and may be negative. - There are no binary strings.
String
s are in UCS-2 (rather than UTF-16), whileArrayBuffer
s are not resizable. NaN
andInfinity
are neither keywords nor constants. They are properties of the global object and may be overwritten. Moreover,Boolean(NaN)
yieldsfalse
unlike other languages.
Here is an issue that is shared by almost all common languages, including C and Python:
let a = [ ];
let b = [ ];
function test(x, y) {
x.length = 0, x[0] = "hello"; // modifies the array that `x` would reference
y = [ "hello" ]; // modifies `y` instead of the argument
}
test(a, b); // arguments are in effect pointers
console.log("a = ", a); // [ 'hello' ]
console.log("b = ", b); // []
However, compared with typed languages such as Java, JavaScript has a few advantages:
- A number is always a
Number
. There are no integer types of varieties of widths, which simplifies programming. Unlike integers,Number
s never overflow. - Being untyped, a function can be passed around like objects without knowing its parameters.
Asteria has been highly inspired by JavaScript but has been designed to address such issues.
- Sane and clean.
- Self-consistent.
- Simple to use.
- Lightweight.
- Procedural.
- Dynamically typed.
- Easy to integrate in a C++ project. (C++17 hexadecimal floating-point literals are required.)
- Native to C++ exceptions, particularly
std::bad_alloc
.
- First-class functions.
- Closure functions.
- Exceptions.
- Flexible syntax similar to C++ and JavaScript.
- Regular grammar.
- Argument passing (by value or reference) determined by the argument rather than the parameter.
- Idempotently copyable values basing on copy-on-write.
- Minimal garbage collection support.
- Structured binding similar to C++17.
Asteria is untyped. Variables do not have types. Only values (which can be stored in in variables) do. In this sense functions are considered opaque data.
There are 9 data types:
Asteria | JavaScript | Java | C++ | Remarks |
---|---|---|---|---|
null |
Undefined | the null type | std::nullptr_t |
|
boolean |
Boolean |
boolean |
bool |
|
integer |
N/A | long |
std::int64_t |
signed 64-bit integer in two's complement |
real |
Number |
double |
double |
IEEE-754 double-precision floating-point number |
string |
N/A | byte[] |
std::string |
octet string, commonly refered as byte string |
opaque |
N/A | Object |
std::any |
opaque value used by bindings |
function |
Function |
N/A | N/A | functions and closures |
array |
Array |
N/A | std::vector< std::any> |
|
object |
Object |
N/A | std::unordered_map< std::string, std::any> |
order of elements not preserved |
var foo;
// `foo` refers to a "variable" holding `null`.
const inc = 42;
// `inc` refers to an "immutable variable" holding an `integer` of `42`.
var bar = func() { return ->inc; }; // return by reference
// `bar` refers to an "immutable variable" holding a function.
// `bar()` refers to the same "variable" as `inc`.
func add(x) { return x + inc; }; // return by value
// `add` refers to an "immutable variable" holding a function.
// `add(5)` refers to a "temporary" holding an `integer` of `47`.
$ autoreconf -i # requires autoconf, automake and libtool
$ ./configure
$ make -j$(nproc)
$ ./bin/asteria
BSD 3-Clause License