Skip to content

Repository files navigation

Nikrisht

Website Status Repo Size Last Commit

profound tagline goes here

Introduction

Nikrisht is a small, dynamically typed scripting language with an AST-walking interpreter, built from scratch in JavaScript with JSDoc-based type checking, and runs in both Node and the browser without a build step.

Examples

Prime, naive

# Checks if a number is prime
func isPrime(number) {
    if (number < 2) {
        return false;
    }

    loop (2..<number with i) {
        if (remainder(number, i) == 0) {
            return false;
        }
    }

    return true;
}

write(isPrime(117));

Merge sort

# Sorts an array using Merge Sort algorithm
func mergeSort(array) {
    # Base case, an array of 0 or 1 elements is already sorted
    if (count(array) <= 1) {
        return array;
    }

    # Split the array into two halves
    const middle = floor(count(array) / 2);

    # Sort both halves, recursively
    const left = mergeSort(slice(array, 1, middle));
    const right = mergeSort(slice(array, middle + 1, count(array)));

    # Merge the two sorted halves
    const result = [];
    var i = 1;
    var j = 1;

    loop (i <= count(left) & j <= count(right)) {
        if (left[i] <= right[j]) {
            put(result, left[i]);
            i = i + 1;
        } else {
            put(result, right[j]);
            j = j + 1;
        }
    }

    # Add any remaining elements from the left half.
    loop (i <= count(left)) {
        put(result, left[i]);
        i = i + 1;
    }

    # Add any remaining elements from the right half.
    loop (j <= count(right)) {
        put(result, right[j]);
        j = j + 1;
    }

    return result;
}

write(mergeSort([10, -1, 2, 5, 0, 9]));

Fibonacci, memoized

const cache = {};

func fibonacci(number) {
    if (has(cache, number)) {
        return cache[number];
    }

    if (number <= 1) {
        return number;
    }

    cache[number] = fibonacci(number - 1) + fibonacci(number - 2);
    return cache[number];
}

write(fibonacci(8));

Getting Started

Playground

You can try Nikrisht instantly in your browser without any installation or setup.

Installation

nikrisht on npm npm version npm downloads npm license

npm install -g nikrisht

To run a nikrisht file input.nki:

nki input.nki

Editor setup

Nikrisht VS Code extension

For the best development experience, install the official Nikrisht VS Code Extension to get full syntax highlighting, error checking, and code snippets:

  1. Open VS Code.
  2. Open the Extensions view (Ctrl+Shift+X or Cmd+Shift+X).
  3. Search for Nikrisht by LadyBeGood
  4. Click Install.

Acknowledgements

Foundations

Resources

  • AST Explorer - A web tool to explore the ASTs generated by various parsers.
  • Syntax across languages - A comparison of syntax across many programming languages.
  • regex101 - Regex editor for testing and learning regular expressions. Very helpful while creating syntax-highlighting patterns for the VSCode extension and website playground.
  • Matt Neuburg's guide on writing TextMate grammar - A comprehensive reference for understanding TextMate grammar internals and syntax highlighting behavior. You will have a very hard time dealing with TextMate if you don't digest this guide first.

License

About

A small programming language.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages