Skip to content

NesCafe62/Krypton-lang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Krypton lang

Idea of a new strong-typed language that will be familiar for JavaScript developers, but with native and crossplatform compilation (hopefully better performance and less runtime/compiler complexity) and zero dependencies if possible (that can run even on older machines). Not targeting web platform for now, but might be considered (using wasm or vanilla js, last one can be tricky). No plans for Mac or embedded for now, possible in future by adding LLVM IR target.

The motivation

if I wanted to have the nicest language for me, what it would look like? Simple and readable. But runtime also matters, it should be possible to produce native executable for different platforms that will just run (without dependencies of having system library versions or any runtime - if any then make it just as simple as dump to path and config the app to use that path {with possibility to set such path per app!}). Also make it easy to add any syntax sugar, if its just simple replacement rules.

So, is it too hard problem to make such language? I will try and see

Key goals

  • Strong-typed. Increase the benefits of static analysis
  • Simple, easy to learn (Not as complicated as rust and c++, but not too simple as go). No macros if possible
  • Short and pretty syntax, wihout complication of control flow. No public abstract static final synchronized or things like void ** (*d) (int &, char **(*)(char *, char **))
  • Minimal dependencies
  • Mainly focused on native compilation
  • Cross-platform
  • Extendable language features (including syntax sugar) by extensions. Make it so simple, that every programmer can do it easily
  • Multi-paradigm. Procedural, functional, OOP (same capabilities as in JS and TS, maybe with addition of pipe operator |>)

Language specs

listed only currently implemented types and features

Types

Int32 - 32-bit integer

Bool - boolean true|false (internally 0 or 1 32-bit integer for now)

int - Int32 type alias

bool - Bool type alias


Variables

Immutable by default

int x; // variable declaration
x = 1; // initialization
x = 3; // error

int x2 = 1; // declaration with initialization

let y = 2; // type inferred as int

Mutable variables

mut int x = 1;
x = 3; // no error

mut y = 2; // type inferred as int

Operators

Binary math (int, int -> int):

  • left + right - addition
  • left - right - subtraction
  • left * right - multiplication
  • left / right - integer division
  • left % right - integer division reminder

Binary bitwise (int, int -> int | bool, bool -> bool):

  • left & right - butwise and
  • left | right - butwise or
  • left ^ right - butwise xor (exclusive or)

Binary bitshift (int, int -> int):

  • left >> right - bit shift right
  • left << right - bit shift left

Binary comparison (int, int -> bool | bool, bool -> bool):

  • left == right - bit shift right
  • left != right - bit shift right

(int, int -> int)

  • left < right - less than
  • left <= right - less or equal
  • left > right - greater than
  • left >= right - greater or equal

Unary:

  • - (int right) - unary minus (negation)
  • + (int right) - unary plus
  • ~ (int right) - birwise not
  • ! (bool right) - boolean not

Assignment binary operators (int, int -> int):

  • var += right
  • var -= right
  • var *= right
  • var /= right
  • var %= right
  • var <<= right
  • var >>= right

(int, int -> int | bool, bool -> bool)

  • var &= right
  • var |= right
  • var ^= right

Assignment unary operators (int -> int):

  • var ++
  • var --
  • ++ var
  • -- var

Branching

if (x < 0) {
    // ...
} else if (x == 0) {
    // ...
} else {
    // ...
}

While loop

mut int x = 1;
while (x <= 10) {
    x++;
    io.print(x);
}

For loop

for (mut int i in 1..=10) { // including 10
    // ...
}

for (mut int i in 1..10) { // excluding 10
    // ...
}

Type aliases

type i32 = int;
i32 x = 1; // int variable

type Index = i32;
Index x = 3; // also int variable

Implemented

  • Math operations with precedence and grouping
  • Variables (mutable/immutable)
  • Branching and loops (for, while)
  • Sinlge line, multi-line comments
  • Variables check initialized before use (with consideration of branching)
  • Immutable variables check no reassignment, initialized only once (with consideration of branching)
  • Type checking (need testing)
  • Type inference (need testing)
  • Type aliases (need testing)
  • Transforming AST and tokens stream by compiler extensions (built-in and custom)
  • Dead code removing (uncovered edgecases)
  • Compile time const expression evaluation (uncovered edgecases)
  • Support for generic types in tokenizer (<, >, >> tokens not to be confused with operators. tracking if identifier is a declared type name). to better support extensions that consume/transform a stream of tokens

Todo

  • Update grammar.txt (it's outdated)
  • Other primitive types (currently only int and bool)
  • Nested multi-line comments (/* /* ... */ */)
  • Implement generator for linux fasm and test it
  • Type casting (explicit)
  • String interpolation `x = ${x}`
  • use operator for scopes
  • Modules (imports/exports)
  • Number literals syntax (0xFF, 0b0101, 1_250_000)
  • Strings, arrays, objects (structs and/or classes, need research)
  • Functions, Fn<> types, lambdas
  • Tuple types (as stack or register allocations)
  • Generic types support
  • Option/Nullable type (Opt<T>)
  • Iterators (Enumerable<T>)
  • Custom operator definitions
  • Refs {probably}
  • Built-ins like Map<K,T>, Set<T>

Compiler extensions

Built-in:

  • ExtCheckTypesAndVariables

  • ExtEvaluateConstExpressions

  • ExtRemoveDeadCode

Making custom extension:

... todo readme

Compilation targets

  • Win x86 64-bit (fasm)
  • Linux x86 64-bit (fasm)
  • Win x86 64-bit (nasm) {probably}
  • Linux x86 64-bit (nasm) {probably}
  • LLVM IR {probably}

Editors support

Intellij

Plugin: https://github.com/NesCafe62/Krypton-intellij-plugin

Alternatively TextMate bundle available (but it will not add file type):

https://github.com/NesCafe62/Krypton-intellij-plugin/tree/main/src/main/resources/bundles/krypton

Installation

Requirements:

  • php 7.2 or higher (php used as a temporary language)

  • flat assembly (fasm) util to compile .asm to binary

  1. Clone the repo
$ cd project-path
$ git clone https://github.com/NesCafe62/Krypton-lang.git .
  1. Add php to system path (optional)

Usage makefile

Only windows for now

Set executable and target in Makefile to desired values:

EXECUTABLE = test4
TARGET = fasm-win-x86-64

Compile .kr file to .asm:

$ make compile

Compile .asm file to .exe:

$ make compile-exe

Compile .kr file to .exe:

$ make build

Compile .kr file to .exe and run it:

$ make all

Compile .kr file to .ast:

$ make dump-ast

Clean all compilation files (asm, exe, ast)

$ make clean

Usage cmd

compile .kr file to .asm (target = fasm-win-x86-64)

$ php src/index.php test.kr -t fasm-win-x86-64 > test.asm

or

$ php src/index.php test.kr --target=fasm-win-x86-64 > test.asm

targets:

  • ast
  • llvm
  • fasm-linux-x86-64
  • fasm-win-x86-64
  • avr-atmega328
  • avr-atmega328-hex

compile .kr file and dump AST with -ast flag

$ php src/index.php test.kr -ast > test.ast

compile .kr file and compile to binary using fasm (command for windows)

$ php src/index.php test.kr -t fasm-win-x86-64 > test.asm && fasm.exe test.asm

About

Compiler prototype for Krypton programming language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages