Example #1
0
        public string Visit(BinaryExpression binaryOp)
        {
            var codeWriter = new XzaarCodeWriter();
            var op         = GetBinaryOperator(binaryOp.NodeType);

            if (!IsInsideExpression && noIndent == 0)
            {
                codeWriter.Write("", currentIndent);
            }

            insideExpressionCount++;
            codeWriter.Write(Visit(binaryOp.Left));
            insideExpressionCount--;

            codeWriter.Write(" " + op + " ");
            noIndent++;
            insideExpressionCount++;
            codeWriter.Write(Visit(binaryOp.Right));
            insideExpressionCount--;
            noIndent--;
            if (!IsInsideExpression && noIndent == 0)
            {
                codeWriter.NewLine();
            }
            return(codeWriter.ToString());
        }
        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());
        }
Example #3
0
        public string Visit(StructExpression node)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("class " + node.Name + " {", currentIndent);
            codeWriter.NewLine();
            currentIndent++;

            if (node.Fields != null && node.Fields.Length > 0)
            {
                codeWriter.Write("constructor(", currentIndent);
                var fieldNames = node.Fields.Select(x => Visit(x).TrimWsRn()).ToArray();
                codeWriter.WriteLine(string.Join(", ", fieldNames) + ") {");

                currentIndent++;
                codeWriter.WriteLine(string.Join(Environment.NewLine, fieldNames.Select(x => $"this.{x} = {x};")), currentIndent);
                currentIndent--;

                codeWriter.WriteLine("}", currentIndent);
            }

            currentIndent--;
            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Example #4
0
        public string Visit(IfElseExpression ifElse)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("if (", currentIndent);
            insideExpressionCount++;
            codeWriter.Write(Visit(ifElse.Test));
            insideExpressionCount--;
            codeWriter.Write(") { ");
            codeWriter.NewLine();
            if (ifElse.IfTrue != null)
            {
                currentIndent++;
                codeWriter.Write(Visit(ifElse.IfTrue));
                currentIndent--;
            }

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

            if (ifElse.IfFalse != null && !ifElse.IfFalse.IsEmpty())
            {
                codeWriter.Write("else {", currentIndent);
                codeWriter.NewLine();

                currentIndent++;
                codeWriter.Write(Visit(ifElse.IfFalse));
                currentIndent--;

                codeWriter.Write("}", currentIndent);
                codeWriter.NewLine();
            }
            return(codeWriter.ToString());
        }
Example #5
0
        public string Visit(UnaryExpression unary)
        {
            var codeWriter = new XzaarCodeWriter();
            var indent     = IsInsideExpression ? 0 : currentIndent;

            switch (unary.NodeType)
            {
            case ExpressionType.PostIncrementAssign:
                codeWriter.Write("", indent);
                codeWriter.Write(Visit(unary.Item));
                codeWriter.Write("++");
                break;

            case ExpressionType.PostDecrementAssign:
                codeWriter.Write("", indent);
                codeWriter.Write(Visit(unary.Item));
                codeWriter.Write("--");
                break;

            case ExpressionType.Increment:
                codeWriter.Write("++", indent);
                codeWriter.Write(Visit(unary.Item));
                break;

            case ExpressionType.Decrement:
                codeWriter.Write("--", indent);
                codeWriter.Write(Visit(unary.Item));
                break;
            }
            if (!IsInsideExpression)
            {
                codeWriter.NewLine();
            }
            return(codeWriter.ToString());
        }
Example #6
0
        public string Visit(ForExpression @for)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("for (", currentIndent);
            insideExpressionCount++;
            codeWriter.Write(Visit(@for.Initiator));
            insideExpressionCount--;
            codeWriter.Write("; ");

            insideExpressionCount++;
            codeWriter.Write(Visit(@for.Test));
            insideExpressionCount--;
            codeWriter.Write("; ");
            insideExpressionCount++;
            codeWriter.Write(Visit(@for.Incrementor));
            insideExpressionCount--;
            codeWriter.Write(") {");
            codeWriter.NewLine();

            currentIndent++;
            codeWriter.Write(Visit(@for.Body));
            currentIndent--;

            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
        public string Visit(ConstantExpression value)
        {
            var codeWriter = new XzaarCodeWriter();
            var arrayValue = value.ArrayIndex != null ? "[" + Visit(value.ArrayIndex) + "]" : "";

            if (value.Type.IsArray)
            {
                if (value.Value == null)
                {
                    codeWriter.Write("[]" + arrayValue);
                }
                else
                {
                    if (value.Value is XzaarExpression[] initializer)
                    {
                        var values = initializer.Select(Visit).ToList();
                        codeWriter.Write("[" + string.Join(", ", values.ToArray()) + "]" + arrayValue);
                    }
                    else
                    {
                        codeWriter.Write(value.Value + arrayValue);
                    }
                }
            }
            else if (value.Type.IsNumeric || value.Type.Equals(XzaarBaseTypes.Boolean))
            {
                codeWriter.Write(value.Value + arrayValue);
            }
            else
            {
                codeWriter.Write("\"" + value.Value + "\"" + arrayValue);
            }
            return(codeWriter.GetSourceCode());
        }
Example #8
0
        public string Visit(FunctionCallExpression call)
        {
            var codeWriter       = new XzaarCodeWriter();
            var indent           = IsInsideExpression ? 0 : currentIndent;
            var instance         = call.GetInstance();
            var methodInvocation = call.MethodName + "(";
            var instanceText     = string.Empty;

            if (instance != null)
            {
                instanceText = instance is VariableDefinitionExpression instanceExpr
                    ? instanceExpr.Name + "."
                    : Visit(instance) + ".";
            }
            codeWriter.Write(instanceText + methodInvocation, indent);

            insideExpressionCount++;

            for (int index = 0; index < call.Arguments.Count; index++)
            {
                var arg = call.Arguments[index];
                codeWriter.Write(Visit(arg));
                if (index + 1 < call.ArgumentCount)
                {
                    codeWriter.Write(", ");
                }
            }
            insideExpressionCount--;
            codeWriter.Write(")");
            if (!IsInsideExpression)
            {
                codeWriter.NewLine();
            }
            return(codeWriter.ToString());
        }
        public string Visit(FieldExpression node)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write(node.FieldType + " " + node.Name, currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Example #10
0
        public string Visit(LabelExpression label)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write(@label.Target?.Name + ":", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Example #11
0
        private string VisitGoto(GotoExpression @goto)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("goto " + @goto.Target?.Name, currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Example #12
0
        private string VisitBreak(GotoExpression @goto)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("break", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Example #13
0
        private string WriteInstruction(Operation o)
        {
            var cw          = new XzaarCodeWriter();
            var instruction = o as Instruction;

            if (instruction != null)
            {
                if (instruction.OpCode == OpCode.Return)
                {
                    if (instruction.Arguments.Count > 0)
                    {
                        var returnExpr = instruction.Arguments[0];
                        if (returnExpr is FieldReference field)
                        {
                            var chain = new List <string>();
                            var fRef  = field.Instance;
                            while (fRef != null)
                            {
                                chain.Insert(0, fRef.Name);
                                fRef = (fRef as FieldReference)?.Instance;
                            }
                            chain.Add(field.Name);
                            cw.WriteLine(instruction.OpCode + " " + string.Join(".", chain.ToArray()), indent);
                        }
                        else
                        {
                            cw.WriteLine(instruction.OpCode + " " + instruction.Arguments.ToString(", "), indent);
                        }
                    }
                    else
                    {
                        cw.WriteLine(instruction.OpCode + "", indent);
                    }
                }
                else if (instruction.TargetLabel != null)
                {
                    var labelName = GetLabelName(instruction.TargetLabel);
                    cw.WriteLine(
                        instruction.OpCode + " " + labelName + ": " + instruction.Arguments.ToString(", ") + " " +
                        instruction.OperandArguments.ToString(", "), indent);
                }
                else
                {
                    cw.WriteLine(
                        instruction.OpCode + " " + instruction.Arguments.ToString(", ") + " " +
                        instruction.OperandArguments.ToString(", "), indent);
                }
            }
            else
            {
                var label     = o as Label;
                var labelName = GetLabelName(label);
                cw.WriteLine(labelName + ":", indent - 1);
            }
            return(cw.ToString());
        }
Example #14
0
        public string Visit(LambdaExpression lambda)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("(" + string.Join(", ", lambda.Parameters.Select(Visit).ToArray()) + ") => ");

            codeWriter.Write(Visit(lambda.Body));

            return(codeWriter.ToString());
        }
Example #15
0
        private string Disassemble()
        {
            var codeWriter = new XzaarCodeWriter();

            WriteStructs(codeWriter);
            WriteTypes(codeWriter);
            WriteGlobalVariables(codeWriter);
            WriteGlobalFunctions(codeWriter);
            WriteGlobalInstructions(codeWriter);
            return(codeWriter.ToString());
        }
Example #16
0
 private void WriteGlobalInstructions(XzaarCodeWriter codeWriter)
 {
     codeWriter.WriteLine(".instructions");
     indent++;
     foreach (var t in asm.GlobalInstructions)
     {
         codeWriter.Write(WriteInstruction(t));
     }
     indent--;
     codeWriter.WriteLine(".endInstructions");
 }
Example #17
0
 private void WriteGlobalVariables(XzaarCodeWriter codeWriter)
 {
     codeWriter.WriteLine(".globalVariables");
     indent++;
     foreach (var t in asm.GlobalVariables)
     {
         codeWriter.Write(WriteVariable(t));
     }
     indent--;
     codeWriter.WriteLine(".endGlobalVariables");
 }
Example #18
0
 private void WriteGlobalFunctions(XzaarCodeWriter codeWriter)
 {
     codeWriter.WriteLine(".globalFunctions");
     indent++;
     foreach (var t in asm.GlobalMethods)
     {
         codeWriter.Write(WriteFunction(t));
     }
     indent--;
     codeWriter.WriteLine(".endGlobalFunctions");
 }
Example #19
0
        public string Visit(ParameterExpression parameter)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write(parameter.Name);
            if (parameter.ArrayIndex != null)
            {
                codeWriter.Write("[" + Visit(parameter.ArrayIndex) + "]");
            }
            return(codeWriter.ToString());
        }
Example #20
0
 private void WriteStructs(XzaarCodeWriter codeWriter)
 {
     codeWriter.WriteLine(".structs");
     indent++;
     foreach (var t in asm.Types.Where(t => t.IsStruct))
     {
         codeWriter.Write(WriteStruct(t));
     }
     indent--;
     codeWriter.WriteLine(".endStructs");
 }
Example #21
0
        private string WriteFunction(MethodDefinition methodDefinition)
        {
            var cw = new XzaarCodeWriter();

            cw.WriteLine(".function " + methodDefinition.Name, indent);
            indent++;
            cw.WriteLine(".flags 0", indent);
            cw.WriteLine(".return " + methodDefinition.ReturnType.Name, indent);
            cw.WriteLine(".parameters", indent);
            if (methodDefinition.Parameters != null)
            {
                indent++;

                foreach (var t in methodDefinition.Parameters)
                {
                    cw.Write(WriteParameter(t));
                }

                indent--;
            }
            cw.WriteLine(".endParameters", indent);
            cw.WriteLine(".variables", indent);
            if (methodDefinition.Body.MethodVariables != null)
            {
                indent++;

                foreach (var t in methodDefinition.Body.MethodVariables)
                {
                    cw.Write(WriteVariable(t));
                }

                indent--;
            }
            cw.WriteLine(".endVariables", indent);
            cw.WriteLine(".instructions", indent);
            if (methodDefinition.Body.MethodInstructions != null)
            {
                indent++;

                foreach (var t in methodDefinition.Body.MethodInstructions)
                {
                    cw.Write(WriteInstruction(t));
                }

                indent--;
            }
            cw.WriteLine(".endInstructions", indent);
            indent--;
            cw.WriteLine(".endFunction", indent);
            return(cw.ToString());
        }
Example #22
0
        private string VisitReturn(GotoExpression @goto)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("return", currentIndent);
            if (@goto.Value != null && [email protected]())
            {
                codeWriter.Write(" ");
                insideExpressionCount++;
                codeWriter.Write(Visit(@goto.Value));
                insideExpressionCount--;
            }
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Example #23
0
        public string Visit(LogicalNotExpression node)
        {
            var codeWriter = new XzaarCodeWriter();
            var indent     = IsInsideExpression ? 0 : currentIndent;

            insideExpressionCount++;
            codeWriter.Write("!", indent);
            codeWriter.Write(Visit(node.Expression));
            insideExpressionCount--;
            if (!IsInsideExpression)
            {
                codeWriter.NewLine();
            }
            return(codeWriter.ToString());
        }
Example #24
0
        public string Visit(LoopExpression loop)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("while (true) {", currentIndent);
            codeWriter.NewLine();
            if (loop.Body != null)
            {
                currentIndent++;
                codeWriter.Write(Visit(loop.Body));
                currentIndent--;
            }
            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
        public string Visit(StructExpression node)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("struct " + node.Name + " {", currentIndent);
            codeWriter.NewLine();
            currentIndent++;
            foreach (var field in node.Fields)
            {
                codeWriter.Write(Visit(field));
            }
            currentIndent--;
            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Example #26
0
        public string Visit(DoWhileExpression doWhile)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("do {", currentIndent);
            codeWriter.NewLine();
            currentIndent++;
            codeWriter.Write(Visit(doWhile.Body));
            currentIndent--;
            codeWriter.Write("} while(", currentIndent);
            insideExpressionCount++;
            codeWriter.Write(Visit(doWhile.Test));
            insideExpressionCount--;
            codeWriter.Write(")");
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Example #27
0
        private string WriteParameter(ParameterDefinition parameterDefinition)
        {
            var cw = new XzaarCodeWriter();

            cw.WriteLine(".parameter " + parameterDefinition.Name + " " + parameterDefinition.Type.Name, indent);
            indent++;
            cw.WriteLine(".flags 0", indent);
            if (parameterDefinition.InitialValue != null)
            {
                var val = parameterDefinition.InitialValue is string
                          ? "\"" + parameterDefinition.InitialValue + "\""
                          : parameterDefinition.InitialValue;
                cw.WriteLine(".initialValue " + val, indent);
            }
            indent--;
            cw.WriteLine(".endParameter", indent);
            return(cw.ToString());
        }
        public string Visit(LambdaExpression lambda)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("(" + string.Join(", ", lambda.Parameters.Select(Visit).ToArray()) + ") => ");

            // This will fail on single-line lambdas!!
            if (lambda.Body is BlockExpression)
            {
                codeWriter.Write("{" + Visit(lambda.Body) + "}");
            }
            else
            {
                codeWriter.Write(Visit(lambda.Body));
            }

            return(codeWriter.ToString());
        }
        public string Visit(VariableDefinitionExpression definedVariable)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("var " + definedVariable.Name, IsInsideExpression ? 0 : currentIndent);
            if (definedVariable.AssignmentExpression != null)
            {
                codeWriter.Write(" = ");
                insideExpressionCount++;
                codeWriter.Write(Visit(definedVariable.AssignmentExpression));
                insideExpressionCount--;
            }
            if (!IsInsideExpression)
            {
                codeWriter.NewLine();
            }
            return(codeWriter.ToString());
        }
Example #30
0
        private string WriteField(VariableReference variableReference)
        {
            var cw = new XzaarCodeWriter();

            cw.WriteLine(".field " + variableReference.Name + " " + variableReference.Type.Name, indent);
            indent++;
            cw.WriteLine(".flags 0", indent);
            if (variableReference.InitialValue != null)
            {
                var val = variableReference.InitialValue is string
                          ? "\"" + variableReference.InitialValue + "\""
                          : variableReference.InitialValue;
                cw.WriteLine(".initialValue " + val, indent);
            }

            indent--;
            cw.WriteLine(".endField", indent);
            return(cw.ToString());
        }