예제 #1
0
        private MethodInfo SearchMethod(IVisitable visitable)
        {
            var methodName    = "Evaluate";
            var visitableType = visitable.GetType();

            while ((typeof(IVisitable).IsAssignableFrom(visitableType)))
            {
                Type visitorType = GetType();
                while ((typeof(Visitor).IsAssignableFrom(visitorType)))
                {
                    MethodInfo method = visitorType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic,
                                                              Type.DefaultBinder,
                                                              new[] { visitableType },
                                                              null);

                    if (method != null)
                    {
                        return(method);
                    }
                    else
                    {
                        visitorType = visitorType.GetType().BaseType;
                    }
                }
                visitableType = visitableType.GetType().BaseType;
            }

            return(null);
        }
예제 #2
0
        public void IniciaVisita(IVisitable visitable)
        {
            MethodInfo infoMetodo = this.GetType().GetMethod("visita",
                                                             new Type[] { visitable.GetType() });

            infoMetodo.Invoke(this, new object[] { visitable });
        }
예제 #3
0
        public static byte[] ToByteArray(this IVisitable arg)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);

            Methods[arg.GetType()](arg, writer);
            return(stream.ToArray());
        }
예제 #4
0
 public void Add(IVisitable item)
 {
     if (item.GetType().Equals(typeof(Book)))
     {
         Console.WriteLine("Added Book to Cart --> Price: $" + (item as Book).GetPrice() + " | Weight: " + ((item as Book).GetWeight() * 100) + "g");
     }
     items.Add(item);
 }
        private MethodInfo SearchMethod(IVisitable visitable)
        {
            if (visitable == null)
            {
                throw new ArgumentNullException(nameof(visitable));
            }

            return(GetEvaluateMethod(visitable.GetType()));
        }
예제 #6
0
 public static string ToJson(this IVisitable arg)
 {
     return(Methods[arg.GetType()](arg));
 }
예제 #7
0
        public virtual void Start(IVisitable visitable)
        {
            switch (visitable)
            {
            case Program program:
                Visit(program);
                break;

            case BinaryExpression bin:
                Visit(bin);
                break;

            case ConditionalBranch branch:
                Visit(branch);
                break;

            case DoWhileLoop doWhile:
                Visit(doWhile);
                break;

            case ForLoop forLoop:
                Visit(forLoop);
                break;

            case FunctionCall funcCall:
                Visit(funcCall);
                break;

            case FunctionDefinition funcDef:
                Visit(funcDef);
                break;

            case Literal lit:
                Visit(lit);
                break;

            case ReturnExpression ret:
                Visit(ret);
                break;

            case Scope scope:
                Visit(scope);
                break;

            case AST.Type type:
                Visit(type);
                break;

            case UnaryExpression un:
                Visit(un);
                break;

            case Variable var:
                Visit(var);
                break;

            case VariableDeclaration decl:
                Visit(decl);
                break;

            case WhileLoop whileLoop:
                Visit(whileLoop);
                break;

            case Break brk:
                Visit(brk);
                break;

            case Continue cont:
                Visit(cont);
                break;

            default:
                throw new InvalidOperationException($"Visitor doesn't support visitables of type {visitable.GetType()}");
            }
        }