Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert the maps in the type file to simple arrays #1086

Merged
merged 7 commits into from
Jul 23, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
performance: more defensive coding
added some more defensive coding pieces
  • Loading branch information
Jeff Winkler committed Jul 21, 2021
commit 18bcfb4b9a5878e66eda8a4fd5d30c161a5206b8
35 changes: 16 additions & 19 deletions runtime/parser2/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package parser2

import (
"fmt"
"sync"

"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/errors"
Expand All @@ -34,8 +33,6 @@ const (
typeLeftBindingPowerInstantiation
)

var once sync.Once

type typeNullDenotationFunc func(parser *parser, token lexer.Token) ast.Type

var typeNullDenotations [256]typeNullDenotationFunc
Expand All @@ -55,7 +52,7 @@ var typeLeftDenotations [256]typeLeftDenotationFunc
var typeMetaLeftDenotations [256]typeMetaLeftDenotationFunc

func setTypeNullDenotation(tokenType lexer.TokenType, nullDenotation typeNullDenotationFunc) {
current := typeNullDenotations[tokenType]
current := typeNullDenotations[int(tokenType)]
jwinkler2083233 marked this conversation as resolved.
Show resolved Hide resolved
if current != nil {
panic(fmt.Errorf(
"type null denotation for token %s already exists",
Expand All @@ -66,33 +63,33 @@ func setTypeNullDenotation(tokenType lexer.TokenType, nullDenotation typeNullDen
}

func setTypeLeftBindingPower(tokenType lexer.TokenType, power int) {
current := typeLeftBindingPowers[tokenType]
current := typeLeftBindingPowers[int(tokenType)]
if current > power {
return
}
typeLeftBindingPowers[tokenType] = power
}

func setTypeLeftDenotation(tokenType lexer.TokenType, leftDenotation typeLeftDenotationFunc) {
current := typeLeftDenotations[tokenType]
current := typeLeftDenotations[int(tokenType)]
if current != nil {
panic(fmt.Errorf(
"type left denotation for token %s already exists",
tokenType,
))
}
typeLeftDenotations[tokenType] = leftDenotation
typeLeftDenotations[int(tokenType)] = leftDenotation
}

func setTypeMetaLeftDenotation(tokenType lexer.TokenType, metaLeftDenotation typeMetaLeftDenotationFunc) {
current := typeMetaLeftDenotations[tokenType]
current := typeMetaLeftDenotations[int(tokenType)]
if current != nil {
panic(fmt.Errorf(
"type meta left denotation for token %s already exists",
tokenType,
))
}
typeMetaLeftDenotations[tokenType] = metaLeftDenotation
typeMetaLeftDenotations[int(tokenType)] = metaLeftDenotation
}

type prefixTypeFunc func(right ast.Type, tokenRange ast.Range) ast.Type
Expand Down Expand Up @@ -144,20 +141,20 @@ func defineType(def interface{}) {
}

func init() {
defineArrayType()
defineOptionalType()
defineReferenceType()
defineRestrictedOrDictionaryType()
defineFunctionType()
defineInstantiationType()

for i := 0; i < 256; i++ {
typeLeftBindingPowers[i] = 0
typeNullDenotations[i] = nil
typeLeftDenotations[i] = nil
typeMetaLeftDenotations[i] = nil
}

defineArrayType()
defineOptionalType()
defineReferenceType()
defineRestrictedOrDictionaryType()
defineFunctionType()
defineInstantiationType()

setTypeNullDenotation(
lexer.TokenIdentifier,
func(p *parser, token lexer.Token) ast.Type {
Expand Down Expand Up @@ -693,7 +690,7 @@ func applyTypeMetaLeftDenotation(
// or performing look-ahead

var metaLeftDenotation typeMetaLeftDenotationFunc
metaLeftDenotation = typeMetaLeftDenotations[p.current.Type]
metaLeftDenotation = typeMetaLeftDenotations[int(p.current.Type)]
if metaLeftDenotation == nil {
metaLeftDenotation = defaultTypeMetaLeftDenotation
}
Expand All @@ -712,7 +709,7 @@ func defaultTypeMetaLeftDenotation(
result ast.Type,
done bool,
) {
if rightBindingPower >= typeLeftBindingPowers[p.current.Type] {
if rightBindingPower >= typeLeftBindingPowers[int(p.current.Type)] {
return left, true
}

Expand Down Expand Up @@ -746,7 +743,7 @@ func parseTypeAnnotation(p *parser) *ast.TypeAnnotation {
func applyTypeNullDenotation(p *parser, token lexer.Token) ast.Type {
tokenType := token.Type
var nullDenotation typeNullDenotationFunc
nullDenotation = typeNullDenotations[tokenType]
nullDenotation = typeNullDenotations[int(tokenType)]
if nullDenotation == nil {
panic(fmt.Errorf("unexpected token in type: %s", token.Type))
}
Expand Down