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) {
length := 20
buffer := mem.alloc(length)
itoa(x, buffer, length)
mem.free(buffer, length)
}
itoa(x, buffer, length) {
end := buffer + length
tmp := end
digit := 0
@ -15,7 +20,6 @@ number(x) {
if x == 0 {
sys.write(1, tmp, end - tmp)
mem.free(buffer, length)
return
}
}

View File

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

View File

@ -31,7 +31,7 @@ func (r *Result) finalize() ([]byte, []byte) {
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[1], 0)
final.Syscall()

View File

@ -1,6 +1,8 @@
package core
import (
"fmt"
"git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/errors"
"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.
// After the function call, they must be restored in reverse order.
func (f *Function) CompileCall(root *expression.Expression) error {
funcName := ""
funcNameRoot := root.Children[0]
var (
pkg = f.Package
nameRoot = root.Children[0]
name string
fullName string
)
if funcNameRoot.IsLeaf() {
funcName = funcNameRoot.Token.Text(f.File.Bytes)
if nameRoot.IsLeaf() {
name = nameRoot.Token.Text(f.File.Bytes)
} 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 {
_, exists := f.Functions[funcName]
fullName = fmt.Sprintf("%s.%s", pkg, name)
_, exists := f.Functions[fullName]
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 {
f.Syscall()
} else {
f.Call(funcName)
f.Call(fullName)
}
// Free parameter registers

View File

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

View File

@ -11,8 +11,9 @@ import (
)
// 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{
Package: pkg,
Name: name,
File: file,
Body: body,

View File

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

View File

@ -13,9 +13,9 @@ func (s *Scanner) queue(files ...string) {
}
if stat.IsDir() {
s.queueDirectory(file, "")
s.queueDirectory(file, "main")
} 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)
}
function := core.NewFunction(name, file, body)
function := core.NewFunction(pkg, name, file, body)
parameters := tokens[paramsStart:paramsEnd]
count := 0