-
Notifications
You must be signed in to change notification settings - Fork 2
/
compiler.go
165 lines (157 loc) · 3.33 KB
/
compiler.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
package gosp
import (
"bufio"
"fmt"
"io"
"strings"
)
// KeyValue is a simple hash type
type KeyValue [2]string
// Compiler directives such as import and type declarations
type Directive struct {
Imports []string
Params []KeyValue
}
var printedMeta = false
// Compile operates on the steam provided by in, writing output to out
// funcName and packageName define the package that the generated code should
// inhabit. funcName determines name of the generated closure.
// By default for a/{b,c}.go packageName is "c" and funcName is "A", and "B".
func Compile(in io.Reader, out io.Writer, funcName, packageName string) {
reader := bufio.NewReader(in)
directive := Directive{}
//process all directives at beginning of file
for {
//check for line beginnig with '@'
peekaboo, err := reader.Peek(1)
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
if peekaboo[0] != '@' {
break
}
//found line, read it in and process it.
line, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
if strings.HasPrefix(line, "@import") {
directive.Imports = append(directive.Imports, line[1:])
continue
}
if strings.HasPrefix(line, "@(") {
line = line[2 : len(line)-2]
for _, parameter := range strings.Split(line, ",") {
parameter := strings.Trim(parameter, " ")
kv := strings.Split(parameter, " ")
param := KeyValue{kv[0], kv[1]}
directive.Params = append(directive.Params, param)
}
continue
}
}
data := make([]byte, 1)
LITERAL_OUTPUT := 0
CODE := 1
WAITINGFORBEGINCODE := 2
WAITINGFORENDCODE := 3
state := LITERAL_OUTPUT
fmt.Fprintf(out,
`package %s
import "fmt"
import "io"
`, packageName)
for _, pkg := range directive.Imports {
fmt.Fprintf(out, "%s\n", pkg)
}
if !printedMeta {
printedMeta = true
out.Write([]byte(`
var _ fmt.Stringer
type Template func(io.Writer)
`))
}
var params string
for _, param := range directive.Params {
params += "," + param[0] + " " + param[1]
}
if len(params) == 0 {
params = ""
} else {
params = params[1:]
}
fmt.Fprintf(out,
`func %s(%s) (func(io.Writer)) {
return func(output io.Writer) {
output.Write([]byte(%s`,
funcName,
params,
"`",
)
counter := 0
expressionFlag := 0
OUTER:
for {
n, err := reader.Read(data)
switch err {
case io.EOF:
break OUTER
case nil:
break
default:
panic(err)
}
if n != len(data) {
panic("short read")
}
char := data[0]
counter++
switch state {
case LITERAL_OUTPUT:
if char == '<' {
state = WAITINGFORBEGINCODE
counter = 0
} else {
out.Write([]byte{char})
}
case CODE:
if char == '%' {
state = WAITINGFORENDCODE
counter = 0
} else if char == '=' && counter == 1 {
expressionFlag = 1
out.Write([]byte(`fmt.Fprintf(output, "%v",`))
} else {
out.Write([]byte{char})
}
case WAITINGFORBEGINCODE:
if char == '%' {
state = CODE
counter = 0
out.Write([]byte("`))\n"))
} else {
out.Write([]byte{'<', char})
state = LITERAL_OUTPUT
counter = 0
}
case WAITINGFORENDCODE:
if char == '>' {
state = LITERAL_OUTPUT
counter = 0
if expressionFlag == 1 {
expressionFlag = 0
out.Write([]byte(")"))
}
out.Write([]byte("\noutput.Write([]byte(`"))
} else {
out.Write([]byte{'%'})
state = CODE
counter = 0
}
}
}
out.Write([]byte("`))\n}\n}\n"))
}