-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (92 loc) · 2.58 KB
/
main.go
File metadata and controls
104 lines (92 loc) · 2.58 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
customCommand "github.com/megaboy2/GoExpressionLanguage/custom_command"
"github.com/megaboy2/GoExpressionLanguage/evaluator"
)
var supportedParsers = []evaluator.UserInputParser{
evaluator.MathematicalParser{},
evaluator.NumberBaseSystemParser{},
}
func getEvaluator(input string, context evaluator.Context) evaluator.Evaluator {
for _, parser := range supportedParsers {
evaluator := parser.Parse(input, context)
if evaluator != nil {
return evaluator
}
}
return nil
}
func getNumbers(bound int) string {
var res []string
if bound <= 10 {
for index := 48; index <= 48+bound-1; index++ {
res = append(res, string(rune(index)))
}
} else {
for index := 48; index <= 57; index++ {
res = append(res, string(rune(index)))
}
for index := 65; index <= 65+bound-11; index++ {
res = append(res, string(rune(index)))
}
}
return strings.Join(res, " ")
}
func printSupportedInputNumbers(inputBaseSystem string) {
inputBaseSystemNumber, err := strconv.Atoi(inputBaseSystem)
if err != nil {
fmt.Printf("Unknown input base system: %s. Expected number.\n", inputBaseSystem)
} else {
fmt.Printf("The following symbols will be acceppted as an input base system: %s\n", getNumbers(inputBaseSystemNumber))
}
}
func printSupportedOutputNumbers(outputBaseSystem string) {
inputBaseSystemNumber, err := strconv.Atoi(outputBaseSystem)
if err != nil {
fmt.Printf("Unknown output base system: %s. Expected number.\n", outputBaseSystem)
} else {
fmt.Printf("The following symbols will be used as an output base system: %s\n", getNumbers(inputBaseSystemNumber))
}
}
func readCustomInput(input string, context evaluator.Context) {
splittedUserInput := strings.Fields(input)
if splittedUserInput[0] == "help" {
customCommand.NewHelp().PrintHelp()
} else if len(splittedUserInput) == 2 {
if splittedUserInput[0] == "ibase" {
printSupportedInputNumbers(splittedUserInput[1])
}
if splittedUserInput[0] == "obase" {
printSupportedOutputNumbers(splittedUserInput[1])
}
context.AddExecutionVariable(splittedUserInput[0], splittedUserInput[1])
}
}
func main() {
reader := bufio.NewReader(os.Stdin)
context := evaluator.NewContext()
for true {
fmt.Print("> ")
input, _ := reader.ReadString('\n')
input = strings.Trim(input, "\r\n")
if input == "exit" {
break
}
evaluator := getEvaluator(input, context)
if evaluator == nil {
readCustomInput(input, context)
continue
}
err := evaluator.Validate(context)
if err != nil {
fmt.Println(err)
continue
}
evaluator.Eval(context)
}
}