Exemplo n.º 1
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())
                                     ));
                }
            }
        }
Exemplo n.º 2
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;
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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());
 }
Exemplo n.º 5
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());
        }
Exemplo n.º 6
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());
        }
Exemplo n.º 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();
        }
Exemplo n.º 8
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;
        }
Exemplo n.º 9
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());
        }
Exemplo n.º 10
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();
        }
Exemplo n.º 11
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());
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 13
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();
        }
Exemplo n.º 14
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();
        }
Exemplo n.º 15
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());
        }
Exemplo n.º 16
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;
        }
Exemplo n.º 17
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());
        }
Exemplo n.º 18
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();
 }
Exemplo n.º 19
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();
        }
Exemplo n.º 20
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());
            }
        }
Exemplo n.º 21
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());
        }
Exemplo n.º 22
0
        BuildMethodPattern(List <SExpression> args, ExecEnvironment env, SExprAtomic headAtom)
        {
            List <Type>   pattern   = new List <Type>();
            List <object> arguments = new List <object>();

            args.ForEach(arg =>
            {
                var a = arg.Evaluate(env);
                if (a is SList && (a as SList).Get <List <SValue> >().Count == 2)
                {
                    var list     = (a as SList).Get <List <SValue> >();
                    var typeName = list[0] as SString;
                    if (typeName == null)
                    {
                        throw new VMException("type name must be a string", headAtom);
                    }

                    var t = Type.GetType(typeName.Get <String>());
                    pattern.Add(t);

                    if (t == typeof(Object))
                    {
                        arguments.Add(list[1].Underlying);
                    }
                    else
                    {
                        arguments.Add(Convert.ChangeType(list[1].Underlying, t));
                    }
                }
                else
                {
                    if (a is SString)
                    {
                        pattern.Add(typeof(string));
                        arguments.Add((string)a.Underlying);
                    }
                    else if (a is SBool)
                    {
                        pattern.Add(typeof(bool));
                        arguments.Add((bool)a.Underlying);
                    }
                    else
                    {
                        throw new VMException("you must specify a type to avoid ambiguousness", headAtom);
                    }
                }
            });

            return(new Tuple <List <Type>, List <object> >(pattern, arguments));
        }
Exemplo n.º 23
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());
        }
Exemplo n.º 24
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);
            }
        }
Exemplo n.º 25
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);
        }
Exemplo n.º 26
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);
        }
Exemplo n.º 27
0
 public static SExpression Cast(SExprAtomic e)
 {
     if (e.Token.TType == SToken.TokenType.STRING)
     {
         return(new SString((string)e.Token.TValue));
     }
     else if (e.Token.TType == SToken.TokenType.NUMBER)
     {
         return(new SNumber((decimal)e.Token.TValue));
     }
     else if (e.Token.TType == SToken.TokenType.SEXPRESSION)
     {
         return((SExpression)e.Token.TValue);
     }
     else
     {
         return(new SEVariable((string)e.Token.TValue, e, null));
     }
 }
Exemplo n.º 28
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);
     }
 }
Exemplo n.º 29
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);
            }
        }
Exemplo n.º 30
0
        public static List <string> CompoundToArguments(SExprComp c, SExprAtomic pos)
        {
            List <string> ret = new List <string>();

            for (var i = 0; i < c.Atomics.Count; i++)
            {
                var a = c.Atomics[i];
                if (!(a is SExprAtomic) || (a as SExprAtomic).Token.TType != SToken.TokenType.ATOMIC)
                {
                    throw new VMException("argument name must be an atom", pos);
                }

                var name = (string)(a as SExprAtomic).Token.TValue;
                if (i != c.Atomics.Count - 1 && name.Length > 3 && name.Substring(name.Length - 3) == "...")
                {
                    throw new VMException("argument list must be at the end of the declaration", pos);
                }

                ret.Add(name);
            }

            return(ret);
        }