From ed6ae1d306b61689e186247d852887f0afe8f9c6 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 20 Jun 2025 15:52:13 +0200 Subject: [PATCH] Added core tests --- src/compiler/Compile.go | 18 +++++------------- src/core/Function.go | 29 +++++++++++------------------ src/core/Function_test.go | 20 ++++++++++++++++++++ src/scanner/Scan.go | 2 +- 4 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 src/core/Function_test.go diff --git a/src/compiler/Compile.go b/src/compiler/Compile.go index ca29c0d..bb08cfc 100644 --- a/src/compiler/Compile.go +++ b/src/compiler/Compile.go @@ -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 } \ No newline at end of file diff --git a/src/core/Function.go b/src/core/Function.go index 5878ade..74fa130 100644 --- a/src/core/Function.go +++ b/src/core/Function.go @@ -9,18 +9,20 @@ import ( // Function is the smallest unit of code. type Function struct { - name string - file *fs.File - Input []*Parameter - Output []*Parameter - Body token.List + Name string + UniqueName string + File *fs.File + Input []*Parameter + Output []*Parameter + Body token.List } // 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 } \ No newline at end of file diff --git a/src/core/Function_test.go b/src/core/Function_test.go new file mode 100644 index 0000000..c9c57b8 --- /dev/null +++ b/src/core/Function_test.go @@ -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) +} \ No newline at end of file diff --git a/src/scanner/Scan.go b/src/scanner/Scan.go index d46798b..3781a02 100644 --- a/src/scanner/Scan.go +++ b/src/scanner/Scan.go @@ -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 {