Kinx v1.0.0, Officially Released!
Tags: ruby, javascript, kinx
Published: 2021-03-16 11:54:17

Hi everyone!

I released Kinx v1.0.0 officially! Please see [Release Page](https://github.com/Kray-G/kinx/releases) of [Kinx](https://github.com/Kray-G/kinx)

# Introduction

The Kinx v1.0.0 is a 1st official release version including all features developed from the beginning of this project.

* Reference
    * First motivation ... [The post of introduction](https://dev.to/krayg/kinx-as-a-script-language-4iko)
        * Kinx, I wanted a scripting language with a syntax of C family.
    * Repository ... https://github.com/Kray-G/kinx
        * I am waiting for pull requests.

By the way, this is a top secret but the next goal is a syntax highlight on GitHub. It's so hard because I hear one of the criteria is that the Kinx is used in hundreds of repository...

...I hope a little you will create a repository which using Kinx if possible.

## Small Description About Kinx

### The Concept

The concept is, **"Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers."**

- Looks like JavaScript ... It provides C family syntax.
- Feels like Ruby ... Enjoy programming.
- Fitting in C programmers ... It is easy to use for many programmers.

_"C programmers"_ in this context means _"almost all programmers."_ Because I think almost all programmers have used or known C language at least. And _"Providing C Like Syntax"_ as well also means _"friendly for almost all programmers."_

### Features

Mainly there are features below.

*   **Dynamic typing** and **Object-Oriented programming**.
*   **Classes** and inheritance, **Higher order functions**, **Lexical scoping**, **Closures**, **Fiber**, **Garbage Collection**, and so on.
*   **Native function** by JIT compiling supported with some limitations. Very fast.
*   Useful libraries are provided as **All-In-One**.

### Libraries

Kinx is including useful libraries below in the standard package as **All-In-One**.

* **Zip** ... Zip/Unzip with not only zip password but also AES password.
* **Xml** ... Supporting XML as DOM structure.
* **libCurl** ... Only HTTP has been already implemented.
* **SSH** ... SSH login and you can execute commands.
* **Socket** ... Supporting a simple TCP/UDP socket.
* **Iconv** ... Converting between text encodings.
* **SQLite3** ... Database class as a useful library wrapping SQLite3.
* **Regular Expression** ... Regular expression is of course supported.
* **Parser Combinator** ... Exactly original implementation of Parser Combinator named as `Parsek` like `Parsec`.
* **PDF** ... PDF core library based on HaruPDF.
* **JIT** ... JIT library for various platforms by an abstracted assembler library.

# Installation

**Windows/Linux**

You can download an installer from [Releases](https://github.com/Kray-G/kinx/releases) page.

**Scoop (for Windows)**

Or you can also install it by [scoop](https://scoop.sh).
Type the following.

```powershell
# scoop bucket add is needed only at the first time.
scoop bucket add kinx https://github.com/Kray-G/kinx
scoop install kinx
```

# How To Run

After confirming the command path of `kinx` or `kinx.exe`, run your script as follows.

```
# For Windows
$ kinx.exe [options] [<script-file>]

# For Linux
$ kinx [options] [<script-file>]
```

# Specification

Specification will be mostly completed so far for v1.0.0. Please look at this and try to use Kinx.

* See [Kinx Specification (v1.0.0)](https://github.com/Kray-G/kinx/blob/v1.0.0/docs/index.md) for details.

# Examples

There are a lot of examples in the document on the repository, but I will only show a small example in this section. Please see [the document](https://github.com/Kray-G/kinx/blob/v1.0.0/docs/index.md).

## hello, world.

Here is the 1st script that everyone should write.

```js
System.println("hello, world.");
```

## Fibonacci

You will think it seems to be a JavaScript.

```js
function fib(n) {
    if (n < 3) return n;
    return fib(n-2) + fib(n-1);
}

System.println("fib(34) = ", fib(34));
```

## Fibonacci in `native`

Replacing `function` by `native`, that makes faster. Please try it now.

```js
native fib(n) {
    if (n < 3) return n;
    return fib(n-2) + fib(n-1);
}

System.println("fib(34) = ", fib(34));
```

## Closure

The function object has a lexical scope and you can use a closure.

```javascript
function newCounter() {
    var i = 0;          // a lexical variable.
    return function() { // an anonymous function.
        ++i;            // a reference to a lexical variable.
        return i;
    };
}

var c1 = newCounter();
System.println(c1()); // 1
System.println(c1()); // 2
System.println(c1()); // 3
System.println(c1()); // 4
System.println(c1()); // 5
```

## Lambda

Anonymous function is easy to be written.

```javascript
function calc(x, y, func) {
    return func(x, y);
}

System.println("add = " + calc(10, 2, { => _1 + _2 }));
System.println("sub = " + calc(10, 2, { => _1 - _2 }));
System.println("mul = " + calc(10, 2, { => _1 * _2 }));
System.println("div = " + calc(10, 2, { => _1 / _2 }));
// add = 12
// sub = 8
// mul = 20
// div = 5
```

## Class

Of course, a class is available.

```javascript
class A {
    var flag_ = false;  // a local private variable.
    public flagOn() {
        @flagOnActual();
    }
    public flagOnActual() {
        @flag = true;   // a public variable.
    }
}

var a = new A();
a.flagOn();
System.println(a.flag ? "true" : "false");  // => true
```

## Module

Module can mixin into a class.

```javascript
module M {
    public method1() {
        System.println("This is a method1");
    }
}

class A {
    mixin M;
}

new A().method1();  // => This is a method1
```

## Fiber

You can use Fiber.

```javascript
var fiber = new Fiber {
    System.println("fiber 1");
    yield;
    System.println("fiber 2");
};

System.println("main 1");
fiber.resume();
System.println("main 2");
fiber.resume();
System.println("main 3");
// main 1
// fiber 1
// main 2
// fiber 2
// main 3
```

## Spread/Rest Operator

It was introduced by ES6 of JavaScript. I wanted it so much, and it is so useful. There's a lot of use cases, but here is a simple example.

```javascript
function sample(a1, a2, ...a3) {
    // a1 = 1
    // a2 = 2
    // a3 = [3, 4, 5]
}

sample(1, 2, 3, 4, 5);
```

## Pattern Matching

It supports an assignment by pattern matching. Of course it can be also used in declaration and a function argument.

```javascript
[a, b, , ...c] = [1, 2, 3, 4, 5, 6];
{ x, y } = { x: 20, y: { a: 30, b: 300 } };
{ x: d, y: { a: e, b: 300 } } = { x: 20, y: { a: 30, b: 300 } };

System.println("a = ", a);
System.println("b = ", b);
System.println("c = ", c);
System.println("d = ", d);
System.println("e = ", e);
System.println("x = ", x);
System.println("y = ", y);

// => .y.b requires 300, but it is 3 in actual.
{ x: d, y: { a: e, b: 300 } } = { x: 20, y: { a: 30, b: 3 } };

// a = 1
// b = 2
// c = [4, 5, 6]
// d = 20
// e = 30
// x = 20
// y = {"a":30,"b":300}
// Uncaught exception: No one catch the exception.
// NoMatchingPatternException: Pattern not matched
// Stack Trace Information:
//         at <main-block>(test.kx:14)
```

## Pipeline Operator

Pipeline operator is also supported.

```javascript
function doubleSay(str) {
    return "%{str}, %{str}";
}
function capitalize(str) {
    return str.toUpper(0, 1);
}
function exclaim(str) {
    return str + '!';
}

var result = exclaim(capitalize(doubleSay("hello")));
System.println(result); // => "Hello, hello!"

var result = "hello"
  |> doubleSay
  |> capitalize
  |> exclaim;

System.println(result); // => "Hello, hello!"
```

## Function Composition Operator

You can also composite functions!

```javascript
const double = &(n) => n * 2;
const increment = &(n) => n + 1;

// Normal case.
var r1 = double(increment(double(double(5)))); // 42
System.println(r1);

// Function composition operator is higher priority than a pipeline operator.
var r2 = 5 |> double +> double +> increment +> double; // 42
System.println(r2);
```

# Conclusion

I finally release it officially, but of course there may be a bug or some unexpected behavior.

If you try to use it, I am very appreciated. When you faced a problem, please let me know via [Issues](https://github.com/Kray-G/kinx/issues) in the [repository](https://github.com/Kray-G/kinx). I will welcome any report.

Again, please see [Kinx specification](https://github.com/Kray-G/kinx/blob/v1.0.0/docs/index.md) for details.

Many thanks.