Esempio n. 1
0
        public override GamaValueRef VisitExprOpLogic([NotNull] GamaParser.ExprOpLogicContext context)
        {
            var lhs = Visit(context.expr(0));
            var rhs = Visit(context.expr(1));
            var op  = context.opLogic().GetText();

            GamaFunctionList oplist;

            if (op == "&&")
            {
                oplist = lhs.Type.Meta.Operators.And;
            }
            else if (op == "||")
            {
                oplist = lhs.Type.Meta.Operators.Or;
            }
            else
            {
                oplist = lhs.Type.Meta.Operators.Xor;
            }

            var target = !IsEmptyTT ? TopTT : null;

            GamaFunctionRef fnref = null;

            if (target == null)
            {
                fnref = oplist.FindFunction(new[] { lhs.Type, rhs.Type });
            }
            else
            {
                fnref = oplist.FindFunction(target, new[] { lhs.Type, rhs.Type });
            }

            if (fnref == null)
            {
                GamaCompiledFunctionList coplist;
                if (op == "&&")
                {
                    coplist = lhs.Type.Meta.CompiledOperators.And;
                }
                else if (op == "||")
                {
                    coplist = lhs.Type.Meta.CompiledOperators.Or;
                }
                else
                {
                    coplist = lhs.Type.Meta.CompiledOperators.Xor;
                }

                GamaCompiledFunctionRef cfnref = null;
                if (target == null)
                {
                    cfnref = coplist.FindFunction(lhs.Type, rhs.Type);
                }
                else
                {
                    cfnref = coplist.FindFunction(target, new[] { lhs.Type, rhs.Type });
                }

                if (cfnref == null)
                {
                    Parent.NamespaceContext.Context.AddError(new ErrorNoViableOperator(context));
                    return(null);
                }
                var cbuilder = Parent.Builder;
                Parent.CurrentBlock.PositionBuilderAtEnd(cbuilder);
                return(cfnref.Call(cbuilder, lhs, rhs));
            }

            /* LLVM */
            var block   = Parent.CurrentBlock;
            var builder = Parent.Builder;

            block.PositionBuilderAtEnd(builder);
            var result = builder.BuildCall(fnref.Value, new LLVMValueRef[] { lhs.Value, rhs.Value });

            return(new GamaValueRef(fnref.ReturnType, result, false));
        }
Esempio n. 2
0
        public override GamaValueRef VisitExprOpComp([NotNull] GamaParser.ExprOpCompContext context)
        {
            var lhs = Visit(context.expr(0));
            var rhs = Visit(context.expr(1));
            var op  = context.opComp().GetText();

            GamaFunctionList oplist;

            if (op == ">")
            {
                oplist = lhs.Type.Meta.Operators.Gt;
            }
            else if (op == "<")
            {
                oplist = lhs.Type.Meta.Operators.Lt;
            }
            else if (op == ">=")
            {
                oplist = lhs.Type.Meta.Operators.Ge;
            }
            else if (op == "<=")
            {
                oplist = lhs.Type.Meta.Operators.Le;
            }
            else if (op == "==")
            {
                oplist = lhs.Type.Meta.Operators.Eq;
            }
            else
            {
                oplist = lhs.Type.Meta.Operators.Neq;
            }

            GamaFunctionRef fnref = oplist.FindFunction(InstanceTypes.Bool, new[] { lhs.Type, rhs.Type });

            if (fnref == null)
            {
                GamaCompiledFunctionList coplist;
                if (op == ">")
                {
                    coplist = lhs.Type.Meta.CompiledOperators.Gt;
                }
                else if (op == "<")
                {
                    coplist = lhs.Type.Meta.CompiledOperators.Lt;
                }
                else if (op == ">=")
                {
                    coplist = lhs.Type.Meta.CompiledOperators.Ge;
                }
                else if (op == "<=")
                {
                    coplist = lhs.Type.Meta.CompiledOperators.Le;
                }
                else if (op == "==")
                {
                    coplist = lhs.Type.Meta.CompiledOperators.Eq;
                }
                else
                {
                    coplist = lhs.Type.Meta.CompiledOperators.Neq;
                }

                GamaCompiledFunctionRef cfnref = coplist.FindFunction(InstanceTypes.Bool, new[] { lhs.Type, rhs.Type });

                if (cfnref == null)
                {
                    Parent.NamespaceContext.Context.AddError(new ErrorNoViableOperator(context));
                    return(null);
                }
                var cbuilder = Parent.Builder;
                Parent.CurrentBlock.PositionBuilderAtEnd(cbuilder);
                return(cfnref.Call(cbuilder, lhs, rhs));
            }

            /* LLVM */
            var block   = Parent.CurrentBlock;
            var builder = Parent.Builder;

            block.PositionBuilderAtEnd(builder);
            var result = builder.BuildCall(fnref.Value, new LLVMValueRef[] { lhs.Value, rhs.Value });

            return(new GamaValueRef(fnref.ReturnType, result, false));
        }