Added assembler merging

This commit is contained in:
2023-10-31 14:50:53 +01:00
parent b6be9357bf
commit 5c12992fca
6 changed files with 122 additions and 63 deletions

View File

@ -1,7 +1,35 @@
package build
import "git.akyoto.dev/cli/q/src/token"
import (
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/linux"
"git.akyoto.dev/cli/q/src/register"
"git.akyoto.dev/cli/q/src/token"
)
// Function represents a function.
type Function struct {
Tokens token.List
Name string
Head token.List
Body token.List
Assembler asm.Assembler
}
// Compile turns a function into machine code.
func (f *Function) Compile() {
for i, t := range f.Body {
if t.Kind == token.Identifier && t.String() == "print" {
message := f.Body[i+2].Bytes
f.Assembler.MoveRegisterNumber(register.Syscall0, linux.Write)
f.Assembler.MoveRegisterNumber(register.Syscall1, 1)
f.Assembler.MoveRegisterData(register.Syscall2, message)
f.Assembler.MoveRegisterNumber(register.Syscall3, uint64(len(message)))
f.Assembler.Syscall()
}
}
}
// String returns the function name.
func (f *Function) String() string {
return f.Name
}