Exemple #1
0
        /// <summary>
        /// Determines whether the environment contains a definition for
        /// a variable with the given symbol
        /// </summary>
        /// <param name="symbol"></param>
        /// <returns>True or false</returns>
        public bool Contains(Symbol symbol)
        {
            if (hashtable.ContainsKey(symbol))
                return true;

            if (previousEnvironment != null)
                return previousEnvironment.Contains(symbol);

            return false;
        }
Exemple #2
0
		/// <summary>
		/// Returns a symbol given its name. If necessary the symbol is
		/// created and interned.
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static Symbol FromName(string name) 
		{
            name = name.ToLower();
			Symbol symbol = (Symbol)symbolTable[name];
			if(symbol == null) 
			{
				symbol = new Symbol(name);
				symbolTable.Add(name, symbol);
			}
			return symbol;
		}
Exemple #3
0
 /// <summary>
 /// Asssigns value to a local variable symbol in this
 /// local environment (irrespective of whether symbol
 /// is defined in any parent environments).
 /// </summary>
 public object AssignLocal(Symbol symbol, object value)
 {
     hashtable[symbol] = value;
     return value;
 }
Exemple #4
0
        /// <summary>
        /// Returns the environment in which a given variable is defined, or null
        /// </summary>
        private Environment GetEnvironment(Symbol symbol)
        {
            if (hashtable.ContainsKey(symbol))
                return this;

            if (previousEnvironment == null)
                return null;

            return previousEnvironment.GetEnvironment(symbol);
        }
Exemple #5
0
        /// <summary>
        /// Sets a variable with given symbol to a given value
        /// </summary>
        public object Set(Symbol symbol, object value)
        {
            if ((hashtable.ContainsKey(symbol)) || (previousEnvironment == null))
                return this.AssignLocal(symbol, value);

            return previousEnvironment.Set(symbol, value);
        }
Exemple #6
0
        public object GetValue(Symbol symbol)
        {
            if (hashtable.ContainsKey(symbol))
            {
                return hashtable[symbol];
            }

            if (previousEnvironment != null)
            {
                return previousEnvironment.GetValue(symbol);
            }

            throw new LSharpException("Reference to undefined identifier: " + symbol.Name,this);
        }
Exemple #7
0
        /// <summary>
        /// Sets a variable with given symbol to a given value
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public object Assign(Symbol symbol, object value)
        {
            Environment environment = GetEnvironment(symbol);

            if (environment == null)
                environment = this;

            return environment.AssignLocal(symbol, value);
        }
Exemple #8
0
        public object GetValue(Symbol symbol)
        {
            object o = hashtable[symbol];

            if ((o == null) && (previousEnvironment != null))
                o = previousEnvironment.GetValue(symbol);

            return o;
        }
Exemple #9
0
 public static bool Bound(Symbol symbol, Environment environment)
 {
     if (environment.Contains(symbol))
         return true;
     else
         return false;
 }
Exemple #10
0
 /// <summary>
 /// Binds the given symbol to the given value in the given
 /// environment
 /// </summary>
 public static object VarSet(Symbol symbol, Object value, Environment environment)
 {
     environment.Set(symbol, value);
     return value;
 }
Exemple #11
0
 /// <summary>
 /// Returns the value that symbol is bound to in the
 /// given environment
 /// </summary>
 public static object VarRef(Symbol symbol, Environment environment)
 {
     object value = environment.GetValue(symbol);
     return value;
 }
Exemple #12
0
 public static Expression CompileVarRef(Symbol symbol)
 {
     return Expression.Call(null, varRefMethodInfo, Expression.Constant(symbol), environmentParameter);
 }
Exemple #13
0
    public static string GenerateAssignLocal(Symbol s, object value, LSharp.Environment environment)
    {
        if (environment.Contains(s))
        {
            string ns = MakeUnique(s.Name);

            environment.AssignLocal(s, ns);

            if (value is string)
            {
                return string.Format(@"{1}
object {0} = retval;", ns, value) + NewLine;
            }
            else
            {
                return string.Format(@"{2} {0} = {1};",
                                     ns, value, value.GetType()) + NewLine;
            }
        }
        else
        {
            environment.AssignLocal(s, s.Name);

            if (value is string)
            {
                return string.Format(@"{1}
object {0} = retval;", s.Name, value) + NewLine;
            }
            else
            {
                return string.Format(@"{2} {0} = {1};", s.Name, value, value.GetType()) + NewLine;
            }
        }
    }
Exemple #14
0
        public static string GenerateAssign(Symbol s, object value, LSharp.Environment environment)
    {
        if (environment.Contains(s))
        {
            object sn = environment.GetValue(s);
            return string.Format(@"{1}
{0} = retval;",
                                 sn, value) + NewLine;
        }
        else
        {
            environment.Assign(s, s.Name);
            return string.Format(@"{1}
object {0} = retval;",
                                 s.Name, value) + NewLine;
        }
    }