Search in sources :

Example 26 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqPutFunction method call.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
    Object coll = arg1.getValue(env);
    Object key = arg2.getValue(env);
    Object val = arg3.getValue(env);
    if (coll == null) {
        throw new NullPointerException("null seq");
    }
    Class<?> clazz = coll.getClass();
    Object previousVal = null;
    if (List.class.isAssignableFrom(clazz)) {
        int index = ((Number) key).intValue();
        previousVal = ((List) coll).set(index, val);
    } else if (Map.class.isAssignableFrom(clazz)) {
        previousVal = ((Map) coll).put(key, val);
    } else if (clazz.isArray()) {
        int index = ((Number) key).intValue();
        previousVal = ArrayUtils.get(coll, index);
        ArrayUtils.set(coll, index, val);
    } else {
        throw new IllegalArgumentException(arg1.desc(env) + " can't put elements.");
    }
    return AviatorRuntimeJavaType.valueOf(previousVal);
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map)

Example 27 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqRemoveFunction method call.

@SuppressWarnings("rawtypes")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object coll = arg1.getValue(env);
    Object element = arg2.getValue(env);
    if (coll == null) {
        throw new NullPointerException("null seq");
    }
    Class<?> clazz = coll.getClass();
    if (Collection.class.isAssignableFrom(clazz)) {
        ((Collection) coll).remove(element);
        return arg1;
    } else if (Map.class.isAssignableFrom(clazz)) {
        ((Map) coll).remove(element);
        return arg1;
    } else {
        throw new IllegalArgumentException(arg1.desc(env) + " is not a collection or map.");
    }
}
Also used : Collection(java.util.Collection) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map)

Example 28 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqReverseFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
    Object first = arg1.getValue(env);
    if (first == null) {
        return AviatorNil.NIL;
    }
    Class<?> clazz = first.getClass();
    if (List.class.isAssignableFrom(clazz)) {
        List<?> list = (List<?>) first;
        Collections.reverse(list);
        return arg1;
    } else if (clazz.isArray()) {
        int length = ArrayUtils.getLength(first);
        for (int i = 0; i < length / 2; i++) {
            Object temp = ArrayUtils.get(first, i);
            ArrayUtils.set(first, i, ArrayUtils.get(first, length - 1 - i));
            ArrayUtils.set(first, length - 1 - i, temp);
        }
        return arg1;
    } else {
        throw new IllegalArgumentException(arg1.desc(env) + " is not an array or list.");
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) List(java.util.List)

Example 29 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class OperationRuntime method eval.

/**
 * Eval with arguments array.
 *
 * @param args
 * @param opType
 * @return
 */
public static AviatorObject eval(final Map<String, Object> env, final AviatorObject[] args, final OperatorType opType) {
    AviatorFunction func = RuntimeUtils.getInstance(env).getOpFunction(opType);
    AviatorObject ret = eval0(env, args, opType, func);
    if (RuntimeUtils.isTracedEval(env)) {
        trace(env, opType, ret, args);
    }
    return ret;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction)

Example 30 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqSortFunction method call.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object first = arg1.getValue(env);
    Comparator comparator = (Comparator) arg2.getValue(env);
    if (first == null) {
        return AviatorNil.NIL;
    }
    if (comparator == null) {
        throw new IllegalArgumentException("null comparator");
    }
    Class<?> clazz = first.getClass();
    if (List.class.isAssignableFrom(clazz)) {
        List<?> list = (List<?>) first;
        Object[] a = list.toArray();
        Arrays.sort(a, comparator);
        return AviatorRuntimeJavaType.valueOf(Arrays.asList(a));
    } else if (clazz.isArray()) {
        int length = ArrayUtils.getLength(first);
        Object[] dup = (Object[]) Array.newInstance(first.getClass().getComponentType(), length);
        for (int i = 0; i < length; i++) {
            dup[i] = ArrayUtils.get(first, i);
        }
        // System.arraycopy(array, 0, dup, 0, dup.length);
        Arrays.sort(dup, comparator);
        return AviatorRuntimeJavaType.valueOf(dup);
    } else {
        throw new IllegalArgumentException(arg1.desc(env) + " is not an array or list.");
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) List(java.util.List) Comparator(java.util.Comparator)

Aggregations

AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)110 Test (org.junit.Test)51 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)25 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)22 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)16 HashMap (java.util.HashMap)12 Map (java.util.Map)11 List (java.util.List)10 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)9 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)7 Env (com.googlecode.aviator.utils.Env)6 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)5 LinkedList (java.util.LinkedList)5 Collection (java.util.Collection)4 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)3 Sequence (com.googlecode.aviator.runtime.type.Sequence)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3