// Parses code
        public static Chunk Parse(string UserCode)
        {
            bool Success;
            Parser Parser = new Parser();

            Chunk Chunk = Parser.ParseChunk(new TextInput(UserCode), out Success);

            if (Success)
                return Chunk;
            else
                throw new ArgumentException("Interpreter: Code has syntax errors:\r\n" + Parser.GetErrorMessages());
        }
Example #2
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            m_env = new Env();
            m_sunImg = Content.Load<Texture2D>("sun");
            string sunSpriteSer = @"
            {Sprites:[
            {Offset:{X:0,Y:0},Rectangle:{Height:122,Width:158,X:0,Y:0}},
            {Offset:{X:4,Y:0},Rectangle:{Height:122,Width:135,X:158,Y:0}}
            ]}
            ";
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);
            sw.Write(sunSpriteSer);
            sw.Flush();
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(SpriteSheet));
            m_sunSS = (SpriteSheet)serializer.ReadObject(ms);
            m_sun = new Item(m_sunImg, m_sunSS);
            string script = "a != b";
            /*
            string script = @"
            item.SetSprite((env.GetTimeMs() / 200) % 2);
            item.SetSprite = 0;
            ";
            */
            Parser parser = new Parser();
            TextInput ti = new TextInput(script);
            bool succ;
            m_script = parser.ParseChunk(ti, out succ);
            if (!succ)
                Debug.WriteLine(parser.GetEorrorMessages());
            m_globals = new LuaTable();
            LuaTable methods = new LuaTable();
            methods.SetNameValue("__index", methods);
            methods.SetNameValue("__newindex", methods);
            methods.Register("SetSprite", m_sun.SetSprite_FromScript);
            m_globals.SetNameValue("item", new Language.Lua.LuaUserdata(m_sun, methods));

            methods = new LuaTable();
            methods.SetNameValue("__index", methods);
            methods.Register("GetTimeMs", m_env.GetTimeMs);
            m_globals.SetNameValue("env", new LuaUserdata(m_env, methods));

            m_env.TimeMs = 0;
        }