From af1b032a5073abc4b1792ee6be1133dd3b58fdc2 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 10 Feb 2024 18:57:53 -0500 Subject: [PATCH] OOPS --- parser/expression.go | 42 +++++++++++++++++++++--------------------- parser/literal.go | 14 +++++++------- parser/misc.go | 4 ++-- parser/parser.go | 30 +++++++++++++++--------------- parser/toplevel.go | 10 +++++----- parser/type.go | 18 +++++++++--------- 6 files changed, 59 insertions(+), 59 deletions(-) diff --git a/parser/expression.go b/parser/expression.go index 0f1bb41..6e35a6c 100644 --- a/parser/expression.go +++ b/parser/expression.go @@ -15,7 +15,7 @@ var startTokensExpression = []lexer.TokenKind { lexer.String, } -func (this parser) parseExpression () (entity.Expression, error) { +func (this *parser) parseExpression () (entity.Expression, error) { // Steps that this function takes to parse expressions: // 1. Run a decision tree to parse the expression. // 2. After that, test for infix operators (. and =). We will not need @@ -90,7 +90,7 @@ func (this parser) parseExpression () (entity.Expression, error) { return expression, nil } -func (this parser) parseExpressionRoot () (entity.Expression, error) { +func (this *parser) parseExpressionRoot () (entity.Expression, error) { err := this.expectDesc(descriptionExpression, startTokensExpression...) if err != nil { return nil, err } @@ -106,7 +106,7 @@ func (this parser) parseExpressionRoot () (entity.Expression, error) { panic(this.bug()) } -func (this parser) parseExpressionRootIdent () (entity.Expression, error) { +func (this *parser) parseExpressionRootIdent () (entity.Expression, error) { err := this.expect(lexer.Ident) if err != nil { return nil, err } name := this.value() @@ -140,7 +140,7 @@ func (this parser) parseExpressionRootIdent () (entity.Expression, error) { panic(this.bug()) } -func (this parser) parseExpressionRootLParen() (entity.Expression, error) { +func (this *parser) parseExpressionRootLParen() (entity.Expression, error) { err := this.expect(lexer.LParen) if err != nil { return nil, err } pos := this.pos() @@ -154,7 +154,7 @@ func (this parser) parseExpressionRootLParen() (entity.Expression, error) { panic(this.bug()) } -func (this parser) parseExpressionRootLBracket () (entity.Expression, error) { +func (this *parser) parseExpressionRootLBracket () (entity.Expression, error) { err := this.expect(lexer.LBracket) if err != nil { return nil, err } pos := this.pos() @@ -170,7 +170,7 @@ func (this parser) parseExpressionRootLBracket () (entity.Expression, error) { panic(this.bug()) } -func (this parser) parseExpressionRootLBracketIdent (pos errors.Position) (entity.Expression, error) { +func (this *parser) parseExpressionRootLBracketIdent (pos errors.Position) (entity.Expression, error) { err := this.expect(lexer.Ident) if err != nil { return nil, err } name := this.value() @@ -182,7 +182,7 @@ func (this parser) parseExpressionRootLBracketIdent (pos errors.Position) (entit panic(this.bug()) } -func (this parser) parseDereferenceOrSubscriptCore (pos errors.Position) (entity.Expression, error) { +func (this *parser) parseDereferenceOrSubscriptCore (pos errors.Position) (entity.Expression, error) { err := this.expect(lexer.Dot) if err != nil { return nil, err } @@ -222,7 +222,7 @@ func (this parser) parseDereferenceOrSubscriptCore (pos errors.Position) (entity } } -func (this parser) parseExpressionRootLBracketSymbol (pos errors.Position) (entity.Expression, error) { +func (this *parser) parseExpressionRootLBracketSymbol (pos errors.Position) (entity.Expression, error) { err := this.expectValue ( lexer.Symbol, appendCopy(valuesOperator, "\\", "#", "@", "~", "~~")...) @@ -239,7 +239,7 @@ func (this parser) parseExpressionRootLBracketSymbol (pos errors.Position) (enti panic(this.bug()) } -func (this parser) parseCallCore (pos errors.Position, module string) (*entity.Call, error) { +func (this *parser) parseCallCore (pos errors.Position, module string) (*entity.Call, error) { err := this.expect(lexer.Ident) if err != nil { return nil, err } call := &entity.Call { @@ -266,7 +266,7 @@ func (this parser) parseCallCore (pos errors.Position, module string) (*entity.C return call, nil } -func (this parser) parseMethodCallCore (pos errors.Position, source entity.Expression) (*entity.MethodCall, error) { +func (this *parser) parseMethodCallCore (pos errors.Position, source entity.Expression) (*entity.MethodCall, error) { err := this.expect(lexer.LBracket) if err != nil { return nil, err } err = this.expectNext(lexer.Ident) @@ -295,7 +295,7 @@ func (this parser) parseMethodCallCore (pos errors.Position, source entity.Expre return call, nil } -func (this parser) parseReturnOrBreakCore (pos errors.Position) (entity.Expression, error) { +func (this *parser) parseReturnOrBreakCore (pos errors.Position) (entity.Expression, error) { err := this.expectValue(lexer.Ident, "break", "return") if err != nil { return nil, err } name := this.value() @@ -331,7 +331,7 @@ func (this parser) parseReturnOrBreakCore (pos errors.Position) (entity.Expressi panic(this.bug()) } -func (this parser) parseSliceCore (pos errors.Position) (*entity.Slice, error) { +func (this *parser) parseSliceCore (pos errors.Position) (*entity.Slice, error) { err := this.expectValue(lexer.Symbol , "\\") if err != nil { return nil, err } slice := &entity.Slice { @@ -372,7 +372,7 @@ func (this parser) parseSliceCore (pos errors.Position) (*entity.Slice, error) { return slice, nil } -func (this parser) parseLengthCore (pos errors.Position) (*entity.Length, error) { +func (this *parser) parseLengthCore (pos errors.Position) (*entity.Length, error) { err := this.expectValue(lexer.Symbol , "#") if err != nil { return nil, err } length := &entity.Length { @@ -391,7 +391,7 @@ func (this parser) parseLengthCore (pos errors.Position) (*entity.Length, error) return length, nil } -func (this parser) parseReferenceCore (pos errors.Position) (*entity.Reference, error) { +func (this *parser) parseReferenceCore (pos errors.Position) (*entity.Reference, error) { err := this.expectValue(lexer.Symbol , "@") if err != nil { return nil, err } reference := &entity.Reference { @@ -410,7 +410,7 @@ func (this parser) parseReferenceCore (pos errors.Position) (*entity.Reference, return reference, nil } -func (this parser) parseValueOrBitCastCore (pos errors.Position) (entity.Expression, error) { +func (this *parser) parseValueOrBitCastCore (pos errors.Position) (entity.Expression, error) { err := this.expectValue(lexer.Symbol , "~", "~~") if err != nil { return nil, err } tokValue := this.value() @@ -450,7 +450,7 @@ var valuesOperator = []string { "!", "|", "&", "^", "<<", ">>", "<", ">", "<=", ">=", "=", } -func (this parser) parseOperationCore (pos errors.Position) (*entity.Operation, error) { +func (this *parser) parseOperationCore (pos errors.Position) (*entity.Operation, error) { // TODO: maybe come up with a more elegant way of writing this? // possibly make a verion of expectValue that matches a list of kinds // and a list of values. could also make a version that accepts any kind @@ -486,7 +486,7 @@ func (this parser) parseOperationCore (pos errors.Position) (*entity.Operation, return operation, nil } -func (this parser) parseBlock () (*entity.Block, error) { +func (this *parser) parseBlock () (*entity.Block, error) { err := this.expectDesc("Block", lexer.LBrace) if err != nil { return nil, err } block := &entity.Block { @@ -514,7 +514,7 @@ func (this parser) parseBlock () (*entity.Block, error) { var descriptionDeclaration = "declaration" var startTokensDeclaration = []lexer.TokenKind { lexer.Ident } -func (this parser) parseDeclaration () (*entity.Declaration, error) { +func (this *parser) parseDeclaration () (*entity.Declaration, error) { err := this.expectDesc(descriptionDeclaration, startTokensDeclaration...) if err != nil { return nil, err } name := this.value() @@ -523,7 +523,7 @@ func (this parser) parseDeclaration () (*entity.Declaration, error) { return this.parseDeclarationCore(pos, name) } -func (this parser) parseDeclarationCore (pos errors.Position, name string) (*entity.Declaration, error) { +func (this *parser) parseDeclarationCore (pos errors.Position, name string) (*entity.Declaration, error) { err := this.expect(lexer.Colon) if err != nil { return nil, err } this.next() @@ -537,7 +537,7 @@ func (this parser) parseDeclarationCore (pos errors.Position, name string) (*ent }, nil } -func (this parser) parseIfElse () (*entity.IfElse, error) { +func (this *parser) parseIfElse () (*entity.IfElse, error) { err := this.expectValue(lexer.Ident, "if") if err != nil { return nil, err } ifElse := &entity.IfElse { @@ -566,7 +566,7 @@ func (this parser) parseIfElse () (*entity.IfElse, error) { return ifElse, nil } -func (this parser) parseLoop () (*entity.Loop, error) { +func (this *parser) parseLoop () (*entity.Loop, error) { err := this.expectValue(lexer.Ident, "loop") if err != nil { return nil, err } pos := this.pos() diff --git a/parser/literal.go b/parser/literal.go index 16d3fdc..7a9a136 100644 --- a/parser/literal.go +++ b/parser/literal.go @@ -4,7 +4,7 @@ import "git.tebibyte.media/sashakoshka/fspl/lexer" import "git.tebibyte.media/sashakoshka/fspl/errors" import "git.tebibyte.media/sashakoshka/fspl/entity" -func (this parser) parseLiteralInt () (*entity.LiteralInt, error) { +func (this *parser) parseLiteralInt () (*entity.LiteralInt, error) { err := this.expect(lexer.Int) if err != nil { return nil, err } defer this.next() @@ -18,7 +18,7 @@ func (this parser) parseLiteralInt () (*entity.LiteralInt, error) { }, nil } -func (this parser) parseLiteralFloat () (*entity.LiteralFloat, error) { +func (this *parser) parseLiteralFloat () (*entity.LiteralFloat, error) { err := this.expect(lexer.Float) if err != nil { return nil, err } defer this.next() @@ -32,7 +32,7 @@ func (this parser) parseLiteralFloat () (*entity.LiteralFloat, error) { }, nil } -func (this parser) parseLiteralString () (*entity.LiteralString, error) { +func (this *parser) parseLiteralString () (*entity.LiteralString, error) { err := this.expect(lexer.String) if err != nil { return nil, err } defer this.next() @@ -43,7 +43,7 @@ func (this parser) parseLiteralString () (*entity.LiteralString, error) { }, nil } -func (this parser) parseLiteralBoolean () (*entity.LiteralBoolean, error) { +func (this *parser) parseLiteralBoolean () (*entity.LiteralBoolean, error) { err := this.expectValueDesc("boolean", lexer.Ident, "true", "false") if err != nil { return nil, err } defer this.next() @@ -54,7 +54,7 @@ func (this parser) parseLiteralBoolean () (*entity.LiteralBoolean, error) { }, nil } -func (this parser) parseLiteralNil () (*entity.LiteralNil, error) { +func (this *parser) parseLiteralNil () (*entity.LiteralNil, error) { err := this.expectValueDesc("Nil", lexer.Ident, "nil") if err != nil { return nil, err } defer this.next() @@ -64,7 +64,7 @@ func (this parser) parseLiteralNil () (*entity.LiteralNil, error) { }, nil } -func (this parser) parseLiteralArrayCore (pos errors.Position) (*entity.LiteralArray, error) { +func (this *parser) parseLiteralArrayCore (pos errors.Position) (*entity.LiteralArray, error) { err := this.expect(lexer.Star) if err != nil { return nil, err } literal := &entity.LiteralArray { @@ -89,7 +89,7 @@ func (this parser) parseLiteralArrayCore (pos errors.Position) (*entity.LiteralA return literal, nil } -func (this parser) parseLiteralStructCore (pos errors.Position) (*entity.LiteralStruct, error) { +func (this *parser) parseLiteralStructCore (pos errors.Position) (*entity.LiteralStruct, error) { err := this.expect(lexer.Dot) if err != nil { return nil, err } literal := &entity.LiteralStruct { diff --git a/parser/misc.go b/parser/misc.go index 46e4107..d02655a 100644 --- a/parser/misc.go +++ b/parser/misc.go @@ -7,7 +7,7 @@ import "git.tebibyte.media/sashakoshka/fspl/entity" var descriptionSignature = "signature" var startTokensSignature = []lexer.TokenKind { lexer.LBracket } -func (this parser) parseSignature () (*entity.Signature, error) { +func (this *parser) parseSignature () (*entity.Signature, error) { err := this.expectDesc(descriptionSignature, startTokensSignature...) if err != nil { return nil, err } pos := this.pos() @@ -43,7 +43,7 @@ func (this parser) parseSignature () (*entity.Signature, error) { return signature, nil } -func (this parser) parseMember () (*entity.Member, error) { +func (this *parser) parseMember () (*entity.Member, error) { err := this.expectDesc("struct member", lexer.Ident) if err != nil { return nil, err } name := this.value() diff --git a/parser/parser.go b/parser/parser.go index 6a10e91..0a9146a 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -14,14 +14,14 @@ type parser struct { } // newParser creates a new parser that parses the output of the given lexer. -func newParser (lx lexer.Lexer) (parser, error) { - return &Parser { +func newParser (lx lexer.Lexer) (*parser, error) { + return &parser { lexer: lx, }, nil } // parseInto parses the parser's file into the given syntax tree. -func (this parser) parseInto (tree *Tree) error { +func (this *parser) parseInto (tree *Tree) error { this.tree = tree err := this.parse() if err == io.EOF { err = nil } @@ -30,7 +30,7 @@ func (this parser) parseInto (tree *Tree) error { // expect checks the current token to see if it matches a list of token kind(s), // else it returns an error describing what it expected. -func (this parser) expect (allowed ...lexer.TokenKind) error { +func (this *parser) expect (allowed ...lexer.TokenKind) error { // fmt.Println("expect", this.token, allowed) if !this.token.Is(allowed...) { return errors.Errorf ( @@ -43,7 +43,7 @@ func (this parser) expect (allowed ...lexer.TokenKind) error { // expectDesc is like expect, but the expected entitie(s) are described // manually. This can be helpful when a large syntactical entity is expected and // the first token(s) of it offer insufficient information. -func (this parser) expectDesc (description string, allowed ...lexer.TokenKind) error { +func (this *parser) expectDesc (description string, allowed ...lexer.TokenKind) error { // fmt.Println("expectDesc", this.token, description, allowed) if !this.token.Is(allowed...) { return errors.Errorf ( @@ -54,7 +54,7 @@ func (this parser) expectDesc (description string, allowed ...lexer.TokenKind) e } // expectNext is like expect, but gets the next token first. -func (this parser) expectNext (allowed ...lexer.TokenKind) error { +func (this *parser) expectNext (allowed ...lexer.TokenKind) error { err := this.next() if err != nil { return err } // fmt.Println("expectNext", this.token, allowed) @@ -62,7 +62,7 @@ func (this parser) expectNext (allowed ...lexer.TokenKind) error { } // expectNextDesc is like expectDesc, but gets the next token first. -func (this parser) expectNextDesc (description string, allowed ...lexer.TokenKind) error { +func (this *parser) expectNextDesc (description string, allowed ...lexer.TokenKind) error { err := this.next() if err != nil { return err } // fmt.Println("expectNextDesc", this.token, description, allowed) @@ -71,7 +71,7 @@ func (this parser) expectNextDesc (description string, allowed ...lexer.TokenKin // expectValue returns an error if the current token's value does not match the // allowed values. -func (this parser) expectValue (kind lexer.TokenKind, allowed ...string) error { +func (this *parser) expectValue (kind lexer.TokenKind, allowed ...string) error { // fmt.Println("expectValue", this.token, kind, allowed) if !((this.token.Is(kind) || kind == 0) && this.token.ValueIs(allowed...)) { return errors.Errorf ( @@ -83,7 +83,7 @@ func (this parser) expectValue (kind lexer.TokenKind, allowed ...string) error { // expectValueDesc is like expectValue, but the expected value(s) are described // manually. -func (this parser) expectValueDesc (description string, kind lexer.TokenKind, allowed ...string) error { +func (this *parser) expectValueDesc (description string, kind lexer.TokenKind, allowed ...string) error { // fmt.Println("expectValueDesc", this.token, description, kind, allowed) if !this.token.Is(kind) || !this.token.ValueIs(allowed...) { return errors.Errorf ( @@ -93,14 +93,14 @@ func (this parser) expectValueDesc (description string, kind lexer.TokenKind, al return nil } -func (this parser) next () error { +func (this *parser) next () error { token, err := this.lexer.Next() if err != nil { return err } this.token = token return nil } -func (this parser) bug () string { +func (this *parser) bug () string { return fmt.Sprintln ( "Bug detected in the compiler!\n" + "The parser has taken an unexpected control path.", @@ -110,19 +110,19 @@ func (this parser) bug () string { "The token being parsed was:", this.token) } -func (this parser) kind () lexer.TokenKind { +func (this *parser) kind () lexer.TokenKind { return this.token.Kind } -func (this parser) value () string { +func (this *parser) value () string { return this.token.Value } -func (this parser) pos () errors.Position { +func (this *parser) pos () errors.Position { return this.token.Position } -func (this parser) parse () error { +func (this *parser) parse () error { err := this.next() if err != nil { return err } for this.token.Kind != lexer.EOF { diff --git a/parser/toplevel.go b/parser/toplevel.go index 9a7d7ff..3f120d9 100644 --- a/parser/toplevel.go +++ b/parser/toplevel.go @@ -12,7 +12,7 @@ var startTokensTopLevel = []lexer.TokenKind { lexer.EOF, } -func (this parser) parseTopLevel () error { +func (this *parser) parseTopLevel () error { err := this.expectDesc ( descriptionTopLevel, startTokensTopLevel...) @@ -66,7 +66,7 @@ func (this parser) parseTopLevel () error { return nil } -func (this parser) parseAccess () (entity.Access, error) { +func (this *parser) parseAccess () (entity.Access, error) { err := this.expectValueDesc ( "Access control specifier", lexer.Symbol, "-", "~", "+") @@ -81,7 +81,7 @@ func (this parser) parseAccess () (entity.Access, error) { } } -func (this parser) parseFunctionCore (pos errors.Position, access entity.Access) (*entity.Function, error) { +func (this *parser) parseFunctionCore (pos errors.Position, access entity.Access) (*entity.Function, error) { signature, err := this.parseSignature() if err != nil { return nil, err } @@ -124,7 +124,7 @@ func (this parser) parseFunctionCore (pos errors.Position, access entity.Access) return function, nil } -func (this parser) parseMethodCore (pos errors.Position, access entity.Access, typeName string) (*entity.Method, error) { +func (this *parser) parseMethodCore (pos errors.Position, access entity.Access, typeName string) (*entity.Method, error) { function, err := this.parseFunctionCore(pos, access) if err != nil { return nil, err } return &entity.Method { @@ -137,7 +137,7 @@ func (this parser) parseMethodCore (pos errors.Position, access entity.Access, t }, nil } -func (this parser) parseTypedefCore (pos errors.Position, access entity.Access, typeName string) (*entity.Typedef, error) { +func (this *parser) parseTypedefCore (pos errors.Position, access entity.Access, typeName string) (*entity.Typedef, error) { pos = pos.Union(this.pos()) ty, err := this.parseType() if err != nil { return nil, err } diff --git a/parser/type.go b/parser/type.go index 0752137..9db739f 100644 --- a/parser/type.go +++ b/parser/type.go @@ -14,7 +14,7 @@ var startTokensType = []lexer.TokenKind { lexer.LParen, } -func (this parser) parseType () (entity.Type, error) { +func (this *parser) parseType () (entity.Type, error) { err := this.expectDesc(descriptionType, startTokensType...) if err != nil { return nil, err } @@ -69,7 +69,7 @@ func (this parser) parseType () (entity.Type, error) { panic(this.bug()) } -func (this parser) parseTypeNamedCore (module string) (entity.Type, error) { +func (this *parser) parseTypeNamedCore (module string) (entity.Type, error) { err := this.expect(lexer.TypeIdent) if err != nil { return nil, err } @@ -81,7 +81,7 @@ func (this parser) parseTypeNamedCore (module string) (entity.Type, error) { }, nil } -func (this parser) parseTypePointerOrSlice () (entity.Type, error) { +func (this *parser) parseTypePointerOrSlice () (entity.Type, error) { err := this.expectDesc("pointer type or slice type", lexer.Star) if err != nil { return nil, err } start := this.pos() @@ -109,7 +109,7 @@ func (this parser) parseTypePointerOrSlice () (entity.Type, error) { } } -func (this parser) parseTypeArray () (entity.Type, error) { +func (this *parser) parseTypeArray () (entity.Type, error) { err := this.expectDesc("array type", lexer.Int) if err != nil { return nil, err } start := this.pos() @@ -130,7 +130,7 @@ func (this parser) parseTypeArray () (entity.Type, error) { }, nil } -func (this parser) parseTypeStructCore () (entity.Type, error) { +func (this *parser) parseTypeStructCore () (entity.Type, error) { err := this.expect(lexer.Dot) if err != nil { return nil, err } ty := &entity.TypeStruct { @@ -154,7 +154,7 @@ func (this parser) parseTypeStructCore () (entity.Type, error) { return ty, nil } -func (this parser) parseTypeInterfaceCore () (entity.Type, error) { +func (this *parser) parseTypeInterfaceCore () (entity.Type, error) { err := this.expectValue(lexer.Symbol, "~") if err != nil { return nil, err } ty := &entity.TypeInterface { @@ -178,7 +178,7 @@ func (this parser) parseTypeInterfaceCore () (entity.Type, error) { return ty, nil } -func (this parser) parseTypeInt () (entity.Type, error) { +func (this *parser) parseTypeInt () (entity.Type, error) { err := this.expect(lexer.TypeIdent) if err != nil { return nil, err } @@ -196,7 +196,7 @@ func (this parser) parseTypeInt () (entity.Type, error) { }, nil } -func (this parser) parseTypeWord () (entity.Type, error) { +func (this *parser) parseTypeWord () (entity.Type, error) { err := this.expectValue(lexer.TypeIdent, "Int", "UInt") if err != nil { return nil, err } defer this.next() @@ -206,7 +206,7 @@ func (this parser) parseTypeWord () (entity.Type, error) { }, nil } -func (this parser) parseTypeFloat () (entity.Type, error) { +func (this *parser) parseTypeFloat () (entity.Type, error) { err := this.expectValue(lexer.TypeIdent, "F16", "F32", "F64", "F128") if err != nil { return nil, err }