public static IExpression CreateFromExpression(string expression)
        {
            IExpression result = null;

            if (ConstantExpression.IsMatch(expression))
            {
                result = new ConstantExpression();
            }
            else if (AddExpression.IsMatch(expression))
            {
                result = new AddExpression();
            }
            else if (SubExpression.IsMatch(expression))
            {
                result = new SubExpression();
            }
            else if (MulExpression.IsMatch(expression))
            {
                result = new MulExpression();
            }
            else if (DivExpression.IsMatch(expression))
            {
                result = new DivExpression();
            }
            else if (LetExpression.IsMatch(expression))
            {
                result = new LetExpression();
            }
            else if (VariableExpression.IsMatch(expression))
            {
                result = new VariableExpression();
            }

            return(result);
        }
Exemple #2
0
 public TypeDenoter VisitLetExpression(LetExpression ast, Void arg)
 {
     _idTable.OpenScope();
     ast.Declaration.Visit(this);
     ast.Type = ast.Expression.Visit(this);
     _idTable.CloseScope();
     return(ast.Type);
 }
        public void EvaluateTests(string expression, int expectedResult)
        {
            var letExpression = new LetExpression();

            int result = letExpression.Evaluate(expression);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
        public void ValidateExpressionTests(string expression, bool expectedResult, string expectedError)
        {
            var letExpression = new LetExpression();

            bool result = letExpression.ValidateExpression(expression, out string error);

            Assert.That(result, Is.EqualTo(expectedResult));
            Assert.That(error, Is.EqualTo(expectedError));
        }
        public int VisitLetExpression(LetExpression ast, Frame frame)
        {
            ast.Type.Visit(this, null);
            var extraSize = ast.Declaration.Visit(this, frame);
            var valSize   = ast.Expression.Visit(this, frame.Expand(extraSize));

            if (extraSize > 0)
            {
                _emitter.Emit(OpCode.POP, valSize, extraSize);
            }
            return(valSize);
        }
        private Expression ParseLetExpression()
        {
            var       letKeyword = ReadToken();
            TokenInfo nameToken, assignToken;

            List <Parameter> parameters = new List <Parameter>();

            Expression assignedExpression = null;

            if (!Eat(TokenKind.Identifier, out nameToken))
            {
                Diagnostics.AddError(Strings.Error_UnexpectedEndOfFile, nameToken.GetSpan());
            }
            TokenInfo token = PeekToken();

            if (token.Kind != TokenKind.Assign && token.Kind == TokenKind.Identifier)
            {
                while (MaybeEat(TokenKind.Identifier, out token))
                {
                    parameters.Add(new Parameter(token));
                }
            }
            else
            {
                ReadToken();
                Diagnostics.AddError(Strings.Error_ExpectedToken, token.GetSpan());
            }
            if (!Eat(TokenKind.Assign, out assignToken))
            {
                Diagnostics.AddError(Strings.Error_UnexpectedEndOfFile, assignToken.GetSpan());
            }
            token = PeekToken();
            assignedExpression = ParseExpression();
            if (assignedExpression == null)
            {
                Diagnostics.AddError(Strings.Error_ExpectedExpression, token.GetSpan());
            }
            var expr = new LetExpression(letKeyword, nameToken, assignToken, assignedExpression);

            if (parameters.Any())
            {
                expr.Parameters.AddRange(parameters);
            }
            return(expr);
        }
Exemple #7
0
        public static Fetch <B> Select <A, B>(this Fetch <A> self, Expression <Func <A, B> > f)
        {
            var isLet = LetExpression.IsLetExpression(f);

            if (!isLet)
            {
                return(new Select <A, B>(self, f));
            }

            Expression <Func <A, Fetch <A> > > letBind = _ => self;
            var letProject = LetExpression.RewriteLetExpression(f);
            var letPair    = new BindProjectPair(letBind, letProject);

            return(new Bind <A, B, B>(self.CollectedExpressions.Append(letPair), self)
            {
                IsLet = true
            });
        }
Exemple #8
0
        static void Main(string[] args)
        {
            var input = File.ReadAllLines("test.in").First();

            using (var sw = new StreamWriter(new FileStream("test.out", FileMode.Create)))
            {
                try
                {
                    var id  = new Abstraction(new Variable("x"), new Variable("x"));
                    var fst = new Abstraction(new Variable("x"), new Abstraction(new Variable("y"), new Variable("x")));

                    var lambda = new LetExpression(
                        new Variable("id"),
                        id,
                        new LetExpression(
                            new Variable("fst"),
                            fst,
                            new Application(
                                new Application(
                                    new Variable("fst"),
                                    new Application(new Variable("id"), new Variable("x"))
                                    ),
                                new Application(new Variable("id"), new Variable("y"))
                                )
                            )
                        );

                    var ev = new TypeInferer(Lambda.Parse(input));

                    sw.WriteLine(ev.GetLambdaType());
                    ev.PrintContext(sw);
                }
                catch (UnificatorUnresolvedExpection e)
                {
                    sw.WriteLine("The lambda expression has no type");
                }
            }
        }
            ExpressionType TranslateExpression(LetExpression e)
            {
                Env.ValueEnvironment.BeginScope();
                Env.TypeEnvironment.BeginScope();
                ExpList eDec = null, p = null;

                for (DeclarationList ptr = e.Decs; ptr != null; ptr = ptr.Tail)
                {
                    if (eDec == null)
                    {
                        p = eDec = new ExpList(TranslateDeclaration(ptr.Head), null);
                    }
                    else
                    {
                        p = p.Tail = new ExpList(TranslateDeclaration(ptr.Head), null);
                    }
                }
                ExpressionType eBody = TranslateExpression(e.Body);

                Env.ValueEnvironment.EndScope();
                Env.TypeEnvironment.EndScope();
                return(new ExpressionType(Translate.TranslateLetExp(eDec, eBody.Exp, eBody.Type.CoerceTo(Types.Type._void)), eBody.Type));
            }
 void PrintExpression(LetExpression e, int d)
 {
     Say("LetExpression("); SayLn("");
     PrintDeclarationList(e.Decs, d + 1); SayLn(",");
     PrintExpression(e.Body, d + 1); Say(")");
 }
 void PrintExpression(LetExpression e, int d)
 {
     Say("LetExpression("); SayLn("");
     PrintDeclarationList(e.Decs, d + 1); SayLn(",");
     PrintExpression(e.Body, d + 1); Say(")");
 }
        public void IsMatchTests(string expression, bool expectedResult)
        {
            bool result = LetExpression.IsMatch(expression);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
        public void ErrorEvaluateTests(string expression)
        {
            var letExpression = new LetExpression();

            Assert.Throws(typeof(ArithmeticException), () => letExpression.Evaluate(expression));
        }
Exemple #14
0
 public override string ToString()
 {
     return(string.Format("let {0} = {1}", ItemName, LetExpression.ToString()));
 }