Exemple #1
0
 public void Start()
 {
     try
     {
         visitor.Visit(tree);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Exemple #2
0
        public override DynObj VisitFunctionCallExpr([NotNull] KittenGrammarParser.FunctionCallExprContext context)
        {
            var name = context.ID().Symbol.Text;

            if (!memory.ContainsKey(name))
            {
                ReportError("UnrecognizedIdentifierError", $"identifier name {name} not found in the context", context.start.Line);
            }
            var paraList  = memory[name].FuncVal.parameters;
            var valueList = new List <DynObj>();

            if (context.exprList() != null)
            {
                foreach (var item in context.exprList().expr())
                {
                    valueList.Add(Visit(item));
                }
            }
            if (memory[name].FuncVal.t == function.funcType.User)
            {
                if (paraList.Count != valueList.Count)
                {
                    ReportError("ArgumentNumberMismatchError", $"expected {paraList.Count}, but called with {valueList.Count}", context.start.Line);
                }
                Dictionary <string, DynObj> contextMem = new Dictionary <string, DynObj>();
                for (int i = 0; i < paraList.Count; i++)
                {
                    contextMem.Add(paraList[i], valueList[i]);
                }
                var funcVisitor = new KittenVisitor(contextMem);
                return(funcVisitor.Visit(memory[name].FuncVal.IMP));
            }
            if (memory[name].FuncVal.t == function.funcType.Builtin)
            {
                return(memory[name].FuncVal.BuiltinIMP(valueList));
            }
            return(base.VisitFunctionCallExpr(context));
        }