Added main prefix

This commit is contained in:
Eduard Urbach 2024-07-30 20:02:55 +02:00
parent 323952f4bc
commit 0c1b57b4e4
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
9 changed files with 34 additions and 20 deletions

View File

@ -4,6 +4,11 @@ import sys
number(x) { number(x) {
length := 20 length := 20
buffer := mem.alloc(length) buffer := mem.alloc(length)
itoa(x, buffer, length)
mem.free(buffer, length)
}
itoa(x, buffer, length) {
end := buffer + length end := buffer + length
tmp := end tmp := end
digit := 0 digit := 0
@ -15,7 +20,6 @@ number(x) {
if x == 0 { if x == 0 {
sys.write(1, tmp, end - tmp) sys.write(1, tmp, end - tmp)
mem.free(buffer, length)
return return
} }
} }

View File

@ -47,7 +47,7 @@ func Compile(functions <-chan *core.Function, errs <-chan error) (Result, error)
} }
// Check for existence of `main` // Check for existence of `main`
main, exists := all["main"] main, exists := all["main.main"]
if !exists { if !exists {
return result, errors.MissingMainFunction return result, errors.MissingMainFunction

View File

@ -31,7 +31,7 @@ func (r *Result) finalize() ([]byte, []byte) {
Data: make(map[string][]byte, r.DataCount), Data: make(map[string][]byte, r.DataCount),
} }
final.Call("main") final.Call("main.main")
final.RegisterNumber(asm.MOVE, x64.SyscallRegisters[0], linux.Exit) final.RegisterNumber(asm.MOVE, x64.SyscallRegisters[0], linux.Exit)
final.RegisterNumber(asm.MOVE, x64.SyscallRegisters[1], 0) final.RegisterNumber(asm.MOVE, x64.SyscallRegisters[1], 0)
final.Syscall() final.Syscall()

View File

@ -1,6 +1,8 @@
package core package core
import ( import (
"fmt"
"git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/errors" "git.akyoto.dev/cli/q/src/build/errors"
"git.akyoto.dev/cli/q/src/build/expression" "git.akyoto.dev/cli/q/src/build/expression"
@ -11,22 +13,28 @@ import (
// Registers that are in use must be saved if they are modified by the function. // Registers that are in use must be saved if they are modified by the function.
// After the function call, they must be restored in reverse order. // After the function call, they must be restored in reverse order.
func (f *Function) CompileCall(root *expression.Expression) error { func (f *Function) CompileCall(root *expression.Expression) error {
funcName := "" var (
funcNameRoot := root.Children[0] pkg = f.Package
nameRoot = root.Children[0]
name string
fullName string
)
if funcNameRoot.IsLeaf() { if nameRoot.IsLeaf() {
funcName = funcNameRoot.Token.Text(f.File.Bytes) name = nameRoot.Token.Text(f.File.Bytes)
} else { } else {
funcName = funcNameRoot.Children[0].Token.Text(f.File.Bytes) + funcNameRoot.Token.Text(f.File.Bytes) + funcNameRoot.Children[1].Token.Text(f.File.Bytes) pkg = nameRoot.Children[0].Token.Text(f.File.Bytes)
name = nameRoot.Children[1].Token.Text(f.File.Bytes)
} }
isSyscall := funcName == "syscall" isSyscall := name == "syscall"
if !isSyscall { if !isSyscall {
_, exists := f.Functions[funcName] fullName = fmt.Sprintf("%s.%s", pkg, name)
_, exists := f.Functions[fullName]
if !exists { if !exists {
return errors.New(&errors.UnknownFunction{Name: funcName}, f.File, root.Children[0].Token.Position) return errors.New(&errors.UnknownFunction{Name: name}, f.File, root.Children[0].Token.Position)
} }
} }
@ -56,7 +64,7 @@ func (f *Function) CompileCall(root *expression.Expression) error {
if isSyscall { if isSyscall {
f.Syscall() f.Syscall()
} else { } else {
f.Call(funcName) f.Call(fullName)
} }
// Free parameter registers // Free parameter registers

View File

@ -9,6 +9,7 @@ import (
// Function represents the smallest unit of code. // Function represents the smallest unit of code.
type Function struct { type Function struct {
register.Machine register.Machine
Package string
Name string Name string
File *fs.File File *fs.File
Body []token.Token Body []token.Token

View File

@ -11,11 +11,12 @@ import (
) )
// NewFunction creates a new function. // NewFunction creates a new function.
func NewFunction(name string, file *fs.File, body []token.Token) *Function { func NewFunction(pkg string, name string, file *fs.File, body []token.Token) *Function {
return &Function{ return &Function{
Name: name, Package: pkg,
File: file, Name: name,
Body: body, File: file,
Body: body,
Machine: register.Machine{ Machine: register.Machine{
Assembler: asm.Assembler{ Assembler: asm.Assembler{
Instructions: make([]asm.Instruction, 0, 8), Instructions: make([]asm.Instruction, 0, 8),

View File

@ -11,8 +11,8 @@ type Expression struct {
Parent *Expression Parent *Expression
Children []*Expression Children []*Expression
Token token.Token Token token.Token
Precedence int8
Value int Value int
Precedence int8
IsFolded bool IsFolded bool
} }

View File

@ -13,9 +13,9 @@ func (s *Scanner) queue(files ...string) {
} }
if stat.IsDir() { if stat.IsDir() {
s.queueDirectory(file, "") s.queueDirectory(file, "main")
} else { } else {
s.queueFile(file, "") s.queueFile(file, "main")
} }
} }
} }

View File

@ -198,7 +198,7 @@ func (s *Scanner) scanFile(path string, pkg string) error {
name = fmt.Sprintf("%s.%s", pkg, name) name = fmt.Sprintf("%s.%s", pkg, name)
} }
function := core.NewFunction(name, file, body) function := core.NewFunction(pkg, name, file, body)
parameters := tokens[paramsStart:paramsEnd] parameters := tokens[paramsStart:paramsEnd]
count := 0 count := 0