-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (97 loc) · 2.33 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"path/filepath"
"strconv"
"tkom/ast"
"tkom/interpreter"
"tkom/lexer"
"tkom/parser"
)
const (
IDENTIFIERLIMIT = 500
STRING_LIMIT = 1000
INT_LIMIT = math.MaxInt
MAX_RECURSION_DEPTH = 200
)
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Fprintf(os.Stderr, "%v\n", r)
}
}()
if len(os.Args) < 2 {
fmt.Println("Missing parameter, provide file name and arguments or use '-' to run from stream")
return
}
var program *ast.Program
var err error
if len(os.Args) >= 2 && os.Args[1] == "-" {
program, err = parseProgramFromStdin()
} else {
fileName := os.Args[1]
ext := filepath.Ext(fileName)
if ext != ".fl" {
log.Fatal("File must have '.fl' extension")
os.Exit(1)
}
program, err = parseProgramFromFile(fileName)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
visitor := interpreter.NewCodeVisitor(MAX_RECURSION_DEPTH)
visitor.MaxRecursionDepth = MAX_RECURSION_DEPTH
for _, fd := range program.Functions {
visitor.FunctionsMap[fd.Name] = fd
}
arguments := os.Args[2:]
functionCallArgs := make([]ast.Expression, len(arguments))
for i, arg := range arguments {
if intValue, err := strconv.Atoi(arg); err == nil {
functionCallArgs[i] = &ast.IntExpression{Value: intValue}
} else {
functionCallArgs[i] = &ast.StringExpression{Value: arg}
}
}
functionCall := &ast.FunctionCall{
Name: "main",
Arguments: functionCallArgs,
}
visitor.VisitFunctionCall(functionCall)
}
func parseProgramFromFile(fileName string) (*ast.Program, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer func() {
if r := recover(); r != nil {
file.Close()
fmt.Fprintf(os.Stderr, "%v\n", r)
os.Exit(1)
}
}()
reader := bufio.NewReader(file)
return parseProgram(reader)
}
func parseProgramFromStdin() (*ast.Program, error) {
reader := bufio.NewReader(os.Stdin)
return parseProgram(reader)
}
func parseProgram(reader *bufio.Reader) (*ast.Program, error) {
source, _ := lexer.NewScanner(reader)
lex := lexer.NewLexer(source, IDENTIFIERLIMIT, STRING_LIMIT, INT_LIMIT)
errorHandler := func(err error) {
panic(err)
}
lex.ErrorHandler = errorHandler
parser := parser.NewParser(lex, errorHandler)
program := parser.ParseProgram()
return program, nil
}