Gscript
💪🏻This is a statically and strongly typed language written in Go.|GScript 是用 Go 编写的静态、强类型的脚本语言。
Install / Use
/learn @crossoverJie/GscriptREADME
_ _
___ ___ ___ ___|_|___| |_
| . |_ -| _| _| | . | _|
|_ |___|___|_| |_| _|_|
|___| |_|
🎮Play | 📘Features | 🌰Demo | 🔧Install | 👾REPL | 🎉Syntax | 🎁Standard library | 💡Contact Author | 🇨🇳中文
</div><br>Introduction
This is a statically and strongly typed language written in Go, the syntax of Java and Go is referenced.
Target
Provides the ability to write Go language in script form, retains the advantages of Go language (goroutine, etc.), and adds more syntactic sugar for ease of use.
The current Alpha version is for study and experimentation only.
hello_world.gs:
println("hello world");
❯ gscript hello_world.gs
hello world
Playground
Online address: https://gscript.crossoverjie.top/

Source code:https://github.com/crossoverJie/gscript-homepage
Features
- [x] Class declaration
- [x] Function declaration and call
- [x] Primitive type:
int/string/float/bool/byte - [x] Array type
- [x]
niltype - [x]
anytype - [x] Function type
- [x] Closure:Functions as First-Class Objects
- [x] Native function
- [x] Standard library
- [x] Map
- [x] Variable arguments
- [x] Operator overloading
- [x] Native support
json - [x] Native support
http - [x] Example
- [x] LeetCode
- [x] Print fibonacci
- [x] Print YangHui triangle
- [x] HTTP Service
- [ ] PKG manager
- [ ] Unit Test command line
Demo
Hello world
println("hello world");
Print fibonacci
func int() fib(){
int a = 0;
int b = 1;
int fibonacci(){
int c = a;
a = b;
b = a+c;
return c;
}
return fibonacci;
}
func int() f = fib();
for (int i = 0; i < 10; i++){
println(f());
}
Webapp
This is a dynamic web application written in GScript.
https://gscript.crossoverjie.top/api/index
Source code: https://github.com/crossoverjie/gscript-homepage
YangHui triangle
int num(int x,int y){
if (y==1 || y ==x) {
return 1;
}
int v1 = num(x - 1, y - 1);
int v2 = num(x - 1, y);
int c = v1+v2;
// int c = num(x - 1, y - 1)+num(x - 1, y);
return c;
}
printTriangle(int row){
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= row - i; j++) {
print(" ");
}
for (int j = 1; j <= i; j++) {
print(num(i, j) + " ");
}
println("");
}
}
printTriangle(7);
// output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
More examples:https://github.com/crossoverJie/gscript/tree/main/example
Install
Binary
Download the latest binaries here
🐳Docker
docker pull crossoverjie/gscript
REPL
docker run --rm -it crossoverjie/gscript:latest gscript
Run script
docker run --rm -v $PWD:/usr/src/gscript -w /usr/src/gscript crossoverjie/gscript gscript {yourpath}/temp.gs
Build from Source Code
git clone https://github.com/crossoverJie/gscript.git
cd gscript
make build-code
./gscript
REPL
> ./gscript

Syntax
Primitive
The current version supports four primitive type: int/string/float/bool and nil type.
Variable declaration syntax: type identifier (= expr)?.
int a=10;
string b,c;
float e = 10.1;
bool f = false;
byte by = 1;
string x = ^
{
"name": "bob",
"age": 20,
"skill": {
"lang": [
{
"go": {
"feature": [
"goroutine",
true
]
}
}
]
}
}^
Array
Array declaration syntax: ('[' DECIMAL_LITERAL ']')? '{' (variableInitializer (',' variableInitializer)* (',')? )? '}'
// Declare and initialize
int[] a={1,2,3};
println(a);
// Declare an empty array and specify the length
int[] table = [4]{};
println("");
// Append data to array.
append(a,4);
println(a);
for(int i=0;i<len(a);i++){
println(a[i]);
}
// Access to data by index.
int b=a[2];
println(b);
// byte array
string s = "10";
byte[] a= toByteArray(s);
printf("a=%v ",a);
string s1 = toString(a);
printf("s1=%s",s1);
assertEqual(s1,s);
// slice an array into a new array.
int[] a = {1,2,3};
int s=1;
int[] b = a[s:len(a)];
println(b);
// output: [2 3]
any type
An any type may hold values of all type.
any a =10;
println(a);
int fun1(any a,int b){
return a+b;
}
int v =fun1(1,2);
println(v);
assertEqual(v,3);
any v2 = fun1(1,2);
println(v2);
assertEqual(v2,3);
int fun2(int a, any b){
return a+b;
}
int v3 =fun2(1,2);
println(v3);
assertEqual(v3,3);
int fun3(any a, any b){
return a+b;
}
int v4 =fun3(1,2);
println(v4);
assertEqual(v4,3);
int fun4(any a, any b){
return a+b;
}
string v5 =fun4("10", "20");
println(v5);
assertEqual(v5,"1020");
Example: Standard library
Class
class ListNode{
int value;
ListNode next;
ListNode(int v, ListNode n){
value =v;
next = n;
}
}
// The new keyword is not required to call the constructor.
ListNode l1 = ListNode(1, nil);
// Using . to access object property or method.
println(l1.value);
The default comes with a parameterless constructor
class Person{
int age=10;
string name="abc";
int getAge(){
return 100+age;
}
}
// parameterless constructor
Person xx= Person();
println(xx.age);
assertEqual(xx.age, 10);
println(xx.getAge());
assertEqual(xx.getAge(), 110);
function
// cycle linked list
bool hasCycle(ListNode head){
if (head == nil){
return false;
}
if (head.next == nil){
return false;
}
ListNode fast = head.next;
ListNode slow = head;
bool ret = false;
for (fast.next != nil){
if (fast.next == nil){
return false;
}
if (fast.next.next == nil){
return false;
}
if (slow.next == nil){
return false;
}
if (fast == slow){
ret = true;
return true;
}
fast = fast.next.next;
slow = slow.next;
}
return ret;
}
ListNode l1 = ListNode(1, nil);
bool b1 =hasCycle(l1);
println(b1);
assertEqual(b1, false);
ListNode l4 = ListNode(4, nil);
ListNode l3 = ListNode(3, l4);
ListNode l2 = ListNode(2, l3);
bool b2 = hasCycle(l2);
println(b2);
assertEqual(b2, false);
l4.next = l2;
bool b3 = hasCycle(l2);
println(b3);
assertEqual(b3, true);
Function declaration syntax: typeTypeOrVoid? IDENTIFIER formalParameters ('[' ']')*
add(int a){}
When there is no return value, the return type can also be ignored.
Closure
Function type syntax: func typeTypeOrVoid '(' typeList? ')'
// External variable, global shared.
int varExternal =10;
func int(int) f1(){
// Closure variable.
int varInner = 20;
int innerFun(int a){
println(a);
int c=100;
varExternal++;
varInner++;
return varInner;
}
return innerFun;
}
// f2 is a function type, the return type and parameter are both int.
func int(int) f2 = f1();
for(int i=0;i<2;i++){
println("varInner=" + f2(i) + ", varExternal=" + varExternal);
}
println("=======");
func int(int) f3 = f1();
for(int i=0;i<2;i++){
println("varInner=" + f3(i) + ", varExternal=" + varExternal);
}
Output:
0
varInner=21, varExternal=11
1
varInner=22, varExternal=12
=======
0
varInner=21, varExternal=13
1
varInner=22, varExternal=14
Variable arguments
GScript also support variable arguments:
int add(string s, int ...num){
println(s);
int sum = 0;
for(int i=0;i<len(num);i++){
int v = num[i];
sum = sum+v;
}
return sum;
}
int x = add("abc", 1,2,3,4);
println(x);
assertEqual(x, 10);
Operator overloading
Operator that Gscript support:
+-*/== != < <= > >=
Overloading function must be operator, and append operator.
class Person{
int age;
Person(int a){
age = a;
}
}
Person operator + (Person p1, Person p2){
Person pp = Person(p1.age+p2.age);
return pp;
}
Person operator - (Person p1, Person p2){
Person pp = Person(p1.age-p2.age);
return pp;
}
Person operator * (Person p1, Person p2){
Person pp = Person(p1.age * p2.age);
return pp;
}
Person operator / (Person p1, Person p2){
Person pp = Person(p1.age / p2.age);
return pp;
}
bool operator == (Person p1, Person p2){
return p1.age==p2.age;
}
bool operator != (Person p1, Person p2){
return p1.age!=p2.age;
}
bool operator > (Person p1, Person p2){
return p1.age>p2.age;
}
bool operator >= (Person p1, Person p2){
return p1.age>=p2.age;
}
bool operator < (Person p1, Person p2){
return p1.age<p2.age;
}
bool operator <= (Person p1, Person p2){
return p1.age<=p2.age;
}
Person p1 = Person(10);
Person p2 = Person(20);
//Person p3 = operator(p1,p2);
Person p3 = p1+p2;
println("p3.ag
