Search in sources :

Example 1 with ArgumentList

use of dyvilx.tools.compiler.ast.parameter.ArgumentList in project Dyvil by Dyvil.

the class IValue method fromObject.

static IValue fromObject(Object o) {
    if (o == null) {
        return new NullValue();
    }
    Class c = o.getClass();
    if (c == Character.class) {
        return new CharValue(null, o.toString(), true);
    } else if (c == Boolean.class) {
        return new BooleanValue((Boolean) o);
    } else if (c == Integer.class) {
        return new IntValue((Integer) o);
    } else if (c == Long.class) {
        return new LongValue((Long) o);
    } else if (c == Float.class) {
        return new FloatValue((Float) o);
    } else if (c == Double.class) {
        return new DoubleValue((Double) o);
    } else if (c == String.class) {
        return new StringValue((String) o);
    } else if (c == int[].class) {
        final ArrayExpr valueList = new ArrayExpr();
        final ArgumentList values = valueList.getValues();
        valueList.setElementType(Types.INT);
        for (int i : (int[]) o) {
            values.add(new IntValue(i));
        }
        return valueList;
    } else if (c == long[].class) {
        final ArrayExpr valueList = new ArrayExpr();
        final ArgumentList values = valueList.getValues();
        valueList.setElementType(Types.LONG);
        for (long l : (long[]) o) {
            values.add(new LongValue(l));
        }
        return valueList;
    } else if (c == float[].class) {
        final ArrayExpr valueList = new ArrayExpr();
        final ArgumentList values = valueList.getValues();
        valueList.setElementType(Types.FLOAT);
        for (float f : (float[]) o) {
            values.add(new FloatValue(f));
        }
        return valueList;
    } else if (c == double[].class) {
        final ArrayExpr valueList = new ArrayExpr();
        final ArgumentList values = valueList.getValues();
        valueList.setElementType(Types.DOUBLE);
        for (double d : (double[]) o) {
            values.add(new DoubleValue(d));
        }
        return valueList;
    } else if (c == dyvilx.tools.asm.Type.class) {
        dyvilx.tools.asm.Type type = (dyvilx.tools.asm.Type) o;
        return new ClassOperator(Types.fromASMType(type));
    }
    return null;
}
Also used : ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) IType(dyvilx.tools.compiler.ast.type.IType)

Example 2 with ArgumentList

use of dyvilx.tools.compiler.ast.parameter.ArgumentList in project Dyvil by Dyvil.

the class MapExpr method withType.

@Override
public IValue withType(IType type, ITypeContext typeContext, MarkerList markers, IContext context) {
    if (!Types.isSuperClass(type, MapType.MapTypes.IMMUTABLE_MAP_CLASS.getClassType())) {
        final Annotation annotation = type.getTheClass().getAnnotation(LazyTypes.MAP_CONVERTIBLE_CLASS);
        if (annotation == null) {
            return null;
        }
        // [ x : y, ... ] -> Type(x : y, ...))
        final int size = this.keys.size();
        final ArgumentList arguments = new ArgumentList(size);
        for (int i = 0; i < size; i++) {
            arguments.add(new ColonOperator(this.keys.get(i), this.values.get(i)));
        }
        return new LiteralConversion(this, annotation, arguments).withType(type, typeContext, markers, context);
    }
    final int size = this.keys.size();
    final IType keyType = this.keyType = Types.resolveTypeSafely(type, MapType.MapTypes.KEY_VARIABLE);
    final IType valueType = this.valueType = Types.resolveTypeSafely(type, MapType.MapTypes.VALUE_VARIABLE);
    for (int i = 0; i < size; i++) {
        final IValue key = TypeChecker.convertValue(this.keys.get(i), keyType, typeContext, markers, context, KEY_MARKER_SUPPLIER);
        this.keys.set(i, key);
        final IValue value = TypeChecker.convertValue(this.values.get(i), valueType, typeContext, markers, context, VALUE_MARKER_SUPPLIER);
        this.values.set(i, value);
    }
    return this;
}
Also used : ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) Annotation(dyvilx.tools.compiler.ast.attribute.annotation.Annotation) IType(dyvilx.tools.compiler.ast.type.IType)

Example 3 with ArgumentList

use of dyvilx.tools.compiler.ast.parameter.ArgumentList in project Dyvil by Dyvil.

the class ApplyAccess method resolve.

@Override
public IValue resolve(MarkerList markers, IContext context) {
    // -> with(x..., { statements })
    if (this.arguments.size() == 1 && this.receiver instanceof ICall) {
        final ICall call = (ICall) this.receiver;
        IValue argument = this.arguments.getFirst();
        if (argument instanceof Closure) {
            argument = argument.resolve(markers, context);
            final ArgumentList oldArgs = call.getArguments();
            call.resolveReceiver(markers, context);
            call.resolveArguments(markers, context);
            call.setArguments(oldArgs.appended(null, argument));
            final IValue resolvedCall = call.resolveCall(markers, context, false);
            if (resolvedCall != null) {
                return resolvedCall;
            }
            // Revert
            call.setArguments(oldArgs);
            this.receiver = call.resolveCall(markers, context, true);
            return this.resolveCall(markers, context, true);
        }
    }
    return super.resolve(markers, context);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) Closure(dyvilx.tools.compiler.ast.statement.Closure) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList)

Example 4 with ArgumentList

use of dyvilx.tools.compiler.ast.parameter.ArgumentList in project Dyvil by Dyvil.

the class ApplyAccess method toCompoundAssignment.

@Override
public IValue toCompoundAssignment(IValue rhs, SourcePosition position, MarkerList markers, IContext context, SideEffectHelper helper) {
    // x(y...) (op)= z
    // -> x(y...) = x(y...) (op) z
    // note that x and y... are each only evaluated once
    final IValue applyReceiver = helper.processValue(this.receiver);
    final ArgumentList applyArguments = helper.processArguments(this.arguments);
    this.arguments = applyArguments;
    this.receiver = applyReceiver;
    return new ApplyAssignment(position, applyReceiver, applyArguments, rhs).resolveCall(markers, context, true);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList)

Example 5 with ArgumentList

use of dyvilx.tools.compiler.ast.parameter.ArgumentList in project Dyvil by Dyvil.

the class ICall method toLambda.

default IValue toLambda(MarkerList markers, IContext context, int wildcards) {
    SourcePosition position = this.getPosition();
    final IParameter[] parameters = new IParameter[wildcards];
    for (int i = 0; i < wildcards; i++) {
        parameters[i] = new CodeParameter(null, position, Name.fromRaw("wildcard$" + i), Types.UNKNOWN, new AttributeList());
    }
    int parIndex = 0;
    final IValue receiver = this.getReceiver();
    if (receiver != null && receiver.isPartialWildcard()) {
        this.setReceiver(receiver.withLambdaParameter(parameters[parIndex++]));
    }
    final ArgumentList arguments = this.getArguments();
    for (int i = 0, size = arguments.size(); i < size; i++) {
        final IValue argument = arguments.get(i, null);
        if (argument.isPartialWildcard()) {
            arguments.set(i, null, argument.withLambdaParameter(parameters[parIndex++]));
        }
    }
    final LambdaExpr lambdaExpr = new LambdaExpr(position, parameters, wildcards);
    lambdaExpr.setImplicitParameters(true);
    lambdaExpr.setValue(this);
    return lambdaExpr.resolve(markers, context);
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) SourcePosition(dyvil.source.position.SourcePosition) LambdaExpr(dyvilx.tools.compiler.ast.expression.LambdaExpr) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter)

Aggregations

ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)37 IValue (dyvilx.tools.compiler.ast.expression.IValue)17 IType (dyvilx.tools.compiler.ast.type.IType)10 SourcePosition (dyvil.source.position.SourcePosition)8 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)7 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)5 ArrayExpr (dyvilx.tools.compiler.ast.expression.ArrayExpr)5 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)5 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)5 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)5 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)4 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)4 MarkerLevel (dyvil.util.MarkerLevel)3 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)3 StringValue (dyvilx.tools.compiler.ast.expression.constant.StringValue)3 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)3 Marker (dyvilx.tools.parsing.marker.Marker)3 Reason (dyvil.annotation.Deprecated.Reason)2 Name (dyvil.lang.Name)2 IClass (dyvilx.tools.compiler.ast.classes.IClass)2