hetu_script 0.1.0 hetu_script: ^0.1.0 copied to clipboard
Hetu is a lightweight script language written in Dart for embedding in Flutter apps.
Hetu Script #
Table of Content: #
Introduction #
Hetu is a lightweight script language written in Dart for embedding in Flutter apps.
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.
Quick start #
Hetu's grammar is made similar to modern language like typescript/swift/kotlin:
- 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.
In your Dart code, you can interpret an script file by this:
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 the following command in any directory on your computer. (If you are facing any problems, please check this official document about pub global activate)
hetu [file_name] [invoke_func]
If [file_name] is provided, evaluate the file in [function] mode.
If [invoke_func] is provided, evaluate the file in [module] mode and call a certain function with given name.
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 will print out the eval result (in this case the type of this function)
>>>hello()
42 // repl print
>>>