예제 #1
0
파일: Cons.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            IScheminType head = args.Car();
            IScheminType rest = args.Cdr().Car();

            if ((rest as ScheminList) != null)
            {
                ScheminList temp = (ScheminList) rest;

                if (temp.Empty)
                {
                    return new ScheminList(head);
                }
                else
                {
                    ScheminList consd = new ScheminList(head);

                    foreach (IScheminType type in temp)
                    {
                        consd.Append(type);
                    }

                    return consd;
                }
            }

            var append = new ScheminList(head);
            append.Append(rest);
            return append;
        }
예제 #2
0
파일: Display.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            IScheminType toDisplay = args.Car();
            ScheminPort writeTo = eval.CurrentOutputPort;

            IScheminType port = args.Cdr().Car();
            if ((port as ScheminPort) != null)
            {
                writeTo = (ScheminPort) port;
            }

            if (toDisplay.GetType() == typeof(ScheminString))
            {
                ScheminString temp = (ScheminString) toDisplay;
                writeTo.OutputStream.Write(temp.Value);
                writeTo.OutputStream.Flush();
            }
            else
            {
                writeTo.OutputStream.Write(toDisplay.ToString());
                writeTo.OutputStream.Flush();
            }

            return new ScheminList(false);
        }
예제 #3
0
파일: Define.cs 프로젝트: jgouly/schemin
        public override void CheckArguments(ScheminList args)
        {
            IScheminType first = args.Car();

            if ((first as ScheminList) == null)
            {
                if (args.Length != 2)
                {
                    throw new BadArgumentsException("expected 2 arguments");
                }

                if ((first as ScheminAtom) == null)
                {
                    throw new BadArgumentsException("first argument must be a symbol");
                }
            }
            else
            {
                ScheminList arguments = (ScheminList) args.Car();
                IScheminType name = arguments.Car();

                if (args.Length != 2)
                {
                    throw new BadArgumentsException("expected 2 arguments");
                }

                if ((name as ScheminAtom) == null)
                {
                    throw new BadArgumentsException("must supply a symbol for definition");
                }

                return;
            }
        }
예제 #4
0
        public void TestDivide()
        {
            var prim = PrimitiveFactory.Get("/");
            ScheminDecimal test_decimal = new ScheminDecimal(3.0m);
            ScheminInteger test_integer = new ScheminInteger(3);
            ScheminInteger test_divisor_int = new ScheminInteger(2);
            ScheminDecimal test_divisor_decimal = new ScheminDecimal(2);

            ScheminList decimal_args = new ScheminList(test_decimal);
            decimal_args.Append(test_divisor_decimal);

            ScheminList int_args = new ScheminList(test_integer);
            int_args.Append(test_divisor_int);

            ScheminList mixed_args = new ScheminList(test_integer);
            mixed_args.Append(test_divisor_decimal);

            ScheminDecimal decimal_result = (ScheminDecimal) prim.Execute(null, null, decimal_args);
            ScheminInteger int_result = (ScheminInteger) prim.Execute(null, null, int_args);
            ScheminDecimal mixed_result = (ScheminDecimal) prim.Execute(null, null, mixed_args);

            ScheminInteger expected = new ScheminInteger(1);

            Assert.AreEqual(1.5m, decimal_result.DecimalValue());
            Assert.AreEqual(expected.IntegerValue(), int_result.IntegerValue());
            Assert.AreEqual(1.5m, mixed_result.DecimalValue());
        }
예제 #5
0
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminString filename = (ScheminString) args.Car();
            FileStream fs = new FileStream(filename.Value, FileMode.Append, FileAccess.Write, FileShare.Write);
            ScheminPort filePort = new ScheminPort(fs, ScheminPort.PortType.OutputPort);

            return filePort;
        }
예제 #6
0
파일: VectorRef.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminVector vec = (ScheminVector) args.Car();
            ScheminInteger pos = (ScheminInteger) args.Cdr().Car();

            int pos_int = (int) pos.IntegerValue();
            return vec.List[pos_int];
        }
예제 #7
0
파일: Lambda.cs 프로젝트: jgouly/schemin
        public override void CheckArguments(ScheminList args)
        {
            if (args.Length < 2)
            {
                throw new BadArgumentsException("expected at least 2 arguments");
            }

            return;
        }
예제 #8
0
파일: Symbol.cs 프로젝트: jgouly/schemin
        public override void CheckArguments(ScheminList args)
        {
            if (args.Length != 1)
            {
                throw new BadArgumentsException("expected 1 argument");
            }

            return;
        }
예제 #9
0
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminPort toCheck = (ScheminPort) args.Car();
            if (toCheck.Closed)
            {
                return ScheminBool.True;
            }

            return ScheminBool.False;
        }
예제 #10
0
파일: Symbol.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            IScheminType type = args.Car();

            if ((type as ScheminAtom) != null)
            {
                return ScheminBool.True;
            }
            return ScheminBool.False;
        }
예제 #11
0
파일: Begin.cs 프로젝트: jgouly/schemin
 public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
 {
     if (args.Length > 0)
     {
         return args.Last();
     }
     else
     {
         return new ScheminList(true);
     }
 }
예제 #12
0
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminList arg = (ScheminList) args.Car();
            if (arg.Empty)
            {
                return new ScheminList(false);
            }

            arg.UnQuote();
            return arg;
        }
예제 #13
0
파일: Define.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            bool deffun = false;

            if ((args.Car() as ScheminList) != null)
            {
                deffun = true;
            }

            if (!deffun)
            {
                ScheminAtom symbol = (ScheminAtom) args.Car();
                IScheminType definition = args.Cdr().Car();

                if (env.bindings.ContainsKey(symbol.Name))
                {
                    env.RemoveBinding(symbol);
                    env.AddBinding(symbol, definition);
                }
                else
                {
                    env.AddBinding(symbol, definition);
                }

                return new ScheminList(false);
            }
            else
            {
                ScheminList arguments = (ScheminList) args.Car();
                ScheminList expression = args.Cdr();

                ScheminAtom name = (ScheminAtom) arguments.Car();
                ScheminList argSymbols = arguments.Cdr();

                ScheminList lamArgs = new ScheminList(argSymbols, expression);
                lamArgs.UnQuote();

                ScheminLambda lam = new ScheminLambda(lamArgs, env);

                if (env.bindings.ContainsKey(name.Name))
                {
                    env.RemoveBinding(name);
                    env.AddBinding(name, lam);
                }
                else
                {
                    env.AddBinding(name, lam);
                }

                return new ScheminList(false);
            }
        }
예제 #14
0
        public void TestStringRef()
        {
            var prim = PrimitiveFactory.Get("string-ref");
            ScheminString test = new ScheminString("test");

            ScheminList args = new ScheminList(test);
            args.Append(new ScheminInteger(0));

            ScheminChar result = (ScheminChar) prim.Execute(null, null, args);
            char expected = 't';

            Assert.AreEqual(result.Value, expected);
        }
예제 #15
0
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminChar first = (ScheminChar) args.Car();
            ScheminChar second = (ScheminChar) args.Cdr().Car();

            int result = first.Value.CompareTo(second.Value);

            if (result > 0)
            {
                return ScheminBool.True;
            }

            return ScheminBool.False;
        }
예제 #16
0
파일: Let.cs 프로젝트: jgouly/schemin
        public override void CheckArguments(ScheminList args)
        {
            IScheminType first = args.Car();

            if ((first as ScheminList) == null)
            {
                if ((first as ScheminAtom) == null)
                {
                    throw new BadArgumentsException("first argument must be a symbol");
                }
            }

            return;
        }
예제 #17
0
파일: ClosePort.cs 프로젝트: jgouly/schemin
 public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
 {
     ScheminPort toClose = (ScheminPort) args.Car();
     if (toClose.Type == ScheminPort.PortType.InputPort)
     {
         toClose.InputStream.Close();
     }
     else
     {
         toClose.OutputStream.Close();
     }
     toClose.Closed = true;
     return new ScheminList(false);
 }
예제 #18
0
파일: Null.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            if ((args.Car() as ScheminList) == null)
            {
                return ScheminBool.False;
            }

            ScheminList listArg = (ScheminList) args.Car();
            if (listArg.Empty)
            {
                return ScheminBool.True;
            }

            return ScheminBool.False;
        }
예제 #19
0
파일: If.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminBool condition = args.Car().BoolValue();
            IScheminType then = args.Cdr().Car();
            IScheminType otherwise = args.Cdr().Cdr().Car();

            if (condition.Value)
            {
                return then;
            }
            else
            {
                return otherwise;
            }
        }
예제 #20
0
파일: ClosePort.cs 프로젝트: jgouly/schemin
        public override void CheckArguments(ScheminList args)
        {
            IScheminType first = args.Car();

            if (args.Length != 1)
            {
                throw new BadArgumentsException("expected 1 argument");
            }

            if ((first as ScheminPort) == null)
            {
                throw new BadArgumentsException("argument must be a port");
            }

            return;
        }
예제 #21
0
        public ScheminLambda(ScheminList definition, Environment closure)
        {
            this.Arguments = definition.Car();

            if (definition.Cdr().Length == 1)
            {
                this.Definition = definition.Cdr().Car();
            }
            else
            {
                ScheminList def = definition.Cdr();
                def.Cons(new ScheminPrimitive("begin"));
                this.Definition = def;
            }

            this.Closure = closure;
        }
예제 #22
0
        public override void CheckArguments(ScheminList args)
        {
            IScheminType first = args.Car();
            IScheminType second = args.Cdr().Car();

            if (args.Length > 2 || args.Length < 1)
            {
                throw new BadArgumentsException("expected 1 or 2 arguments");
            }

            if ((first as ScheminInteger) == null)
            {
                throw new BadArgumentsException("first argument must be an integer");
            }

            return;
        }
예제 #23
0
파일: LessThan.cs 프로젝트: jgouly/schemin
        public override void CheckArguments(ScheminList args)
        {
            IScheminType first = args.Car();
            IScheminType second = args.Cdr().Car();

            if (args.Length != 2)
            {
                throw new BadArgumentsException("expected 2 arguments");
            }

            if ((first as IScheminNumeric) == null || (second as IScheminNumeric == null))
            {
                throw new BadArgumentsException("arguments must be numeric");
            }

            return;
        }
예제 #24
0
파일: Display.cs 프로젝트: jgouly/schemin
        public override void CheckArguments(ScheminList args)
        {
            if (args.Length < 1 || args.Length > 2)
            {
                throw new BadArgumentsException("expected 1 or 2 arguments");
            }

            if (args.Length == 2)
            {
                IScheminType port = args.Cdr().Car();
                if ((port as ScheminPort) == null)
                {
                    throw new BadArgumentsException("second argument must be a port");
                }
            }

            return;
        }
예제 #25
0
파일: Apply.cs 프로젝트: jgouly/schemin
        public override void CheckArguments(ScheminList args)
        {
            IScheminType first = args.Car();
            IScheminType last = args.Cdr().Last();

            if ((first as ScheminPrimitive) == null && (first as ScheminLambda) == null
            && (first as ScheminContinuation) == null && (first as ScheminRewriter) == null)
            {
                throw new BadArgumentsException("first argument must be a procedure");
            }

            if ((last as ScheminList) == null)
            {
                throw new BadArgumentsException("last argument must be a list");
            }

            return;
        }
예제 #26
0
파일: Vector.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminVector vec = new ScheminVector();

            if ((args as ScheminList) != null)
            {
                ScheminList temp = (ScheminList) args;
                foreach (IScheminType type in temp)
                {
                    vec.List.Add(type);
                }
            }
            else
            {
                vec.List.Add(args);
            }

            return vec;
        }
예제 #27
0
파일: LessThan.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            IScheminNumeric first = (IScheminNumeric) args.Car();
            IScheminNumeric second = (IScheminNumeric) args.Cdr().Car();

            if ((first as ScheminDecimal) != null || (second as ScheminDecimal) != null)
            {
                if (first.DecimalValue() < second.DecimalValue())
                {
                    return ScheminBool.True;
                }
            }
            else
            {
                if (first.IntegerValue() < second.IntegerValue())
                {
                    return ScheminBool.True;
                }
            }

            return ScheminBool.False;
        }
예제 #28
0
파일: Evaluator.cs 프로젝트: jgouly/schemin
        public IScheminType Evaluate(ScheminList ast)
        {
            IScheminType last = null;

            try
            {
                foreach (IScheminType type in ast)
                {
                    last = EvaluateInternal(type);
                }
            }
            catch (BadArgumentsException b)
            {
                CurrentOutputPort.OutputStream.WriteLine("bad arguments: " + b.Message);
            }
            catch (Exception e)
            {
                CurrentOutputPort.OutputStream.WriteLine("error: " + e.Message);
            }

            return last;
        }
예제 #29
0
파일: Cond.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminList conditions = (ScheminList) args;
            ScheminList builtIf = new ScheminList();
            builtIf.UnQuote();

            ScheminList firstCondition = (ScheminList) conditions.Car();
            if ((firstCondition.Car() as ScheminAtom) != null)
            {
                ScheminAtom atom = (ScheminAtom) firstCondition.Car();
                if (atom.Name == "else")
                {
                    ScheminList elseClause = firstCondition.Cdr();
                    elseClause.Cons(new ScheminPrimitive("begin"));

                    return elseClause;
                }
            }

            builtIf.Append(new ScheminPrimitive("if"));
            builtIf.Append(firstCondition.Car());

            ScheminList beginExpression = firstCondition.Cdr();
            beginExpression.Cons(new ScheminPrimitive("begin"));

            builtIf.Append(beginExpression);

            if (conditions.Cdr().Length > 0)
            {
                ScheminList nextConditions = conditions.Cdr();
                nextConditions.Cons(new ScheminPrimitive("cond"));

                builtIf.Append(nextConditions);
            }

            return builtIf;
        }
예제 #30
0
파일: SetBang.cs 프로젝트: jgouly/schemin
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args)
        {
            ScheminAtom symbol = (ScheminAtom) args.Car();
            IScheminType definition = args.Cdr().Car();

            Environment parent = env;
            while (parent != null)
            {
                IScheminType value;
                parent.bindings.TryGetValue(symbol.Name, out value);

                if (value != null)
                {

                    parent.RemoveBinding(symbol);
                    parent.AddBinding(symbol, definition);
                    return new ScheminList(false);
                }

                parent = parent.parent;
            }

            throw new UnboundAtomException(string.Format("Unbound atom: {0}", symbol));
        }