Exemplo n.º 1
0
        private static Expression ParsePrimaryExpression(IEnumerator<Token> tokens, SymbolTable table)
        {
            if (tokens.Current.Type == TokenType.Symbol && tokens.Current.Text == "(")
            {
                tokens.MoveNext();
                var expression = ParseExpression(tokens, table);

                if (tokens.Current.Type == TokenType.Symbol && tokens.Current.Text == ")")
                {
                    tokens.MoveNext();
                    return expression;
                }
                else
                {
                    throw new Exception("')' expected");
                }
            }
            else if (tokens.Current.Type == TokenType.Id)
            {
                string name = tokens.Current.Text;
                int id = table.Lookup(name);
                tokens.MoveNext();

                if (id != 0)
                    return new BoundSymbolExpression { Symbol = new SymbolRef(id) };
                else
                    return new FreeSymbolExpression { Name = name };
            }
            else
            {
                return null;
            }
        }
Exemplo n.º 2
0
        public static string ConvertToString(Expression expression, SymbolTable table)
        {
            if (expression is BoundSymbolExpression)
            {
                var boundSymbol = (BoundSymbolExpression)expression;

                return table.Lookup(boundSymbol.Symbol.Symbol);
            }
            else if (expression is FreeSymbolExpression)
            {
                var freeSymbol = (FreeSymbolExpression)expression;

                if (freeSymbol.Display != null)
                    return freeSymbol.Display(freeSymbol, table);

                return freeSymbol.Name;
            }
            else if (expression is AbstractionExpression)
            {
                var abstraction = (AbstractionExpression)expression;

                return "\\" + table.Lookup(abstraction.Left.Symbol) + "." + ConvertToString(abstraction.Right, table);
            }
            else if (expression is ApplicationExpression)
            {
                var application = (ApplicationExpression)expression;
                string left;
                string right;

                if (application.Left is AbstractionExpression)
                    left = "(" + ConvertToString(application.Left, table) + ")";
                else
                    left = ConvertToString(application.Left, table);

                if ((application.Right is AbstractionExpression) || (application.Right is ApplicationExpression))
                    right = "(" + ConvertToString(application.Right, table) + ")";
                else
                    right = ConvertToString(application.Right, table);

                return left + " " + right;
            }
            else if (expression is LazyExpression)
            {
                var lazy = (LazyExpression)expression;

                return "<#" + lazy.Id.ToString() + ">";
            }
            else if (expression is BuiltinExpression)
            {
                var builtin = (BuiltinExpression)expression;

                if (builtin.Display != null)
                    return builtin.Display(expression, table);

                string left = builtin.Left != null ? ConvertToString(builtin.Left, table) : "";
                string right = builtin.Right != null ? ConvertToString(builtin.Right, table) : "";

                if (right == "")
                    return "<BUILTIN>(" + left + ")";
                else
                    return "<BUILTIN>(" + left + ")(" + right + ")";
            }
            else
            {
                return "???";
            }
        }