public string Visit(FunctionExpression function)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("fn " + function.Name + "(", currentIndent);
            codeWriter.Write(string.Join(", ", function.Parameters.Select(v => v.Name + ":" + v.Type.Name).ToArray()));
            codeWriter.Write(") ");
            if (function.ReturnType.Name != "void")
            {
                codeWriter.Write("-> " + function.ReturnType.Name + " ");
            }
            codeWriter.Write("{");
            codeWriter.NewLine();

            var body = function.GetBody();

            if (body != null)
            {
                currentIndent++;
                codeWriter.Write(Visit(body));
                currentIndent--;
            }

            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
        public object Visit(FunctionExpression function)
        {
            var methods = ctx.GetCurrentTypeMethods();
            var method  = ctx.CurrentMethod = methods.First(x => x.Name == function.Name);

            if (ctx.GetMethodBodyDefined(method))
            {
                throw new InvalidOperationException($"A function with the same name '{function.Name}' already exists");
            }

            // il.Emit(OpCodes.Ldarg_0);

            var body = Visit(function.GetBody());

            var il = method.GetILGenerator();

            // TODO: Check if previous item was a return or not, because we don't want duplicates
            if (il.LastEmittedOpCode != OpCodes.Ret)
            {
                il.Return();
            }

            ctx.SetMethodBodyDefined(method, true);

            ctx.CurrentMethod = ctx.MainMethod;

            return(method);
        }
Exemplo n.º 3
0
        public FunctionExpression Visit(FunctionExpression function)
        {
            codeWriter.Write("fn " + function.Name + "(", currentIndent);
            codeWriter.Write(string.Join(", ", function.GetParameters().Select(v => v.Type.Name + " " + v.Name).ToArray()));
            codeWriter.Write(") {");
            codeWriter.NewLine();

            var body = function.GetBody();

            if (body != null)
            {
                currentIndent++;
                Visit(body);
                currentIndent--;
            }

            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(null);
        }