/
decl.go
52 lines (43 loc) · 1.05 KB
/
decl.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 ir
import (
"fmt"
"github.com/rthornton128/calc/ast"
)
type Declaration struct {
object
Body Object
Params []string
}
func MakeDeclaration(d *ast.DeclExpr, parent *Scope) *Declaration {
scope := NewScope(parent)
params := make([]string, len(d.Params))
for i, p := range d.Params {
params[i] = p.Name
scope.Insert(MakeParam(p, scope))
}
return &Declaration{
object: newObject(d.Name.Name, d.Type.Name, d.Pos(), ast.FuncDecl, scope),
Params: params,
Body: MakeExpr(d.Body, scope),
}
}
func (d *Declaration) String() string {
var out string
for _, s := range d.Params {
out += d.scope.Lookup(s).String()
}
return fmt.Sprintf("decl {%s %s (%s) %s}", d.name, d.typ, out, d.Body)
}
type Param struct {
object
id int
}
func MakeParam(p *ast.Ident, parent *Scope) *Param {
return &Param{object: newObject(p.Name, p.Type.Name, p.Pos(), ast.VarDecl,
parent)}
}
func (p *Param) ID() int { return p.id }
func (p *Param) SetID(id int) { p.id = id }
func (p *Param) String() string {
return fmt.Sprintf("{%s %s}", p.name, p.typ)
}