Exemple #1
0
        public object Execute(FunctionEnvironment functionEnvironment, VariableEnvironment variableEnvironment)
        {
            foreach (Tuple <BogieLangType, string> param in Parameters)
            {
                if (!variableEnvironment.IsVariableDeclared(param.Item2))
                {
                    throw new Exception("Missing parameter: " + param.Item2);
                }
            }

            foreach (Body body in Body)
            {
                object obj = body.Execute(functionEnvironment, variableEnvironment);
                if (obj != null)
                {
                    BogieLangType returningType = BogieLangTypeHelpr.ObjectToType(obj);
                    if (returningType == ReturnBogieLangType)
                    {
                        return(obj);
                    }
                    else
                    {
                        throw new Exception("Function " + Identifier + " returned a " + returningType + ", but should return a " + ReturnBogieLangType);
                    }
                }
            }
            return(null);
        }
Exemple #2
0
        public object Execute(FunctionEnvironment functionEnvironment, VariableEnvironment variableEnvironment)
        {
            if (!functionEnvironment.IsFunctionDefined(Identifier))
            {
                throw new Exception("Calling undefined function: " + Identifier);
            }
            VariableEnvironment localVariables = new VariableEnvironment();

            localVariables.ParentEnvironment = variableEnvironment;//todo: switch

            FunctionDefinition functionDefinition = functionEnvironment.GetDefinedFunction(Identifier);

            for (int i = 0; i < Arguments.Count; i++)
            {
                object        obj           = Arguments[i].Execute(functionEnvironment, variableEnvironment);
                BogieLangType bogieLangType = BogieLangTypeHelpr.ObjectToType(obj);
                if (functionDefinition.Parameters[i].Item1 == bogieLangType)
                {
                    localVariables.DeclareVariable(functionDefinition.Parameters[i].Item2, functionDefinition.Parameters[i].Item1);
                    localVariables.DefineVariable(functionDefinition.Parameters[i].Item2, obj);
                }
            }

            return(functionDefinition.Execute(functionEnvironment, localVariables));
        }
 public void DefineVariable(string identifier, object obj)
 {
     if (IsVariableDeclared(identifier))
     {
         if (DeclaredVariables[identifier].BogieLangType == BogieLangTypeHelpr.ObjectToType(obj))
         {
             DeclaredVariables[identifier].Value = obj;
         }
         else
         {
             throw new Exception(identifier + " is of type " + DeclaredVariables[identifier].BogieLangType + ", got a " + BogieLangTypeHelpr.ObjectToType(obj));
         }
     }
     else if (ParentEnvironment != null)
     {
         ParentEnvironment.DefineVariable(identifier, obj);
     }
     else
     {
         throw new Exception("Undeclaration variable: " + identifier);
     }
 }
Exemple #4
0
        public static FunctionDefinition Compile(BogieLangParser.FunctionDefinitionContext functionDefinitionContext)
        {
            FunctionDefinition result = new FunctionDefinition();

            Antlr4.Runtime.Tree.ITerminalNode[] types       = functionDefinitionContext.TYPE();
            Antlr4.Runtime.Tree.ITerminalNode[] identifiers = functionDefinitionContext.IDENTIFIER();

            result.ReturnBogieLangType = BogieLangTypeHelpr.StringToType(types[0].GetText());
            result.Identifier          = identifiers[0].GetText();

            for (int i = 1; i < types.Length; i++)
            {
                BogieLangType bogieLangType = BogieLangTypeHelpr.StringToType(types[i].GetText());
                string        identifier    = identifiers[i].GetText();
                result.Parameters.Add(new Tuple <BogieLangType, string>(bogieLangType, identifier));
            }

            foreach (BogieLangParser.BodyContext context in functionDefinitionContext.body())
            {
                result.Body.Add(Runtime.Body.Compile(context));
            }

            return(result);
        }