mirror of
https://github.com/RoBaertschi/tt.git
synced 2025-04-18 23:13:29 +00:00
begin ir emit
This commit is contained in:
parent
c8d4e1c0a7
commit
8f115f8c1a
1
ttir/emit.go
Normal file
1
ttir/emit.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package ttir
|
22
ttir/ttir.go
22
ttir/ttir.go
@ -1,5 +1,10 @@
|
|||||||
package ttir
|
package ttir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type Program struct {
|
type Program struct {
|
||||||
Functions []Function
|
Functions []Function
|
||||||
}
|
}
|
||||||
@ -9,6 +14,16 @@ type Function struct {
|
|||||||
Instructions []Instruction
|
Instructions []Instruction
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Function) String() string {
|
||||||
|
var builder strings.Builder
|
||||||
|
builder.WriteString(fmt.Sprintf("fn %s\n", f.Name))
|
||||||
|
for _, i := range f.Instructions {
|
||||||
|
builder.WriteString(" ")
|
||||||
|
builder.WriteString(i.String())
|
||||||
|
}
|
||||||
|
return builder.String()
|
||||||
|
}
|
||||||
|
|
||||||
type Instruction interface {
|
type Instruction interface {
|
||||||
String() string
|
String() string
|
||||||
instruction()
|
instruction()
|
||||||
@ -18,7 +33,9 @@ type Ret struct {
|
|||||||
op Operand
|
op Operand
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Ret) String() {}
|
func (r *Ret) String() string {
|
||||||
|
return fmt.Sprintf("ret %s\n", r.op)
|
||||||
|
}
|
||||||
func (r *Ret) instruction() {}
|
func (r *Ret) instruction() {}
|
||||||
|
|
||||||
type Operand interface {
|
type Operand interface {
|
||||||
@ -30,4 +47,7 @@ type Constant struct {
|
|||||||
Value int64
|
Value int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Constant) String() string {
|
||||||
|
return fmt.Sprintf("%d", c.Value)
|
||||||
|
}
|
||||||
func (c *Constant) operand() {}
|
func (c *Constant) operand() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user