From 13c7a34d9ab4a9018c3a4e0154dd3043ae354051 Mon Sep 17 00:00:00 2001 From: dzikoysk Date: Sat, 23 Jan 2021 23:30:50 +0100 Subject: [PATCH] GH-550 Add simple type.panda test and fix call to another constructor through 'this' keyword --- examples/lang/type.panda | 33 +++++++++++++++++++ .../resource/syntax/type/SelfConstructor.java | 4 ++- .../panda/examples/lang/TypeTest.groovy | 14 ++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 examples/lang/type.panda create mode 100644 panda/src/test/groovy/org/panda_lang/panda/examples/lang/TypeTest.groovy diff --git a/examples/lang/type.panda b/examples/lang/type.panda new file mode 100644 index 000000000..575a8a1c8 --- /dev/null +++ b/examples/lang/type.panda @@ -0,0 +1,33 @@ +module lang + +main { + Foo defaultFoo = new Foo() + log 'Static' == Foo.staticField + log 'Static' == defaultFoo.staticField + log 'Default' == defaultFoo.field + log 'Default' == defaultFoo.field() + + Foo foo = new Foo('Custom') + log 'Custom' == foo.field + log 'Custom' == foo.field() +} + +open type Foo { + + open static String staticField = 'Static' + + internal String field + + constructor (String field) { + this.field = field + } + + constructor () { + this('Default') + } + + shared field () -> String { + return field + } + +} \ No newline at end of file diff --git a/panda/src/main/java/org/panda_lang/panda/language/resource/syntax/type/SelfConstructor.java b/panda/src/main/java/org/panda_lang/panda/language/resource/syntax/type/SelfConstructor.java index 51318b558..71ded775c 100644 --- a/panda/src/main/java/org/panda_lang/panda/language/resource/syntax/type/SelfConstructor.java +++ b/panda/src/main/java/org/panda_lang/panda/language/resource/syntax/type/SelfConstructor.java @@ -20,6 +20,7 @@ import org.panda_lang.language.architecture.dynamic.AbstractExecutableStatement; import org.panda_lang.language.architecture.expression.Expression; import org.panda_lang.language.architecture.expression.ExpressionUtils; +import org.panda_lang.language.architecture.type.member.constructor.ConstructorScope.ConstructorFrame; import org.panda_lang.language.architecture.type.member.constructor.TypeConstructor; import org.panda_lang.language.architecture.type.signature.AdjustedMember; import org.panda_lang.language.interpreter.source.Localizable; @@ -44,7 +45,8 @@ public SelfConstructor(Localizable localizable, AdjustedMember @Override public @Nullable Object execute(ProcessStack stack, Object instance) throws Exception { - return constructor.invoke(stack, instance, ExpressionUtils.evaluate(stack, instance, arguments)); + ConstructorFrame constructorFrame = (ConstructorFrame) instance; + return constructor.invoke(stack, constructorFrame.getInstance(), ExpressionUtils.evaluate(stack, instance, arguments)); } } diff --git a/panda/src/test/groovy/org/panda_lang/panda/examples/lang/TypeTest.groovy b/panda/src/test/groovy/org/panda_lang/panda/examples/lang/TypeTest.groovy new file mode 100644 index 000000000..91f6c5c3a --- /dev/null +++ b/panda/src/test/groovy/org/panda_lang/panda/examples/lang/TypeTest.groovy @@ -0,0 +1,14 @@ +package org.panda_lang.panda.examples.lang + +import groovy.transform.CompileStatic +import org.junit.jupiter.api.Test + +@CompileStatic +class TypeTest extends LangTestSpecification { + + @Test + void 'should handle types' () { + launch 'type.panda' + } + +}