-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
71 lines (60 loc) · 1.49 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
//go:build !wasm
// +build !wasm
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"unicode/utf8"
"github.com/Suryansh-23/amrit/evaluator"
"github.com/Suryansh-23/amrit/lexer"
"github.com/Suryansh-23/amrit/object"
"github.com/Suryansh-23/amrit/parser"
"github.com/Suryansh-23/amrit/repl"
)
const EXT = ".amr"
func main() {
out := os.Stdout
in := os.Stdin
if len(os.Args) == 1 {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is the Amrit Programming Language!\n",
user.Username)
fmt.Printf("Feel free to type in commands\n")
repl.Start(in, out)
} else {
fpath := os.Args[1]
if filepath.Ext(fpath) != EXT {
io.WriteString(out, fmt.Sprintf("the file type is invalid: `%s`, must be of `.amr` filetype\n", fpath))
return
}
progScript, err := ioutil.ReadFile(fpath)
if err != nil {
io.WriteString(out, fmt.Sprintf("the following error occured while opening %s:\n\t%s", fpath, err.Error()))
return
}
if !utf8.Valid(progScript) {
io.WriteString(out, fmt.Sprintf("the file `%s` is not encoded in utf-8 format, try resaving the file again.\n", fpath))
return
}
env := object.NewEnvironment()
l := lexer.New(string(progScript))
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
repl.PrintParserErrors(out, p.Errors())
return
}
stdout := []string{}
evaluator.Eval(program, env, &stdout)
for _, s := range stdout {
io.WriteString(out, s)
}
}
}