0.1.1 • Published 10 months ago

@kodeko/akita-core v0.1.1

Weekly downloads
-
License
UNIRO-ROCL
Repository
github
Last release
10 months ago

@kodeko/akita-core

@kodeko/akita-core is a free-to-use string-based interpreter that allows people to develop packages with a simple syntax. The syntax is based on a keyword that represents a function, characters to indicate function opening and closing, and a character to separate arguments. A good analogy to use would be the way function calls are made, for example: myFunction(1, 2, 3), where "myFunction" is the keyword, "(" is the opening symbol, ")" is the closing symbol, and "," is the argument separator.

Installation

You can install @kodeko/akita-core using the following npm command:

npm install @kodeko/akita-core

Example

Once installed, you can use the akita interpreter in your project as follows:

import {
	akita_functions_mod,
	Interpreter,
	withPrefix,
} from "@kodeko/akita-core";

void (async function main() {
	// Load the defult functions and adds as prefix $
	await Interpreter.load_functions(akita_functions_mod, withPrefix("$"));
	// Creates a new Interpreter
	const itr = new Interpreter();
	// Sets the input to execute
	itr.lexer.set_input("$log[$get[great]]");
	// Executes the input
	await itr.solve({
		extra: {
			variables: {
				great: "Hello world!",
			},
		},
	});
})();
// result
Hello world!

In the above example, we import the interpreter and helpers and then execute a script represented by the string "$log[$get[great]]". The execute method of the akita interpreter will parse and execute the script, returning the result. In this case, the result will be logged to the console.

How Works?

The akita interpreter works through a lexer based on regular expressions, which detects keywords and their arguments. It then stores this information in objects and replaces their values with SYSTEM_FUNCTION(\). For example:

// Input
$log[$get[great]]
// Output
SYSTEM_RESULT(0)
// Object Data
[
  {
    id: 'SYSTEM_FUNCTION(0)',
    prototype: undefined,
    total: '$log[SYSTEM_FUNCTION(1)]',
    name: '$log',
    pos: 0,
    _id: 0,
    inside: 'SYSTEM_FUNCTION(1)',
    fields: [
      {
        value: 'SYSTEM_FUNCTION(1)',
        overloads: [
          {
            id: 'SYSTEM_FUNCTION(1)',
            prototype: undefined,
          }
        ]
      }
    ]
  }
]

In this case, SYSTEM_FUNCTION(1) represents the keyword $log, and inside $log is SYSTEM_FUNCTION(0), which is $object.

After this process, an array is traversed that executes the functions from top to bottom and from left to right. The value of each function is returned as SYSTEM_RESULT(\), allowing for handling of objects, numbers, classes, etc. First, it tries to execute $log, but since it contains within its arguments another function ($get), that one will be executed first. That is, that "always" the functions inside will be executed, and once that is executed, $log is executed.

// Final interpreter object data
{
  results: { 'SYSTEM_RESULT(1)': 'Hello world!', 'SYSTEM_RESULT(0)': '' },
  extra: { variables: { great: 'Hello world!' } },
  input: 'SYSTEM_RESULT(1)',
  parents: [],
  epd: null,
}

Additional Considerations


Enjoy using the package in your project! If you have any further questions or need more information, feel free to ask on my Discord DM or our Discord Server.

0.1.0

10 months ago

0.1.1

10 months ago

0.0.9

10 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago