begin ir emit

This commit is contained in:
Robin 2025-01-21 06:07:33 +01:00
parent c8d4e1c0a7
commit 8f115f8c1a
2 changed files with 22 additions and 1 deletions

1
ttir/emit.go Normal file
View File

@ -0,0 +1 @@
package ttir

View File

@ -1,5 +1,10 @@
package ttir
import (
"fmt"
"strings"
)
type Program struct {
Functions []Function
}
@ -9,6 +14,16 @@ type Function struct {
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 {
String() string
instruction()
@ -18,7 +33,9 @@ type Ret struct {
op Operand
}
func (r *Ret) String() {}
func (r *Ret) String() string {
return fmt.Sprintf("ret %s\n", r.op)
}
func (r *Ret) instruction() {}
type Operand interface {
@ -30,4 +47,7 @@ type Constant struct {
Value int64
}
func (c *Constant) String() string {
return fmt.Sprintf("%d", c.Value)
}
func (c *Constant) operand() {}