# gscript **Repository Path**: firegod01cn/gscript ## Basic Information - **Project Name**: gscript - **Description**: 💪🏻This is a statically and strongly typed language written in Go. - **Primary Language**: Go - **License**: BSD-3-Clause - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-09-21 - **Last Updated**: 2022-09-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
``` _ _ ___ ___ ___ ___|_|___| |_ | . |_ -| _| _| | . | _| |_ |___|___|_| |_| _|_| |___| |_| ``` 📘[Features](#features) | 🌰[Demo](#demo) | 👾[REPL](#repl) | 🎉[Syntax](#syntax) | 🎁[Standard library](#standard-library) | 🔧[Install](https://github.com/crossoverJie/gscript/releases) | 💡[Contact Author](#contact-author) | 🇨🇳[中文文档](https://github.com/crossoverJie/gscript/blob/main/README-ZH.md)

# Introduction This is a **statically and strongly** typed language written in Go, the syntax of Java and Go is referenced. > The current version is for study and experimentation only. hello_world.gs: ```js println("hello world"); ``` ```shell ❯ gscript hello_world.gs hello world ``` # Features - [x] [Class declaration](#class). - [x] [Function declaration and call](#function). - [x] [Primitive type](#primitive): `int/string/float/bool`. - [x] [Array type](#array). - [x] `nil` type. - [x] [`any` type](#any-type). - [x] [Function type](#closure). - [x] [Closure:Functions as First-Class Objects](#closure). - [x] [Native function](#native-function): `len()/hash()/assertEqual()/JSON()/JSONGet()`. - [x] [Standard library](#standard-library):`Map/LinkedList/Array`. - [x] [Map](#map) - [x] [Variable arguments](#variable-arguments) - [x] [Operator overloading](#operator-overloading). - [x] [Native support](#native-function) `json`. - [x] [Native support `http`](#http). - [x] Example - [x] LeetCode - [x] [LeetCode: Linked list cycle](https://github.com/crossoverJie/gscript/blob/main/example/linked_list_cycle141.gs) - [x] [LeetCode: Two sum](https://github.com/crossoverJie/gscript/blob/main/example/leetcode/two_sum.gs) - [x] [Print fibonacci](#print-fibonacci) - [x] [HTTP Service](https://github.com/crossoverJie/gscript/blob/main/example/http_server.gs) - [ ] Unit Test command line. # Demo ## Hello world ```js println("hello world"); ``` ## Print fibonacci ```js 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/index](https://gscript.crossoverjie.top/index) Source code: [https://github.com/crossoverjie/gscript-homepage](https://github.com/crossoverjie/gscript-homepage) --- More examples:[https://github.com/crossoverJie/gscript/tree/main/example](https://github.com/crossoverJie/gscript/tree/main/example) # REPL ```shell > ./gscript ``` ![](doc/repl.gif) # Syntax ## Primitive The current version supports four primitive type: `int/string/float/bool` and `nil` type. Variable declaration syntax: `type identifier (= expr)?`. ```js int a=10; string b,c; float e = 10.1; bool f = false; string x = ` { "name": "bob", "age": 20, "skill": { "lang": [ { "go": { "feature": [ "goroutine", true ] } } ] } } ``` ## Array Array declaration syntax: `('[' DECIMAL_LITERAL ']')? '{' (variableInitializer (',' variableInitializer)* (',')? )? '}'` ```js // 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 When there is no return value, the return type can also be ignored. ## Closure Function type syntax: `func typeTypeOrVoid '(' typeList? ')'` ```js // 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: ```shell 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: ```js int add(string s, int ...num){ println(s); int sum = 0; for(int i=0;i >=` > Overloading function must be **operator**, and append operator. ```js 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; println("b3=="+b3); assertEqual(b3,false); bool b4 = p1 >= p2; println("b4=="+b4); assertEqual(b4,false); bool b5 = p1 < p2; println("b5=="+b5); assertEqual(b5,true); bool b6 = p1 <= p2; println("b6=="+b6); assertEqual(b6,true); ``` More examples: [https://github.com/crossoverJie/gscript/tree/main/example](https://github.com/crossoverJie/gscript/tree/main/example) # Standard library Standard library source code: [https://github.com/crossoverJie/gscript/tree/main/internal](https://github.com/crossoverJie/gscript/tree/main/internal) ## Native function ```js printf("hello %s ","123"); printf("hello-%s-%s ","123","abc"); printf("hello-%s-%d ","123",123); string s = sprintf("nice to meet %s", "you"); println(s); assertEqual(s,"nice to meet you"); int[] a={1,2,3}; // len return array length. println(len(a)); // Append data to array. append(a,4); println(a); // output: [1,2,3,4] // Assert function assertEqual(len(a),4); // Return hashcode int hashcode = hash(key); // Serialize to JSON string. class P{ string name; P(string n){ name = n; } } class Object{ P p; int x; Object(P pp, int xx){ p = pp; x = xx; } } P p1 = P("abc"); Object o1 = Object(p1, 100); string json = JSON(o1); println(json); //{"p":{"name":"abc"},"x":100} // Query json with path int x = JSONGet(json,"x"); println(x); assertEqual(x,100); string name = JSONGet(json,"p.name"); println(name); assertEqual(name,"abc"); // Get command-line arguments. string[] args = getOSArgs(); ``` > Reference JSON query syntax: [xjson](https://github.com/crossoverJie/xjson#arithmetic-syntax) ## Map Function Definition: ```js class Map{ put(any key, any value){} any get(any key){} } ``` ```js int count =100; Map m1 = Map(); for (int i=0;i hello

current ^+ local +^

hahaha

^; string queryPath = ctx.queryPath(); println("queryPath = " + queryPath); // http://127.0.0.1:8000/p/2?id=100 string id = ctx.formValue("id"); println("id="+id); ctx.HTML(200, html); } httpHandle("get", "/p", handle); httpHandle("get", "/p/1", handle1); httpHandle("get", "/p/2", handle2); httpRun(":8000"); ``` ## Contact author > crossoverJie#gmail.com ![qrcode_for_gh_3a954a025f10_258.jpg](https://i.loli.net/2019/07/09/5d245f3e955ce61699.jpg)