Пример #1
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();

            mysBuiltin f = new mysBuiltin();

            f.Signature.Add( typeof(bool) );
            f.Signature.Add( typeof(List<mysToken>) );

            f.Function = (args, state, sss) => {
                mysToken condition = args[ 0 ];
                List<mysToken> positive = (List<mysToken>)args[ 1 ].Value;

                EvaluationMachine em;
                if ( (bool)condition.Value ) {
                    em = new EvaluationMachine(
                        positive,
                        state,
                        sss
                    );
                } else {
                    return null;
                }

                return em.Evaluate();
            };

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "when", functionGroup, global );
        }
Пример #2
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();

            mysBuiltin f = new mysBuiltin();

            f.Signature.Add( typeof(bool) );
            f.Signature.Add( typeof(List<mysToken>) );
            f.Signature.Add( typeof(List<mysToken>) );

            f.Function = (args, state, sss) => {
                bool condition = (bool)args[ 0 ].Value;
                List<mysToken> positive = (List<mysToken>)args[ 1 ].Value;
                List<mysToken> negative = (List<mysToken>)args[ 2 ].Value;

                return new EvaluationMachine(
                    condition
                        ? positive
                        : negative,
                    state,
                    sss
                ).Evaluate();
            };

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "if", functionGroup, global );
        }
Пример #3
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();

            setupIntIntVariant();

            mysBuiltin.DefineInGlobal( "*", functionGroup, global );
        }
Пример #4
0
        static void setupIntIntVariant( mysSymbolSpace global )
        {
            mysBuiltin variant = new mysBuiltin();

            variant.Signature.Add( typeof(int) );
            variant.Signature.Add( typeof(int) );

            variant.Function = (args, state, sss) =>
                new mysToken(
                    (int)args[ 0 ].Value +
                    (int)args[ 1 ].Value
                );

            mysBuiltin.AddVariant( "+", variant, global );
        }
Пример #5
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();

            mysBuiltin f = new mysBuiltin();

            f.Signature.Add( typeof(List<mysToken>) );

            f.Function = (args, state, sss) =>
                ((List<mysToken>)args[ 0 ].Value)
                    .Car();

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "car", functionGroup, global );
        }
Пример #6
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();

            mysBuiltin f = new mysBuiltin();

            f.Signature.Add( typeof(NUMBER) );
            f.Signature.Add( typeof(NUMBER) );

            f.Function = (args, state, sss) => new mysToken(
                NUMBER.Promote( args[ 0 ] ) >
                NUMBER.Promote( args[ 1 ] )
            );

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( ">", functionGroup, global );
        }
Пример #7
0
        static void defineFunction(
			mysSymbol symbol,
			mysFunction f,
			mysSymbolSpace ss
		)
        {
            mysFunctionGroup fg = null;

            // if symbol defined and of wrong type, undef it
            if (
                ss.Defined( symbol ) &&
                ss.GetValue( symbol ).Type != typeof(mysFunctionGroup)
            ) {
                // we could just overwrite it with define,
                // but I'd rather be entirely sure that we delete
                // the old value beforehand.
                ss.Undefine( symbol );
            }

            // if we're defined at this point, we know it's a function group
            if  ( ss.Defined( symbol ) ) {
                fg = ss.GetValue( symbol ).Value as mysFunctionGroup;
            } else {
                // create
                fg = new mysFunctionGroup();

                ss.Define( symbol, new mysToken( fg ) );
            }

            mysFunction collision = fg.Variants.FirstOrDefault( v =>
                v.Signature.Count() == f.Signature.Count() &&
                v.Signature
                    .Zip( f.Signature, (a, b) => a == b )
                    .Count() == v.Signature.Count()
            );

            if ( collision != null ) {
                // overwrite a conflicting sig! should probably
                // notify the user about this when it happens.
                fg.Variants.Remove( collision );
            }

            fg.Variants.Add( f );
        }
Пример #8
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();
            mysBuiltin f;

            f = new mysBuiltin();

            f.Signature.Add( typeof(int) );

            f.Function = (args, state, sss) => {
                List<int> range = new List<int>();

                for ( int i = 1; i <= (int)args[ 0 ].Value; i++ ) {
                    range.Add( i );
                }

                return new mysToken( range ).Quote();
            };

            functionGroup.Variants.Add( f );

            f = new mysBuiltin();

            f.Signature.Add( typeof(int) );
            f.Signature.Add( typeof(int) );

            f.Function = (args, state, sss) => {
                List<mysToken> range = new List<mysToken>();

                for (
                    long i = (long)args[ 0 ].Value;
                    i <= (long)args[ 1 ].Value;
                    i++
                ) {
                    range.Add( new mysToken( (int)i ) );
                }

                return new mysToken( range );
            };

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "range", functionGroup, global );
        }
Пример #9
0
        public static void Setup( mysSymbolSpace global )
        {
            mysFunctionGroup assign = new mysFunctionGroup();
            mysBuiltin assignVariant = new mysBuiltin();

            assignVariant = new mysBuiltin();
            assignVariant.Signature.Add( typeof(mysSymbol) );
            assignVariant.Signature.Add( typeof(ANY) );

            assignVariant.Function = (args, state, sss) => {
                mysSymbol assignsymbol = args[ 0 ].Value as mysSymbol;
                mysToken value = args[ 1 ];

                return Evaluate( assignsymbol, value, sss );
            };

            assign.Variants.Add( assignVariant );

            mysBuiltin.DefineInGlobal( "def", assign, global );
        }
Пример #10
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();
            mysBuiltin f;

            // actual cons version

            f = new mysBuiltin();

            f.Signature.Add( typeof(ANY) );
            f.Signature.Add( typeof(ANY) );

            f.Function = (args, state, sss) => {
                List<mysToken> outList = new List<mysToken>();

                for ( int i = 0; i < 2; i++ ) {
                    if ( args[ i ].Type == typeof(List<mysToken>) ) {
                        outList.AddRange( (List<mysToken>)args[ i ].Value );
                    } else {
                        outList.Add( args[ i ] );
                    }
                }

                return new mysToken( outList ).Quote();
            };

            functionGroup.Variants.Add( f );

            // make-list version

            f = new mysBuiltin();

            f.Signature.Add( typeof(ANY) );

            f.Function = (args, state, sss) =>
                new mysToken( new List<mysToken>() { args[ 0 ] } );

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "cons", functionGroup, global );
        }
Пример #11
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();

            mysBuiltin f = new mysBuiltin();

            f.Signature.Add( typeof(List<mysToken>) );

            f.Function = (args, state, sss) => {
                List<mysToken> l =
                    ( (List<mysToken>)args[ 0 ].Value )
                    .Skip( 1 )
                    .ToList()
                ;

                return new mysToken( l ).Quote();
            };

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "cdr", functionGroup, global );
        }
Пример #12
0
        public static void Setup(
			mysSymbolSpace global
		)
        {
            Builtins.Core.Load.Setup( global );
            Builtins.Core.Lambda.Setup( global );
            Builtins.Core.Assign.Setup( global );
            Builtins.Core.Eval.Setup( global );
            Builtins.Core.ToString.Setup( global );
            Builtins.Core.InNamespace.Setup( global );

            Builtins.Arithmetic.Addition.Setup( global );
            Builtins.Arithmetic.Subtraction.Setup( global );
            Builtins.Arithmetic.Multiplication.Setup( global );
            Builtins.Arithmetic.Division.Setup( global );

            Builtins.Collections.Range.Setup( global );

            Builtins.ListHandling.Reverse.Setup( global );
            Builtins.ListHandling.Car.Setup( global );
            Builtins.ListHandling.Cdr.Setup( global );
            Builtins.ListHandling.Cons.Setup( global );
            Builtins.ListHandling.Len.Setup( global );

            Builtins.Flow.If.Setup( global );
            Builtins.Flow.When.Setup( global );

            Builtins.Comparison.Equals.Setup( global );
            Builtins.Comparison.GreaterThan.Setup( global );

            Builtins.Looping.For.Setup( global );
            Builtins.Looping.While.Setup( global );

            Builtins.Clr.ClrType.Setup( global );
            Builtins.Clr.NewClrObject.Setup( global );
            Builtins.Clr.ClrDot.Setup( global );
            Builtins.Clr.Set.Setup( global );
        }
Пример #13
0
 public static void Setup( mysSymbolSpace global )
 {
     setupIntIntVariant( global );
 }
Пример #14
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();
            mysBuiltin f = new mysBuiltin();

            f = new mysBuiltin();
            f.Signature.Add( typeof(ANY) );

            f.Function = (args, state, sss) =>
                new mysToken( args[ 0 ].ToString() );

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "string", functionGroup, global );
        }
Пример #15
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();
            mysBuiltin f = new mysBuiltin();

            f = new mysBuiltin();
            f.Signature.Add( typeof(string) );

            f.Function = (args, state, sss) => {
                string path = (string)args[ 0 ].Value;

                string source = System.IO.File.ReadAllText(
                    System.IO.Path.Combine(
                        System.IO.Directory.GetCurrentDirectory(),
                        path
                    )
                );

                // a bit clutzy atm, but I guess it does the trick for
                // the time being
                source = source.Replace( "@this-file", path );

                source = source.Replace(
                    "@this-folder",
                    path.Substring( 0, path.LastIndexOf( '/' ) )
                );

                List<mysToken> tokens = ParseMachine.Parse( state, source );

                state.Evaluate( tokens );

                return null;
            };

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "load", functionGroup, global );
        }
Пример #16
0
        public static void Setup( mysSymbolSpace global )
        {
            mysFunctionGroup lambda = new mysFunctionGroup();
            mysBuiltin lambdaVariant = new mysBuiltin();

            lambdaVariant = new mysBuiltin();
            lambdaVariant.Signature.Add( typeof(List<mysToken>) );
            lambdaVariant.Signature.Add( typeof(List<mysToken>) );

            lambdaVariant.Function = (args, state, sss) => {
                mysSymbolSpace ss = sss.Peek();

                List<mysToken> sig = (List<mysToken>)args[ 0 ].Value;
                List<mysToken> body = (List<mysToken>)args[ 1 ].Value;

                return Evaluate(
                    sig,
                    body,
                    sss
                );
            };

            lambda.Variants.Add( lambdaVariant );

            mysBuiltin.DefineInGlobal( "=>", lambda, global );
        }
Пример #17
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();
            mysBuiltin f;

            // single value version

            f = new mysBuiltin();

            f = new mysBuiltin();
            f.Signature.Add( typeof(mysSymbol) );
            f.Signature.Add( typeof(List<mysToken>) );

            f.Function = (args, state, sss) => {
                mysSymbol symbol = args[ 0 ].Value as mysSymbol;
                string ssName = symbol.StringRepresentation.ToLower();
                List<mysToken> body = (List<mysToken>)args[ 1 ].Value;

                if ( !state.nameSpaces.ContainsKey( ssName ) ) {
                    state.nameSpaces.Add(
                        ssName,
                        new mysSymbolSpace()
                    );
                }

                sss.Push( state.nameSpaces[ ssName ] );

                EvaluationMachine em = new EvaluationMachine(
                    body,
                    state,
                    sss
                );

                mysToken result = em.Evaluate();

                sss.Pop();

                return result;
            };

            functionGroup.Variants.Add( f );

            // list version

            f = new mysBuiltin();

            f = new mysBuiltin();
            f.Signature.Add( typeof(List<mysToken>) );
            f.Signature.Add( typeof(List<mysToken>) );

            f.Function = (args, state, sss) => {
                List<mysToken> spaceList = (List<mysToken>)args[ 0 ].Value;
                List<mysToken> body = (List<mysToken>)args[ 1 ].Value;

                if ( !spaceList.All(
                    v => v.Type == typeof(mysSymbol)
                ) ) {
                    throw new FormatException();
                }

                foreach( mysToken t in spaceList ) {
                    mysSymbol symbol = t.Value as mysSymbol;
                    string ssName = symbol.StringRepresentation.ToLower();

                    if ( !state.nameSpaces.ContainsKey( ssName ) ) {
                        state.nameSpaces.Add(
                            ssName,
                            new mysSymbolSpace()
                        );
                    }

                    sss.Push( state.nameSpaces[ ssName ] );
                }

                EvaluationMachine em = new EvaluationMachine(
                    body,
                    state,
                    sss
                );

                // really needs to be fixed
                mysToken result = em.Evaluate();

                sss.Pop();

                return result;
            };

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "in", functionGroup, global );
        }
Пример #18
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();
            mysBuiltin f = new mysBuiltin();

            f = new mysBuiltin();
            f.Signature.Add( typeof(IList) );

            f.Function = (args, state, sss) =>
                new EvaluationMachine(
                    args[ 0 ].Value as List<mysToken>,
                    state,
                    sss
                ).Evaluate();

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "eval", functionGroup, global );
        }
Пример #19
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();

            mysBuiltin f = new mysBuiltin();

            f.Signature.Add( typeof(IList) );

            f.Function = (args, state, sss) => {
                List<mysToken> l = new List<mysToken>();
                l.AddRange((List<mysToken>)args[ 0 ].Value);
                l.Reverse();

                return new mysToken( l );
            };

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "reverse", functionGroup, global );
        }
Пример #20
0
        public static void Setup( mysSymbolSpace global )
        {
            functionGroup = new mysFunctionGroup();
            mysBuiltin f;

            f = new mysBuiltin();

            f.Signature.Add( typeof(IList) );

            f.Function = (args, state, sss) =>
                new mysToken( ( (IList)args[ 0 ].Value ).Count );

            functionGroup.Variants.Add( f );

            mysBuiltin.DefineInGlobal( "len", functionGroup, global );
        }