Search in sources :

Example 1 with CodeMethod

use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.

the class Property method initSetter.

@Override
public IMethod initSetter() {
    if (this.setter != null) {
        return this.setter;
    }
    final Name name = Name.from(this.name.unqualified + "_=", this.name.qualified + "_$eq");
    this.setter = new CodeMethod(this.enclosingClass, name, Types.VOID);
    this.setter.setPosition(this.position);
    this.setterParameter = new CodeParameter(this.setter, this.position, Names.newValue, this.type);
    this.setter.getParameters().add(this.setterParameter);
    return this.setter;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) Name(dyvil.lang.Name)

Example 2 with CodeMethod

use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.

the class Property method initGetter.

@Override
public IMethod initGetter() {
    if (this.getter != null) {
        return this.getter;
    }
    final CodeMethod getter = new CodeMethod(this.enclosingClass, this.name, this.type);
    getter.setPosition(this.position);
    return this.getter = getter;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod)

Example 3 with CodeMethod

use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.

the class Template method parse.

@Override
public void parse() {
    // class NAME { }
    final CodeClass theClass = new CodeClass(null, this.name, AttributeList.of(Modifiers.PUBLIC));
    final ClassBody classBody = new ClassBody(theClass);
    theClass.setBody(classBody);
    this.addClass(theClass);
    this.templateClass = theClass;
    // func generate(spec, writer) -> void = { ... }
    final CodeMethod genMethod = new CodeMethod(theClass, Name.fromRaw("generate"), Types.VOID, AttributeList.of(Modifiers.PUBLIC | Modifiers.OVERRIDE));
    final StatementList directives = new StatementList();
    genMethod.setValue(directives);
    classBody.addMethod(genMethod);
    this.genMethod = genMethod;
    // func main(args) -> void
    final CodeMethod mainMethod = new CodeMethod(theClass, Name.fromRaw("main"), Types.VOID, AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC));
    classBody.addMethod(mainMethod);
    this.mainMethod = mainMethod;
    // Parse
    new ParserManager(DyvilSymbols.INSTANCE, this.tokens.iterator(), this.markers).parse(new BlockParser(this, directives));
}
Also used : ParserManager(dyvilx.tools.parsing.ParserManager) BlockParser(dyvilx.tools.gensrc.parser.BlockParser) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) CodeClass(dyvilx.tools.compiler.ast.classes.CodeClass) ClassBody(dyvilx.tools.compiler.ast.classes.ClassBody)

Example 4 with CodeMethod

use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.

the class CaseClassMetadata method createUnapplyMethod.

private CodeMethod createUnapplyMethod() {
    // static final func unapply<TypeParams...>(value: This) -> (T...)
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
    final IType type = this.getUnapplyReturnType();
    final CodeMethod unapply = new CodeMethod(this.theClass, Names.unapply, type, attributes);
    unapply.setPosition(position);
    unapply.getTypeParameters().addAll(this.theClass.getTypeParameters());
    final CodeParameter parameter = new CodeParameter(unapply, position, Names.value, this.theClass.getThisType());
    unapply.getParameters().add(parameter);
    // = (value.classParams...)
    final TupleExpr tupleExpr = new TupleExpr(position);
    final ArgumentList arguments = tupleExpr.getValues();
    for (IParameter param : this.theClass.getParameters()) {
        // value
        final FieldAccess thisAccess = new FieldAccess(position, null, parameter);
        // value.classParam
        final IValue fieldAccess;
        if (param.isOverride()) {
            // if the class parameter is marked as 'override', we have to resolve it from a super-class
            // the easiest way to do this is by name
            fieldAccess = new FieldAccess(position, thisAccess, param.getName());
        } else {
            fieldAccess = new FieldAccess(position, thisAccess, param);
        }
        arguments.add(fieldAccess);
    }
    unapply.setValue(tupleExpr);
    return unapply;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) TupleExpr(dyvilx.tools.compiler.ast.expression.TupleExpr) IType(dyvilx.tools.compiler.ast.type.IType)

Example 5 with CodeMethod

use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.

the class EnumClassMetadata method createFromNameMethod.

private CodeMethod createFromNameMethod() {
    // @BytecodeName("valueOf")
    // public static func from(name: String) -> EnumType
    final SourcePosition position = this.theClass.position();
    final IType type = this.theClass.getClassType();
    final CodeMethod method = new CodeMethod(this.theClass, Name.fromRaw("from"), type, AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC));
    method.setPosition(position);
    method.setInternalName("valueOf");
    final CodeParameter parameter = new CodeParameter(Name.fromRaw("name"), Types.STRING);
    method.getParameters().add(parameter);
    // = Enum.valueOf(class<EnumType>, name)
    final MethodCall valueOfCall = new MethodCall(position, new ClassAccess(Types.ENUM), Name.fromRaw("valueOf"), new ArgumentList(new ClassOperator(this.theClass.getClassType()), new FieldAccess(parameter)));
    method.setValue(valueOfCall);
    return method;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) ClassOperator(dyvilx.tools.compiler.ast.expression.ClassOperator) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)9 SourcePosition (dyvil.source.position.SourcePosition)6 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)5 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)5 IType (dyvilx.tools.compiler.ast.type.IType)5 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)4 IValue (dyvilx.tools.compiler.ast.expression.IValue)3 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)3 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)2 Name (dyvil.lang.Name)1 ClassBody (dyvilx.tools.compiler.ast.classes.ClassBody)1 CodeClass (dyvilx.tools.compiler.ast.classes.CodeClass)1 CastOperator (dyvilx.tools.compiler.ast.expression.CastOperator)1 ClassOperator (dyvilx.tools.compiler.ast.expression.ClassOperator)1 InstanceOfOperator (dyvilx.tools.compiler.ast.expression.InstanceOfOperator)1 TupleExpr (dyvilx.tools.compiler.ast.expression.TupleExpr)1 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)1 NullValue (dyvilx.tools.compiler.ast.expression.constant.NullValue)1 VarargsOperator (dyvilx.tools.compiler.ast.expression.intrinsic.VarargsOperator)1