public ErlangExpression Parse(string code)
        {
            var expression   = ErlangSyntaxNode.ParseExpression(new TokenBuffer(ErlangToken.Tokenize(new TextBuffer(code))));
            var compiledExpr = ErlangExpression.Compile(expression);

            return(compiledExpr);
        }
Beispiel #2
0
        private static void VerifyModuleAttributes(string text, params string[] expected)
        {
            var module = ErlangSyntaxNode.Parse(text);
            var actual = module.Attributes.Select(a => a.ToString()).ToArray();

            ArrayEquals(expected, actual);
        }
Beispiel #3
0
        private static bool TryBind(string code, ErlangValue value)
        {
            var frame        = new ErlangStackFrame("test", "test", 0);
            var expression   = ErlangSyntaxNode.ParseExpression(new TokenBuffer(ErlangToken.Tokenize(new TextBuffer(code))));
            var compiledExpr = ErlangExpression.Compile(expression);

            return(ErlangBinder.TryBindParameter(compiledExpr, value, frame));
        }
Beispiel #4
0
        private static ErlangValue BindAndFetchVariable(string code, ErlangValue valueToBind, string variable)
        {
            var frame        = new ErlangStackFrame("test", "test", 0);
            var expression   = ErlangSyntaxNode.ParseExpression(new TokenBuffer(ErlangToken.Tokenize(new TextBuffer(code))));
            var compiledExpr = ErlangExpression.Compile(expression);

            Assert.True(ErlangBinder.TryBindParameter(compiledExpr, valueToBind, frame), $"Failure binding to '{code}'.");
            return(frame.GetVariable(variable));
        }
Beispiel #5
0
 public void ParseOtp()
 {
     foreach (var file in Directory.EnumerateFiles(OTPSources, "*.erl"))
     {
         Console.WriteLine($"Parsing {file}.");
         var code         = File.ReadAllText(file);
         var expression   = ErlangSyntaxNode.ParseExpression(new TokenBuffer(ErlangToken.Tokenize(new TextBuffer(code))));
         var compiledExpr = ErlangExpression.Compile(expression);
     }
 }
Beispiel #6
0
        public static ErlangExpression Compile(ErlangSyntaxNode syntax)
        {
            Func <IEnumerable <ErlangSyntaxNode>, ErlangExpression[]> CompileChildren = (ErlangSyntaxNodes) => ErlangSyntaxNodes.Select(n => Compile(n)).ToArray();
            var type = syntax.GetType();

            if (type == typeof(ErlangAtomSyntax))
            {
                return(new ErlangAtomExpression(((ErlangAtomSyntax)syntax).Atom.Text));
            }
            else if (type == typeof(ErlangVariableSyntax))
            {
                return(new ErlangVariableExpression(((ErlangVariableSyntax)syntax).Variable.Text));
            }
            else if (type == typeof(ErlangConstantSyntax))
            {
                var token = ((ErlangConstantSyntax)syntax).Token;
                return(new ErlangConstantExpression(token.IsIntegral ? new ErlangNumber(token.IntegerValue) : new ErlangNumber(token.DoubleValue)));
            }
            else if (type == typeof(ErlangTupleSyntax))
            {
                return(new ErlangTupleExpression(CompileChildren(((ErlangTupleSyntax)syntax).Items.Select(i => i.Item))));
            }
            else if (type == typeof(ErlangUnaryOperationSyntax))
            {
                var unary = (ErlangUnaryOperationSyntax)syntax;
                return(new ErlangUnaryExpression(Compile(unary.Expression), unary.Operator.OperatorKind));
            }
            else if (type == typeof(ErlangBinaryOperationSyntax))
            {
                var binop = (ErlangBinaryOperationSyntax)syntax;
                return(new ErlangBinaryExpression(Compile(binop.Left), Compile(binop.Right), binop.Operator.OperatorKind));
            }
            else if (type == typeof(ErlangParentheticalExpressionSyntax))
            {
                return(Compile(((ErlangParentheticalExpressionSyntax)syntax).Expression));
            }
            else if (type == typeof(ErlangListRegularSyntax))
            {
                var list = (ErlangListRegularSyntax)syntax;
                return(new ErlangListExpression(list.Items.Select(i => Compile(i.Item)).ToArray(), list.Tail == null ? null : Compile(list.Tail)));
            }
            else if (type == typeof(ErlangListStringSyntax))
            {
                var list = (ErlangListStringSyntax)syntax;
                return(new ErlangListExpression(list.String.Value.ToCharArray().Select(c => new ErlangConstantExpression(c)).ToArray(), null));
            }
            else if (type == typeof(ErlangFunctionInvocationSyntax))
            {
                var func   = (ErlangFunctionInvocationSyntax)syntax;
                var module = func.ModuleReference == null ? null : func.ModuleReference.Module.Text;
                return(new ErlangFunctionInvocationExpression(module, func.Function.Text, func.Parameters.Select(p => Compile(p.Value)).ToArray()));
            }
            else if (type == typeof(ErlangCaseSyntax))
            {
                var @case      = (ErlangCaseSyntax)syntax;
                var expression = Compile(@case.Expression);
                return(new ErlangCaseExpression(expression, @case.Branches.Select(b => Compile(b)).Cast <ErlangCaseBranchExpression>().ToArray()));
            }
            else if (type == typeof(ErlangCaseBranchSyntax))
            {
                var branch  = (ErlangCaseBranchSyntax)syntax;
                var pattern = Compile(branch.Pattern);
                return(new ErlangCaseBranchExpression(pattern,
                                                      branch.Guard == null ? null : CompileGuard(branch.Guard),
                                                      branch.Expressions.Select(s => Compile(s.Expression)).ToArray()));
            }
            else if (type == typeof(ErlangFunctionDefinitionSyntax))
            {
                var func = (ErlangFunctionDefinitionSyntax)syntax;
                return(new ErlangFunctionOverloadExpression(
                           func.Parameters.Select(p => Compile(p.Value)).ToArray(),
                           func.Guard == null ? null : CompileGuard(func.Guard),
                           func.Expressions.Select(s => Compile(s.Expression)).ToArray()));
            }
            else if (type == typeof(ErlangFunctionGroupSyntax))
            {
                var func = (ErlangFunctionGroupSyntax)syntax;
                return(new ErlangFunctionGroupExpression(func.Name, func.Airity, func.Definitions.Select(def => Compile(def)).Cast <ErlangFunctionOverloadExpression>().ToArray()));
            }
            else if (type == typeof(ErlangModuleSyntax))
            {
                throw new ArgumentException("Cannot compile module syntax.  Use ErlangCompiledModule.Compile() instead.");
            }
            throw new ArgumentException("Cannot compile syntax; unsupported type.");
        }
Beispiel #7
0
        public static ErlangModule Compile(string code)
        {
            var syntax = ErlangSyntaxNode.Parse(code);

            return(Compile(syntax));
        }