Added core tests
All checks were successful
/ test (push) Successful in 14s

This commit is contained in:
Eduard Urbach 2025-06-20 15:52:13 +02:00
parent fe3212506d
commit ed6ae1d306
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
4 changed files with 37 additions and 32 deletions

View file

@ -4,30 +4,22 @@ import (
"maps"
"git.urbach.dev/cli/q/src/build"
"git.urbach.dev/cli/q/src/core"
"git.urbach.dev/cli/q/src/scanner"
)
// Compile waits for the scan to finish and compiles all functions.
func Compile(b *build.Build) (Result, error) {
result := Result{}
func Compile(b *build.Build) (*core.Environment, error) {
all, err := scanner.Scan(b)
if err != nil {
return result, err
return nil, err
}
if len(all.Files) == 0 {
return result, NoInputFiles
}
for _, function := range all.Functions {
err := function.ResolveTypes()
if err != nil {
return result, err
}
return nil, NoInputFiles
}
compileFunctions(maps.Values(all.Functions))
return result, nil
return all, nil
}

View file

@ -9,8 +9,9 @@ import (
// Function is the smallest unit of code.
type Function struct {
name string
file *fs.File
Name string
UniqueName string
File *fs.File
Input []*Parameter
Output []*Parameter
Body token.List
@ -19,8 +20,9 @@ type Function struct {
// NewFunction creates a new function.
func NewFunction(name string, file *fs.File) *Function {
return &Function{
name: name,
file: file,
Name: name,
File: file,
UniqueName: fmt.Sprintf("%s.%s", file.Package, name),
}
}
@ -29,16 +31,7 @@ func (f *Function) IsExtern() bool {
return f.Body == nil
}
// ResolveTypes parses the input and output types.
func (f *Function) ResolveTypes() error {
for _, param := range f.Input {
param.name = param.tokens[0].String(f.file.Bytes)
}
return nil
}
// String returns the package and function name.
// String returns the unique name.
func (f *Function) String() string {
return fmt.Sprintf("%s.%s", f.file.Package, f.name)
return f.UniqueName
}

20
src/core/Function_test.go Normal file
View file

@ -0,0 +1,20 @@
package core_test
import (
"testing"
"git.urbach.dev/cli/q/src/build"
"git.urbach.dev/cli/q/src/compiler"
"git.urbach.dev/go/assert"
)
func TestFunction(t *testing.T) {
b := build.New("../../examples/hello")
env, err := compiler.Compile(b)
assert.Nil(t, err)
main, exists := env.Functions["main.main"]
assert.True(t, exists)
assert.False(t, main.IsExtern())
assert.Equal(t, main.UniqueName, "main.main")
assert.Equal(t, main.String(), main.UniqueName)
}

View file

@ -36,7 +36,7 @@ func Scan(b *build.Build) (*core.Environment, error) {
continue
}
all.Functions[f.String()] = f
all.Functions[f.UniqueName] = f
case file, ok := <-s.files:
if !ok {