-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (65 loc) · 2.09 KB
/
Copy pathmain.go
File metadata and controls
80 lines (65 loc) · 2.09 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
package main
import (
"bufio"
"fmt"
"go/interpreter/evaluator"
"go/interpreter/lexer"
"go/interpreter/object"
"go/interpreter/parser"
"go/interpreter/repl"
"os"
"os/user"
)
func main() {
// Check if the user has provided a file path as an argument
if len(os.Args) > 2 {
// If the user has provided more than one argument, print a message and exit
fmt.Printf("Either run this program without arguments or with the a valid input file\n")
os.Exit(0)
} else if len(os.Args) == 2 {
// If the user has provided a file path, run the interpreter
filePath := os.Args[1]
// check for right file extension
if filePath[len(filePath)-7:] != ".turtls" {
fmt.Printf("Please provide a file with the .turtls extension\n")
os.Exit(0)
}
// Open the file
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(file)
// Create a variable to store the file content
var fileContent string
// Iterate over each line and concatenate it to the fileContent string
for scanner.Scan() {
fileContent += scanner.Text() + "\n"
}
// Check for scanner errors
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return
}
env := object.NewEnvironment()
l := lexer.New(fileContent)
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
p.PrintParserErrors(os.Stdout)
}
evaluator.Eval(program, env)
os.Exit(0)
}
// If the user has not provided any arguments, start the REPL
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! Welcome to TurtlScript!\n", user.Username)
fmt.Printf("Type .quit to quit.\n")
repl.Start(os.Stdin, os.Stdout)
}