mirror of
https://github.com/RoBaertschi/tt.git
synced 2025-04-16 05:53:30 +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
|
||||
|
||||
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() {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user