Skip to content

Commit

Permalink
Get annotations working on fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason2605 committed Oct 31, 2023
1 parent e311391 commit 1c448f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/vm/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,7 @@ static bool isFieldAnnotation(Compiler *compiler) {
if (match(compiler, TOKEN_LEFT_PAREN)) {
do {
advance(compiler->parser);
} while (check(compiler, TOKEN_RIGHT_PAREN));
} while (!match(compiler, TOKEN_RIGHT_PAREN));
}
} while (match(compiler, TOKEN_AT));

Expand Down
24 changes: 20 additions & 4 deletions tests/classes/annotations.du
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class NoAnnotations {}
@NumberAnnotation(10)
@DecimalNumberAnnotation(10.5)
@NilAnnotation(nil)
@ListAnnotation([1, 2, 3, 4])
@DictAnnotation({1: 2, "test": "test", nil: nil, true: false, "dict": {"nested": "dict"}, "vals": [1, 2]})
class Test {

@EmptyAnnotation
Expand All @@ -21,6 +23,8 @@ class Test {
@NumberAnnotation(10)
@DecimalNumberAnnotation(10.5)
@NilAnnotation(nil)
@ListAnnotation([1, 2, 3, 4])
@DictAnnotation({1: 2, "test": "test", nil: nil, true: false, "dict": {"nested": "dict"}, "vals": [1, 2]})
methodOnAClass() {}

private noMethodAnnotations() {}
Expand All @@ -39,6 +43,8 @@ class AnnotatedClassVars {
@NumberAnnotation(10)
@DecimalNumberAnnotation(10.5)
@NilAnnotation(nil)
@ListAnnotation([1, 2, 3, 4])
@DictAnnotation({1: 2, "test": "test", nil: nil, true: false, "dict": {"nested": "dict"}, "vals": [1, 2]})
var classVariable = 1;

@EmptyAnnotation
Expand All @@ -47,6 +53,8 @@ class AnnotatedClassVars {
@NumberAnnotation(10)
@DecimalNumberAnnotation(10.5)
@NilAnnotation(nil)
@ListAnnotation([1, 2, 3, 4])
@DictAnnotation({1: 2, "test": "test", nil: nil, true: false, "dict": {"nested": "dict"}, "vals": [1, 2]})
const classConstant = 2;
}

Expand All @@ -57,26 +65,30 @@ class TestClassAnnotations < UnitTest {
}

testClassAnnotations() {
this.assertEquals(Test.classAnnotations.len(), 6);
this.assertEquals(Test.classAnnotations.len(), 8);
this.assertEquals(Test.classAnnotations['EmptyAnnotation'], nil);
this.assertEquals(Test.classAnnotations['TrueAnnotation'], true);
this.assertEquals(Test.classAnnotations['FalseAnnotation'], false);
this.assertEquals(Test.classAnnotations['NumberAnnotation'], 10);
this.assertEquals(Test.classAnnotations['DecimalNumberAnnotation'], 10.5);
this.assertEquals(Test.classAnnotations['NilAnnotation'], nil);
this.assertEquals(Test.classAnnotations['ListAnnotation'], [1, 2, 3, 4]);
this.assertEquals(Test.classAnnotations['DictAnnotation'], {1: 2, "test": "test", nil: nil, true: false, "dict": {"nested": "dict"}, "vals": [1, 2]});
}

testMethodAnnotations() {
this.assertEquals(NoAnnotations.methodAnnotations, nil);

this.assertEquals(Test.methodAnnotations.len(), 1);
this.assertEquals(Test.methodAnnotations['methodOnAClass'].len(), 6);
this.assertEquals(Test.methodAnnotations['methodOnAClass'].len(), 8);
this.assertEquals(Test.methodAnnotations['methodOnAClass']['EmptyAnnotation'], nil);
this.assertEquals(Test.methodAnnotations['methodOnAClass']['TrueAnnotation'], true);
this.assertEquals(Test.methodAnnotations['methodOnAClass']['FalseAnnotation'], false);
this.assertEquals(Test.methodAnnotations['methodOnAClass']['NumberAnnotation'], 10);
this.assertEquals(Test.methodAnnotations['methodOnAClass']['DecimalNumberAnnotation'], 10.5);
this.assertEquals(Test.methodAnnotations['methodOnAClass']['NilAnnotation'], nil);
this.assertEquals(Test.methodAnnotations['methodOnAClass']['ListAnnotation'], [1, 2, 3, 4]);
this.assertEquals(Test.methodAnnotations['methodOnAClass']['DictAnnotation'], {1: 2, "test": "test", nil: nil, true: false, "dict": {"nested": "dict"}, "vals": [1, 2]});
}

testInitAnnotation() {
Expand All @@ -86,21 +98,25 @@ class TestClassAnnotations < UnitTest {
testClassVarAnnotations() {
this.assertEquals(AnnotatedClassVars.fieldAnnotations.len(), 2);

this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable'].len(), 6);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable'].len(), 8);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable']['EmptyAnnotation'], nil);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable']['TrueAnnotation'], true);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable']['FalseAnnotation'], false);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable']['NumberAnnotation'], 10);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable']['DecimalNumberAnnotation'], 10.5);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable']['NilAnnotation'], nil);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable']['ListAnnotation'], [1, 2, 3, 4]);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classVariable']['DictAnnotation'], {1: 2, "test": "test", nil: nil, true: false, "dict": {"nested": "dict"}, "vals": [1, 2]});

this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant'].len(), 6);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant'].len(), 8);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant']['EmptyAnnotation'], nil);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant']['TrueAnnotation'], true);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant']['FalseAnnotation'], false);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant']['NumberAnnotation'], 10);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant']['DecimalNumberAnnotation'], 10.5);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant']['NilAnnotation'], nil);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant']['ListAnnotation'], [1, 2, 3, 4]);
this.assertEquals(AnnotatedClassVars.fieldAnnotations['classConstant']['DictAnnotation'], {1: 2, "test": "test", nil: nil, true: false, "dict": {"nested": "dict"}, "vals": [1, 2]});
}
}

Expand Down

0 comments on commit 1c448f1

Please sign in to comment.