Пример #1
0
 public ILispValue eval()
 {
     if (value.Count == 0)
     {
         return(new LispArray(new List <ILispValue>()));
     }
     if (force_eval)
     {
         if (value [0].getType() == ELispType.Native)
         {
             return(((ILispNative)value [0]).execute(this));
         }
         if (value [0].getType() == ELispType.LispValue)
         {
             var first = value [0].eval();
             if (first.getType() != ELispType.Native)
             {
                 LispArray ret = new LispArray(new List <ILispValue>(value.Count));
                 ret.value.Add(first);
                 for (int i = 1; i < value.Count; i++)
                 {
                     ret.value.Add(value [i].eval());
                 }
                 return(ret);
             }
             else
             {
                 return(((ILispNative)first).execute(this));
             }
         }
     }
     return(this);
 }
Пример #2
0
 public ILispValue execute(LispArray array)
 {
     if (array.getSize() == 2)
     {
         if (array [1].getType() == ELispType.LispValue)
         {
             if (((LispArray)array [1]).getSize() > 1)
             {
                 var tmp = new List <ILispValue> ();
                 var src = ((LispArray)array [1]).getInner();
                 for (int idx = 1; idx < src.Count; idx++)
                 {
                     tmp.Add(src [idx]);
                 }
                 return(new LispArray(tmp));
             }
             else
             {
                 return(new LispBoolean(false));
             }
         }
         else
         {
             return(new LispBoolean(false));
         }
     }
     else
     {
         return(new LispString("tail require exactly one operand"));
     }
 }
Пример #3
0
        public ILispValue execute(LispArray array)
        {
            int max_it            = array.getSize();
            List <ILispValue> tmp = new List <ILispValue> ();
            ILispValue        ret = new LispBoolean(true);

            if (array.getSize() >= 2)
            {
                for (int idx = 1; idx < max_it; idx++)
                {
                    tmp.Add(array [idx].eval());
                }

                for (int idx = 0; idx < max_it - 2; idx++)
                {
                    if (tmp [idx].getType() != tmp [idx + 1].getType() || tmp [idx].getString() == tmp [idx + 1].getString())
                    {
                        ret = new LispBoolean(false);
                        break;
                    }
                }
                return(ret);
            }
            else
            {
                return(new LispString("ERROR : === require at least 2 operands"));
            }
        }
Пример #4
0
 public ILispValue execute(LispArray array)
 {
     if (array [1].getType() == ELispType.String && array.getSize() == 2)
     {
         return(ctx.bank [array [1].eval().getString()]);
     }
     else
     {
         return(new LispBoolean(false));
     }
 }
Пример #5
0
 public ILispValue execute(LispArray array)
 {
     if (array.getSize() == 2)
     {
         value = array [1];
         return(value);
     }
     else
     {
         return(value);
     }
 }
Пример #6
0
        public ILispValue execute(LispArray array)
        {
            int max_it = array.getSize();

            for (int idx = 1; idx < max_it; idx++)
            {
                if (array [idx].eval().getBoolean())
                {
                    return(new LispBoolean(true));
                }
            }

            return(new LispBoolean(false));
        }
Пример #7
0
 public ILispValue execute(LispArray array)
 {
     if (array.getSize() == 3)
     {
         while (array [1].eval().getBoolean())
         {
             array [2].eval();
         }
         return(new LispBoolean(true));
     }
     else
     {
         return(new LispString("ERROR : (while cond block) enforced"));
     }
 }
Пример #8
0
        public ILispValue execute(LispArray array)
        {
            int max_it            = array.getSize();
            List <ILispValue> tmp = new List <ILispValue> ();
            ILispValue        ret;

            if (array.getSize() > 1)
            {
                for (int idx = 1; idx < max_it; idx++)
                {
                    tmp.Add(array [idx].eval());
                }
                if (tmp [0].getType() == ELispType.Floating)
                {
                    if (tmp.Count == 1)
                    {
                        return(new LispFloating(-tmp [0].getFloating()));
                    }
                    double a = tmp [0].getFloating();
                    for (int idx = 1; idx < tmp.Count; idx++)
                    {
                        a -= tmp [idx].getFloating();
                    }
                    ret = new LispFloating(a);
                }
                else
                {
                    if (tmp.Count == 1)
                    {
                        return(new LispInteger(-tmp [0].getInteger()));
                    }
                    BigInteger a = tmp [0].getInteger();
                    for (int idx = 1; idx < tmp.Count; idx++)
                    {
                        a -= tmp [idx].getInteger();
                    }
                    ret = new LispInteger(a);
                }
                return(ret);
            }
            else
            {
                return(new LispString("ERROR : - require at least one operand"));
            }
        }
Пример #9
0
 public ILispValue execute(LispArray array)
 {
     if (array [1].getType() == ELispType.LispValue)
     {
         if (((LispArray)array [1]).getSize() != 0)
         {
             return(((LispArray)array [1]) [0]);
         }
         else
         {
             return(new LispBoolean(false));
         }
     }
     else
     {
         return(new LispBoolean(false));
     }
 }
Пример #10
0
 public ILispValue execute(LispArray array)
 {
     if (array.getSize() == 4 || array.getSize() == 3)
     {
         if (array [1].eval().getBoolean())
         {
             return(array [2].eval());
         }
         else
         {
             return(array [3].eval());
         }
     }
     else
     {
         return(new LispString("ERROR : (if cond then) or (if cond then else) enforced"));
     }
 }
Пример #11
0
        public ILispValue execute(LispArray array)
        {
            int max_it            = array.getSize();
            List <ILispValue> tmp = new List <ILispValue> ();
            List <ILispValue> rng = new List <ILispValue> ();

            if (array.getSize() == 3)
            {
                for (int idx = 1; idx < max_it; idx++)
                {
                    tmp.Add(array [idx].eval());
                }

                if (tmp [0].getInteger() < tmp [1].getInteger())
                {
                    for (BigInteger n = tmp [0].getInteger(); !n.Equals(tmp [1].getInteger()); n++)
                    {
                        rng.Add(new LispInteger(n));
                    }
                    rng.Add(new LispInteger(tmp [1].getInteger()));
                }
                else if (tmp [0].getInteger() > tmp [1].getInteger())
                {
                    for (BigInteger n = tmp [0].getInteger(); !n.Equals(tmp [1].getInteger()); n--)
                    {
                        rng.Add(new LispInteger(n));
                    }
                    rng.Add(new LispInteger(tmp [1].getInteger()));
                }

                return(new LispArray(rng));
            }
            else
            {
                return(new LispString("ERROR : range require exactly 2 operands"));
            }
        }
Пример #12
0
        public ILispValue execute(LispArray array)
        {
            int max_it            = array.getSize();
            List <ILispValue> tmp = new List <ILispValue> ();
            ILispValue        ret = new LispBoolean(false);

            if (array.getSize() == 3)
            {
                for (int idx = 1; idx < max_it; idx++)
                {
                    tmp.Add(array [idx].eval());
                }

                if (tmp [0].getString() != tmp[1].getString())
                {
                    ret = new LispBoolean(true);
                }
                return(ret);
            }
            else
            {
                return(new LispString("ERROR : != require exactly 2 operands"));
            }
        }
Пример #13
0
        public ILispValue execute(LispArray array)
        {
            int max_it            = array.getSize();
            List <ILispValue> tmp = new List <ILispValue> ();;
            ILispValue        ret;

            if (array.getSize() > 1)
            {
                for (int idx = 1; idx < max_it; idx++)
                {
                    tmp.Add(array [idx].eval());
                }
                if (tmp [0].getType() == ELispType.Floating)
                {
                    double a = 1;
                    for (int idx = 0; idx < tmp.Count; idx++)
                    {
                        a *= tmp [idx].getFloating();
                    }
                    ret = new LispFloating(a);
                }
                else if (tmp [0].getType() == ELispType.LispValue && tmp.Count == 1)
                {
                    List <ILispValue> tmp2 = new List <ILispValue> ();
                    for (int idx = 0; idx < ((LispArray)tmp[0]).getSize(); idx++)
                    {
                        tmp2.Add(((LispArray)tmp[0])[idx].eval());
                    }
                    if (tmp2 [0].getType() == ELispType.Floating)
                    {
                        double a = 1;

                        for (int idx = 0; idx < tmp2.Count; idx++)
                        {
                            a *= tmp2 [idx].getFloating();
                        }
                        ret = new LispFloating(a);
                    }
                    else
                    {
                        BigInteger a = 1;

                        for (int idx = 0; idx < tmp2.Count; idx++)
                        {
                            a *= tmp2 [idx].getInteger();
                        }
                        ret = new LispInteger(a);
                    }
                }
                else
                {
                    BigInteger a = 1;
                    for (int idx = 0; idx < tmp.Count; idx++)
                    {
                        a *= tmp [idx].getInteger();
                    }
                    ret = new LispInteger(a);
                }
                return(ret);
            }
            else
            {
                return(new LispString("ERROR : * require at least one operand"));
            }
        }
Пример #14
0
        /// <summary>
        /// Parse the specified code using a specified registry and bank.
        /// </summary>
        /// <param name="data">The code to parse</param>
        /// <param name="registry">Registry of the context</param>
        /// <param name="bank">Bank of the context</param>
        public ILispValue parse(string data, Dictionary <string, ILispNative> registry, Dictionary <string, ILispValue> bank)
        {
            ParserState state = new ParserState();

            state.position = 0;
            state.data     = data + " ";
            state.root     = new List <ILispValue>();

            for (; state.position < state.data.Length; state.position++)
            {
                int  sign = 1;
                bool no_eval;
                switch (state.data [state.position])
                {
                case '\'':
                case '(':
                    if (state.data [state.position] == '\'')
                    {
                        no_eval = true;
                        state.position++;
                    }
                    else
                    {
                        no_eval = false;
                    }
                    var       n            = resolve_parenthesis(ref state);
                    string    child        = state.data.Substring(n.beg + 1, n.end - n.beg - 1);
                    LispArray tmp_insertee = (LispArray)parse(child, registry, bank);
                    tmp_insertee.force_eval = !no_eval;
                    state.root.Add(tmp_insertee);
                    state.position--;
                    break;

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case '.':
                case '-':
                    if (
                        Char.IsDigit(state.data [state.position]) ||
                        state.data [state.position] == '.' ||
                        (
                            Char.IsDigit(state.data [state.position + 1]) &&
                            state.data [state.position] == '-'
                        ))
                    {
                        if (state.data [state.position] == '-')
                        {
                            sign = -1;
                            state.position++;
                        }
                        ILispValue symn = resolve_numeric(ref state, sign);
                        state.root.Add(symn);
                        state.position--;
                    }
                    else
                    {
                        goto default;
                    }
                    break;

                case '"':
                    ILispValue symst = new LispString(resolve_string(ref state));
                    state.root.Add(symst);
                    break;

                case ' ':
                case '\t':
                case '\n':
                    break;

                default:
                    string syms = resolve_symbol(ref state);
                    if (syms != "")
                    {
                        state.root.Add(registry [syms]);
                        state.position--;
                    }
                    break;
                }
            }

            return(new LispArray(state.root));
        }