예제 #1
0
파일: Runtime.cs 프로젝트: pema99/PemaLang
        public void ExposeMethod(MethodInfo Method, string Identifier = "", object Target = null)
        {
            var           Params     = Method.GetParameters();
            List <string> Parameters = new List <string>();

            foreach (var Param in Params)
            {
                Parameters.Add(Param.Name);
            }
            ScopeInfo Scope = new ScopeInfo();
            FuncInfo  Func  = new FuncInfo(Identifier == "" ? Method.Name : Identifier, Scope, Parameters);

            Scope.Expressions.Add
            (
                new ReturnExpr
                (
                    new LambdaExpr(delegate
            {
                List <object> Values = new List <object>();
                foreach (string Param in Parameters)
                {
                    Values.Add(GetVariable(Param));
                }
                Register = Method.Invoke(Target, Values.ToArray());
            })
                )
            );
            Globals[Identifier == "" ? Method.Name : Identifier] = Func;
        }
예제 #2
0
        public override void Evaluate(Runtime Runtime)
        {
            FuncExpr.Evaluate(Runtime);
            if (!(Runtime.Register is FuncInfo))
            {
                if (FuncExpr is VarExpr)
                {
                    throw new Exception("Runtime error. \'" + (FuncExpr as VarExpr).Identifier + "\' is not a function.");
                }
                else
                {
                    throw new Exception("Runtime error. Attempt to call non-function.");
                }
            }
            FuncInfo Function = (FuncInfo)Runtime.Register;

            if (Function.Parameters.Count != Parameters.Count)
            {
                //Parameter mismatch
                throw new Exception("Invalid amount of parameters for function \'" + Function.Identifier + "\' Found " + Parameters.Count + ", expected " + Function.Parameters.Count);
            }

            Runtime.RunScope(Function.Body, Parameters);
            if (Runtime.Returning)
            {
                Runtime.Returning = false;
            }

            //Return null from function if no return statement was present
            else
            {
                Runtime.Register = null;
            }
        }
예제 #3
0
        private void FuncDecl()
        {
            FuncInfo Func = FuncParse(false);

            if (Globals.ContainsKey(Func.Identifier))
            {
                throw new Exception("Variable \'" + Func.Identifier + "\' already declared");
            }

            Globals.Add(Func.Identifier, new ConstantExpr(Func));
        }
예제 #4
0
파일: Runtime.cs 프로젝트: pema99/PemaLang
 public object Call(FuncInfo Func, params object[] Parameters)
 {
     RunScopeConstant(Func.Body, Parameters);
     return(Register);
 }