-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (47 loc) · 1.02 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/yuniruyuni/lang/ast"
"github.com/yuniruyuni/lang/gen"
"github.com/yuniruyuni/lang/parse"
"github.com/yuniruyuni/lang/token"
)
func outputLL(root ast.AST) string {
ll := gen.LLFile{AST: root}
return string(ll.Generate())
}
func tokenize(code string) ([]*token.Token, error) {
t := token.Tokenizer{}
tks := t.Tokenize(code)
if len(tks) == 0 {
return nil, errors.New("failed to tokenize")
}
return tks, nil
}
func Compile(code string) (string, error) {
tks, err := tokenize(code)
if err != nil {
return "", fmt.Errorf("failed to tokenize code: %s", err.Error())
}
root, err := parse.Parse(tks)
if err != nil {
return "", fmt.Errorf("failed to parse code: %s", err.Error())
}
return outputLL(root), nil
}
func main() {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic("cannot read stdin")
}
code := string(bytes)
ll, err := Compile(code)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(-1)
}
fmt.Println(ll)
}