-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (75 loc) · 1.62 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
package main
import (
e "aura/src/evaluator"
l "aura/src/lexer"
obj "aura/src/object"
p "aura/src/parser"
"aura/src/repl"
"fmt"
"os"
"path/filepath"
)
// validate that given path exists, have a file and the extension
// is .aura
func validatePath(path string) error {
fileInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("la ruta %s no existe", path)
}
if fileInfo.IsDir() {
return fmt.Errorf("la ruta indicada no contiene un archivo: %s", path)
}
if filepath.Ext(path) != ".aura" {
return fmt.Errorf(
"el archivo %s no es una archivo aura valido",
filepath.Base(path),
)
}
return nil
}
// read the file in the path and evaluate the file
func ReadFile(path string) {
defer func() {
// we handle a posible panic in the parser
// and the evaluator
if r := recover(); r != nil {
fmt.Printf("Error: %s", r)
return
}
}()
source, err := os.ReadFile(path)
if err != nil {
fmt.Println("No se pudo leer el archivo")
return
}
if len(source) == 0 {
return
}
lexer := l.NewLexer(string(source))
parser := p.NewParser(lexer)
env := obj.NewEnviroment(nil)
program := parser.ParseProgam()
if len(parser.Errors()) > 0 {
for _, err := range parser.Errors() {
fmt.Println(err)
}
// we dont evaluate the program if it has syntax errors
return
}
evaluated := e.Evaluate(program, env)
if evaluated != nil && evaluated != obj.SingletonNUll {
fmt.Println(evaluated.Inspect())
}
}
func main() {
if len(os.Args) < 2 {
repl.StartRpl()
return
}
filePath := os.Args[1]
if err := validatePath(filePath); err != nil {
fmt.Println(err.Error())
return
}
ReadFile(filePath)
}