예제 #1
0
        public Expr VariableAssign(AnalyzerContext analyzer, string variableName, ExprBuilderObj rightExpr)
        {
            var right    = analyzer.Analyze(rightExpr); //new ExprLazy(() => analyzer.Analyze(rightExpr));
            var variable = new Variable(variableName, () => right.Type, right.IsConstant ? right : null);

            analyzer.Context.AddVariable(variable);

            // In case of a function: New type is defined mapping the given variable name
            // to the return type of the function.
            // TODO For testing?
            analyzer.Context.AddType(variable.Name, () => {
                var funType = variable.Type.ConcreteType as TypeFunction;
                if (funType == null)
                {
                    throw new CompilationException("Not a type: " + variable.Name);
                }
                // Generic types
                var genType = variable.Type as TypeGenericContext;
                if (genType != null)
                {
                    return(new TypeGenericContext(funType.ReturnType, genType.GenericParameters));
                }
                return(funType.ReturnType);
            });
            //Console.WriteLine("Type defined: " + variable.Name);

            return(new ExprImpl(TypeSingleton.Void, exprWriter.WriteVariable(analyzer.Context.GetReferenceIndex(variable), right)));
        }
예제 #2
0
        public Expr ContextVariable(AnalyzerContext analyzer, ExprBuilderObj leftExpr, string varName)
        {
            var type        = new TypeContext(new Variable(varName, analyzer.ExpectedType ?? TypeSingleton.Any));
            var left        = analyzer.Analyze(leftExpr, type);
            var contextType = left.Type.ConcreteType as TypeContext;
            var variable    = contextType.Variables.GetAssignableTo(varName, analyzer.ExpectedType);

            if (variable == null)
            {
                analyzer.Fail("Unknown context variable name: " + varName);
            }
            var variable2 = contextType.Variables.GetAssignableTo(varName, analyzer.ExpectedType);

            var refIndex = contextType.Variables.GetReferenceIndex(variable) + 1; // TODO +1 is for the base context
            var expr     = new ExprImpl(variable.Type, exprWriter.ReadContextVariable(refIndex));

            return(new ExprImpl(variable.Type, exprWriter.Sequence(left, expr)));
        }
예제 #3
0
        public Expr CheckedCast(AnalyzerContext analyzer, TypeName type, ExprBuilderObj expr)
        {
            var typeCast = type.Resolve(analyzer.Context);

            return(new ExprImpl(typeCast, analyzer.Analyze(expr, typeCast)));
        }