forked from refaktor/rye-fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (114 loc) · 2.99 KB
/
main.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
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
//go:build linux || darwin || windows
// +build linux darwin windows
package main
import (
"regexp"
"github.com/refaktor/rye/contrib"
"fmt"
"io"
"log"
"os"
"github.com/refaktor/rye/env"
"github.com/refaktor/rye/evaldo"
"github.com/refaktor/rye/loader"
"github.com/refaktor/rye-front/current"
)
/* type TagType int
type RjType int
type Series []any
type anyword struct {
kind RjType
idx int
}
type node struct {
kind RjType
value any
}
var CODE []any */
//
// main function. Dispatches to appropriate mode function
//
func main() {
evaldo.ShowResults = true
if len(os.Args) == 1 {
main_rye_repl(os.Stdin, os.Stdout, false, false)
} else if len(os.Args) == 2 {
main_rye_file(os.Args[1], false, false)
}
}
func main_rye_file(file string, sig bool, subc bool) {
info := true
//util.PrintHeader()
//defer profile.Start(profile.CPUProfile).Stop()
bcontent, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
content := string(bcontent)
if info {
pattern := regexp.MustCompile(`^; (#[^\n]*)`)
lines := pattern.FindAllStringSubmatch(content, -1)
for _, line := range lines {
if line[1] != "" {
fmt.Println(line[1])
}
}
}
block, genv := loader.LoadString(content, sig)
switch val := block.(type) {
case env.Block:
es := env.NewProgramState(block.(env.Block).Series, genv)
evaldo.RegisterBuiltins(es)
contrib.RegisterBuiltins(es, &evaldo.BuiltinNames) // TODO -- remove this in next Rye release
current.RegisterBuiltins(es)
if subc {
ctx := es.Ctx
es.Ctx = env.NewEnv(ctx)
}
evaldo.EvalBlock(es)
evaldo.MaybeDisplayFailureOrError(es, genv)
case env.Error:
fmt.Println(val.Message)
}
}
func main_rye_repl(_ io.Reader, _ io.Writer, subc bool, here bool) {
input := " 123 " // "name: \"Rye\" version: \"0.011 alpha\""
// userHomeDir, _ := os.UserHomeDir()
// profile_path := filepath.Join(userHomeDir, ".rye-profile")
fmt.Println("Welcome to Rye shell. Use ls and ls\\ \"pr\" to list the current context.")
//if _, err := os.Stat(profile_path); err == nil {
//content, err := os.ReadFile(profile_path)
//if err != nil {
// log.Fatal(err)
//}
// input = string(content)
//} else {
// fmt.Println("There was no profile.")
//}
block, genv := loader.LoadString(input, false)
es := env.NewProgramState(block.(env.Block).Series, genv)
evaldo.RegisterBuiltins(es)
contrib.RegisterBuiltins(es, &evaldo.BuiltinNames) // TODO -- remove this in next Rye release
current.RegisterBuiltins(es)
evaldo.EvalBlock(es)
if subc {
ctx := es.Ctx
es.Ctx = env.NewEnv(ctx) // make new context with no parent
}
if here {
if _, err := os.Stat(".rye-here"); err == nil {
content, err := os.ReadFile(".rye-here")
if err != nil {
log.Fatal(err)
}
inputH := string(content)
block, genv := loader.LoadString(inputH, false)
block1 := block.(env.Block)
es = env.AddToProgramState(es, block1.Series, genv)
evaldo.EvalBlock(es)
} else {
fmt.Println("There was no `here` file.")
}
}
evaldo.DoRyeRepl(es, evaldo.ShowResults)
}