gscript
gscript copied to clipboard
yet another script language/使用Go编写的脚本语言
The Gscript Language
English|中文
Gscript is a light, dynamic script language written in Go.
example:
import fs
import os
# define a function
func writeSomeMsg(filepath) {
# open, create and truncate text.txt in write-only mode
let file = fs.open(filepath,"wct");
# write some message into file
file.write("hello world!\n")
file.write("gscript is a good language!")
# close file
file.close();
}
let filepath = "./text.txt"
try{
writeSomeMsg(filepath)
let stat = fs.stat(filepath)
print("size of text.txt is " + stat.size + "B");
# read all data from text.txt
let data = fs.readFile(filepath);
print("message of", filepath, "is:")
print(data.toString())
# remove text.txt
fs.remove(filepath)
print("success!")
}
catch(e){
print("operation failed, error msg:",e)
os.exit(0)
}
Features
-
Function
-
Multiple return values
-
Closure
-
Recursive function call
-
-
All standard libraries are packaged into one executable, which means, to install or use gscript it's no need to configure some environment variables.
-
Object-oriented programming
-
Debug mode
-
Can generate human-readable assemble-like codes
-
Simple syntax, easy to learn, especially to coders with JavaScript or Python experiences.
-
Modular support
-
Exception support: try, catch and throw
-
Complied/executed as bytecode on stack-based VM
Install
Since compiler and VM are written in pure Go(no cgo) and does not use any platform-dependent interfaces, so Gscript is a cross-platform language.
You can install Gscript in two ways:
-
Compile from source code.
note: we use the new feature "embed" of Go 1.16, so make sure your Go version is greater than or equal to 1.16.
git clone [email protected]:gufeijun/gscript.git cd gscript sh build.shthen the compiler will generated to
bin/gsc. -
Download from releases.
gsc means gscript compiler. You can add the executable to PATH as you wish.
Then all you need is just having fun.
Quick Start
open file main.gs and write codes below:
print("hello world");
run the script:
gsc run main.gs
you will get following output:
hello world
you can also do something more challenging:
print(fib(20))
func fib(x) {
if (x == 0) return 0;
if (x == 1) return 1;
return fib(x-1) + fib(x-2)
}
run the script, you will get 6765.
Usage
we demonstrated the command run of gsc above. You can use gsc --help for more details about how to use gsc.
-
use
gsc debug <source file>orgsc debug <bytecode file>to enter debug mode, in which we can execute virtual instruction one by one and view stack and variable table changes in real time. -
use
gsc build <source file>to generate bytecode, besides, you can use-oflag to specific name of output file. -
use
gsc build -a <source file>orgsc build -a <bytecode file>to generate human-readable assemble-like codes.Take the
main.gsin sectionQuick Startas an example, run the following command:gsc build -a main.gs -o output.gscasmIt will generate
output.gsasm:/home/xxx/gscript/main.gs(MainProto): MainFunc: 0 LOAD_CONST "hello world" 9 LOAD_BUILTIN "print" 14 CALL 0 1 17 STOP -
use
gsc run <source file>orgsc run <bytecode file>to run the script.