-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathimport.go
More file actions
186 lines (154 loc) · 3.95 KB
/
Copy pathimport.go
File metadata and controls
186 lines (154 loc) · 3.95 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package evaluator
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/NuruProgramming/Nuru/ast"
"github.com/NuruProgramming/Nuru/lexer"
"github.com/NuruProgramming/Nuru/object"
"github.com/NuruProgramming/Nuru/parser"
)
var searchPaths []string
type maktaba int
const (
MAKTABA_YA_NURU maktaba = iota
MAKTABA_YA_HAPA
MAKTABA_YA_NJE
)
func evalImport(node *ast.Import, env *object.Environment) object.Object {
/*
Maktaba yanafuata mkondo wa:
msingi => ni maktaba ya kawaida (stdlib)
hapa => ni maktaba haya, tunaanza na faili "nuru.toml" (ama faili ya kisawa)
_ => majina mengine ni ya maktaba ya nje
*/
// Go through the Identifiers
for k, v := range node.Identifiers {
vs := strings.Split(v.Value, "/")
if len(vs) <= 0 {
break
}
switch vs[0] {
case "msingi":
loc := mahali_pa_maktaba(MAKTABA_YA_NURU)
ss := strings.Split(v.Value, "/")[1:]
fi := filepath.Join(loc, filepath.Join(ss...))
return evalImportFile(k, &ast.Identifier{Value: fi}, env)
case "hapa":
loc := mahali_pa_maktaba(MAKTABA_YA_HAPA, node.Filename)
ss := strings.Split(v.Value, "/")[1:]
fi := filepath.Join(loc, filepath.Join(ss...))
return evalImportFile(k, &ast.Identifier{Value: fi}, env)
default:
log.Printf("Maktaba ya nje hazijazaidiwa '%s'\n", k)
return NULL
}
}
return NULL
}
func mahali_pa_maktaba(mkt maktaba, mahali ...string) string {
switch mkt {
case MAKTABA_YA_NURU:
loc := os.Getenv("MAKTABA_YA_NURU")
if len(loc) > 0 {
return loc
}
var usr_lib string
var lib_ string
if runtime.GOOS == "windows" {
uhd, _ := os.UserHomeDir()
usr_lib = filepath.Join(filepath.Base(uhd), "nuru", "maktaba")
} else {
usr_lib = "/usr/lib/nuru/maktaba"
lib_ = "/lib/nuru/maktaba"
}
if fileExists(usr_lib) {
return usr_lib
}
if fileExists(lib_) {
return lib_
}
return usr_lib
case MAKTABA_YA_HAPA:
// Hii tunahitaji kuenda nyuma hadi mahali tutakapo pata faili "nuru.toml"
var mkt__ string = mahali[0]
var nuru_config string = "nuru.toml"
// Check if the current dir has "nuru.toml"
for {
if filepath.Dir(mkt__) == mkt__ {
break
}
if fileExists(filepath.Join(mkt__, nuru_config)) {
return mkt__
}
mkt__ = filepath.Dir(mkt__)
}
return mkt__
default:
return ""
}
}
func evalImportFile(name string, ident *ast.Identifier, env *object.Environment) object.Object {
addSearchPath("")
filename := findFile(ident.Value)
if filename == "" {
return newError("Moduli %s haipo", name)
}
var scope *object.Environment
scope, err := evaluateFile(name, filename, env)
if err != nil {
return err
}
return importFile(name, env, scope)
}
func addSearchPath(path string) {
searchPaths = append(searchPaths, path)
}
func findFile(name string) string {
basename := fmt.Sprintf("%s.nuru", name)
for _, path := range searchPaths {
file := filepath.Join(path, basename)
if fileExists(file) {
return file
}
}
return ""
}
func fileExists(file string) bool {
_, err := os.Stat(file)
return err == nil
}
func evaluateFile(name, file string, env *object.Environment) (*object.Environment, object.Object) {
source, err := os.ReadFile(file)
if err != nil {
return nil, &object.Error{Message: fmt.Sprintf("Tumeshindwa kufungua pakeji: %s", file)}
}
l := lexer.New(string(source))
p := parser.New(l, file)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
return nil, &object.Error{Message: fmt.Sprintf("Pakeji %s ina makosa yafuatayo:\n%s", file, strings.Join(p.Errors(), "\n"))}
}
pkg := &object.Package{
Name: &ast.Identifier{Value: name},
Env: env,
Scope: object.NewEnclosedEnvironment(env),
}
result := Eval(program, pkg.Scope)
env.Set(name, pkg)
if isError(result) {
return nil, result
}
return pkg.Env, nil
}
func importFile(name string, env *object.Environment, scope *object.Environment) object.Object {
value, ok := scope.Get(name)
if !ok {
return newError("%s sio pakeji", name)
}
env.Set(name, value)
return NULL
}