Ejemplo n.º 1
0
        public SchemeLambdaTemplate( SchemeAST ast, ISchemeEnvironment currentEnv )
        {
            if (ast.children.Count < 2)
            {
                throw new SchemeWrongNumberOfArguments(String.Format("Lambda expects exactly two arguments. You have given me: {0}", ast.children.Count));
            }

            //really not beautiful, but this is the price we pay for using the AST data structure
            if (ast.children[0].currentObject != SchemeVoid.instance) // this is false for lambdas with no arguments
            {
                _params.Add((SchemeSymbol)ast.children[0].currentObject);
                foreach (SchemeAST child in ast.children[0].children)
                {
                    _params.Add((SchemeSymbol)child.currentObject);
                }
            }
            _implementation = new SchemeAST();

            _lambdaEnv = currentEnv ;

            for (int i = 1; i < ast.children.Count; i++)
            {
                _implementation.children.Add((SchemeAST)ast.children[i].Clone());
                _implementation.children[_implementation.children.Count - 1].parent = ast;
            }
        }
Ejemplo n.º 2
0
 public List<SchemeObject> evaluate(SchemeAST AST)
 {
     if (currentEnvironment == null)
     {
         currentEnvironment = root;
     }
     return evaluate(ref AST);
 }
Ejemplo n.º 3
0
        internal static List<SchemeObject> lookupSymbolsFromEnv( ref SchemeAST currentAST, ISchemeEnvironment environment )
        {
            List<SchemeObject> ret = new List<SchemeObject>();
            ret.Add( environment.get( (SchemeSymbol) currentAST.currentObject ) );
             // not nice, but it works
              /*  if( ret[0] == null ) // this holds true if the currentObjcet is NOT in the symbol table. Then it might either be an integer or a float and has not been redefined or the function is unknown
            {
                ret.RemoveAt( 0 );
                int intValue;
                var symbol = ( (SchemeSymbol) currentAST.currentObject ).value;
                if( int.TryParse( symbol, out intValue ) )
                {
                    ret.Add( new SchemeInteger( intValue ) );
                }
                else //TODO extend for floats
                {
                    throw new SchemeUndefinedSymbolException( String.Format( "Undefined Symbol: {0}", symbol ) );
                }
            }   */

            foreach (SchemeAST child in currentAST.children)
            {
                if( child.currentObject.GetType() == typeof( SchemeSymbol ) )
                {
                    var symbol = (SchemeSymbol) child.currentObject;
                    ret.Add( environment.get( symbol ) );

                    if( ret[ret.Count -1]== null )  //objcet is not in symbol list, check for integer and float!
                    {
                            throw new SchemeUndefinedSymbolException( String.Format( "Undefined Symbol: {0}", symbol.value ) );
                    }
                }
                else
                {
                    ret.Add( child.currentObject );
                }
            }
            return ret;
        }
Ejemplo n.º 4
0
        private bool updateToNextLevelChild(ref SchemeAST currentAst, ISchemeEnvironment environment)
        {
            Type type = currentAst.currentObject.GetType();

            if (type == typeof(SchemeSymbol))
            {
                SchemeType t = environment.get((SchemeSymbol)currentAst.currentObject);
                if (t == null)
                {
                    throw new SchemeUndefinedSymbolException(String.Format("Undefined Symbol: {0}", currentAst.currentObject.ToString()), currentAst.fileName, currentAst.sourceLength, currentAst.sourceOffset);
                }
                Type obj = t.GetType();
                if (obj == typeof(SchemeBuiltInLambda)) // or typeof if. this is needed for parts which should not be evaluated.
                {
                    return false;
                }
                if (obj == typeof(SchemeBuiltInIf))
                {
                    var tmpRoot = new SchemeAST();
                    var condition = currentAst.children[0];
                    var oldParent = condition.parent;

                    tmpRoot.children.Add(condition);
                    condition.parent = tmpRoot;
                    var evaluated = evaluate(ref tmpRoot);    //evaluate the if condition
                    tmpRoot = new SchemeAST(currentAst, evaluated[0]);
                    currentAst.children[0] = tmpRoot;

                    return false;
                }
            }

            foreach (SchemeAST child in currentAst.children)
            {
                if (child.children.Count != 0)
                {
                    currentAst = child;
                    return true;
                }
            }
            return false;
        }
Ejemplo n.º 5
0
        private void updateParent(ref SchemeAST currentAST, SchemeObject newValue)
        {
            /*   if (currentAST.currentObject.GetType() == typeof(SchemeLambda))
               {
                   //  foreach (SchemeType obj in currrentEn )
                   foreach (string key in currentEnvironment.getDict().Keys)
                   {
                       currentEnvironment.parent().set(new SchemeSymbol(key), currentEnvironment.getDict()[key]);
                   }
               } */
            if (currentAST.hasOwnEnviornment)// && currentEnvironment.parent() != null )
            {
                /* if (newValue is SchemeLambda)
                 {
                     foreach (string key in currentEnvironment.getDict().Keys)
                     {
                         currentEnvironment.parent().set(new SchemeSymbol(key), currentEnvironment.getDict()[key]);
                     }
                 }           */
                currentEnvironment = currentEnvironment.parent();
            }

            int postition = currentAST.parent.children.IndexOf(currentAST);
            currentAST.parent.children.Remove(currentAST);
            // if(  newValue != SchemeVoid.instance )
            //  {
            var returnValue = new SchemeAST(currentAST.parent, newValue);
            currentAST.parent.children.Insert(postition, returnValue);
            //  }
        }
Ejemplo n.º 6
0
        private SchemeType getType(ref SchemeAST ast, ISchemeEnvironment environment)
        {
            SchemeObject ret = ast.currentObject;
            if (ast.currentObject.GetType() == typeof(SchemeSymbol))
            {
                ret = environment.get((SchemeSymbol)ret);
            }

            if (!(ret is SchemeType))
            {
                return null;
            }

            return (SchemeType)ret;
        }
Ejemplo n.º 7
0
        private ISchemeFunction getFunction(ref SchemeAST ast, ISchemeEnvironment environment)
        {
            SchemeObject ret = ast.currentObject;
            if (ast.currentObject.GetType() == typeof(SchemeSymbol))
            {
                ret = environment.get((SchemeSymbol)ret);
            }

            if (!(ret is ISchemeFunction))
            {
                return null;
            }

            return (ISchemeFunction)ret;
        }
Ejemplo n.º 8
0
        private SchemeObject evaluateSchemeAST(ref SchemeAST ast, ISchemeEnvironment environment)
        {
            var func = getFunction(ref ast, environment);
            if (func != null)
            {

                return func.evaluate(ref ast, this);
            }
            else
            {
                var type = getType(ref ast, environment);
                if (type != null)
                {

                    return type;
                }
                else
                {
                    throw new SchemeNoSuchMethodException(String.Format("{0} is no valid Scheme Method!", ast.currentObject.ToString()));
                }
            }
        }
Ejemplo n.º 9
0
 public ISchemeEnvironment getClonedEnv(ISchemeEnvironment parent)
 {
     var tmp = new SchemeEnvironment(_symbolTable, parent);
     return (ISchemeEnvironment)tmp;
 }
Ejemplo n.º 10
0
 public SchemeEnvironment(ISchemeEnvironment parent)
 {
     Debug.Assert(parent != null, "Parent must not be null! use SchemeEnvironmentroot.Singleton!");
     _parent = parent;
 }
Ejemplo n.º 11
0
 public SchemeEnvironment(Dictionary<string, SchemeType> dict, ISchemeEnvironment parent)
 {
     _parent = parent;
     _symbolTable = dict;
 }
Ejemplo n.º 12
0
 public void setParent(ISchemeEnvironment parent)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 13
0
 public ISchemeEnvironment getClonedEnv(ISchemeEnvironment parent)
 {
     return new SchemeEnvironment(_symbolTable, parent);
 }
Ejemplo n.º 14
0
 public void setParent(ISchemeEnvironment parent)
 {
     this._parent = parent;
 }
Ejemplo n.º 15
0
 public SchemeLambdaImpl(SchemeAST implementation, List<SchemeSymbol> _params, ISchemeEnvironment env)
 {
     this._implementation = implementation;
     this._params = _params;
     this._lambdaEnv = env;
 }
Ejemplo n.º 16
0
 public SchemeLambda( SchemeAST ast, ISchemeEnvironment currentEnv )
 {
     Debug.Assert( false, "Should never be called!" );
 }