Hetu Script

Warning

Hetu is early WIP! We are focusing on making Hetu stable and feature complete right now.

Discussion group:

Discord: https://discord.gg/Q8JWQYEw

QQ 群: 812529118

Introduction

Hetu is a lightweight script language purely written in Dart for embedding in Flutter apps. The main goal is to enable Flutter app have hotfix and scripting ability.

We did not choose to use another existing language to achieve the goal. Because we want to keep the language simple, and keep it away from interference of other language's complex implementation and their irrelative-to-Flutter eco-system, and make the debug process pain-free and remain in Dart realms.

It takes very little time to bind almost anything in Dart/Flutter into Hetu and use similar grammar to write your app. And to communicate with classes & functions in Dart is very easy.

Quick start

Hetu's grammar is close to typescript/kotlin/swift and other modern languages, need very little time to get familar with.

  • Optional semicolon.
  • Function is declared with 'fun, get, set, construct'.
  • Optional type annotation. Variable declared with 'let, const' will infer its type from its initializer expression.

Syntax referrence

In your Dart code, you can interpret a script file:

import 'package:hetu_script/hetu_script.dart';

void main() async {
  var hetu = Hetu();
  await hetu.init();
  await hetu.import('hello.ht', invokeFunc: 'main');
}

While hello.ht is the script file written in Hetu, here is an example:

// Define a class.
class Person {
  var name: str
  construct (name: str) {
    this.name = name
  }
  fun greeting {
    print('Hi! I\'m', name)
  }
}

// This is where the script starts executing.
fun main {
  var ht = Person('Hetu')
  ht.greeting()
}

Binding

Hetu script is purely written in Dart, so passing object to and from script is extremely easy.

Check this page for more information about how to bind external classes, functions, enums and how to passing object and functions between Dart and script.

import 'package:hetu_script/hetu_script.dart';

void main() async {
  var hetu = Hetu();
  await hetu.init(externalFunctions: {
    'hello': () => {'greeting': 'hello'},
  });
  await hetu.eval(r'''
      external fun hello
      fun main {
        var dartValue = hello()
        print('dart value:', dartValue)
        dartValue['foo'] = 'bar'
        return dartValue
      }''');

  var hetuValue = hetu.invoke('main');

  print('hetu value: $hetuValue');
}

Command line tool

Hetu has a command line REPL tool for testing. You can activate by the following command:

dart pub global activate hetu_script

Then you can use command line tool 'hetu' in any directory on your computer. (If you are facing any problems, please check this official document about pub global activate)

Currently there are two command: run for evaluate a script file and fmt for format the scrip file.

More information can be found by enter hetu -h.

If no command and option is provided, enter REPL mode.

In REPL mode, every exrepssion you entered will be evaluated and print out immediately.

If you want to write multiple line in REPL mode, use '\' to end a line.

>>>var a = 42
>>>a
42
>>>fun hello {\
return a }
>>>hello
function hello() -> any // repl print
>>>hello()
42 // repl print
>>>

Referrences:

Apps that embedded Hetu:

NameAuthorDescriptionDownloadSource
Monster Hunter Otomo: RiserockingdiceA unofficial game companion app for Capcom's Monster Hunter: RiseiOSClosed Source

Libraries

abstract_interpreter
abstract_parser
analyzer
ast
ast_compilation
auto_binding
buildin_class
buildin_function
buildin_instance
bytecode_reader
cast
class
class_declaration
class_namespace
compiler
compiler2
const_table
crc32b
CRC32 Copyright (C) 2012, Kai Sellgren Licensed under the MIT License. http://www.opensource.org/licenses/mit-license.php
disassembler
enum
error_handler
error_processor
errors
external_class
external_function
external_instance
formatter
function
function_declaration
function_type
hetu2js
hetu_lib
hetu_script
HETU SCRIPT [...]
instance
instance_namespace
interpreter
lexer
lexicon
line_info
literal_type
namespace
nominal_type
object
opcode
parameter
parser
semantic
source
source_provider
source_range
structural_type
token
type
typed_function_declaration
typed_parameter_declaration
typed_variable_declaration
union_type
utilities
value_type
variable
variable_declaration