-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
58 lines (46 loc) · 1.31 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
package main
import (
"log"
"os"
"gismolang.org/compiler/config"
"gismolang.org/compiler/interpreter"
"gismolang.org/compiler/parser"
"gismolang.org/compiler/tokenizer"
"gismolang.org/compiler/tokenizer/tokentype"
)
func main() {
file := "ENVIRONMENT"
code := os.Getenv("GISMO_CODE")
config.OutputEnabled = os.Getenv("NO_OUT") == ""
if code == "" {
// Ensure a file argument is passed
if len(os.Args) < 2 {
log.Fatal("Usage: gismo <file-path>")
}
// Read file content
file = os.Args[1]
text, err := os.ReadFile(file)
if err != nil {
log.Fatalf("Failed to read file '%s': %v", file, err)
}
code = string(text)
}
before, _ := os.ReadFile(config.BeforePath)
after, _ := os.ReadFile(config.AfterPath)
// Tokenize the file content
tokens := []*tokenizer.Token{}
if before != nil {
tokens = append(tokens, tokenizer.Tokenize(string(before), file)...)
tokens = append(tokens, &tokenizer.Token{TokenType: tokentype.Newline,})
}
tokens = append(tokens, tokenizer.Tokenize(code, file)...)
if after != nil {
tokens = append(tokens, &tokenizer.Token{TokenType: tokentype.Newline,})
tokens = append(tokens, tokenizer.Tokenize(string(after), file)...)
}
// Parse the tokens into an AST
ast := parser.Parse(tokens, file)
config.Init()
defer config.Deinit()
interpreter.Interpret(ast)
}