From 320e023d744ad3d58994db6941ca0fed51b049af Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 21 Jun 2025 21:28:21 +0200 Subject: [PATCH] Improved parameter handling --- lib/io/write.q | 2 +- lib/os/os_linux.q | 3 -- lib/os/os_linux_x86.q | 3 ++ src/core/Function_test.go | 5 +++ src/core/Parameter.go | 18 +++-------- src/scanner/errors_test.go | 2 ++ src/scanner/scanSignature.go | 31 +++++++++++++++++-- .../testdata/errors/MissingParameter4.q | 1 + .../testdata/errors/MissingParameter5.q | 1 + 9 files changed, 45 insertions(+), 21 deletions(-) delete mode 100644 lib/os/os_linux.q create mode 100644 lib/os/os_linux_x86.q create mode 100644 src/scanner/testdata/errors/MissingParameter4.q create mode 100644 src/scanner/testdata/errors/MissingParameter5.q diff --git a/lib/io/write.q b/lib/io/write.q index 6ee5598..28dcda7 100644 --- a/lib/io/write.q +++ b/lib/io/write.q @@ -1,5 +1,5 @@ import os -write(buffer []byte) -> int { +write(buffer []byte) -> (written int) { return os.write(0, buffer, len(buffer)) } \ No newline at end of file diff --git a/lib/os/os_linux.q b/lib/os/os_linux.q deleted file mode 100644 index b3f7ebb..0000000 --- a/lib/os/os_linux.q +++ /dev/null @@ -1,3 +0,0 @@ -write(fd int, buffer *byte, length int) -> int { - return syscall(1, fd, buffer, length) -} \ No newline at end of file diff --git a/lib/os/os_linux_x86.q b/lib/os/os_linux_x86.q new file mode 100644 index 0000000..0b690fd --- /dev/null +++ b/lib/os/os_linux_x86.q @@ -0,0 +1,3 @@ +write(fd int, buffer *byte, length int) -> (written int) { + return syscall(1, fd, buffer, length) +} \ No newline at end of file diff --git a/src/core/Function_test.go b/src/core/Function_test.go index c9c57b8..42efe79 100644 --- a/src/core/Function_test.go +++ b/src/core/Function_test.go @@ -12,9 +12,14 @@ 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) + + write, exists := env.Functions["io.write"] + assert.True(t, exists) + write.Output[0].Type() } \ No newline at end of file diff --git a/src/core/Parameter.go b/src/core/Parameter.go index 633ab40..87d2dae 100644 --- a/src/core/Parameter.go +++ b/src/core/Parameter.go @@ -7,22 +7,12 @@ import ( // Parameter is an input or output parameter in a function. type Parameter struct { - name string - typ types.Type - tokens token.List + Name string + TypeTokens token.List + typ types.Type } -// NewParameter creates a new parameter with the given list of tokens. -func NewParameter(tokens token.List) *Parameter { - return &Parameter{tokens: tokens} -} - -// Name returns the name of the parameter. -func (p *Parameter) Name() string { - return p.name -} - -// Type returns the type of the parameter. +// Type returns the data type of the parameter. func (p *Parameter) Type() types.Type { return p.typ } \ No newline at end of file diff --git a/src/scanner/errors_test.go b/src/scanner/errors_test.go index e1661e8..464fd3c 100644 --- a/src/scanner/errors_test.go +++ b/src/scanner/errors_test.go @@ -32,6 +32,8 @@ var errs = []struct { {"MissingParameter.q", scanner.MissingParameter}, {"MissingParameter2.q", scanner.MissingParameter}, {"MissingParameter3.q", scanner.MissingParameter}, + {"MissingParameter4.q", scanner.MissingParameter}, + {"MissingParameter5.q", scanner.MissingParameter}, {"MissingType.q", scanner.MissingType}, } diff --git a/src/scanner/scanSignature.go b/src/scanner/scanSignature.go index efa2fcf..497e820 100644 --- a/src/scanner/scanSignature.go +++ b/src/scanner/scanSignature.go @@ -63,7 +63,7 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind return nil, i, errors.New(InvalidFunctionDefinition, file, tokens[i].Position) } - return nil, i, nil + return nil, i, errors.New(InvalidFunctionDefinition, file, tokens[i].Position) } if groupLevel > 0 { @@ -98,7 +98,10 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind return nil, i, errors.New(MissingType, file, param[0].End()) } - function.Input = append(function.Input, core.NewParameter(param)) + function.Input = append(function.Input, &core.Parameter{ + Name: param[0].String(file.Bytes), + TypeTokens: param[1:], + }) } if typeStart != -1 { @@ -109,8 +112,30 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind outputTokens := tokens[typeStart:typeEnd] + if len(outputTokens) == 0 { + return nil, i, errors.New(MissingParameter, file, tokens[typeStart].Position) + } + + errorPos := token.Position(typeStart) + for param := range outputTokens.Split { - function.Output = append(function.Output, core.NewParameter(param)) + if len(param) == 0 { + return nil, i, errors.New(MissingParameter, file, errorPos) + } + + if len(param) == 1 { + function.Output = append(function.Output, &core.Parameter{ + Name: "", + TypeTokens: param, + }) + } else { + function.Output = append(function.Output, &core.Parameter{ + Name: param[0].String(file.Bytes), + TypeTokens: param[1:], + }) + } + + errorPos = param[len(param)-1].End() + 1 } } diff --git a/src/scanner/testdata/errors/MissingParameter4.q b/src/scanner/testdata/errors/MissingParameter4.q new file mode 100644 index 0000000..0b4d9f1 --- /dev/null +++ b/src/scanner/testdata/errors/MissingParameter4.q @@ -0,0 +1 @@ +f() -> {} \ No newline at end of file diff --git a/src/scanner/testdata/errors/MissingParameter5.q b/src/scanner/testdata/errors/MissingParameter5.q new file mode 100644 index 0000000..908ece7 --- /dev/null +++ b/src/scanner/testdata/errors/MissingParameter5.q @@ -0,0 +1 @@ +f() -> (int,) {} \ No newline at end of file