Search in sources :

Example 1 with Expression

use of com.github.anba.es6draft.ast.Expression in project es6draft by anba.

the class SwitchStatementGenerator method stringSwitchEntries.

private static long[] stringSwitchEntries(List<SwitchClause> clauses, boolean hasDefault) {
    long[] entries = new long[clauses.size() - (hasDefault ? 1 : 0)];
    for (int i = 0, j = 0, size = clauses.size(); i < size; ++i) {
        Expression expr = clauses.get(i).getExpression();
        if (expr != null) {
            entries[j++] = Entry(((StringLiteral) expr).getValue().hashCode(), i);
        }
    }
    // sort values in ascending order
    Arrays.sort(entries);
    return entries;
}
Also used : BinaryExpression(com.github.anba.es6draft.ast.BinaryExpression) UnaryExpression(com.github.anba.es6draft.ast.UnaryExpression) Expression(com.github.anba.es6draft.ast.Expression)

Example 2 with Expression

use of com.github.anba.es6draft.ast.Expression in project es6draft by anba.

the class SwitchStatementGenerator method charSwitchEntries.

private static long[] charSwitchEntries(List<SwitchClause> clauses, boolean hasDefault) {
    long[] entries = new long[clauses.size() - (hasDefault ? 1 : 0)];
    for (int i = 0, j = 0, size = clauses.size(); i < size; ++i) {
        Expression expr = clauses.get(i).getExpression();
        if (expr != null) {
            entries[j++] = Entry(((StringLiteral) expr).getValue().charAt(0), i);
        }
    }
    // sort values in ascending order
    Arrays.sort(entries);
    return entries;
}
Also used : BinaryExpression(com.github.anba.es6draft.ast.BinaryExpression) UnaryExpression(com.github.anba.es6draft.ast.UnaryExpression) Expression(com.github.anba.es6draft.ast.Expression)

Example 3 with Expression

use of com.github.anba.es6draft.ast.Expression in project es6draft by anba.

the class SwitchStatementGenerator method intSwitchEntries.

private static long[] intSwitchEntries(List<SwitchClause> clauses, boolean hasDefault) {
    long[] entries = new long[clauses.size() - (hasDefault ? 1 : 0)];
    for (int i = 0, j = 0, size = clauses.size(); i < size; ++i) {
        Expression expr = clauses.get(i).getExpression();
        if (expr != null) {
            int value;
            if (expr instanceof NumericLiteral) {
                value = ((NumericLiteral) expr).intValue();
            } else {
                value = -((NumericLiteral) ((UnaryExpression) expr).getOperand()).intValue();
            }
            entries[j++] = Entry(value, i);
        }
    }
    // sort values in ascending order
    Arrays.sort(entries);
    return entries;
}
Also used : NumericLiteral(com.github.anba.es6draft.ast.NumericLiteral) BinaryExpression(com.github.anba.es6draft.ast.BinaryExpression) UnaryExpression(com.github.anba.es6draft.ast.UnaryExpression) Expression(com.github.anba.es6draft.ast.Expression)

Example 4 with Expression

use of com.github.anba.es6draft.ast.Expression in project es6draft by anba.

the class PropertyGenerator method visit.

/**
     * 12.2.5.8 Runtime Semantics: PropertyDefinitionEvaluation
     * <p>
     * PropertyDefinition : PropertyName : AssignmentExpression
     */
@Override
public ValType visit(PropertyValueDefinition node, CodeVisitor mv) {
    Expression propertyValue = node.getPropertyValue();
    PropertyName propertyName = node.getPropertyName();
    String propName = PropName(propertyName);
    long propIndex = propName != null ? IndexedMap.toIndex(propName) : -1;
    // stack: [<object>] -> []
    if (propName == null) {
        assert propertyName instanceof ComputedPropertyName;
        ValType type = propertyName.accept(this, mv);
        expressionBoxed(propertyValue, mv);
        if (IsAnonymousFunctionDefinition(propertyValue)) {
            SetFunctionName(propertyValue, type, mv);
        }
        mv.loadExecutionContext();
        mv.lineInfo(node);
        mv.invoke(Methods.ScriptRuntime_defineProperty);
    } else if ("__proto__".equals(propName) && codegen.isEnabled(CompatibilityOption.ProtoInitializer)) {
        expressionBoxed(propertyValue, mv);
        mv.loadExecutionContext();
        mv.lineInfo(node);
        mv.invoke(Methods.ScriptRuntime_defineProtoProperty);
    } else if (IndexedMap.isIndex(propIndex)) {
        mv.lconst(propIndex);
        expressionBoxed(propertyValue, mv);
        if (IsAnonymousFunctionDefinition(propertyValue)) {
            SetFunctionName(propertyValue, propName, mv);
        }
        mv.loadExecutionContext();
        mv.lineInfo(node);
        mv.invoke(Methods.ScriptRuntime_defineProperty_long);
    } else {
        mv.aconst(propName);
        expressionBoxed(propertyValue, mv);
        if (IsAnonymousFunctionDefinition(propertyValue)) {
            SetFunctionName(propertyValue, propName, mv);
        }
        mv.loadExecutionContext();
        mv.lineInfo(node);
        mv.invoke(Methods.ScriptRuntime_defineProperty_String);
    }
    return null;
}
Also used : PropertyName(com.github.anba.es6draft.ast.PropertyName) ComputedPropertyName(com.github.anba.es6draft.ast.ComputedPropertyName) Expression(com.github.anba.es6draft.ast.Expression) ComputedPropertyName(com.github.anba.es6draft.ast.ComputedPropertyName)

Example 5 with Expression

use of com.github.anba.es6draft.ast.Expression in project es6draft by anba.

the class SwitchStatementGenerator method emitGenericSwitch.

/**
     * <h3>Generic-switch</h3>
     * 
     * <pre>
     * switch (v) {
     * case key1: ...
     * case key2: ...
     * }
     * 
     * var $v = v;
     * if (strictEquals($v, key1)) goto L1
     * if (strictEquals($v, key2)) goto L2
     * goTo (default | break)
     * L1: ...
     * L2: ...
     * </pre>
     * 
     * @param clauses
     *            the switch clauses
     * @param labels
     *            the labels for each switch clause
     * @param defaultClause
     *            the label for the default clause
     * @param lblExit
     *            the exit label
     * @param switchValue
     *            the variable which holds the switch value
     * @param mv
     *            the code visitor
     */
private void emitGenericSwitch(List<SwitchClause> clauses, Jump[] labels, Jump defaultClause, Jump lblExit, Variable<?> switchValue, CodeVisitor mv) {
    assert switchValue.getType().equals(Types.Object);
    Jump switchDefault = defaultClause != null ? defaultClause : lblExit;
    int index = 0;
    for (SwitchClause switchClause : clauses) {
        Jump caseLabel = labels[index++];
        Expression expr = switchClause.getExpression();
        if (expr != null) {
            mv.load(switchValue);
            // 13.11.10 Runtime Semantics: CaseSelectorEvaluation
            expressionBoxed(expr, mv);
            invokeDynamicOperator(BinaryExpression.Operator.SHEQ, mv);
            mv.ifne(caseLabel);
        }
    }
    mv.goTo(switchDefault);
}
Also used : BinaryExpression(com.github.anba.es6draft.ast.BinaryExpression) UnaryExpression(com.github.anba.es6draft.ast.UnaryExpression) Expression(com.github.anba.es6draft.ast.Expression) SwitchClause(com.github.anba.es6draft.ast.SwitchClause) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Aggregations

Expression (com.github.anba.es6draft.ast.Expression)5 BinaryExpression (com.github.anba.es6draft.ast.BinaryExpression)4 UnaryExpression (com.github.anba.es6draft.ast.UnaryExpression)4 ComputedPropertyName (com.github.anba.es6draft.ast.ComputedPropertyName)1 NumericLiteral (com.github.anba.es6draft.ast.NumericLiteral)1 PropertyName (com.github.anba.es6draft.ast.PropertyName)1 SwitchClause (com.github.anba.es6draft.ast.SwitchClause)1 Jump (com.github.anba.es6draft.compiler.assembler.Jump)1