<commit_before>﻿using Converter.Emitter.OutputFunctions.HelperFunctions;
using Converter.Emitter.OutputFunctions.RootEmitters;
using ICSharpCode.NRefactory.CSharp;
using System;
using System.Linq;

namespace Converter.Emitter.OutputFunctions
{
    static class ExpressionEmitter
    {
        /// <summary>
        /// Evaluates given expression and adds the corresponding typescript code to the output
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="arguments"></param>
        internal static void Output(Expression expression, EmitterArguments arguments){
            var output = arguments.Output;
            switch (expression.GetType().Name)
            {
                case "AnonymousMethodExpression":
                    var ame = (AnonymousMethodExpression)expression;
                    ParameterEmitter.Output(ame.Parameters, arguments);
                    output.Add("=>");
                    Emitter.ProcessNode(ame.Body,arguments);
                    break;
                case "AsExpression":
                    var ase = (AsExpression)expression;
                    output.AddWithoutSpace("Statements.As(");
                    Output(ase.Expression,arguments);
                    output.AddWithoutSpace(",");
                    output.AddWithoutSpace(AstTypeToStringConverter.Convert(ase.Type,arguments));
                    output.AddWithoutSpace(")");
                    break;
                case "AssignmentExpression":
                    var ae = (AssignmentExpression)expression;
                    Output(ae.Left,arguments);
                    AssignmentOperatorEmitter.Output(ae.Operator,output);
                    Output(ae.Right,arguments);
                    break;
                case "BaseReferenceExpression":
                    var bre = (BaseReferenceExpression)expression;
                    output.AddWithoutSpace("super");
                    break;
                case "BinaryOperatorExpression":
                    var binopExp = (BinaryOperatorExpression)expression;
                    Output(binopExp.Left,arguments);
                    BinaryOperatorEmitter.Output(binopExp.Operator,output);
                    Output(binopExp.Right,arguments);
                    break;
                //example : var a= (ISomething)someThingElse;
                case "CastExpression":
                    var castExpr = (CastExpression)expression;
                    output.AddWithoutSpace("<");
                    output.AddWithoutSpace(AstTypeToStringConverter.Convert(castExpr.Type,arguments));
                    output.AddWithoutSpace(">");
                    Output(castExpr.Expression,arguments);
                    break;
                case "ConditionalExpression":
                    var ce = (ConditionalExpression)expression;
                    Output(ce.Condition, arguments);
                    output.Add(" ?");
                    Output(ce.TrueExpression,arguments);
                    output.Add(" :");
                    Output(ce.FalseExpression,arguments);
                    break;
                case "DirectionExpression":
                    //example : {ref someThing} 
                    //example : {out someThing}
                    var de = (DirectionExpression)expression;
                    //TODO : skip ref/out ..
                    Output(de.Expression,arguments);
                    break;
                case "IdentifierExpression":
                    //check if Identifier is a locally known identifier (which needs "this." prefix).
                    string identStr = expression.ToString();

                    if (arguments.TypeTree.ActiveType.NameExists(identStr))
                    {
                        output.AddWithoutSpace("this.");
                    }
                    output.AddWithoutSpace(identStr);
                    break;
                case "InvocationExpression":
                    var ie = (InvocationExpression)expression;
                    Output(ie.Target,arguments);
                    output.Add("(");
                    for (int i = 0; i < ie.Arguments.Count; i++)
                    {
                        Output(ie.Arguments.ElementAt(i),arguments);
                        if (i < ie.Arguments.Count - 1) { output.Add(","); }
                    }
                    output.Add(")");
                    break;
                case "IndexerExpression":
                    var indexExpr = (IndexerExpression)expression;
                    Output(indexExpr.Target,arguments);
                    output.AddWithoutSpace("[");
                    new ArgumentsEmitter(arguments).Output(indexExpr.Arguments);
                    output.AddWithoutSpace("]");
                    break;
                case "IsExpression":
                    var isExpr = (IsExpression)expression;
                    output.AddWithoutSpace("System.Statements.is(");
                    Output(isExpr.Expression,arguments);
                    output.AddWithoutSpace(@",""");
                    output.AddWithoutSpace(AstTypeToStringConverter.Convert(isExpr.Type,arguments));
                    output.AddWithoutSpace(@""")");
                    break;
                case "MemberReferenceExpression":
                    var mre = (MemberReferenceExpression)expression;
                    Output(mre.Target,arguments);
                    output.AddWithoutSpace(".");
                    output.AddWithoutSpace(mre.MemberName);
                    break;
                case "NullExpression":
                    output.Add("null");
                    break;
                case "NullReferenceExpression":
                    output.Add("null");
                    break;
                case "ObjectCreateExpression":
                    var oce = (ObjectCreateExpression)expression;
                    //TODO : find out if we need to use the static-Create on internal objects or the New ;
                    output.Add(oce.ToString());
                    break;
                case "ParenthesizedExpression":
                    var pe = (ParenthesizedExpression)expression;
                    output.AddWithoutSpace("(");
                    Output(pe.Expression,arguments);
                    output.AddWithoutSpace(")");
                    break;
                case "PrimitiveExpression":
                    PrimitiveValueEmitter.Output(((PrimitiveExpression)expression).Value,output);
                    break;
                case "TypeOfExpression":
                    var toe = (TypeOfExpression)expression;
                    output.AddWithoutSpace("System.Statements.typeOf(");
                    output.AddWithoutSpace(AstTypeToStringConverter.Convert(toe.Type,arguments));
                    output.AddWithoutSpace(")");
                    break;
                case "TypeReferenceExpression":
                    //TODO : better 
                    //FIND type
                    bool wasFound = false;
                    foreach (var ns in arguments.ReferencedNamespaces)
                    {
                        if (arguments.TypeTree.ExistsTypeInNamespace(expression.ToString(), ns))
                        {
                            output.AddWithoutSpace(ns + "." + expression.ToString());
                            wasFound = true;
                        }
                    }
                    if (!wasFound)
                    {
                        output.AddWithoutSpace(expression.ToString());
                    }
                    break;
                case "ThisReferenceExpression":
                    output.AddWithoutSpace("this");
                    break;
                case "UnaryOperatorExpression":<commit_msg>improved namespace resolving<commit_after>﻿using Converter.Emitter.OutputFunctions.HelperFunctions;
using Converter.Emitter.OutputFunctions.RootEmitters;
using ICSharpCode.NRefactory.CSharp;
using System;
using System.Linq;

namespace Converter.Emitter.OutputFunctions
{
    static class ExpressionEmitter
    {
        /// <summary>
        /// Evaluates given expression and adds the corresponding typescript code to the output
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="arguments"></param>
        internal static void Output(Expression expression, EmitterArguments arguments){
            var output = arguments.Output;
            switch (expression.GetType().Name)
            {
                case "AnonymousMethodExpression":
                    var ame = (AnonymousMethodExpression)expression;
                    ParameterEmitter.Output(ame.Parameters, arguments);
                    output.Add("=>");
                    Emitter.ProcessNode(ame.Body,arguments);
                    break;
                case "AsExpression":
                    var ase = (AsExpression)expression;
                    output.AddWithoutSpace("Statements.As(");
                    Output(ase.Expression,arguments);
                    output.AddWithoutSpace(",");
                    output.AddWithoutSpace(AstTypeToStringConverter.Convert(ase.Type,arguments));
                    output.AddWithoutSpace(")");
                    break;
                case "AssignmentExpression":
                    var ae = (AssignmentExpression)expression;
                    Output(ae.Left,arguments);
                    AssignmentOperatorEmitter.Output(ae.Operator,output);
                    Output(ae.Right,arguments);
                    break;
                case "BaseReferenceExpression":
                    var bre = (BaseReferenceExpression)expression;
                    output.AddWithoutSpace("super");
                    break;
                case "BinaryOperatorExpression":
                    var binopExp = (BinaryOperatorExpression)expression;
                    Output(binopExp.Left,arguments);
                    BinaryOperatorEmitter.Output(binopExp.Operator,output);
                    Output(binopExp.Right,arguments);
                    break;
                //example : var a= (ISomething)someThingElse;
                case "CastExpression":
                    var castExpr = (CastExpression)expression;
                    output.AddWithoutSpace("<");
                    output.AddWithoutSpace(AstTypeToStringConverter.Convert(castExpr.Type,arguments));
                    output.AddWithoutSpace(">");
                    Output(castExpr.Expression,arguments);
                    break;
                case "ConditionalExpression":
                    var ce = (ConditionalExpression)expression;
                    Output(ce.Condition, arguments);
                    output.Add(" ?");
                    Output(ce.TrueExpression,arguments);
                    output.Add(" :");
                    Output(ce.FalseExpression,arguments);
                    break;
                case "DirectionExpression":
                    //example : {ref someThing} 
                    //example : {out someThing}
                    var de = (DirectionExpression)expression;
                    //TODO : skip ref/out ..
                    Output(de.Expression,arguments);
                    break;
                case "IdentifierExpression":
                    //check if Identifier is a locally known identifier (which needs "this." prefix).
                    string identStr = expression.ToString();

                    if (arguments.TypeTree.ActiveType.NameExists(identStr))
                    {
                        output.AddWithoutSpace("this.");
                    }
                    output.AddWithoutSpace(identStr);
                    break;
                case "InvocationExpression":
                    var ie = (InvocationExpression)expression;
                    Output(ie.Target,arguments);
                    output.Add("(");
                    for (int i = 0; i < ie.Arguments.Count; i++)
                    {
                        Output(ie.Arguments.ElementAt(i),arguments);
                        if (i < ie.Arguments.Count - 1) { output.Add(","); }
                    }
                    output.Add(")");
                    break;
                case "IndexerExpression":
                    var indexExpr = (IndexerExpression)expression;
                    Output(indexExpr.Target,arguments);
                    output.AddWithoutSpace("[");
                    new ArgumentsEmitter(arguments).Output(indexExpr.Arguments);
                    output.AddWithoutSpace("]");
                    break;
                case "IsExpression":
                    var isExpr = (IsExpression)expression;
                    output.AddWithoutSpace("System.Statements.is(");
                    Output(isExpr.Expression,arguments);
                    output.AddWithoutSpace(@",""");
                    output.AddWithoutSpace(AstTypeToStringConverter.Convert(isExpr.Type,arguments));
                    output.AddWithoutSpace(@""")");
                    break;
                case "MemberReferenceExpression":
                    var mre = (MemberReferenceExpression)expression;
                    Output(mre.Target,arguments);
                    output.AddWithoutSpace(".");
                    output.AddWithoutSpace(mre.MemberName);
                    break;
                case "NullExpression":
                    output.Add("null");
                    break;
                case "NullReferenceExpression":
                    output.Add("null");
                    break;
                case "ObjectCreateExpression":
                    var oce = (ObjectCreateExpression)expression;
                    //TODO : find out if we need to use the static-Create on internal objects or the New ;
                    var objectNameSpace = arguments.ReferencedNamespaces.FirstOrDefault(ns => arguments.TypeTree.ExistsTypeInNamespace(oce.Type.ToString(), ns));
                    if (objectNameSpace != null)
                    {
                        output.Add("new " + objectNameSpace + "." + oce.Type + "()");
                    }
                    else
                    {
                        output.Add("new " + oce.Type + "()");
                    }
                    break;
                case "ParenthesizedExpression":
                    var pe = (ParenthesizedExpression)expression;
                    output.AddWithoutSpace("(");
                    Output(pe.Expression,arguments);
                    output.AddWithoutSpace(")");
                    break;
                case "PrimitiveExpression":
                    PrimitiveValueEmitter.Output(((PrimitiveExpression)expression).Value,output);
                    break;
                case "TypeOfExpression":
                    var toe = (TypeOfExpression)expression;
                    output.AddWithoutSpace("System.Statements.typeOf(");
                    output.AddWithoutSpace(AstTypeToStringConverter.Convert(toe.Type,arguments));
                    output.AddWithoutSpace(")");
                    break;
                case "TypeReferenceExpression":
                    //TODO : better 
                    //FIND type
                    var typeNameSpace = arguments.ReferencedNamespaces.FirstOrDefault(ns => arguments.TypeTree.ExistsTypeInNamespace(expression.ToString(), ns));
                    if (typeNameSpace != null)
                    {
                        output.AddWithoutSpace(typeNameSpace + "." + expression);
                    }
                    else
                    {
                        output.AddWithoutSpace(expression.ToString());
                    }
                    break;
                case "ThisReferenceExpression":
                    output.AddWithoutSpace("this");
                    break;
                case "UnaryOperatorExpression":
{'fraction_non_alphanumeric': 0.09127432517263026, 'fraction_numerical': 0.00025109855618330194, 'mean_word_length': 1.331820843091335, 'pattern_counts': {'":': 44, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '377549', 'n_tokens_mistral': 3484, 'n_tokens_neox': 3197, 'n_words': 592}