-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (63 loc) · 1.22 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
package main
import (
"errors"
"fmt"
"github.com/Karocyt/expertsystem/internal/lexer"
"github.com/Karocyt/expertsystem/internal/solver"
"io/ioutil"
"os"
)
func getInput() (string, error) {
var s string
if len(os.Args) == 2 {
buf, e := ioutil.ReadFile(os.Args[1])
s = string(buf)
return s, e
} else if len(os.Args) > 2 {
return s, errors.New(fmt.Sprintf("Usage: %s {filename}", os.Args[0]))
} else {
return s, errors.New(fmt.Sprintf("Stdin mode ? Not Today.\nUsage: %s {filename}", os.Args[0]))
}
return s, nil
}
func print_result(b *solver.Builder) (e error) {
for _, s := range b.Queries {
val, e := b.Eval_rules(s)
if e != nil {
return e
} else {
fmt.Printf("Query results:\n\t%s = %t\n", s, val)
}
}
return e
}
func mainfunc() int {
content, e := getInput()
if e != nil {
fmt.Println(e)
return 1
}
l := lexer.New(content, os.Args[1])
if l.Error != nil {
fmt.Println(e)
return 1
}
s, e := solver.New(l.Tokens)
if l.Error != nil {
e = l.Error
}
if e != nil {
fmt.Println(e)
return 1
}
//fmt.Println("\nQueries:\t", s.Queries, "\nVariables:\t", s.Variables)
e = print_result(&s)
if e != nil {
fmt.Println(e)
return 1
}
return 0
}
func main() {
os.Exit(mainfunc())
}