-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (61 loc) · 1.64 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
package main
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/mochatek/frolang/evaluator"
"github.com/mochatek/frolang/lexer"
"github.com/mochatek/frolang/object"
"github.com/mochatek/frolang/parser"
"github.com/mochatek/frolang/repl"
)
var RESET = "\033[0m"
var RED = "\033[31m"
var GREEN = "\033[32m"
func main() {
// Windows doesn't natively support color in cmd
if runtime.GOOS == "windows" {
RESET = ""
RED = ""
GREEN = ""
}
// If source file path was not passed, then start the REPL
if len(os.Args) == 1 {
repl.Start(os.Stdin, os.Stdout)
return
}
// Read source code from the file into a string
filePath := os.Args[1]
if parts := strings.Split(filePath, "."); strings.ToLower(parts[len(parts)-1]) != "fro" {
fmt.Printf("%sSCRIPT ERROR: %s is not a valid FroLang script.\n\tFile extension should be: .fro%s\n", RED, filePath, RESET)
return
}
contentBytes, err := os.ReadFile(filePath)
if err != nil {
fmt.Printf("%sSCRIPT ERROR: %s%s\n", RED, err, RESET)
return
}
sourceCode := string(contentBytes)
// Parse the program
lex := lexer.New(sourceCode)
par := parser.New(lex)
program := par.ParseProgram()
// Evaluate the AST if there was no errors. Else show errors
if len(par.Errors()) != 0 {
for _, message := range par.Errors() {
fmt.Printf("%sPARSE ERROR: %s%s\n", RED, message, RESET)
}
} else {
env := object.NewEnvironment()
result := evaluator.Eval(program, env)
// Show errors/result if any
if result != nil {
if result.Type() == object.ERROR_OBJ {
fmt.Printf("%s%s%s\n", RED, result.Inspect(), RESET)
} else {
fmt.Printf("%s%s%s\n", GREEN, result.Inspect(), RESET)
}
}
}
}