Ejemplo n.º 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)));
        }
Ejemplo n.º 2
0
        public Expr Variable(AnalyzerContext analyzer, string varName)
        {
            var variable = analyzer.Context.GetVariableAssignableTo(varName, analyzer.ExpectedType ?? TypeSingleton.Any);

            if (variable == null)
            {
                analyzer.Fail("No such variable: " + varName + " " + analyzer.ExpectedType);
            }
            if (variable.ConstantValue != null)
            {
                return(variable.ConstantValue);
            }
            return(new ExprImpl(variable.Type, exprWriter.ReadVariable(analyzer.Context.GetReferenceIndex(variable))));
        }
Ejemplo n.º 3
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)));
        }
Ejemplo n.º 4
0
        public Expr CheckedCast(AnalyzerContext analyzer, TypeName type, ExprBuilderObj expr)
        {
            var typeCast = type.Resolve(analyzer.Context);

            return(new ExprImpl(typeCast, analyzer.Analyze(expr, typeCast)));
        }
Ejemplo n.º 5
0
        public Expr Sequence(AnalyzerContext analyzer, IEnumerable <ExprBuilderObj> exprs)
        {
            var exprList = exprs.Select(expr => expr.Build(analyzer)).ToArray();

            return(new ExprImpl(exprList.Last().Type, exprWriter.Sequence(exprList)));
        }
Ejemplo n.º 6
0
 public Expr Function(AnalyzerContext analyzer, IEnumerable <(string, TypeName)> valueArgs, ExprBuilderObj bodyExpr)
Ejemplo n.º 7
0
 public Expr LiteralBooleanFalse(AnalyzerContext analyzer)
 => new ExprImpl(TypeSingleton.Boolean, exprWriter.BoolLiteralFalse, constant: true);
Ejemplo n.º 8
0
 public Expr LiteralInt(AnalyzerContext analyzer, string val)
 => new ExprImpl(TypeSingleton.Integer, exprWriter.IntLiteral(int.Parse(val)), constant: true);
Ejemplo n.º 9
0
 public Expr LiteralString(AnalyzerContext analyzer, string val)
 => new ExprImpl(TypeSingleton.String, exprWriter.StringLiteral(val), constant: true);