-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcomp.go
52 lines (42 loc) · 935 Bytes
/
comp.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
package dsl
import (
"fmt"
"os"
)
type Compiler struct {
err string
ast Node
codegen CodeGenerator
}
func NewCompiler() *Compiler {
return &Compiler{}
}
func (c *Compiler) SetAstRoot(root Node) {
c.ast = root
}
func (c *Compiler) Parse(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
ret := yyParse(NewLexerWithInit(file, func(y *Lexer) { y.p = c }))
// nex creates the function: func NewLexerWithInit(in io.Reader, initFun func(*Lexer)) *Lexer
// go tool yacc creates the function: func yyParse(yylex yyLexer) int
if ret == 1 {
return fmt.Errorf(filename + c.err)
}
return nil
}
func (c *Compiler) PlotAst(filename string) error {
err := plot(c.ast, filename)
if err != nil {
return err
}
err = open(filename)
return err
}
func (c *Compiler) GenerateCode() error {
c.codegen = NewAsmCodeGenerator()
err := c.codegen.CompTopScope(c.ast)
return err
}