This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bee.go
117 lines (101 loc) · 2.21 KB
/
bee.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
package main
import (
"flag"
"fmt"
"os"
)
const (
// The symbol for built-ins
BUILT_IN = '@'
// Splits 'lines'
SPLITTER = ';'
// Sections
SECTION_GOTO = '^'
SECTION_DEF = '#'
BLOCKING = '!'
// Commenting
COMMENT_START = "buzz"
// Disposable, result
DISPOSABLE = string(BUILT_IN) + ":disposable"
RESULT = string(BUILT_IN) + ":result"
// Symbols that cannot be used in object names
INVALID_SYMBOLS = "{}[]-=+/\\,.<>:;()!\"£$%^&*|?~'@ "
// Define, set, func_in
SET, FUNC_IN = '<', ':'
// Tuple, func
TUPLE_B, FUNC_B = "()", "[]"
)
var (
// Arguments provided through the command-line
fileName string
codeString string
generateGlobal bool
// All code
program string
)
func read(fn string) string {
if raw, err := os.ReadFile(fn); err != nil {
fmt.Printf("'%s' does not exist or missing access.\n", fn)
os.Exit(1)
return ""
} else {
return string(raw)
}
}
func start(parsed [][]*Token) {
for r := 0; r < len(parsed); r++ {
if pointer, err := Run(parsed[r]); err != nil {
fmt.Println(err.Error())
os.Exit(1)
} else if pointer == -1 {
for _, g := range global {
switch c := g.Data.(type) {
case int:
if c == -1 {
g.Data = r
}
}
}
} else if pointer == -2 {
continue
} else {
// regenerate (otherwise you encounter some.. strange stuff)
parsed, _ = GenerateTokens(program)
r = pointer
}
}
}
func main() {
// Get the code
if codeString != "" {
program = codeString
} else {
if program == "" {
if fileName == "" {
fmt.Println("No input provided.")
os.Exit(1)
} else {
program = read(fileName)
}
}
}
// Parse code
if parsed, err := GenerateTokens(program); err != nil {
fmt.Println(err.Error())
os.Exit(1)
} else {
start(parsed)
}
if generateGlobal {
for id, object := range global {
fmt.Printf("ID: %d. SYMBOL: '%s'. DATA: '%+s'\n", id, object.Symbol, object.Data)
}
}
}
func init() {
// parse flags
flag.StringVar(&fileName, "filename", "", "Specifies the input file.")
flag.StringVar(&codeString, "code", "", "When used, runs the code provided.")
flag.BoolVar(&generateGlobal, "final", false, "Generates a list of all final objects - useful for debugging.")
flag.Parse()
}