Improved verbose output

This commit is contained in:
Eduard Urbach 2024-06-27 17:13:48 +02:00
parent 61024bb133
commit d86d411959
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 44 additions and 11 deletions

22
src/build/Debug.go Normal file
View File

@ -0,0 +1,22 @@
package build
import "git.akyoto.dev/cli/q/src/build/token"
type debug struct {
pos int
instruction token.List
}
func (f *Function) debugLine(instructionIndex int) token.List {
for _, record := range f.debug {
if record.pos == instructionIndex {
return record.instruction
}
if record.pos > instructionIndex {
return nil
}
}
return nil
}

View File

@ -6,6 +6,7 @@ import (
"git.akyoto.dev/cli/q/src/build/arch/x64"
"git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/config"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/fs"
@ -24,6 +25,7 @@ type Function struct {
CPU cpu.CPU
Error error
count struct{ loop int }
debug []debug
}
// Compile turns a function into machine code.
@ -59,6 +61,14 @@ func (f *Function) CompileTokens(body token.List) error {
if start != -1 {
instruction := body[start:i]
if config.Verbose {
f.debug = append(f.debug, debug{
pos: len(f.Assembler.Instructions),
instruction: instruction,
})
}
err := f.CompileInstruction(instruction)
if err != nil {
@ -182,11 +192,6 @@ func (f *Function) ExpressionsToRegisters(expressions []*expression.Expression,
return nil
}
func (f *Function) Log(messages ...any) {
fmt.Printf("[%s] ", f.Name)
fmt.Println(messages...)
}
// TokenToRegister moves a token into a register.
// It only works with identifiers, numbers and strings.
func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
@ -227,27 +232,33 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
// PrintAsm shows the assembly instructions.
func (f *Function) PrintAsm() {
ansi.Dim.Println("╭────────────────────────────────────────────────╮")
ansi.Dim.Println("╭──────────────────────────────────────╮")
for i, x := range f.Assembler.Instructions {
instruction := f.debugLine(i)
if instruction != nil {
ansi.Dim.Println("├──────────────────────────────────────┤")
}
for _, x := range f.Assembler.Instructions {
ansi.Dim.Print("│ ")
if x.Mnemonic == asm.LABEL {
ansi.Yellow.Printf("%-46s", x.Data.String()+":")
ansi.Yellow.Printf("%-36s", x.Data.String()+":")
} else {
ansi.Green.Printf("%-8s", x.Mnemonic.String())
if x.Data != nil {
fmt.Printf("%-38s", x.Data.String())
fmt.Printf("%-28s", x.Data.String())
} else {
fmt.Printf("%-38s", "")
fmt.Printf("%-28s", "")
}
}
ansi.Dim.Print(" │\n")
}
ansi.Dim.Println("╰────────────────────────────────────────────────╯")
ansi.Dim.Println("╰──────────────────────────────────────╯")
}
// String returns the function name.