Skip to content

Elaphe is a compiler which converts dart file into python bytecode.

License

Notifications You must be signed in to change notification settings

organic-nailer/elaphe

Repository files navigation

Elaphe

Elaphe is a compiler that translates Dart programs into bytecode that runs on the Python virtual machine. With Elaphe, you can utilize the rich libraries of Python while using the syntax and static type checking of Dart.

ElapheはDartのプログラムをPython VM上で動くバイトコードに変換するコンパイラです。Dartの文法や静的な型チェックを使いながらPythonの豊富なライブラリを利用することができます。

Install

Elaphe currently supports Linux, macOS and Windows. Elaphe requires Rust for installation.

Linux(bash)

  • git clone https://github.com/organic-nailer/elaphe.git
  • cd ./elaphe
  • cargo build --release
  • mkdir -p ~/.elaphe/bin && cp -r ./target/release/elaphe ~/.elaphe/bin && cp -r ./target/release/template ~/.elaphe/bin && cp -r ./target/release/script ~/.elaphe/bin
  • echo 'export PATH="$PATH:$HOME/.elaphe/bin"' >> ~/.bashrc
  • source ~/.bashrc

macOS(zsh)

  • git clone https://github.com/organic-nailer/elaphe.git
  • cd ./elaphe
  • cargo build --release
  • mkdir -p ~/.elaphe/bin && cp -r ./target/release/elaphe ~/.elaphe/bin && cp -r ./target/release/template ~/.elaphe/bin && cp -r ./target/release/script ~/.elaphe/bin
  • echo 'export PATH="$PATH:$HOME/.elaphe/bin"' >> ~/.zshrc
  • source ~/.zshrc

Windows(PowerShell)

  • git clone https://github.com/organic-nailer/elaphe.git
  • cd ./elaphe
  • cargo build --release
  • mkdir -Force ~/.elaphe/bin; cp -r -Force ./target/release/elaphe.exe ~/.elaphe/bin; cp -r -Force ./target/release/template ~/.elaphe/bin; cp -r -Force ./target/release/script ~/.elaphe/bin
  • [Environment]::SetEnvironmentVariable("PATH", [Environment]::GetEnvironmentVariable("PATH", "USER") + ";" + $HOME + "\.elaphe\bin", "USER")
  • $ENV:PATH = [Environment]::GetEnvironmentVariable("PATH", "MACHINE") + ";" + [Environment]::GetEnvironmentVariable("PATH", "USER")

Getting Started

Elaphe currently only supports Python3.9.

$ elaphe init foo
$ cd foo
foo$ python -V
Python 3.9.15
foo$ elaphe run main.dart

Example

First, create a Python 3.9 environment using Anaconda or Miniconda. Make sure that the conda command is available beforehand.

$ conda create -n elaphe_env python=3.9
$ conda activate elaphe_env
$ python -V
Python 3.9.15
$ conda install numpy matplotlib

Create a new project with elaphe init, and adds libraries to use with elaphe add.

$ elaphe init elaphe_example
$ cd elaphe_example
elaphe_example$ elaphe add numpy
elaphe_example$ elaphe add matplotlib

Next, rewrite main.dart as following:

import 'elaphe/numpy.d.dart' as np;
import 'elaphe/matplotlib/pyplot.d.dart' as plt;

void main() {
  final x = np.arange(0, 10, 0.1);
  final y = np.sin(x);
  plt.plot(x, y);
  plt.show();
}

Finally, compile and execute the program with elaphe run.

elaphe_example$ elaphe run main.dart

Commands

elaphe init

elaphe init <directory>

Creates a new project in the specified directory.

elaphe add

elaphe add <python package>

Generates a type declaration file(*.d.dart) of the specified Python package in the project. The Python package must be present in the current Python environment.

elaphe build

elaphe build <target dart file>

Compiles the specified Dart file and generates the main.pyc file, which can be executed with python main.pyc.

elaphe run

elaphe run <target dart file>

elaphe run -c <dart code>

Compiles and runs the specified Dart file. If the -c option is passed, Elaphe interprets and executes the following text as a Dart program.

elaphe/core

sl()

external dynamic sl([int? start, int? end, int? step]);

There is a sl() function available in Dart syntax that allows the use of slices.

Limitation

Supported Python VM

Due to the constraints of the generated bytecode, Elaphe currently supports Python 3.9 only. However, future plans include adding support for other versions.

Compilation Target

Elaphe only supports compilation of a single file (main.dart). Compilation of multiple files and importing other dart files is not supported.

Dart Libraries

Elaphe does not support Dart libraries, including not only Flutter but also standard libraries such as dart:core and dart:math. Instead, Python libraries can be used.

Dart Syntax

Elaphe only supports a limited subset of Dart syntax. Therefore, some syntax cannot be used. Plans are underway to gradually support more syntax.

For the corresponding formal syntax, please refer to the following document. The red text indicates what is currently supported.

https://docs.google.com/document/d/1c956nDwu3t9qNN0C4HBvl9U6WSvCUY3umqpzohqlrKs/edit?usp=sharing

  • Variables
  • Functions
    • async keyword
    • sync keyword
    • generator
    • generics
    • covariant keyword
    • this keyword
  • Classes
    • abstract
    • generics
    • superclass
    • mixin
    • simple constructor
    • constructor with initializers
    • factory constructor
    • method declaration
    • static keyword
    • getter/setter
    • operator
    • late keyword
    • instance variable declaration
    • covariant keyword
    • const keyword
    • constructor redirection
  • Extensions
  • Enums
  • Generics
  • Metadata
  • Expressions
    • Assignment Expression
    • Expression List
    • Primary
      • this
      • function
      • null
      • bool
      • numeric
      • String
        • Single Quote
        • Double Quote
        • Multiline
        • format
        • Escape
      • List
        • Normal
        • Spread
      • Set/Map
        • Normal
        • if
        • for
    • Throw
    • new keyword
    • const keyword
    • Cascade
    • Conditional
    • IfNull
    • Logical operators
    • Equality operators
    • Relational operators
    • Bitwise operators
    • Shift operators
      • Not Support >>> and >>>=
    • Additive/Multiplicative operators
    • Unary operators
    • await keyword
    • Increment/Decrement
    • Selector
      • !
      • ?.xxx
      • ?[]
      • .xxx
      • []
      • Arguments
    • Type Cast
    • Type Test
  • Statements
    • Label
    • Block
    • Local Variable Declaration
    • Local Function Declaration
    • for
    • await for
    • for in
    • while
    • do
    • switch
      • case
      • default
      • label
    • if
    • rethrow
    • try
    • break
    • continue
    • return
    • yield
    • yield*
    • Expression Statement
    • assert
  • Libraries and Scripts
    • part keyword
    • export keyword
    • import keyword
      • Normal
      • as
      • show/hide
  • Static Types
    • void
    • function type
    • Identifier
    • Identifier.Identifier
  • Other
    • built-in identifier as identifier
    • other identifier as identifier

Supplementary Information

Elaphe logo is designed by Bing Image Creator

The font used in Elaphe's logo is Confortaa

About

Elaphe is a compiler which converts dart file into python bytecode.

Resources

License

Stars

Watchers

Forks

Packages

No packages published