Click here to Skip to main content
15,885,546 members
Articles / Containers / Virtual Machine

Twiggery Scripting Language

Rate me:
Please Sign up or sign in to vote.
4.82/5 (14 votes)
12 Aug 2010LGPL313 min read 63.7K   1.1K   36  
Twiggery Scripting Language
using System;
using System.Collections.Generic;
using System.Text;

namespace Twiggery
{
    /// <summary>
    /// Broken processor
    /// Helper for process break, continue assembling
    /// </summary>
    public sealed class BrokenTwig
    {
        #region Singleton
        private static BrokenTwig self = null;

        /// <summary>
        /// Constructor
        /// </summary>
        private BrokenTwig()
        {
            stackReturn = new Stack<int>();
            stackBreak = new Stack<Stack<int>>();
            stackContinue = new Stack<Stack<int>>();
            stackIfElseBlockFalse = new Stack<int>();
            stackIfElseBlockTrue = new Stack<Stack<int>>();
        }

        /// <summary>
        /// Get the only instance
        /// </summary>
        /// <returns>The only instance of brokenprocessor</returns>
        public static BrokenTwig GetInstance()
        {
            if (null == self)
                self = new BrokenTwig();

            return self;
        }
        #endregion
        /// <summary>
        /// return statement stack
        /// </summary>
        private Stack<int> stackReturn = null;

        /// <summary>
        /// break statement stack
        /// </summary>
        private Stack<Stack<int>> stackBreak = null;

        /// <summary>
        /// continue statement stack
        /// </summary>
        private Stack<Stack<int>> stackContinue = null;

        /// <summary>
        /// Jump to next judgement if an 'if' twig is false
        /// </summary>
        private Stack<int> stackIfElseBlockFalse = null;

        /// <summary>
        /// Execute 'if' twig body if it is true
        /// </summary>
        private Stack<Stack<int>> stackIfElseBlockTrue = null;

        /// <summary>
        /// Initialize
        /// </summary>
        public void Initialize()
        {
            stackReturn.Clear();
            stackBreak.Clear();
            stackContinue.Clear();
            stackIfElseBlockFalse.Clear();
            stackIfElseBlockTrue.Clear();
        }

        /// <summary>
        /// Begin a 'function' twig
        /// </summary>
        /// <param name="vm">VM</param>
        public void BlockBeginFunction(ref TVM vm)
        {
        }

        /// <summary>
        /// End a 'function' twig
        /// </summary>
        /// <param name="vm">VM</param>
        public void BlockEndFunction(ref TVM vm)
        {
            if (stackIfElseBlockTrue.Count != 0)
            {
                BlockEndIfElseTrue(ref vm, vm.AsmCount);
            }
            while (stackReturn.Count != 0)
            {
                int index = stackReturn.Pop();
                vm[index].Operand[0] = vm.AsmCount - 1;
            }
        }

        /// <summary>
        /// Begin a loop
        /// </summary>
        /// <param name="vm">VM</param>
        public void BlockBeginLoop(ref TVM vm)
        {
            stackBreak.Push(new Stack<int>());
            stackContinue.Push(new Stack<int>());
        }

        /// <summary>
        /// End a loop
        /// </summary>
        /// <param name="vm">VM</param>
        /// <param name="condition">The index of a TASM instruction compiled from a loop condition</param>
        /// <param name="outside">The index of a TASM instruction compiled from the statement after loop body</param>
        public void BlockEndLoop(ref TVM vm, int condition, int outside)
        {
            while (stackBreak.Peek().Count != 0)
            {
                int index = stackBreak.Peek().Pop();
                vm[index].Operand[0] = outside;
            }
            stackBreak.Pop();
            while (stackContinue.Peek().Count != 0)
            {
                int index = stackContinue.Peek().Pop();
                vm[index].Operand[0] = condition;
            }
            stackContinue.Pop();
        }

        /// <summary>
        /// Process 'return'
        /// </summary>
        /// <param name="asmIndex">Index of a TASM instruction compiled from a 'return' twig</param>
        public void JumpReturn(int asmIndex)
        {
            stackReturn.Push(asmIndex);
        }

        /// <summary>
        /// Process 'break'
        /// </summary>
        /// <param name="asmIndex">Index of a TASM instruction compiled from a 'break' twig</param>
        public void JumpBreak(int asmIndex)
        {
            stackBreak.Peek().Push(asmIndex);
        }

        /// <summary>
        /// Process 'continue'
        /// </summary>
        /// <param name="asmIndex">Index of a TASM instruction compiled from a 'continue' twig</param>
        public void JumpContinue(int asmIndex)
        {
            stackContinue.Peek().Push(asmIndex);
        }

        /// <summary>
        /// Begin processing if an 'if[[-elseif]-else]' twig is false
        /// </summary>
        /// <param name="vm">VM</param>
        public void BlockBeginIfElseFalse(ref TVM vm)
        {
        }

        /// <summary>
        /// End processing if an 'if[[-elseif]-else]' twig is false
        /// </summary>
        /// <param name="vm">VM</param>
        /// <param name="nextBlock">The first index of a TASM instruction compiled from an 'elseif' or an 'else'</param>
        public void BlockEndIfElseFalse(ref TVM vm, int nextBlock)
        {
            int index = stackIfElseBlockFalse.Pop();
            vm[index].Operand[0] = nextBlock;
        }

        /// <summary>
        /// Begin processing if an 'if[[-elseif]-else]' twig is true
        /// </summary>
        /// <param name="vm">VM</param>
        public void BlockBeginIfElseTrue(ref TVM vm)
        {
            stackIfElseBlockTrue.Push(new Stack<int>());
        }

        /// <summary>
        /// End processing if an 'if[[-elseif]-else]' twig is true
        /// </summary>
        /// <param name="vm">VM</param>
        /// <param name="nextBlock">The first index of a TASM instruction compiled from a statement after an 'if[[-elseif]-else]'</param>
        public void BlockEndIfElseTrue(ref TVM vm, int nextBlock)
        {
            while (stackIfElseBlockTrue.Peek().Count != 0)
            {
                int index = stackIfElseBlockTrue.Peek().Pop();
                vm[index].Operand[0] = nextBlock;
            }
            stackIfElseBlockTrue.Pop();
        }

        /// <summary>
        /// Process if an 'if[[-elseif]-else]' twig is false
        /// </summary>
        /// <param name="asmIndex">Index of a TASM instruction</param>
        public void JumpIfElseFalse(int asmIndex)
        {
            stackIfElseBlockFalse.Push(asmIndex);
        }

        /// <summary>
        /// Process if an 'if[[-elseif]-else]' twig is true
        /// </summary>
        /// <param name="asmIndex">Index of a TASM instruction</param>
        public void JumpIfElseTrue(int asmIndex)
        {
            stackIfElseBlockTrue.Peek().Push(asmIndex);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Architect
China China
Video game player & creator; Hardware geek & maker.

Comments and Discussions