Separated compiler into its own package

This commit is contained in:
Eduard Urbach 2024-07-18 10:08:38 +02:00
parent 38043ca12a
commit 61dc691c65
Signed by: eduard
GPG key ID: 49226B848C78F6C8
14 changed files with 199 additions and 167 deletions

72
src/build/core/State.go Normal file
View file

@ -0,0 +1,72 @@
package core
import (
"bytes"
"fmt"
"git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/go/color/ansi"
)
// State is the data structure we embed in each function to preserve compilation State.
type State struct {
Assembler asm.Assembler
Functions map[string]*Function
Err error
scopes []*Scope
registerHistory []uint64
finished chan struct{}
cpu cpu.CPU
count counter
}
// counter stores how often a certain statement appeared so we can generate a unique label from it.
type counter struct {
branch int
data int
loop int
subBranch int
}
// PrintInstructions shows the assembly instructions.
func (s *State) PrintInstructions() {
ansi.Dim.Println("╭──────────────────────────────────────────────────────────────────────────────╮")
for i, x := range s.Assembler.Instructions {
ansi.Dim.Print("│ ")
switch x.Mnemonic {
case asm.LABEL:
ansi.Yellow.Printf("%-44s", x.Data.String()+":")
case asm.COMMENT:
ansi.Dim.Printf("%-44s", x.Data.String())
default:
ansi.Green.Printf("%-12s", x.Mnemonic.String())
if x.Data != nil {
fmt.Printf("%-32s", x.Data.String())
} else {
fmt.Printf("%-32s", "")
}
}
registers := bytes.Buffer{}
used := s.registerHistory[i]
for _, reg := range s.cpu.All {
if used&(1<<reg) != 0 {
registers.WriteString("⬤ ")
} else {
registers.WriteString("◯ ")
}
}
ansi.Dim.Print(registers.String())
ansi.Dim.Print(" │\n")
}
ansi.Dim.Println("╰──────────────────────────────────────────────────────────────────────────────╯")
}