Ejemplo n.º 1
0
        public void Visit(FuncDefinition funcDef)
        {
            Environment.NewScope();

            // Return statements can now be applied
            inFunction = true;

            // Add arguments to new scope (reverse order because of stack)
            for (int currParam = funcDef.Parameters.Count - 1; currParam >= 0; currParam--)
            {
                dynamic arg = Environment.PopFromTheStack();

                if (arg is double && funcDef.Parameters[currParam].Type == "NumToken")
                {
                    Environment.AddItem(new NumItem(funcDef.Parameters[currParam].Name, arg));
                }
                else if (arg is string && funcDef.Parameters[currParam].Type == "StrToken")
                {
                    Environment.AddItem(new StrItem(funcDef.Parameters[currParam].Name, arg));
                }
                else if (arg is TurtleItem && funcDef.Parameters[currParam].Type == "TurtleToken")
                {
                    Environment.AddReferenceToTurtle(funcDef.Parameters[currParam].Name, Environment.GetItem(arg.Name));
                }
            }

            // Run body of function
            funcDef.Body.Accept(this);

            // Exit function mode
            inFunction = false;
            Environment.DeleteScope();
        }
Ejemplo n.º 2
0
        public void Visit(FuncDefinition funcDef)
        {
            Console.Write("func ");
            Console.Write(funcDef.Name);

            Console.Write("(");
            int argsCntr = funcDef.Parameters.Count;

            foreach (VarDeclaration arg in funcDef.Parameters)
            {
                arg.Accept(this);

                if (argsCntr != 1)
                {
                    Console.Write(",");
                    argsCntr--;
                }
            }
            Console.Write(")");

            funcDef.Body.Accept(this);
        }