Example usage for org.objectweb.asm Opcodes GETFIELD

List of usage examples for org.objectweb.asm Opcodes GETFIELD

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes GETFIELD.

Prototype

int GETFIELD

To view the source code for org.objectweb.asm Opcodes GETFIELD.

Click Source Link

Usage

From source file:com.khubla.jvmbasic.jvmbasicc.compiler.RTLHelper.java

License:Open Source License

/**
 * push a value onto the Execution Context stack
 * <p>/*w w  w .  ja va2 s  . c om*/
 * <code>
 * executionContext.push(db);
 * </code>
 * </p>
 */
public static void push(GenerationContext generationContext, double db) {
    generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
    generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
            EXECUTIONCONTEXT_NAME, JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
    generationContext.getMethodVisitor().visitLdcInsn(new Double(db));
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf",
            "(D)Ljava/lang/Double;");
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, JASIC_RUNTIME_EXECUTIONCONTEXT,
            "push", "(Ljava/lang/Double;)V");
}

From source file:com.khubla.jvmbasic.jvmbasicc.compiler.RTLHelper.java

License:Open Source License

/**
 * push a value onto the Execution Context stack
 * <p>/*w  w w .j  a va2s  .  c  o  m*/
 * <code>
 * executionContext.push(i);
 * </code>
 * </p>
 */
public static void push(GenerationContext generationContext, int i) {
    generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
    generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
            EXECUTIONCONTEXT_NAME, JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
    generationContext.getMethodVisitor().visitIntInsn(Opcodes.BIPUSH, i);
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
            "(I)Ljava/lang/Integer;");
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, JASIC_RUNTIME_EXECUTIONCONTEXT,
            "push", "(Ljava/lang/Integer;)V");
}

From source file:com.khubla.jvmbasic.jvmbasicc.compiler.RTLHelper.java

License:Open Source License

/**
 * push a value onto the Execution Context stack
 * <p>/* ww  w .ja  v  a 2 s.  co m*/
 * <code>
 * executionContext.push(str);
 * </code>
 * </p>
 */
public static void push(GenerationContext generationContext, String str) {
    generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
    generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
            EXECUTIONCONTEXT_NAME, JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
    generationContext.getMethodVisitor().visitLdcInsn(str);
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, JASIC_RUNTIME_EXECUTIONCONTEXT,
            "push", "(Ljava/lang/String;)V");
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.absfuncRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {// ww w . j a  v  a  2 s  .  c  om
        /*
         * get the argument
         */
        Dispatcher.dispatchChildren(generationContext);
        /*
         * pop the argument and apply abs
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, RTLHelper.JASIC_RUNTIME_MATH,
                "ABS", "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Ljava/lang/Double;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/Double;)V");
        return true;
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.addingExpressionRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {// w  w w.j av  a  2s .c o m
        /*
         * get the operands
         */
        Dispatcher.dispatchChildren(generationContext);
        /*
         * if there are 3 values, we add them
         */
        if (generationContext.getParseTree().getChildCount() == 3) {
            /*
             * operation
             */
            final AddingExpressionContext addingExpressionContext = (AddingExpressionContext) generationContext
                    .getParseTree();
            final CommonToken commonToken = (CommonToken) addingExpressionContext.getChild(1).getPayload();
            if (commonToken.getType() == jvmBasicParser.PLUS) {
                /*
                 * add
                 */
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                        "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "resolveValue",
                        "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        "com/khubla/jvmbasic/jvmbasicrt/Value", "getDouble", "()Ljava/lang/Double;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double",
                        "doubleValue", "()D");
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                        "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "resolveValue",
                        "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        "com/khubla/jvmbasic/jvmbasicrt/Value", "getDouble", "()Ljava/lang/Double;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double",
                        "doubleValue", "()D");
                generationContext.getMethodVisitor().visitInsn(Opcodes.DADD);
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double",
                        "valueOf", "(D)Ljava/lang/Double;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/Double;)V");
            } else {
                /*
                 * swap the stack arguments so we have the prompt on top and the variable name next
                 */
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "swap", "()V");
                /*
                 * subtract
                 */
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                        "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "resolveValue",
                        "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        "com/khubla/jvmbasic/jvmbasicrt/Value", "getDouble", "()Ljava/lang/Double;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double",
                        "doubleValue", "()D");
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
                generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                        generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                        "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "resolveValue",
                        "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        "com/khubla/jvmbasic/jvmbasicrt/Value", "getDouble", "()Ljava/lang/Double;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double",
                        "doubleValue", "()D");
                generationContext.getMethodVisitor().visitInsn(Opcodes.DSUB);
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double",
                        "valueOf", "(D)Ljava/lang/Double;");
                generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/Double;)V");
            }
            return true;
        } else {
            /**
             * do nothing
             */
            return true;
        }
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.atnfuncRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {/*w w  w. j a  v a2s.c o m*/
        /*
         * get the argument
         */
        Dispatcher.dispatchChildren(generationContext);
        /*
         * pop the argument and apply abs
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, RTLHelper.JASIC_RUNTIME_MATH,
                "ATAN", "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Ljava/lang/Double;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/Double;)V");
        return true;
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.cosfuncRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {// w w w.  j  a va2s  . c o m
        /*
         * get the argument
         */
        Dispatcher.dispatchChildren(generationContext);
        /*
         * pop the argument and apply abs
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, RTLHelper.JASIC_RUNTIME_MATH,
                "COS", "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Ljava/lang/Double;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/Double;)V");
        return true;
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.exponentExpressionRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {/*w  w  w  .j  a  v  a  2 s.com*/
        /*
         * get the argument
         */
        Dispatcher.dispatchChildren(generationContext);
        /*
         * exponentiate if there were 3 args
         */
        if (generationContext.getParseTree().getChildCount() == 3) {
            /*
             * pop the argument and apply exp
             */
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                    "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC,
                    RTLHelper.JASIC_RUNTIME_MATH, "EXP",
                    "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Ljava/lang/Double;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/Double;)V");
        }
        return true;
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.ifstmtRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {/*from  w w w.j  a v a2 s  .c o m*/
        if (generationContext.getParseTree().getChildCount() == 4) {
            /*
             * dispatch into the test
             */
            final GenerationContext testGenerationContext = new GenerationContext(generationContext,
                    generationContext.getParseTree().getChild(1));
            Dispatcher.dispatchChildren(testGenerationContext);
            /*
             * check the result
             */
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                    "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    "com/khubla/jvmbasic/jvmbasicrt/Value", "getBoolean", "()Ljava/lang/Boolean;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean",
                    "booleanValue", "()Z");
            /*
             * check result
             */
            final Label l1 = new Label();
            generationContext.getMethodVisitor().visitJumpInsn(Opcodes.IFEQ, l1);
            /*
             * process the contained tree
             */
            final GenerationContext statementGenerationContext = new GenerationContext(generationContext,
                    generationContext.getParseTree().getChild(3));
            Dispatcher.dispatch(statementGenerationContext);
            /*
             * label to skip to
             */
            generationContext.getMethodVisitor().visitLabel(l1);
            generationContext.getMethodVisitor().visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            return true;
        } else if (generationContext.getParseTree().getChildCount() == 3) {
            /*
             * dispatch into the test
             */
            final GenerationContext testGenerationContext = new GenerationContext(generationContext,
                    generationContext.getParseTree().getChild(1));
            Dispatcher.dispatchChildren(testGenerationContext);
            /*
             * check the result
             */
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                    "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    "com/khubla/jvmbasic/jvmbasicrt/Value", "getBoolean", "()Ljava/lang/Boolean;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean",
                    "booleanValue", "()Z");
            /*
             * check result
             */
            final Label l1 = new Label();
            generationContext.getMethodVisitor().visitJumpInsn(Opcodes.IFEQ, l1);
            /*
             * process the contained tree
             */
            final GenerationContext statementGenerationContext = new GenerationContext(generationContext,
                    generationContext.getParseTree().getChild(2));
            Dispatcher.dispatch(statementGenerationContext);
            /*
             * label to skip to
             */
            generationContext.getMethodVisitor().visitLabel(l1);
            generationContext.getMethodVisitor().visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            return true;
        } else {
            throw new Exception(
                    "Invalid number of tokens in subtree '" + generationContext.getParseTree().getChildCount()
                            + "' expected 3 on line number: " + generationContext.getLineNumber());
        }
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.inputstmtRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {//from w  w w .j ava 2  s .  c o m
        /*
         * process the contained nodes. This will push the String prompt onto the stack, and a variable name
         */
        Dispatcher.dispatchChildren(generationContext);
        /*
         * input with a prompt? INPUT <prompt> , <variable>
         */
        if (generationContext.getParseTree().getChildCount() == 4) {
            /*
             * swap the stack arguments so we have the prompt on top and the variable name next
             */
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "swap", "()V");
            /*
             * print the prompt
             */
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out",
                    "Ljava/io/PrintStream;");
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                    "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    "com/khubla/jvmbasic/jvmbasicrt/Value", "getAsString", "()Ljava/lang/String;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream",
                    "print", "(Ljava/lang/String;)V");
        }
        /*
         * read a line and push onto value stack
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
        generationContext.getMethodVisitor().visitInsn(Opcodes.DUP);
        generationContext.getMethodVisitor().visitLdcInsn("\"");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder",
                "<init>", "(Ljava/lang/String;)V");
        generationContext.getMethodVisitor().visitTypeInsn(Opcodes.NEW, "java/io/BufferedReader");
        generationContext.getMethodVisitor().visitInsn(Opcodes.DUP);
        generationContext.getMethodVisitor().visitTypeInsn(Opcodes.NEW, "java/io/InputStreamReader");
        generationContext.getMethodVisitor().visitInsn(Opcodes.DUP);
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                "inputStream", "Ljava/io/InputStream;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESPECIAL, "java/io/InputStreamReader",
                "<init>", "(Ljava/io/InputStream;)V");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESPECIAL, "java/io/BufferedReader",
                "<init>", "(Ljava/io/Reader;)V");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/BufferedReader",
                "readLine", "()Ljava/lang/String;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder",
                "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
        generationContext.getMethodVisitor().visitLdcInsn("\"");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder",
                "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder",
                "toString", "()Ljava/lang/String;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/String;)V");
        /*
         * swap the stack arguments so we have the variable name on top and the value below it
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "swap", "()V");
        /*
         * set the variable
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                "com/khubla/jvmbasic/jvmbasicrt/Value", "getString", "()Ljava/lang/String;");
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "setVariable",
                "(Ljava/lang/String;Lcom/khubla/jvmbasic/jvmbasicrt/Value;)V");
        return true;
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}