-
Notifications
You must be signed in to change notification settings - Fork 0
/
gostd.go
50 lines (44 loc) · 830 Bytes
/
gostd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"math"
)
type gostdmodules struct {
mods map[string][]*gostdmodobj
}
type gostdmodobj struct {
name string
o *obj
}
func (g *gostdmodules) objs(modname string) ([]*gostdmodobj, bool) {
objs, ok := g.mods[modname]
return objs, ok
}
// register standard module/object which are written in Go here.
var gostdmods = &gostdmodules{mods: map[string][]*gostdmodobj{
"math": {
{
"Pi",
&obj{
typ: tF64,
fval: math.Pi,
},
},
{
"Add",
&obj{
typ: tGoStdModFunc,
gostdmodfunc: func(objs ...*obj) (*obj, error) {
result := int64(0)
for _, o := range objs {
if o.typ != tI64 {
return NIL, fmt.Errorf("arg for add() must be i64")
}
result += o.ival
}
return &obj{typ: tI64, ival: result}, nil
},
},
},
},
}}