In the last article “Finally Implemented a Programming Language of My Own”, I introduced the programming language GScript I wrote, and mentioned in the article that I hope that I can finally use it GScript
Develop a website.
So far it has indeed been done, the home page address:
https://gscript.crossoverjie.top/index
It’s a bit of a stretch to call it a website, but it’s also a dynamic page because it returns HTML
so at the current stage, as long as you don’t take too much trouble, you can actually write a “qualified” website, which is a bit like what we learned in the past. Java
time servlet
.
The source address of this page is here: https://github.com/crossoverjie/gscript-homepage
In fact, there are only about 40 lines of code in total:
class GScript{
string author;
string[] features;
string since;
GScript(string a, string[] f, string s){
author = a;
features = f;
since = s;
}
}
func (HttpContext) index(HttpContext ctx){
string[] features = {"statically", "strongly"};
GScript gs = GScript("crossoverJie",features, "2022");
string j = JSON(gs);
println(j);
string local = getCurrentTime("Asia/Shanghai","2006-01-02 15:04:05");
println("local=" + local);
string html = ^
<html>
<title>GScript</title>
<pre>
_ _
___ ___ ___ ___|_|___| |_
| . |_ -| _| _| | . | _|
|_ |___|___|_| |_| _|_|
|___| |_| v0.0.7
^+ j +^
</pre>
<h1>current ^+ local +^</h1>
<p><a href="https://github.com/crossoverjie/gscript-homepage">GScript-homepace source code</a></p>
</html>
^;
ctx.HTML(200, html);
}
httpHandle("GET", "/index", index);
string[] args = getOSArgs();
if (len(args) ==3){
httpRun(":" + args[2]);
}else {
httpRun(":8000");
}
all use GScript
The provided standard library is implemented, and the built-in HTTP package will be discussed in detail later.
Let’s focus on the following v0.0.8
What’s new in this version compared to the previous one.
Because I implemented an http service from the perspective of a developer, and also used GScript
Brushed two simple LeetCodes; in order to make this process smoother and more in line with the way a modern language is used, I really updated a lot of things this time.
Source code for brushing questions: https://github.com/crossoverJie/gscript/tree/main/example/leetcode
Probably as follows:
any
Type support, simplifying standard library implementations.- Can use
^^
To declare multi-line strings, it is convenient to declare complex strings. - More perfect type deduction, fixed the bug that the type could not be deduced in some cases in the previous version.
- Operator overloading is supported.
- Basic http package, can develop http service, currently can respond
JSON
as well asHTML
. - Added built-in functions: get the current time according to the time zone, get application startup parameters, etc.
JSON
The sequence table and query, the syntax level is adapted to XJSON.- fixed in multiple
block
Not correct in nested casereturn
bug.
In fact, it can be seen from these updates that the last version was only a simple and usable state, and now this version can be used to write complex logic. Of course, there are still some more friendly compilation prompts and runtime errors.
Let’s take a closer look at some of the updates below.
any type
first of all any
Generic type, this is similar to the one in Java Object
and in Go interface{}
which greatly facilitates us to write some standard libraries.
Taking the built-in hash and len functions as an example, it is very troublesome and unnecessary to implement each type once; now it only needs to be defined once, and the amount of code is directly saved several times.
Similarly, the previously implemented Map only supports storing the string type, but now it can store any type of data.
Friends who are interested in the implementation process of any can share it separately in the future.
operator overloading
Friends who write go or Java should know that neither language can operate on two objects, and the compiler will report an error directly.
But it’s still quite useful in some special scenarios, so I refer to C#
The syntax is in GScript
also realized.
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 p1 = Person(10);
Person p2 = Person(20);
Person p3 = p1+p2;
println("p3.age="+p3.age);
assertEqual(p3.age, 30);
The declared function name must be operator
followed by the operator to implement overloading.
The supported operators are:+-*/ < >= <= > ==
.
JSON support
The current version supports serialization of objects and basic types, but does not support deserialization to objects at the moment, but can be based on JSON
Strings query data through a certain syntax.
Two JSON related functions are built in:
// return JSON string
string JSON(any a){}
// JSON query with path
any JSONGet(string json, string path){}
class Person{
int age;
string name;
float weight;
bool man;
Person(string n, int a, float w, bool m){
name = n;
age = a;
weight = w;
man =m;
}
}
Person p1 = Person("abc",10,99.99,true);
Person p2 = Person("a",11,999.99,false);
string json = JSON(p1);
println(json);
// output:{"age":10,"man":true,"name":"abc","weight":99.99}
Take this code as an example, calling JSON
A function can serialize an object as JSON
string.
class Person{
int age;
string name;
float weight;
bool man;
Person(string n, int a, float w, bool m){
name = n;
age = a;
weight = w;
man =m;
}
}
Person p1 = Person("abc",10,99.99,true);
string json = JSON(p1);
println(json);
int age = JSONGet(json, "age");
println(age);
assertEqual(age,10);
use JSONGet
The function can query arbitrary data in a JSON string. This function is realized by adapting XJSON, so XJSON
All supported query syntaxes can be implemented.
string j=^{"age":10, "abc":{"def":"def"},"list":[1,2,3]}^;
String def = JSONGet(j, "abc.def");
println(def);
assertEqual(def,"def");
int l1 = JSONGet(j, "list[0]");
println(l1);
assertEqual(l1,1);
string str=^
{
"name": "bob",
"age": 20,
"skill": {
"lang": [
{
"go": {
"feature": [
"goroutine",
"channel",
"simple",
true
]
}
}
]
}
}
^;
String g = JSONGet(str, "skill.lang[0].go.feature[0]");
println(g);
assertEqual(g,"goroutine");
such as complex nesting JSON
and can also obtain data through query syntax.
HTTP package
The HTTP package is the focus of this upgrade. The following functions and classes are provided in the standard library:
// http lib
// Response json
FprintfJSON(int code, string path, string json){}
// Resonse html
FprintfHTML(int code, string path, string html){}
// path (relative paths may omit leading slash)
string QueryPath(string path){}
string FormValue(string path, string key){}
class HttpContext{
string path;
JSON(int code, any v){
string json = JSON(v);
FprintfJSON(code, path, json);
}
HTML(int code, any v) {
string html = v;
FprintfHTML(code, path, html);
}
string queryPath() {
string p = QueryPath(path);
return p;
}
string formValue(string key){
string v = FormValue(path, key);
return v;
}
}
// Bind route
httpHandle(string method, string path, func (HttpContext) handle){
// println("path="+path);
HttpContext ctx = HttpContext();
handle(ctx);
}
// Run http server.
httpRun(string addr){}
The specific use process:
- Implement your own business logic by defining a function variable.
- Register the route.
- Start the HTTP service.
in your own handle
through the HttpContext
The object gets the request context, and can get request parameters and response data. For specific usage examples, please refer to this code.
This update is smoother than I expected, because the syntax tree and compiler have been basically implemented and will not be changed much. Now the new feature is nothing more than the implementation of some syntactic sugar at runtime, most of which are manual labor; maybe It’s the stimulant effect of freshness, pain and pleasure most of the time.
For example, these two days are mainly repairing multiple layers block
encountered while nesting return
The bug that the statement could not be returned correctly took two nights of life and death; I finally found a solution after analyzing the AST countless times. Now I think about it, there is still too little relevant experience.
Friends who are interested in this bug can like it and share it later.
The focus of the next stage is to organize the compilation information to make the development experience better.Then take the time SQL
The standard library is implemented, so you can happily CURD
.
Finally, I hope that friends who are interested in the project or the compiling principle can download and use it, put forward valuable opinions, and welcome to add me to WeChat to communicate.
v0.0.8 download address: https://github.com/crossoverJie/gscript/releases/tag/v0.0.8
#milestone #Implemented #website #programming #language #crossoverJies #personal #page #News Fast Delivery