-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
199 lines (178 loc) · 3.63 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"errors"
"fmt"
"io/ioutil"
"strconv"
)
var source []byte
var sourceIndex int = 0
func getChar() (byte, error) {
if sourceIndex == len(source) {
return 0, errors.New("EOF")
}
char := source[sourceIndex]
sourceIndex++
return char, nil
}
func ungetChar() {
sourceIndex--
}
type Token struct {
kind string // "intliteral", "punct"
value string
}
func readNumber(char byte) string {
number := []byte{char}
for {
char, err := getChar()
if err != nil {
break
}
if '0' <= char && char <= '9' {
number = append(number, char)
} else {
ungetChar()
break
}
}
return string(number)
}
func tokenize() []*Token {
var tokens []*Token
fmt.Printf("# Tokens : ")
for {
char, err := getChar()
if err != nil {
break
}
switch char {
case ' ', '\t', '\n':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
intliteral := readNumber(char)
token := &Token{
kind: "intliteral",
value: intliteral,
}
tokens = append(tokens, token)
fmt.Printf(" '%s'", token.value)
case ';', '+', '-', '*', '/':
token := &Token{
kind: "punct",
value: string([]byte{char}),
}
tokens = append(tokens, token)
fmt.Printf(" '%s'", token.value)
default:
panic(fmt.Sprintf("tokenizer: Invalid char: '%c'", char))
}
}
fmt.Printf("\n")
return tokens
}
var tokens []*Token
var tokenIndex int = 0
func getToken() *Token {
if tokenIndex == len(tokens) {
return nil
}
token := tokens[tokenIndex]
tokenIndex++
return token
}
type Expr struct {
kind string // "intliteral", "unary"
intval int // for intliteral
operator string // "-", "+", ...
operand *Expr // for unary expr
left *Expr // for binary expr
right *Expr // for binary expr
}
func parseUnaryExpr() *Expr {
token := getToken()
switch token.kind {
case "intliteral":
intval, err := strconv.Atoi(token.value)
if err != nil {
panic(err)
}
return &Expr{
kind: "intliteral",
intval: intval,
}
case "punct":
return &Expr{
kind: "unary",
operator: token.value,
operand: parseUnaryExpr(),
}
default:
panic("Unexpected token")
}
}
func parse() *Expr {
expr := parseUnaryExpr()
for {
token := getToken()
if token == nil || token.value == ";" {
return expr
}
switch token.value {
case "+", "-", "*", "/":
return &Expr{
kind: "binary",
operator: token.value,
left: expr,
right: parseUnaryExpr(),
}
default:
panic("unexpected token:" + token.value)
}
}
}
func generateExpr(expr *Expr) {
switch expr.kind {
case "intliteral":
fmt.Printf(" movq $%d, %%rax\n", expr.intval)
case "unary":
switch expr.operator {
case "-":
fmt.Printf(" movq $-%d, %%rax\n", expr.operand.intval)
case "+":
fmt.Printf(" movq $%d, %%rax\n", expr.operand.intval)
default:
panic("generator: Unknown unary operator:" + expr.operator)
}
case "binary":
fmt.Printf(" movq $%d, %%rax\n", expr.left.intval)
fmt.Printf(" movq $%d, %%rcx\n", expr.right.intval)
switch expr.operator {
case "+":
fmt.Printf(" addq %%rcx, %%rax\n")
case "-":
fmt.Printf(" subq %%rcx, %%rax\n")
case "*":
fmt.Printf(" imulq %%rcx, %%rax\n")
case "/":
fmt.Printf(" movq $0, %%rdx\n")
fmt.Printf(" idiv %%rcx\n")
default:
panic("generator: Unknown binary operator:" + expr.operator)
}
default:
panic("generator: Unknown expr.kind:" + expr.kind)
}
}
func generateCode(expr *Expr) {
fmt.Printf(" .global main\n")
fmt.Printf("main:\n")
generateExpr(expr)
fmt.Printf(" ret\n")
}
func main() {
source, _ = ioutil.ReadFile("/dev/stdin")
tokens = tokenize()
expr := parse()
generateCode(expr)
}