Esempio n. 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 );
        }
Esempio n. 2
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 );
        }