Пример #1
0
        public ErlangExpression Parse(string code)
        {
            var expression   = ErlangSyntaxNode.ParseExpression(new TokenBuffer(ErlangToken.Tokenize(new TextBuffer(code))));
            var compiledExpr = ErlangExpression.Compile(expression);

            return(compiledExpr);
        }
Пример #2
0
        public static ErlangModule Compile(ErlangModuleSyntax syntax)
        {
            var module = new ErlangCompiledModule();

            // get the module's name
            var atom = syntax.Attributes.OfType <ErlangAttributeSyntax>().Single(at => at.Name.Text == "module").Parameters.Single().Value as ErlangAtomSyntax;

            module.Name = atom.Atom.Text;

            // get visibility
            var publicFunctions = new HashSet <Tuple <string, int> >();
            var exportAll       =
                (from at in syntax.Attributes.OfType <ErlangAttributeSyntax>()
                 where at.Name.Text == "compile" &&
                 at.Parameters.Count == 1
                 let param = at.Parameters[0]
                             where param.Value is ErlangAtomSyntax && ((ErlangAtomSyntax)param.Value).Atom.Text == "export_all"
                             select true).Any(b => b);

            if (!exportAll)
            {
                foreach (var functionReference in
                         from at in syntax.Attributes.OfType <ErlangAttributeSyntax>()
                         where at.Name.Text == "export" && at.Parameters.Count == 1
                         let param = at.Parameters[0]
                                     where param.Value is ErlangListRegularSyntax
                                     let list = (ErlangListRegularSyntax)param.Value
                                                from item in list.Items
                                                where item.Item is ErlangFunctionReferenceSyntax
                                                select(ErlangFunctionReferenceSyntax) item.Item)
                {
                    publicFunctions.Add(Tuple.Create(functionReference.Function.Text, (int)functionReference.Airity.IntegerValue));
                }
            }

            // compile functions
            var functionList      = new List <ErlangTuple>();
            var exportedFunctions = new List <ErlangTuple>();

            foreach (var group in syntax.FunctionGroups)
            {
                var key = Tuple.Create(group.Name, group.Airity);
                functionList.Add(new ErlangTuple(new ErlangAtom(group.Name), new ErlangNumber(group.Airity)));
                var function = (ErlangFunctionGroupExpression)ErlangExpression.Compile(group);
                function.Module   = module;
                function.IsPublic = exportAll || publicFunctions.Contains(key);
                if (function.IsPublic)
                {
                    exportedFunctions.Add(new ErlangTuple(new ErlangAtom(group.Name), new ErlangNumber(group.Airity)));
                }
                module.AddFunction(key.Item1, key.Item2, function);
            }

            functionList.Add(new ErlangTuple(new ErlangAtom("module_info"), new ErlangNumber(0)));
            functionList.Add(new ErlangTuple(new ErlangAtom("module_info"), new ErlangNumber(1)));
            exportedFunctions.Add(new ErlangTuple(new ErlangAtom("module_info"), new ErlangNumber(0)));
            exportedFunctions.Add(new ErlangTuple(new ErlangAtom("module_info"), new ErlangNumber(1)));

            module.AllFunctions = new ErlangList(functionList.ToArray());
            module.ModuleInfo   = new ErlangList(
                new ErlangTuple(new ErlangAtom("exports"), new ErlangList(exportedFunctions.ToArray())),
                new ErlangTuple(new ErlangAtom("imports"), new ErlangList()),    // always empty.  may be removed in future versions
                new ErlangTuple(new ErlangAtom("attributes"), new ErlangList()), // TODO: populate attributes
                new ErlangTuple(new ErlangAtom("compile"), new ErlangList())     // TODO: process compile options
                );

            return(module);
        }