Giter VIP home page Giter VIP logo

hetu-script's Introduction

Hetu Script

Introduction

中文介绍

Hetu is a lightweight script language written purely in Dart. It is intended to be embedded in Flutter/Dart apps & games to enable hotfixes and scripting.

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

It meant to be used as a scripting language like lua, however, it is made to communicate with classes & functions in Dart very easily.

Syntax referrence

语法参考

In your Dart code, you can interpret an script file by this:

import 'package:hetu_script/hetu_script.dart';

void main() async {
  var hetu = HTAstInterpreter();
  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: String
  construct (name: String) {
    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()
}

Hetu's grammar is almost same to typescript, except a few things:

  • Function is declared with 'fun'.
  • Variable declared with keyword 'let' or 'const' and without a type will be given a type if it has an initialization.

Binding

To call Dart functions in Hetu, just init Hetu with 'externalFunctions'.

Then define those dart funtion in Hetu with 'external' keyword.

Then you can call those functions in Hetu.

You can pass object from Dart to Hetu by the return value of external functions.

You can pass object from Hetu to Dart by the return value of Interpreter's [invoke] function;

import 'package:hetu_script/hetu_script.dart';

void main() async {
  var hetu = HTAstInterpreter();
  await hetu.init(externalFunctions: {
    'hello': (List<dynamic> positionalArgs, Map<String, dynamic> namedArgs) => {'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');
}

And the output should be:

dart value: {greeting: hello}
hetu value: {greeting: hello, foo: bar}

Command line tool

On Windows, there is a hetu.exe under project directory to use in Command line.

Usage:

hetu [file_name] [invoke_func]

If no option is provided, enter REPL mode.

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

>>>var a = 42
42

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

>>>fun hello {\
return 6 * 7} // press enter
function hello(): any // repl print
>>>hello()
42 // repl print
>>>

If [file_name] is provided, evaluate the file in function mode.

If [invoke_name] is provided, evaluate the file in library mode and call a certain function with given name.

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.