示例#1
0
        public SESet(SExprAtomic ha, SExprComp c, bool imm) : base(ha, c)
        {
            if (c.Atomics.Count < 1)
            {
                throw new VMException("it takes at least 1 argument", ha);
            }

            var n = c.Atomics.Pop();

            if (n is SExprAtomic && (n as SExprAtomic).Token.TType == SToken.TokenType.ATOMIC)
            {
                varName = (string)(n as SExprAtomic).Token.TValue;
            }
            else
            {
                nameExpr = SExpression.Cast(n);
            }

            if (c.Atomics.Count > 0)
            {
                varValue = SExpression.Cast(c.Atomics.Pop());
            }
            else
            {
                varValue = new SNull();
            }

            makeImmutable = imm;
        }
示例#2
0
        public SECond(SExprAtomic ha, SExprComp c)
        {
            if (c.Atomics.Count < 2)
            {
                throw new VMException("at least 1 branch must be defined", ha);
            }

            condition = SExpression.Cast(c.Atomics.Pop());
            branches  = new List <Tuple <SExpression, SExpression> >();

            foreach (var a in c.Atomics)
            {
                var b = a as SExprComp;
                if (b == null || b.Atomics.Count < 2)
                {
                    throw new VMException("each branch must be a compound", ha);
                }

                var cond = b.Atomics.Pop();

                if (cond is SExprAtomic &&
                    ((SExprAtomic)cond).Token.TType == SToken.TokenType.ATOMIC &&
                    (string)((SExprAtomic)cond).Token.TValue == "_")
                {
                    defaultBranch = SExpression.Cast(b.Atomics.Pop());
                }
                else
                {
                    branches.Add(new Tuple <SExpression, SExpression>(
                                     SExpression.Cast(cond),
                                     SExpression.Cast(b.Atomics.Pop())
                                     ));
                }
            }
        }
示例#3
0
        public override SExpr Clone()
        {
            SExprComp ret = new SExprComp();

            ret.Atomics = Atomics.Select(a => a.Clone()).ToList();
            return(ret);
        }
示例#4
0
        public SERegexMatch(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count > 1)
            {
                regex = SExpression.Cast(c.Atomics.Pop());
                text  = SExpression.Cast(c.Atomics.Pop());

                if (c.Atomics.Count > 0)
                {
                    start = SExpression.Cast(c.Atomics.Pop());
                }
                else
                {
                    start = new SNumber(0);
                }

                if (c.Atomics.Count > 0)
                {
                    length = SExpression.Cast(c.Atomics.Pop());
                }
                else
                {
                    length = new SNumber(-1);
                }
            }
            else
            {
                throw new VMException("it takes at least 2 arguments", ha);
            }
        }
示例#5
0
        public static SExpression Cast(SExprComp c)
        {
            if (c.Atomics.Count == 0)
            {
                return(new SEList(c));
            }

            var _head = c.Atomics.Pop();

            if (_head is SExprComp)
            {
                if (c.Atomics.Count == 0)
                {
                    return(SExpression.Cast(_head));
                }
                else
                {
                    var first = _head;
                    while (first is SExprComp)
                    {
                        first = (first as SExprComp).Atomics[0];
                    }

                    return(new SECall(SExpression.Cast(_head),
                                      (from a in c.Atomics select SExpression.Cast(a)).ToList(),
                                      first as SExprAtomic, c));
                }
            }

            var head = _head as SExprAtomic;

            if (head.Token.TType == SToken.TokenType.ATOMIC)
            {
                string tvalue = (string)head.Token.TValue;
                if (SExpression.KeywordsLookup.ContainsKey(tvalue))
                {
                    return(SExpression.KeywordsLookup[tvalue](head, c));
                }
                else
                {
                    if (c.Atomics.Count == 0)
                    {
                        return(new SEVariable(tvalue, head, c));
                    }
                    else
                    {
                        return(new SECall(tvalue, (from a in c.Atomics select SExpression.Cast(a)).ToList(), head, c));
                    }
                }
            }
            else if (head.Token.TType == SToken.TokenType.STRING || head.Token.TType == SToken.TokenType.NUMBER)
            {
                c.Atomics.Insert(0, head);
                return(new SEList(c));
            }
            else
            {
                throw new VMException("invalid cast", head);
            }
        }
示例#6
0
 public SEInteropGetType(SExprAtomic ha, SExprComp c) : base(ha, c)
 {
     if (c.Atomics.Count != 1)
     {
         throw new VMException("it takes 1 argument", ha);
     }
     typeName = SExpression.Cast(c.Atomics.Pop());
 }
示例#7
0
        public SESub(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count != 2 && c.Atomics.Count != 3)
            {
                throw new VMException("it takes 2 or 3 arguments", ha);
            }

            arguments = (from a in c.Atomics select SExpression.Cast(a)).ToList();
        }
示例#8
0
        public SEKeys(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count != 1)
            {
                throw new VMException("it takes 1 argument");
            }

            dict = SExpression.Cast(c.Atomics.Pop());
        }
示例#9
0
        public SEUnsafe(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count != 1)
            {
                throw new VMException("it takes 1 argument", ha);
            }

            obj = SExpression.Cast(c.Atomics.Pop());
        }
示例#10
0
        public SEGet(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count < 2)
            {
                throw new VMException("it takes at least 2 arguments", ha);
            }

            dict = SExpression.Cast(c.Atomics.Pop());
            keys = c.Atomics.Select(a => SExpression.Cast(a)).ToList();
        }
示例#11
0
        public SEInteropNew(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count < 1)
            {
                throw new VMException("it takes at least 1 argument", ha);
            }

            type      = SExpression.Cast(c.Atomics.Pop());
            arguments = c.Atomics.Select(a => SExpression.Cast(a)).ToList();
        }
示例#12
0
        public SEMultiCore(SExprAtomic ha, SExprComp c, string f) : base(ha, c)
        {
            argumentExprs = (from a in c.Atomics select SExpression.Cast(a)).ToList();
            func          = f;

            if (argumentExprs.Count < 1)
            {
                throw new VMException("it takes at least 1 argument", ha);
            }
        }
示例#13
0
        public SENum(SExprAtomic ha, SExprComp c, bool asc) : base(ha, c)
        {
            if (c.Atomics.Count != 1)
            {
                throw new VMException("it takes 1 argument");
            }

            argument   = SExpression.Cast(c.Atomics.Pop());
            convertAsc = asc;
        }
示例#14
0
        public SEPrint(SExprAtomic ha, SExprComp c, string d) : base(ha, c)
        {
            delim = d;
            if (c.Atomics.Count == 0)
            {
                throw new VMException("it takes at least 1 argument", ha);
            }

            arguments = (from a in c.Atomics select SExpression.Cast(a)).ToList();
        }
示例#15
0
        public SEDel(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count != 2)
            {
                throw new VMException("it takes 2 arguments", ha);
            }

            host  = SExpression.Cast(c.Atomics.Pop());
            index = SExpression.Cast(c.Atomics.Pop());
        }
示例#16
0
        public SEFor(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count < 2)
            {
                throw new VMException("it takes 2 arguments", ha);
            }

            list = SExpression.Cast(c.Atomics.Pop());
            body = SExpression.Cast(c.Atomics.Pop());
        }
示例#17
0
        public SEInteropGetSetMember(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count < 2)
            {
                throw new VMException("it takes at least 2 arguments", ha);
            }

            obj      = SExpression.Cast(c.Atomics.Pop());
            propName = SExpression.Cast(c.Atomics.Pop());
        }
示例#18
0
        public SESplit(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count != 2)
            {
                throw new VMException("it takes 2 arguments", ha);
            }

            text  = SExpression.Cast(c.Atomics.Pop());
            delim = SExpression.Cast(c.Atomics.Pop());
        }
示例#19
0
        public SEStr(SExprAtomic ha, SExprComp c, bool chr) : base(ha, c)
        {
            if (c.Atomics.Count != 1)
            {
                throw new VMException("it takes 1 argument");
            }

            argument   = SExpression.Cast(c.Atomics.Pop());
            convertChr = chr;
        }
示例#20
0
 public SEDict(SExprAtomic ha, SExprComp c) : base(ha, c)
 {
     values = c.Atomics.Select(a =>
     {
         if (!(a is SExprComp) || (a as SExprComp).Atomics.Count != 2)
         {
             throw new VMException("each element of the dict must be a list with 2 elements", ha);
         }
         return(SExpression.Cast(a));
     }).ToList();
 }
示例#21
0
        public SEInteropInvokeStaticMethod(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count < 2)
            {
                throw new VMException("it takes at least 2 arguments", ha);
            }

            type       = SExpression.Cast(c.Atomics.Pop());
            methodName = SExpression.Cast(c.Atomics.Pop());
            arguments  = c.Atomics.Select(a => SExpression.Cast(a)).ToList();
        }
示例#22
0
        public SEEval(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count != 1 && c.Atomics.Count != 2)
            {
                throw new VMException("it takes 1 or 2 arguments");
            }

            text = SExpression.Cast(c.Atomics.Pop());
            if (c.Atomics.Count > 0)
            {
                env = SExpression.Cast(c.Atomics.Pop());
            }
        }
示例#23
0
        public SELambda(SExprAtomic ha, SExprComp c)
        {
            if (c.Atomics.Count < 2)
            {
                throw new VMException("missing lambda body", ha);
            }
            if (!(c.Atomics[0] is SExprComp))
            {
                throw new VMException("the first argument must be the declaration", ha);
            }

            arguments = SELambda.CompoundToArguments(c.Atomics.Pop() as SExprComp, ha);
            body      = SExpression.Cast(c.Atomics.Pop());
        }
示例#24
0
        public SEIf(SExprAtomic ha, SExprComp c)
        {
            if (c.Atomics.Count == 0)
            {
                throw new VMException("missing true branch", ha);
            }
            if (c.Atomics.Count == 1)
            {
                throw new VMException("missing false branch", ha);
            }

            condition   = SExpression.Cast(c.Atomics.Pop());
            trueBranch  = SExpression.Cast(c.Atomics.Pop());
            falseBranch = SExpression.Cast(c.Atomics.Pop());
        }
示例#25
0
        public static SExprComp MakeLambda(string func, params object[] atoms)
        {
            var comp = new SExprComp();

            comp.Atomics.AddRange(atoms.Select(a => {
                if (a is string)
                {
                    return((SExpr) new SExprAtomic(new SToken(SToken.TokenType.ATOMIC, a)));
                }
                else
                {
                    return((SExpr) new SExprAtomic(new SToken(SToken.TokenType.SEXPRESSION, a)));
                }
            }));
            return(comp);
        }
示例#26
0
        private static SExprComp wrap(SExprAtomic ha, SExprComp c, bool inc)
        {
            if (c.Atomics.Count != 1)
            {
                throw new VMException("it takes 1 argument", ha);
            }
            SExprComp ret = new SExprComp();
            SExprComp op  = new SExprComp();

            ret.Atomics = c.Atomics.Take(1).ToList();

            op.Atomics.Add(new SExprAtomic(new SToken(SToken.TokenType.ATOMIC, inc ? "+" : "-")));
            op.Atomics.Add(ret.Clone());
            op.Atomics.Add(new SExprAtomic(new SToken(SToken.TokenType.NUMBER, new Decimal(1))));

            ret.Atomics.Add(op);
            return(ret);
        }
示例#27
0
        private static SExprComp wrap(SExprAtomic ha, SExprComp c, string o)
        {
            if (c.Atomics.Count != 2)
            {
                throw new VMException("it takes 2 arguments", ha);
            }
            SExprComp ret = new SExprComp();
            SExprComp op  = new SExprComp();

            ret.Atomics = c.Atomics.Take(1).ToList();

            op.Atomics.Add(new SExprAtomic(new SToken(SToken.TokenType.ATOMIC, o)));
            op.Atomics.Add(ret.Clone());
            op.Atomics.Add(c.Atomics.Skip(1).Take(1).First().Clone());

            ret.Atomics.Add(op);
            return(ret);
        }
示例#28
0
        public SEType(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            if (c.Atomics.Count == 0)
            {
                throw new VMException("it takes 1 argument", ha);
            }

            var n = c.Atomics.Pop();

            if (n is SExprAtomic && (n as SExprAtomic).Token.TType == SToken.TokenType.ATOMIC)
            {
                vname = (string)(n as SExprAtomic).Token.TValue;
            }
            else
            {
                nameExpr = SExpression.Cast(n);
            }
        }
示例#29
0
 public SERegex(SExprAtomic ha, SExprComp c) : base(ha, c)
 {
     if (c.Atomics.Count > 0)
     {
         re = SExpression.Cast(c.Atomics.Pop());
         if (c.Atomics.Count > 0)
         {
             option = SExpression.Cast(c.Atomics.Pop());
         }
         else
         {
             option = new SString("None");
         }
     }
     else
     {
         throw new VMException("it takes 1 or 2 arguments", ha);
     }
 }
示例#30
0
        public SERange(SExprAtomic ha, SExprComp c) : base(ha, c)
        {
            var args = (from v in c.Atomics select SExpression.Cast(v)).ToList();

            if (args.Count == 2)
            {
                start    = args[0];
                end      = args[1];
                interval = new SNumber(1);
            }
            else if (args.Count == 3)
            {
                start    = args[0];
                interval = args[1];
                end      = args[2];
            }
            else
            {
                throw new VMException("it takes 2 or 3 arguments", ha);
            }
        }