-
Notifications
You must be signed in to change notification settings - Fork 0
/
milho.go
134 lines (106 loc) · 2.71 KB
/
milho.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
package milho
import (
"fmt"
"runtime"
"github.com/danfragoso/milho/compiler"
"github.com/danfragoso/milho/interpreter"
"github.com/danfragoso/milho/mir"
"github.com/danfragoso/milho/parser"
"github.com/danfragoso/milho/tokenizer"
)
var version string
func Version() string {
return fmt.Sprintf("%s_%s:%s", version, runtime.GOARCH, runtime.GOOS)
}
func Run(src string) string {
var ret string
tokens, err := tokenizer.Tokenize(src)
if err != nil {
return fmt.Sprintf("Tokenization error: %s\n", err)
}
ast, err := parser.Parse(tokens)
if err != nil {
return fmt.Sprintf("Parsing error: %s\n", err)
}
expressions, err := interpreter.Run(ast)
for _, expr := range expressions {
ret += "\n" + expr.Value()
}
if err != nil {
ret += fmt.Sprintf("\nEvaluation error: \n%s", err)
}
return ret
}
func RunSession(src string, sess *mir.Session) string {
var ret string
tokens, err := tokenizer.Tokenize(src)
if err != nil {
return fmt.Sprintf("Tokenization error: %s\n", err)
}
ast, err := parser.Parse(tokens)
if err != nil {
return fmt.Sprintf("Parsing error: %s\n", err)
}
expressions, err := interpreter.RunFromSession(ast, sess)
for _, expr := range expressions {
ret += "\n" + expr.Value()
}
if err != nil {
ret += fmt.Sprintf("\nEvaluation error: %s", err)
}
return ret
}
func RunRaw(src string) ([]mir.Expression, error) {
sess := CreateSession()
tokens, err := tokenizer.Tokenize(src)
if err != nil {
return nil, fmt.Errorf("Tokenization error: %s\n", err)
}
ast, err := parser.Parse(tokens)
if err != nil {
return nil, fmt.Errorf("Parsing error: %s\n", err)
}
return interpreter.RunFromSession(ast, sess)
}
func TranspileToJS(src string) (string, error) {
tokens, err := tokenizer.Tokenize(src)
if err != nil {
return "", fmt.Errorf("Tokenization error: %s\n", err)
}
ast, err := parser.Parse(tokens)
if err != nil {
return "", fmt.Errorf("Parsing error: %s\n", err)
}
var combinedSrc string
for _, node := range ast {
tree, err := mir.GenerateMIR(node)
if err != nil {
return "", err
}
combinedSrc += compiler.TranspileJS(tree) + "\n"
}
return combinedSrc, nil
}
func TranspileToLLVM(src string) (string, error) {
tokens, err := tokenizer.Tokenize(src)
if err != nil {
return "", fmt.Errorf("Tokenization error: %s\n", err)
}
ast, err := parser.Parse(tokens)
if err != nil {
return "", fmt.Errorf("Parsing error: %s\n", err)
}
var combinedSrc string
for _, node := range ast {
tree, err := mir.GenerateMIR(node)
if err != nil {
return "", err
}
combinedSrc += compiler.TranspileLLVM(tree) + "\n"
}
return combinedSrc, nil
}
func CreateSession() *mir.Session {
sess, _ := interpreter.CreateSession(&parser.Node{})
return sess
}