diff --git a/examples/point/point.q b/examples/point/point.q index c2a7d5f..de2a763 100644 --- a/examples/point/point.q +++ b/examples/point/point.q @@ -1,7 +1,7 @@ import mem import sys -struct Point { +Point { x int y int } diff --git a/lib/sys/struct_linux.q b/lib/sys/struct_linux.q index 8a22345..adbea0d 100644 --- a/lib/sys/struct_linux.q +++ b/lib/sys/struct_linux.q @@ -1,11 +1,11 @@ -struct sockaddr_in { +sockaddr_in { sin_family int16 - sin_port int16 - sin_addr int64 - sin_zero int64 + sin_port int16 + sin_addr int64 + sin_zero int64 } -struct timespec { - seconds int64 +timespec { + seconds int64 nanoseconds int64 } \ No newline at end of file diff --git a/lib/sys/struct_mac.q b/lib/sys/struct_mac.q index 480199a..9172114 100644 --- a/lib/sys/struct_mac.q +++ b/lib/sys/struct_mac.q @@ -1,7 +1,7 @@ -struct sockaddr_in_bsd { - sin_len int8 +sockaddr_in_bsd { + sin_len int8 sin_family int8 - sin_port int16 - sin_addr int64 - sin_zero int64 + sin_port int16 + sin_addr int64 + sin_zero int64 } \ No newline at end of file diff --git a/src/errors/Common.go b/src/errors/Common.go index f5f4f50..206d4c5 100644 --- a/src/errors/Common.go +++ b/src/errors/Common.go @@ -3,11 +3,10 @@ package errors var ( EmptySwitch = &Base{"Empty switch"} ExpectedConstName = &Base{"Expected a name for the const group"} - ExpectedFunctionParameters = &Base{"Expected function parameters"} + InvalidDefinition = &Base{"Invalid definition"} ExpectedFunctionDefinition = &Base{"Expected function definition"} ExpectedIfBeforeElse = &Base{"Expected an 'if' block before 'else'"} ExpectedPackageName = &Base{"Expected package name"} - ExpectedStructName = &Base{"Expected struct name"} ExpectedDLLName = &Base{"Expected DLL name"} InvalidNumber = &Base{"Invalid number"} InvalidCondition = &Base{"Invalid condition"} diff --git a/src/scanner/scanFile.go b/src/scanner/scanFile.go index 04bf2b5..0d9d7a4 100644 --- a/src/scanner/scanFile.go +++ b/src/scanner/scanFile.go @@ -31,12 +31,29 @@ func (s *Scanner) scanFile(path string, pkg string) error { switch tokens[i].Kind { case token.NewLine: case token.Comment: + case token.Identifier: + if i+1 >= len(tokens) { + return errors.New(errors.InvalidDefinition, file, tokens[i].End()) + } + + next := tokens[i+1] + + switch next.Kind { + case token.GroupStart: + i, err = s.scanFunction(file, tokens, i) + case token.BlockStart: + i, err = s.scanStruct(file, tokens, i) + case token.GroupEnd: + return errors.New(errors.MissingGroupStart, file, next.Position) + case token.BlockEnd: + return errors.New(errors.MissingBlockStart, file, next.Position) + case token.Invalid: + return errors.New(&errors.InvalidCharacter{Character: next.Text(file.Bytes)}, file, next.Position) + default: + return errors.New(errors.InvalidDefinition, file, next.Position) + } case token.Import: i, err = s.scanImport(file, tokens, i) - case token.Struct: - i, err = s.scanStruct(file, tokens, i) - case token.Identifier: - i, err = s.scanFunction(file, tokens, i) case token.Extern: i, err = s.scanExtern(file, tokens, i) case token.Const: diff --git a/src/scanner/scanFunctionSignature.go b/src/scanner/scanFunctionSignature.go index 6cd5b17..1833c61 100644 --- a/src/scanner/scanFunctionSignature.go +++ b/src/scanner/scanFunctionSignature.go @@ -60,7 +60,7 @@ func scanFunctionSignature(file *fs.File, tokens token.List, i int, delimiter to } if paramsStart == -1 { - return nil, i, errors.New(errors.ExpectedFunctionParameters, file, tokens[i].Position) + return nil, i, errors.New(errors.InvalidDefinition, file, tokens[i].Position) } return nil, i, nil @@ -71,7 +71,7 @@ func scanFunctionSignature(file *fs.File, tokens token.List, i int, delimiter to continue } - return nil, i, errors.New(errors.ExpectedFunctionParameters, file, tokens[i].Position) + return nil, i, errors.New(errors.InvalidDefinition, file, tokens[i].Position) } // Return type diff --git a/src/scanner/scanStruct.go b/src/scanner/scanStruct.go index 0de75cf..dc70838 100644 --- a/src/scanner/scanStruct.go +++ b/src/scanner/scanStruct.go @@ -9,12 +9,6 @@ import ( // scanStruct scans a struct. func (s *Scanner) scanStruct(file *fs.File, tokens token.List, i int) (int, error) { - i++ - - if tokens[i].Kind != token.Identifier { - return i, errors.New(errors.ExpectedStructName, file, tokens[i].Position) - } - structName := tokens[i].Text(file.Bytes) structure := types.NewStruct(file.Package, structName) diff --git a/src/token/Kind.go b/src/token/Kind.go index 881bd18..12d7362 100644 --- a/src/token/Kind.go +++ b/src/token/Kind.go @@ -74,7 +74,6 @@ const ( Import // import Loop // loop Return // return - Struct // struct Switch // switch ___END_KEYWORDS___ // ) diff --git a/src/token/Tokenize_test.go b/src/token/Tokenize_test.go index 19ebf16..ce78895 100644 --- a/src/token/Tokenize_test.go +++ b/src/token/Tokenize_test.go @@ -25,7 +25,7 @@ func TestFunction(t *testing.T) { } func TestKeyword(t *testing.T) { - tokens := token.Tokenize([]byte("assert const else extern if import for loop return struct switch")) + tokens := token.Tokenize([]byte("assert const else extern if import for loop return switch")) expected := []token.Kind{ token.Assert, @@ -37,7 +37,6 @@ func TestKeyword(t *testing.T) { token.For, token.Loop, token.Return, - token.Struct, token.Switch, token.EOF, } diff --git a/src/token/identifier.go b/src/token/identifier.go index 27849ed..db48049 100644 --- a/src/token/identifier.go +++ b/src/token/identifier.go @@ -31,8 +31,6 @@ func identifier(tokens List, buffer []byte, i Position) (List, Position) { kind = Loop case "return": kind = Return - case "struct": - kind = Struct case "switch": kind = Switch } diff --git a/tests/errors/ExpectedStructName.q b/tests/errors/ExpectedStructName.q deleted file mode 100644 index 6c8a4ee..0000000 --- a/tests/errors/ExpectedStructName.q +++ /dev/null @@ -1 +0,0 @@ -struct{} \ No newline at end of file diff --git a/tests/errors/ExpectedFunctionParameters.q b/tests/errors/InvalidDefinition.q similarity index 100% rename from tests/errors/ExpectedFunctionParameters.q rename to tests/errors/InvalidDefinition.q diff --git a/tests/errors/UnknownStructField.q b/tests/errors/UnknownStructField.q index bea43e5..aefe455 100644 --- a/tests/errors/UnknownStructField.q +++ b/tests/errors/UnknownStructField.q @@ -1,4 +1,4 @@ -struct A {} +A {} main() { a := new(A) diff --git a/tests/errors_test.go b/tests/errors_test.go index 9403bfc..8165e97 100644 --- a/tests/errors_test.go +++ b/tests/errors_test.go @@ -17,12 +17,11 @@ var errs = []struct { {"EmptySwitch.q", errors.EmptySwitch}, {"ExpectedDLLName.q", errors.ExpectedDLLName}, {"ExpectedFunctionDefinition.q", errors.ExpectedFunctionDefinition}, - {"ExpectedFunctionParameters.q", errors.ExpectedFunctionParameters}, {"ExpectedIfBeforeElse.q", errors.ExpectedIfBeforeElse}, {"ExpectedIfBeforeElse2.q", errors.ExpectedIfBeforeElse}, - {"ExpectedStructName.q", errors.ExpectedStructName}, {"ExpectedPackageName.q", errors.ExpectedPackageName}, {"InvalidCondition.q", errors.InvalidCondition}, + {"InvalidDefinition.q", errors.InvalidDefinition}, {"InvalidInstructionCall.q", &errors.InvalidInstruction{Instruction: "sys.write"}}, {"InvalidInstructionExpression.q", &errors.InvalidInstruction{Instruction: "2+3"}}, {"InvalidInstructionIdentifier.q", &errors.InvalidInstruction{Instruction: "abc"}}, diff --git a/tests/programs/allocator.q b/tests/programs/allocator.q index 4b39208..ba9c959 100644 --- a/tests/programs/allocator.q +++ b/tests/programs/allocator.q @@ -24,12 +24,12 @@ main() { assert b.y == 4 } -struct Allocator { +Allocator { block *any current int } -struct Point { +Point { x int y int } \ No newline at end of file diff --git a/tests/programs/function-pointer-field.q b/tests/programs/function-pointer-field.q index 1484842..f37d9c9 100644 --- a/tests/programs/function-pointer-field.q +++ b/tests/programs/function-pointer-field.q @@ -1,6 +1,6 @@ import sys -struct Struct { +Struct { func *any } diff --git a/tests/programs/struct-lifetime.q b/tests/programs/struct-lifetime.q index a32f469..920ffc4 100644 --- a/tests/programs/struct-lifetime.q +++ b/tests/programs/struct-lifetime.q @@ -3,6 +3,6 @@ main() { c.value += 16 } -struct Counter { +Counter { value int } \ No newline at end of file diff --git a/tests/programs/struct.q b/tests/programs/struct.q index dd23388..15f096e 100644 --- a/tests/programs/struct.q +++ b/tests/programs/struct.q @@ -1,4 +1,4 @@ -struct Point { +Point { x int y int }