forked from octacian/timecalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timecalc.go
86 lines (72 loc) · 1.65 KB
/
timecalc.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
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/nsf/termbox-go"
"os"
"regexp"
)
// DEBUG defines whether extra verbose information is printed
var DEBUG bool
// PrintDebug prints a string only if debug mode is enabled
func PrintDebug(str string) {
if DEBUG {
fmt.Print(str)
}
}
var OperatorRegex = regexp.MustCompile(`^(\+|\-|\*|\/|\%)$`)
var TimeRegex = regexp.MustCompile(`^([\d][\d]?)?:([\d][\d])?:?(?::([\d][\d]))?(?:\.([\d]+))?$`)
var NumberRegex = regexp.MustCompile(`^((\d+)?\.?)\d+$`)
var WhitespaceRegex = regexp.MustCompile(`^([\s])$`)
func main() {
if os.Getenv("DEBUG") == "TRUE" {
DEBUG = true
fmt.Println("Debug mode enabled.")
}
if err := termbox.Init(); err != nil {
panic(err)
}
termbox.SetInputMode(termbox.InputEsc)
for {
reader := bufio.NewReader(os.Stdin)
color.Set(color.FgCyan)
fmt.Print(">>> ")
termLoop:
for {
switch event := termbox.PollEvent(); event.Type {
case termbox.EventKey:
if event.Key == termbox.KeyArrowUp {
} else if event.Key == termbox.KeyArrowDown {
} else if event.Key == termbox.KeyEnter {
panic("")
//break termLoop
}
case termbox.EventError:
panic(event.Err)
case termbox.EventInterrupt:
break termLoop
}
}
text, _ := reader.ReadString('\n')
color.Unset()
tokens, err := Tokenize(text)
if err != nil {
fmt.Print(err, "\n")
} else {
if DEBUG {
fmt.Println(Reconstruct(tokens, true))
}
instructions, err := Parse(tokens)
if DEBUG {
PrintInstructions(instructions)
fmt.Println()
}
if err != nil {
fmt.Print(err, "\n")
} else {
fmt.Println(Compile(instructions))
}
}
}
}