Esempio n. 1
0
        private object Fn(SharpList pList, Scope pCurrentScope)
        {
            SharpVector argBindings = pList [1] as SharpVector;
            if(argBindings == null) {
                Error ("The first argument to fn is not a vector of args", pList);
            }

            SharpVector argBindingsDeepCopy = new SharpVector ();
            foreach(var argBinding in argBindings) {
                argBindingsDeepCopy.Add (argBinding);
            }

            SharpList body = new SharpList ();
            for (int i = 2; i < pList.Count; i++) {
                body.Add(pList[i]);
            }

            Scope closure = new Scope ("Closure" + _closureCounter++, pCurrentScope);
            #if LOG_SCOPES
            Console.WriteLine ("Created new closure: " + closure.name);
            #endif

            string functionName = "Fn" + _functionCounter++;

            return new SharpFunction(args => {

                Scope functionCallScope = new Scope ("FunctionCallScope" + _functionCallScopeCounter++, closure);
            #if LOG_SCOPES
                Console.WriteLine ("Created new function call scope: " + functionCallScope.name);
            #endif

                int argPos = 0;

                for(int argBindingPos = 0; argBindingPos < argBindingsDeepCopy.Count; argBindingPos++) {
                    var argBinding = argBindingsDeepCopy[argBindingPos];

                    if(argBinding is ReservedToken && (argBinding as ReservedToken).type == TokenType.AMPERSAND) {
                        argBindingPos++;
                        SymbolToken finalSymbol = argBindingsDeepCopy[argBindingPos] as SymbolToken;

                        if(finalSymbol == null) {
                            Error ("Final arg binding after ampersand is not a symbol: " + argBindingsDeepCopy[argBindingPos], pList);
                        }

                        var restOfArgs = new SharpList ();
                        while (argPos < args.Length) {
                            restOfArgs.Add(args[argPos++]);
                        }
                        functionCallScope.SetVar(finalSymbol.value, restOfArgs);
                        break;
                    }

                    SymbolToken symbol = argBinding as SymbolToken;
                    if(symbol == null) {
                        Error("Arg binding is not a symbol: " + symbol, pList);
                    }

                    functionCallScope.SetVar(symbol.value, args[argPos++]);
                }

            //				Console.WriteLine(functionName + " was called with " + args.Length + " arguments:");
            //				foreach(var arg in args) {
            //					if(arg == null) {
            //						Console.WriteLine("null");
            //					} else {
            //						Console.WriteLine(arg.ToString());
            //					}
            //				}

                object lastResult = null;
                foreach(var item in body) {
                    //Console.WriteLine("Time to eval " + item);
                    lastResult = Eval (item, functionCallScope);
                }
                return lastResult;
            }, functionName);
        }
Esempio n. 2
0
 SharpVector ReadVector()
 {
     var vector = new SharpVector ();
     _indent++;
     while (CurrentToken().type != TokenType.RIGHT_BRACKET) {
         object child = Read ();
         vector.Add (child);
         //Console.WriteLine (GetIndentSpaces() + "Added " + child.ToString() + " to list");
     }
     Step (); // skip the right bracket
     _indent--;
     return vector;
 }
Esempio n. 3
0
        private object EvalVector(SharpVector pVector, Scope pCurrentScope)
        {
            SharpVector evaledVector = new SharpVector ();

            foreach (var item in pVector) {
                evaledVector.Add (Eval (item, pCurrentScope));
            }

            return evaledVector;
        }