コード例 #1
0
ファイル: GenScope.cs プロジェクト: aakamenov/Rila-Lang
        public bool TryGetVariable(string identifier, out DLR.ParameterExpression variable)
        {
            if (Definitions.TryGetValue(identifier, out variable))
            {
                return(true);
            }

            if (Parent != null)
            {
                return(Parent.TryGetVariable(identifier, out variable));
            }

            return(false);
        }
コード例 #2
0
        public override DLR.Expression GenerateExpressionTree(GenScope scope)
        {
            if (scope.IsInLoop() || scope.IsInLambda())
            {
                throw new InvalidOperationException("Cannot declare a function inside the body of another function or inside a loop!");
            }

            if (scope.Root.FunctionDefinitions.ContainsKey(Name))
            {
                throw new InvalidOperationException($"Function \"{Name}\" is already defined!");
            }

            var functionScope = scope.CreateLambda();
            var parameters    = new DLR.ParameterExpression[Parameters.Count];

            for (var i = 0; i < Parameters.Count; i++)
            {
                var name  = Parameters.ElementAt(i).Name;
                var param = DLR.Expression.Parameter(typeof(object), name);

                parameters[i] = param;
                functionScope.Definitions[name] = param;
            }

            var body = Body.GenerateExpressionTree(functionScope);

            DLR.LambdaExpression function = null;

            if (functionScope.ReturnTarget is null) //void returning function
            {
                function = DLR.Expression.Lambda(body, Name, parameters);
            }
            else
            {
                var wrapper = DLR.Expression.Block(body, DLR.Expression.Label(functionScope.ReturnTarget, body));
                function = DLR.Expression.Lambda(wrapper, Name, parameters);
            }

            scope.Root.FunctionDefinitions[Name] = function.Compile();

            return(function);
        }
コード例 #3
0
ファイル: GenScope.cs プロジェクト: aakamenov/Rila-Lang
 public GenScopeRoot(Rila runtime) : base(runtime)
 {
     FunctionDefinitions = new Dictionary <string, Delegate>();
     RuntimeParameter    = DLR.Expression.Parameter(typeof(Rila));
     Root = this;
 }