Skip to content

Commit fc4c1de

Browse files
committed
🚀 O'zgarishlar go dasturlash tiliga oid yangi test faylini va bir nechta yangi xizmatlarni qo'shdi, bu kodning kengayishi uchun ijobiy qadam.
1 parent a967d88 commit fc4c1de

15 files changed

Lines changed: 207 additions & 38 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ cpu.prof
1010
test.json
1111
main.py
1212
ffi/
13+
test.go
1314

1415
# Test binary, built with `go test -c`
1516
*.test

cmd/fpm/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http/fcgi"
99
"os"
1010

11+
"github.com/UzStack/bug-lang/cmd/fpm/services"
1112
"github.com/UzStack/bug-lang/internal/lexar"
1213
"github.com/UzStack/bug-lang/internal/parser"
1314
"github.com/UzStack/bug-lang/internal/runtime"
@@ -19,6 +20,7 @@ import (
1920
type Job struct {
2021
File string
2122
Response chan Result
23+
Request *http.Request
2224
}
2325

2426
type Result struct {
@@ -34,6 +36,7 @@ type Header struct {
3436
func Worker(jobs <-chan Job) {
3537
for job := range jobs {
3638
code, err := os.ReadFile(job.File)
39+
request := job.Request
3740
if err != nil {
3841
job.Response <- Result{
3942
Body: fmt.Sprintf("Error reading file %s: %v", job.File, err),
@@ -67,6 +70,12 @@ func Worker(jobs <-chan Job) {
6770
})
6871
},
6972
}, -1)
73+
// Load gloabl variables
74+
env.AssignmenVariable("_POST", services.ParsePostData(request), -1)
75+
env.AssignmenVariable("_GET", services.ParseGetData(request), -1)
76+
env.AssignmenVariable("_REQUEST", services.ParseRequest(request), -1)
77+
env.AssignmenVariable("_GLOBALS", services.ParseGlobals(request), -1)
78+
7079
runtime.Interpreter(ast, env)
7180
job.Response <- Result{
7281
Body: buf.String(),
@@ -82,7 +91,7 @@ func handler(w http.ResponseWriter, r *http.Request, jobs chan<- Job) {
8291
file := params["DOCUMENT_ROOT"] + params["DOCUMENT_URI"]
8392
result := make(chan Result)
8493
// Faylni workerga yuborish
85-
jobs <- Job{File: file, Response: result}
94+
jobs <- Job{File: file, Response: result, Request: r}
8695
res := <-result
8796
for _, header := range res.Headers {
8897
w.Header().Set(header.Key, header.Value)

cmd/fpm/services/parser.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package services
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/UzStack/bug-lang/internal/runtime/types"
10+
"github.com/UzStack/bug-lang/pkg/utils"
11+
)
12+
13+
func ParsePostData(request *http.Request) any {
14+
body, err := io.ReadAll(request.Body)
15+
if err != nil {
16+
panic(err.Error())
17+
}
18+
var data any
19+
if err := json.Unmarshal(body, &data); err != nil {
20+
return types.NewMap(nil)
21+
}
22+
return utils.DecodeBug(data)
23+
}
24+
25+
func ParseGetData(request *http.Request) any {
26+
data := types.NewMap(make(map[string]any))
27+
rawQuery := request.URL.RawQuery
28+
if len(rawQuery) == 0 {
29+
return data
30+
}
31+
for _, p := range strings.Split(rawQuery, "&") {
32+
param := strings.Split(p, "=")
33+
if len(param) >= 2 {
34+
data.Add(param[0], utils.DecodeBug(param[1]))
35+
}
36+
}
37+
return data
38+
}
39+
40+
func ParseRequest(request *http.Request) any {
41+
headers := types.NewMap(make(map[string]any))
42+
for key, values := range request.Header {
43+
if len(values) > 1 {
44+
value := types.NewArray([]any{})
45+
for _, v := range values {
46+
value.Add(utils.DecodeBug(v))
47+
}
48+
headers.Add(key, value)
49+
} else if len(values) == 1 {
50+
headers.Add(key, utils.DecodeBug(values[0]))
51+
} else {
52+
headers.Add(key, types.NewNull())
53+
}
54+
}
55+
56+
globals := types.NewMap(make(map[string]any))
57+
globals.Add("RequestURI", types.NewString(request.RequestURI))
58+
globals.Add("Host", types.NewString(request.Host))
59+
globals.Add("Method", types.NewString(request.Method))
60+
globals.Add("Headers", headers)
61+
globals.Add("Path", types.NewString(request.URL.Path))
62+
return globals
63+
}
64+
65+
func ParseGlobals(request *http.Request) any {
66+
globals := types.NewMap(make(map[string]any))
67+
return globals
68+
}

examples/api.bug

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
header("Content-Type", "application/json");
2+
import "json";
23

3-
import "psql";
4+
#import "psql";
45

5-
db = new psql.DB("db");
6-
query = db.query("select * from users");
6+
#db = new psql.DB("db");
7+
#query = db.query("select * from users");
78

8-
users = query.findAll();
9-
print(users);
9+
#users = query.findAll();
10+
#print(users);
11+
12+
13+
data = [
14+
_GET, _POST, _REQUEST, _GLOBALS
15+
];
16+
println(json.encode(data));

internal/parser/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
var STDLIBS = []any{
3030
"math",
3131
"ffi",
32+
"json",
3233
}
3334

3435
type Statement struct {

internal/parser/parser.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ func (p *parser) ParseMapItems() map[string]any {
145145
}
146146
p.Except(lexar.Colon, "Except colon Object")
147147
items[key] = p.ParseAssignmentExpression()
148-
p.Except(lexar.Comma, "Except comma Object")
148+
// p.Except(lexar.Comma, "Except comma Object")
149+
if p.At().Type == lexar.Comma {
150+
p.Next()
151+
}
149152
}
150153
p.Except(lexar.CloseBrace, "Except close brace Object")
151154
return items

internal/runtime/std/libs/json.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package libs
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/UzStack/bug-lang/internal/runtime/types"
7+
"github.com/UzStack/bug-lang/pkg/utils"
8+
)
9+
10+
func JsonEncode(value any) any {
11+
data, err := json.Marshal(utils.EncodeBug(value))
12+
if err != nil {
13+
panic(err.Error())
14+
}
15+
return types.NewString(string(data))
16+
}

internal/runtime/std/load.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,13 @@ import (
66
)
77

88
func Load(env *enviroment.Enviroment) {
9-
env.DeclareVariable("print", &types.NativeFunctionDeclaration{
10-
Type: "native-function",
11-
Call: Print,
12-
}, -1)
13-
env.DeclareVariable("println", &types.NativeFunctionDeclaration{
14-
Type: "native-function",
15-
Call: Println,
16-
}, -1)
17-
env.DeclareVariable("console", &types.NativeFunctionDeclaration{
18-
Type: "native-function",
19-
Call: Print,
20-
}, -1)
21-
env.DeclareVariable("consoleln", &types.NativeFunctionDeclaration{
22-
Type: "native-function",
23-
Call: Println,
24-
}, -1)
9+
env.DeclareVariable("print", types.NewNativeFunction(Print), -1)
10+
env.DeclareVariable("println", types.NewNativeFunction(Println), -1)
11+
env.DeclareVariable("console", types.NewNativeFunction(Print), -1)
12+
env.DeclareVariable("consoleln", types.NewNativeFunction(Println), -1)
2513

26-
env.DeclareVariable("input", &types.NativeFunctionDeclaration{
27-
Type: "native-function",
28-
Call: Input,
29-
}, -1)
30-
env.DeclareVariable("header", &types.NativeFunctionDeclaration{
31-
Type: "native-function",
32-
Call: Header,
33-
}, -1)
14+
env.DeclareVariable("input", types.NewNativeFunction(Input), -1)
15+
env.DeclareVariable("header", types.NewNativeFunction(Header), -1)
3416
env.DeclareVariable("true", types.NewBool(true), -1)
3517
env.DeclareVariable("false", types.NewBool(false), -1)
3618
env.DeclareVariable("null", types.NewNull(), -1)

internal/runtime/std/std.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ var STDLIBS = map[string]map[string]any{
2121
"load": libs.FFILoad,
2222
"call": libs.FFICall,
2323
},
24+
"json": {
25+
"encode": libs.JsonEncode,
26+
},
2427
}
2528

2629
func QuotationMark(value any) bool {
@@ -59,7 +62,7 @@ func Pprint(buf *bytes.Buffer, values ...any) {
5962
case *types.MapValue:
6063
fmt.Fprint(buf, "{")
6164
i := 0
62-
values := v.GetValue().(map[string]any)
65+
values := v.GetValue()
6366
size := len(values)
6467
for key, value := range values {
6568
i++

internal/runtime/types/function.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package types
2+
3+
type NativeFunctionValue struct {
4+
Call any
5+
}
6+
7+
func NewNativeFunction(call any) Object {
8+
return &NativeFunctionValue{
9+
Call: call,
10+
}
11+
}
12+
13+
func (a *NativeFunctionValue) GetValue() any {
14+
return a.Call
15+
}

0 commit comments

Comments
 (0)