-
Notifications
You must be signed in to change notification settings - Fork 0
/
stitch.go
75 lines (62 loc) · 1.45 KB
/
stitch.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
package stitch
import (
"bytes"
"fmt"
// "encoding/json"
"io"
// "os"
"github.com/nirosys/stitch/analysis"
"github.com/nirosys/stitch/ast"
"github.com/nirosys/stitch/parsing"
)
const stitchVersion = "v0.0.1"
func Version() string {
return stitchVersion
}
// Program ////////////////////////////////////////////////////////////////////
type Program struct {
Tree *ast.ASTree
Symbols *analysis.SymbolTable
}
func NewProgram(r io.Reader) *Program {
parser := parsing.NewParser(r)
tree := parser.Parse()
prog := &Program{Tree: tree}
if symbols, err := analysis.Analyze(tree); err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
} else {
prog.Symbols = symbols
}
//enc := json.NewEncoder(os.Stdout)
//enc.SetIndent("", " ")
//enc.Encode(tree)
return prog
}
func ExtendProgram(prog *Program, r io.Reader) (*Program, error) {
parser := parsing.NewParser(r)
tree := parser.Parse()
if symbols, err := analysis.AnalyzeWithSymbols(tree, prog.Symbols); err == nil {
if prog.Tree != nil {
tree.Statements = append(prog.Tree.Statements, tree.Statements...)
}
newProg := &Program{
Tree: tree,
Symbols: symbols,
}
return newProg, nil
} else {
return nil, err
}
}
func (p *Program) String() string {
var buffer bytes.Buffer
stmts := p.Tree.Statements
for _, stmt := range stmts {
buffer.WriteString(stmt.String())
buffer.WriteString(";\n")
}
return buffer.String()
}
func (p *Program) Errors() []string {
return []string{}
}