Exemplo n.º 1
0
        public IForm ParseForm()
        {
            Token token = this.NextToken();

            if (token == null)
                return null;

            if (token.Type == TokenType.Operator && token.Value == "-")
            {
                if (this.TryParseAtom("module"))
                    return this.ParseModuleForm();
                if (this.TryParseAtom("export"))
                    return this.ParseExportForm();
                throw new ParserException("Unknown form");
            }

            this.PushToken(token);

            var fform = this.ParseFunctionForm();
            var fforms = new List<FunctionForm>();
            fforms.Add(fform);

            while (this.TryParseToken(TokenType.Separator, ";"))
                fforms.Add(this.ParseFunctionForm());

            this.ParsePoint();

            if (fforms.Count == 1)
                return fform;

            return new MultiFunctionForm(fforms);
        }
Exemplo n.º 2
0
        public static List MakeList(IList<object> elements, List tail = null)
        {
            if (elements.Count == 0)
                return tail;

            return new List(elements[0], MakeList(elements.Skip(1).ToList(), tail));
        }
Exemplo n.º 3
0
        public object Evaluate(Context context, bool withvars = false)
        {
            IList<object> elements = new List<object>();

            foreach (var expr in this.expressions)
                elements.Add(expr.Evaluate(context, withvars));

            return new Tuple(elements);
        }
Exemplo n.º 4
0
        public object Evaluate(Context context, bool withvars = false)
        {
            Context newcontext = new Context();
            IList<object> parameters = new List<object>();

            foreach (var pexpr in this.parameterexpressions)
                parameters.Add(pexpr.Evaluate(newcontext, true));

            var func = new Function(context, parameters, this.body);

            return func;
        }
Exemplo n.º 5
0
        public object Evaluate(Context context)
        {
            Context newcontext = new Context();
            IList<object> parameters = new List<object>();

            foreach (var pexpr in this.parameterexpressions)
                parameters.Add(pexpr.Evaluate(newcontext, true));

            var func = new Function(context, parameters, this.body);

            context.SetValue(string.Format("{0}/{1}", this.name, parameters.Count), func);

            return func;
        }
Exemplo n.º 6
0
        public object Evaluate(Context context, bool withvars = false)
        {
            object namevalue = this.nameexpression.Evaluate(context, withvars);

            IList<object> arguments = new List<object>();

            foreach (var argexpr in this.argumentexpressions)
                arguments.Add(Machine.ExpandDelayedCall(argexpr.Evaluate(context, withvars)));

            if (namevalue is Atom)
                namevalue = context.GetValue(string.Format("{0}/{1}", ((Atom)namevalue).Name, this.argumentexpressions.Count));

            IFunction func = (IFunction)namevalue;

            return func.Apply(context, arguments);
        }
Exemplo n.º 7
0
        public object Evaluate(Context context, bool withvars = false)
        {
            int arity = this.expressions[0].ParameterExpressions.Count;

            if (!this.expressions.Skip(1).All(f => f.ParameterExpressions.Count == arity))
                throw new Exception("head mismatch");

            IList<Function> functions = new List<Function>();

            foreach (var form in this.Expressions)
                functions.Add((Function)form.Evaluate(context, true));

            var func = new MultiFunction(functions);

            return func;
        }
Exemplo n.º 8
0
        private static object Filter(Context context, IList<object> arguments)
        {
            IFunction function = (IFunction)arguments[0];
            List list = (List)arguments[1];
            IList<object> elements = new List<object>();

            while (list != null)
            {
                var result = function.Apply(context, new object[] { list.Head });

                if (true.Equals(result))
                    elements.Add(list.Head);

                list = (List)list.Tail;
            }

            return List.MakeList(elements);
        }
Exemplo n.º 9
0
        public object Evaluate(Context context)
        {
            string name = this.forms[0].Name;
            int arity = this.forms[0].ParameterExpressions.Count;

            if (!this.forms.Skip(1).All(f => f.Name == name && f.ParameterExpressions.Count == arity))
                throw new Exception("head mismatch");

            IList<Function> functions = new List<Function>();

            foreach (var form in this.Forms)
                functions.Add((Function)form.Evaluate(context));

            var func = new MultiFunction(functions);

            context.SetValue(string.Format("{0}/{1}", name, arity), func);

            return func;
        }
Exemplo n.º 10
0
        public object Evaluate(Context context, bool withvars = false)
        {
            object modulevalue = this.moduleexpression.Evaluate(context, withvars);
            object namevalue = this.nameexpression.Evaluate(context, withvars);

            string modulename = ((Atom)modulevalue).Name;
            string name = string.Format("{0}/{1}", ((Atom)namevalue).Name, this.argumentexpressions.Count);

            IList<object> arguments = new List<object>();

            foreach (var argexpr in this.argumentexpressions)
                arguments.Add(argexpr.Evaluate(context, withvars));

            Module module = context.GetValue(modulename) as Module;

            if (module == null || !module.ExportNames.Contains(name) || !(module.Context.GetValue(name) is IFunction))
                throw new Exception(string.Format("undefined function {0}:{1}", modulename, name));

            IFunction func = (IFunction)module.Context.GetValue(name);

            return func.Apply(context, arguments);
        }
Exemplo n.º 11
0
        private IExpression ParseFunExpression()
        {
            var fexpr = this.ParseSimpleFunExpression();
            var fexprs = new List<FunExpression>();

            fexprs.Add(fexpr);

            while (this.TryParseToken(TokenType.Separator, ";"))
                fexprs.Add(this.ParseSimpleFunExpression());

            this.ParseToken(TokenType.Atom, "end");

            if (fexprs.Count == 1)
                return fexpr;

            return new MultiFunExpression(fexprs);
        }
Exemplo n.º 12
0
        private static object Map(Context context, IList<object> arguments)
        {
            IFunction function = (IFunction)arguments[0];
            List list = (List)arguments[1];
            IList<object> elements = new List<object>();

            while (list != null)
            {
                elements.Add(function.Apply(context, new object[] { list.Head }));
                list = (List)list.Tail;
            }

            return List.MakeList(elements);
        }
Exemplo n.º 13
0
        private IExpression ParseCompositeExpression()
        {
            IExpression expression = this.ParseMatchExpression();

            if (expression == null)
                return null;

            IList<IExpression> expressions = new List<IExpression>();

            expressions.Add(expression);

            while (this.TryParseToken(TokenType.Separator, ","))
                expressions.Add(this.ParseMatchExpression());

            if (expressions.Count == 1)
                return expression;

            return new CompositeExpression(expressions);
        }
Exemplo n.º 14
0
        public bool Match(List list, Context context)
        {
            if (list == null)
                return false;

            var result = ErlSharp.MatchUtilities.MatchObjects(this.head, list.Head, context);

            if (!result)
                return false;

            return ErlSharp.MatchUtilities.MatchObjects(this.tail, list.Tail, context);
        }
Exemplo n.º 15
0
        private IForm ParseExportForm()
        {
            this.ParseToken(TokenType.Separator, "(");
            this.ParseToken(TokenType.Separator, "[");

            IList<string> names = new List<string>();

            if (!this.TryParseToken(TokenType.Separator, "]"))
                while (true)
                {
                    string name = this.ParseAtom();
                    this.ParseToken(TokenType.Operator, "/");
                    int arity = this.ParseInteger();
                    names.Add(string.Format("{0}/{1}", name, arity));

                    if (!this.TryParseToken(TokenType.Separator, ","))
                        break;
                }

            this.ParseToken(TokenType.Separator, "]");
            this.ParseToken(TokenType.Separator, ")");
            this.ParsePoint();

            return new ExportForm(names);
        }
Exemplo n.º 16
0
        private IList<IExpression> ParseExpressionList()
        {
            List<IExpression> expressions = new List<IExpression>();

            for (IExpression expr = this.ParseSimpleExpression(); expr != null; expr = this.ParseSimpleExpression())
            {
                expressions.Add(expr);

                Token token = this.NextToken();

                if (token != null && token.Type == TokenType.Separator && token.Value == ",")
                    continue;

                if (token != null)
                    this.PushToken(token);

                break;
            }

            return expressions;
        }
Exemplo n.º 17
0
        private IExpression ParseReceiveExpression()
        {
            IList<MatchBody> matches = new List<MatchBody>();

            while (true)
            {
                var expr = this.ParseSimpleExpression();

                // TODO review head evaluation
                var head = expr.Evaluate(new Context(), true);
                this.ParseToken(TokenType.Operator, "->");
                var body = this.ParseCompositeExpression();
                matches.Add(new MatchBody(head, body));

                if (!this.TryParseToken(TokenType.Separator, ";"))
                    break;
            }

            this.ParseToken(TokenType.Atom, "end");

            return new ReceiveExpression(matches);
        }