com.khubla.jvmbasic.jvmbasicc.function.impl.rule.addingExpressionRule.java Source code

Java tutorial

Introduction

Here is the source code for com.khubla.jvmbasic.jvmbasicc.function.impl.rule.addingExpressionRule.java

Source

package com.khubla.jvmbasic.jvmbasicc.function.impl.rule;

/*
 * jvmBasic Copyright 2012, khubla.com
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import org.antlr.v4.runtime.CommonToken;
import org.objectweb.asm.Opcodes;

import com.khubla.jvmbasic.jvmbasicc.antlr.jvmBasicParser;
import com.khubla.jvmbasic.jvmbasicc.antlr.jvmBasicParser.AddingExpressionContext;
import com.khubla.jvmbasic.jvmbasicc.compiler.Dispatcher;
import com.khubla.jvmbasic.jvmbasicc.compiler.GenerationContext;
import com.khubla.jvmbasic.jvmbasicc.compiler.RTLHelper;
import com.khubla.jvmbasic.jvmbasicc.function.BaseFunction;

/**
 * @author tome
 */
public class addingExpressionRule extends BaseFunction {
    @Override
    public boolean execute(GenerationContext generationContext) throws Exception {
        try {
            /*
             * 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);
        }
    }
}