Code Monkey home page Code Monkey logo

tokay's Introduction

Tokay

Tokay is a programming language designed for ad-hoc parsing.

Tokay is under development and not considered for production use yet; There are plenty of bugs, incomplete or experimental features and planned concepts. Be part of Tokay's ongoing development, and contribute!

About

Tokay is a language to quickly implement solutions for text processing problems. This can either be just simple data extractions, but also parsing entire structures or parts of it, and turning information into structured parse trees or abstract syntax trees for further processing.

Therefore, Tokay is both a tool for simple one-liners, but can also be used to implement code-analyzers, refactoring tools, interpreters, compilers or transpilers. Actually Tokay's own language parser is implemented in Tokay itself.

Tokay is inspired by awk, but follows its own philosophy, ideas and design principles. It might be usable as a common scripting language for various problems as well, but mainly focuses on the parsing features, which are a fundamental part built into the language.

Tokay is still a very young project and gains much potential. Volunteers are welcome!

Highlights

  • Interpreted, procedural and imperative scripting language
  • Concise and easy to learn syntax and object system
  • Stream-based input processing
  • Automatic parse tree construction and synthesis
  • Left-recursive parsing structures ("parselets") supported
  • Implements a memoizing packrat parsing algorithm internally
  • Robust and fast, as it is written entirely in safe Rust
  • Enabling awk-style one-liners in combination with other tools
  • Generic functions and parselets (*coming soon)
  • Import system to create modularized programs (*coming soon)
  • Embedded interoperability with other programs (*coming soon)

Installation

Using Rusts dependency manager and build-tool cargo, simply install Tokay with

$ cargo install tokay

Alternatively, there's also a tokay and tokay-git package in the Arch Linux AUR.

Examples

Tokay's version of "Hello World" is quite obvious.

print("Hello World")
$ tokay 'print("Hello World")'
Hello World

Tokay can also greet any wor(l)ds that are being fed to it. The next program prints "Hello Venus", "Hello Earth" or "Hello" followed by any other name previously parsed by the builtin Word-token. Any other input than a word is automatically omitted.

world => Word   print("Hello " + $world)
$ tokay 'world => Word   print("Hello " + $world)' -- "World 1337 Venus Mars 42 Max"
Hello World
Hello Venus
Hello Mars
Hello Max

A simple program for counting words which exists of a least three characters and printing a total can be implemented like this:

Word(min=3) ++words accept
end words
$ tokay "Word(min=3) ++words accept; end words" -- "this is just the 1st stage of 42.5 or .1 others"
5

The next, extended version of the program from above counts all words and prints any numbers.

Word ++words accept
Number
end words + " words"
$ tokay 'Word ++words accept; Number; end words + " words"' -- "this is just the 1st stage of 42.5 or .1 others"
(1, 42.5, 0.1, "9 words")

By design, Tokay constructs syntax trees from consumed information automatically.

The next program directly implements a parser and interpreter for simple mathematical expressions, like 1 + 2 + 3 or 7 * (8 + 2) / 5. The result of each expression is printed afterwards. Processing direct and indirect left-recursions without ending in infinite loops is one of Tokay's core features.

_ : [ \t]+                # redefine whitespace to just tab and space

Factor : @{
    Int _                 # built-in 64-bit signed integer token
    '(' _ Expr ')' _
}

Term : @{
    Term '*' _ Factor     $1 * $4
    Term '/' _ Factor     $1 / $4
    Factor
}

Expr : @{
    Expr '+' _ Term       $1 + $4
    Expr '-' _ Term       $1 - $4
    Term
}

Expr _ print("= " + $1)   # gives some neat result output
$ tokay examples/expr_from_readme.tok
1 + 2 + 3
= 6
7 * (8 + 2) / 5
= 14
7*(3-9)
= -42
...

Tokay can also be used for programs without any parsing features.
Next is a recursive attempt for calculating the factorial of an integer.

factorial : @x {
    if !x return 1
    x * factorial(x - 1)
}

factorial(4)
$ tokay examples/factorial.tok
24

And this version of above program calculates the factorial for any integer token matches from the input. Just the invocation is different, and uses the Number token.

factorial : @x {
    if !x return 1
    x * factorial(x - 1)
}

print(factorial(int(Number)))
$ tokay examples/factorial2.tok -- "5 6 ignored 7 other 14 yeah"
120
720
5040
87178291200
$ tokay examples/factorial2.tok
5
120
6
720
ignored 7
5040
other 14
87178291200
...

Documentation

Same as Tokay itself, the documentation is currently established. The latest version can be obtained on the website tokay.dev. The documentation source code is maintained in a separate repository.

Repository

This repository holds all required source files to provide Tokay with examples.

.                  # Build scripts, Cargo.toml, etc.
├── examples       # Example programs
├── macros         # Source of the tokay-macros crate required for building
├── src            # Tokay's source code
│   ├── compiler   # Compiler source
│   ├── value      # Object system source
│   └── vm         # Virtual machine source
└── tests          # Some use-case examples required by the test suite

Contribute

Contributions of any kind, might it be code, bug reports, bugfixes, documentation, support or advertising are always welcome!

Take a look into the bug tracker or watch for //fixme- and //todo-comments in the source code for open issues and things that need to be improved (there are plenty of them).

If you want to create a pull request, ensure that cargo run and cargo test run without errors. When new features where added, don't miss to write some unit tests for them. Run cargo fmt before you finally commit.

Feel free to contact me directly on any questions, or file an issue here.

Logo

The Tokay programming language is named after the Tokay gecko (Gekko gecko) from Asia, shouting out "token" in the night.

The Tokay logo and icon was thankfully designed by Timmytiefkuehl.
Check out the tokay-artwork repository for different versions of the logo as well.

License

Copyright © 2022 by Jan Max Meyer, Phorward Software Technologies.

Tokay is free software under the MIT license.
Please see the LICENSE file for details.

tokay's People

Contributors

jgbyrne avatar phorward avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.