Exemplo n.º 1
0
 /// <summary>
 /// Returns bound expression for the given symbol.
 /// </summary>
 /// <param name="sym"></param>
 /// <returns></returns>
 public IExpression Get(ScmSymbol sym)
 {
     if (Contains(sym))
     {
         return(m_Frame[sym.Token.AsString]);
     }
     return(null);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Evaluates a symbol by just looking up in an environment.
        /// Signals an error if a symbol cannot be found.
        /// </summary>
        /// <param name="symbol">A symbol to look up.</param>
        /// <param name="env">Environment in which the given symbol will be looked up.</param>
        /// <param name="cont">Continuation.</param>
        /// <returns>Bounce or null.</returns>
        private Bounce EvalSymbol(ScmSymbol symbol, Environment env, Cont cont)
        {
            var evalResult = env.Get(symbol);

            if (evalResult is ScmUnassigned)
            {
                throw new EvaluatorException(String.Format(UserMessages.SymbolIsUnassigned, symbol.Token.AsString), symbol);
            }
            return(() => (Bounce)cont(evalResult));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Searches for a given symbol all the way down the stack; throws
 /// an exception if the symbol cannot be found.
 /// </summary>
 /// <param name="sym"></param>
 /// <returns></returns>
 public IExpression Get(ScmSymbol sym)
 {
     foreach (var env in m_Env)
     {
         if (env.Contains(sym))
         {
             return(env.Get(sym));
         }
     }
     throw new EvaluatorException(sym.Token.AsString + " is unbound", sym);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Binds a symbol with an expression.
 /// </summary>
 /// <param name="sym"></param>
 /// <param name="value"></param>
 public void Set(ScmSymbol sym, IExpression value)
 {
     if (!Contains(sym))
     {
         m_Frame.Add(sym.Token.AsString, value);
     }
     else
     {
         m_Frame[sym.Token.AsString] = value;
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Updates the binding with the given expression. If the symbol
 /// cannot be found in the environment an exception is thrown.
 /// </summary>
 /// <param name="sym"></param>
 /// <param name="value"></param>
 public void Set(ScmSymbol sym, IExpression value)
 {
     foreach (var env in m_Env)
     {
         if (env.Contains(sym))
         {
             env.Set(sym, value);
             return;
         }
     }
     throw new EvaluatorException(sym.Token.AsString + " is unbound", sym);
 }
Exemplo n.º 6
0
 public ScmSetSpecialForm(ScmSymbol n, IExpression e)
 {
     m_Name       = n;
     m_Expression = e;
 }
Exemplo n.º 7
0
 public ScmDefineVarSpecialForm(ScmSymbol n, IExpression e)
 {
     m_Name       = n;
     m_Expression = e;
 }
Exemplo n.º 8
0
 public ScmDefineProcSpecialForm(ScmSymbol n, List <ScmSymbol> f, List <IExpression> e, bool dotted)
     : base(f, e, dotted)
 {
     m_ProcedureName = n;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Check whether the given symbol is present in this frame.
 /// </summary>
 /// <param name="sym"></param>
 /// <returns></returns>
 public bool Contains(ScmSymbol sym)
 {
     return(m_Frame.ContainsKey(sym.Token.AsString));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Creates a new binding on the top-most frame.
 /// </summary>
 /// <param name="sym"></param>
 /// <param name="value"></param>
 public void Define(ScmSymbol sym, IExpression value)
 {
     CurrentFrame.Set(sym, value);
 }
Exemplo n.º 11
0
        /// <summary>
        /// This methods analyses the given tokens and produces a list of combinations
        /// and expressions that can be parsed further for syntax consistency etc.
        /// </summary>
        /// <param name="tokens">A list of tokens.</param>
        /// <returns>A list of combinations and expressions.</returns>
        private static List <IExpression> CombineExpressions(List <Token> tokens)
        {
            CheckParanthesisBalance(tokens);

            var            program            = new List <IExpression> ();
            var            eStack             = new Stack <IExpression> ();
            ScmCombination currentCombination = null;

            for (int i = 0; i < tokens.Count; ++i)
            {
                var token = tokens[i];

                if (token is BeginToken)
                {
                    if (currentCombination != null)
                    {
                        eStack.Push(currentCombination);
                    }
                    currentCombination = new ScmCombination(token);
                }
                else if (token is EndToken)
                {
                    IExpression cExpr = currentCombination as IExpression;
                    currentCombination = cExpr as ScmCombination;
                    if (eStack.Count > 0)
                    {
                        var stackExprOnTop = eStack.Peek() as ScmCombination;
                        stackExprOnTop.Expressions.Add(currentCombination);
                        currentCombination = eStack.Pop() as ScmCombination;
                    }
                    else
                    {
                        program.Add(currentCombination);
                        currentCombination = null;
                    }
                }
                else
                {
                    if (token is SymbolToken)
                    {
                        IExpression scmSymbol = new ScmSymbol(token);
                        AddExpression(scmSymbol, currentCombination, program);
                    }
                    else if (token is NumberToken)
                    {
                        IExpression scmNumber = NumberParser(token as NumberToken);
                        AddExpression(scmNumber, currentCombination, program);
                    }
                    else if (token is StringToken)
                    {
                        IExpression scmString = new ScmString(token);
                        AddExpression(scmString, currentCombination, program);
                    }
                    else if (token is DotToken)
                    {
                        IExpression scmString = new ScmDotModifier(token);
                        AddExpression(scmString, currentCombination, program);
                    }
                    else if (token is QuoteToken || token is QuasiquoteToken || token is UnquoteToken)
                    {
                        IExpression scmQuote = null;
                        if (token is QuoteToken)
                        {
                            scmQuote = new ScmQuoteSpecialForm(null);
                        }
                        else if (token is UnquoteToken)
                        {
                            scmQuote = new ScmUnquoteSpecialForm(null);
                        }
                        else
                        {
                            scmQuote = new ScmQuasiquoteSpecialForm(null);
                        }
                        AddExpression(scmQuote, currentCombination, program);
                    }
                }
            }

            return(QuotationSyntacticSugar(program, false));
        }