Skip to content

Commit

Permalink
Implement loop variable types
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Jul 9, 2024
1 parent 8177248 commit 36c0c7e
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 45 deletions.
90 changes: 60 additions & 30 deletions src/main/gen/org/vyperlang/plugin/parser/BaseVyperParser.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/main/gen/org/vyperlang/plugin/psi/VyperForStatement.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.lang.annotation.Annotator
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.psi.PsiElement
import com.intellij.psi.util.childrenOfType
import com.intellij.psi.util.findParentOfType
import com.intellij.util.text.SemVer
import org.vyperlang.plugin.VyperFileType
Expand All @@ -19,11 +20,13 @@ internal const val EXTCALL_NOT_VY3 = "Keyword `extcall` not supported in Vyper 0
internal const val STATICCALL_NOT_VY3 = "Keyword `staticcall` not supported in Vyper 0.3"
internal const val STRUCT_DICT_NOT_VY3 =
"Instantiating a struct using a dictionary is not supported until Vyper 0.4. Use kwargs instead e.g. Foo(a=1, b=2)"
internal const val RANGE_TYPE_NOT_V3 = "Range type not supported in Vyper 0.3"

internal const val STRUCT_DICT_WARN_V4 = "Instantiating a struct using a dictionary is deprecated. Use kwargs instead e.g. Foo(a=1, b=2)"
internal const val NAMED_LOCKS_NOT_V4 = "Named locks are not supported in Vyper 0.4"
internal const val MISSING_STATICCALL = "Missing `staticcall`"
internal const val MISSING_EXTCALL = "Missing `extcall`"
internal const val RANGE_TYPE_REQUIRED_V4 = "Range type required in Vyper 0.4"

internal const val VYPER_VERSION_NOT_SPECIFIED = "Vyper version not specified. Please add `# pragma version ^0.4.0` to the top of the file"

Expand Down Expand Up @@ -52,6 +55,9 @@ class VersionAnnotator : Annotator {
is VyperCallExpression ->
if (VyperResolver.resolveStructCall(element).isNotEmpty())
holder.newAnnotation(HighlightSeverity.ERROR, STRUCT_DICT_NOT_VY3).create()
is VyperForStatement ->
if(element.type != null)
holder.newAnnotation(HighlightSeverity.ERROR, RANGE_TYPE_NOT_V3).create()
}
}

Expand All @@ -62,6 +68,9 @@ class VersionAnnotator : Annotator {
is VyperMemberAccessExpression ->
highlightVy4Modifiers(VyperResolver.resolveInterfaceFunctionModifiers(element), element, holder)
is VyperStructExpression -> holder.newAnnotation(HighlightSeverity.WARNING, STRUCT_DICT_WARN_V4).create()
is VyperForStatement ->
if(element.type == null)
holder.newAnnotation(HighlightSeverity.ERROR, RANGE_TYPE_REQUIRED_V4).create()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/vyperlang/plugin/grammar/Vyper.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ IfStatement ::= if &INDNONE CondStmt
private SimpleStatement ::= LocalVariableDefinition | ExpressionStatement
private ExpressionStatement ::= Expression

ForStatement ::= for &INDNONE Identifier &INDNONE in &INDNONE Expression &INDNONE ':'
ForStatement ::= for &INDNONE Identifier (&INDNONE ':' TYPE)? &INDNONE in &INDNONE Expression &INDNONE ':'
(&INDNONE Statement | <<indented (Statement (&INDEQ Statement)*)>>)
{
pin=1
Expand Down
18 changes: 4 additions & 14 deletions src/main/kotlin/org/vyperlang/plugin/psi/Mixins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,18 @@ abstract class VyperVarLiteralMixin(node: ASTNode) : VyperNamedElementImpl(node)
/**
* Converts the var literal to a reference.
*/
override fun getReference(): VyperReference = when (node.psi.parent) {
override fun getReference(): VyperReference = getReference(node.psi.parent)

private fun getReference(parent: PsiElement?) = when (parent) {
is VyperImplementsDirective -> VyperInterfaceReference(this)
is VyperEventLogExpression -> VyperEventLogReference(this)

is VyperStructExpression -> VyperStructReference(this)
is VyperStructExpressionMember -> VyperStructMemberReference(this)
is VyperMemberAccessExpression -> VyperMemberAccessReference(
this,
node.psi.parent as VyperMemberAccessExpression
)

is VyperMemberAccessExpression -> VyperMemberAccessReference(this, parent)
else -> VyperVarLiteralReference(this) // reference itself
}
}

//abstract class VyperStructTypeMixin(node: ASTNode) : VyperNamedElementImpl(node), VyperStructType {
// override val referenceNameElement: PsiElement get() = findChildByType(IDENTIFIER)!!
// override val referenceName: String get() = referenceNameElement.text
//
// override fun getReference(): VyperReference? = VyperStructReference(this.node.psi as VyperStructType)
//}

abstract class VyperFunctionDefMixin(node: ASTNode) : VyperNamedElementImpl(node), VyperFunctionDefinition {
override val referenceNameElement: PsiElement get() = findChildByType(IDENTIFIER)!!
override val referenceName: String get() = referenceNameElement.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ class TestVersionAnnotator(private val case: TestCase) : BasePlatformTestCase()
Vy3 to listOf(STRUCT_DICT_NOT_VY3),
Vy4 to emptyList(),
),
TestCase.create(
"range with type",
"""
{pragma}
@external
def write_junk_to_memory():
xs: int128[1024] = empty(int128[1024])
for i: uint256 in range(1024):
xs[i] = -(i + 1)
""",
Vy3 to listOf(RANGE_TYPE_NOT_V3),
Vy4 to emptyList(),
),
TestCase.create(
"range without type",
"""
{pragma}
@external
def write_junk_to_memory():
xs: int128[1024] = empty(int128[1024])
for i in range(1024):
xs[i] = -(i + 1)
""",
Vy3 to emptyList(),
Vy4 to listOf(RANGE_TYPE_REQUIRED_V4),
),
).flatten().toTypedArray()
}

Expand Down

0 comments on commit 36c0c7e

Please sign in to comment.