Esempio n. 1
0
        void CheckCrucialRegisters()
        {
            float R0 = MetaSymbol.Cast <float>(Tokens.NUM, symbolTable[0]).Value;

            switch ((int)R0)
            {
            case 1:
                // continue
                return;

            case 2:
                StandardAPI.output(line);
                return;

            case 0:
                // exit
                ready    = false;
                instance = null;
                return;

            case -1:
            default:
                // throw error
                ready = false;
                throw new InterpreterError("Error: (-1) abrupt shutdown");
            }
        }
Esempio n. 2
0
        internal void Push(string variable, MetaSymbol sym)
        {
            // push variable
            //Push(new Symbol<Var>(Tokens.VAR, new Var(preprocessor.AddModule(varId), arg)));
            preprocessor.ParsePreprocessorExp(string.Format("#define {0} R{1}", preprocessor.AddModule(variable), TableSize()));

            Push(sym);
        }
Esempio n. 3
0
        /// <summary>
        /// Sets a register to a value (from R0 to registers-1)
        /// </summary>
        public void SetRegister(int register, MetaSymbol value)
        {
            if (register > registers - 1)
            {
                throw new PermissionError("modify address", register);
            }

            symbolTable[register] = value;
        }
Esempio n. 4
0
        public static Symbol <T> Cast <T>(string expectedType, MetaSymbol sym)
        {
            if (sym.Type != expectedType)
            {
                throw new InterpreterError(Tokens.TYPE_ERR, expectedType, sym.Type);
            }

            return((Symbol <T>)sym);
        }
Esempio n. 5
0
        /// <summary>
        /// Gets a register and casts the value to a desired type
        /// </summary>
        public Symbol <T> GetRegister <T>(int register, string type)
        {
            MetaSymbol value = GetRegister(register);

            if (value.Type != type && type != Tokens.ANY)
            {
                throw new SyntaxError(type, value.Type);
            }

            return((Symbol <T>)value);
        }
Esempio n. 6
0
        /// <summary>
        /// Gets a register and casts the value to a desired type
        /// </summary>
        public Symbol <T> GetAddress <T>(int addr, params string[] types)
        {
            MetaSymbol value = GetRegister(addr);

            foreach (string type in types)
            {
                if (value.Type == type)
                {
                    return((Symbol <T>)value);
                }
            }

            throw new SyntaxError(value.Type, types[0]);
        }
Esempio n. 7
0
        /// <summary>
        /// Handles label definitions and comments
        /// </summary>
        /// <returns></returns>
        bool ParseNonExecutable()
        {
            // split line into words
            words = line.Split(Tokens.SEPARATOR);
            string first = words[0].Trim();

            // statement is documentation for the next open block
            if (first.Length > 1 && first.Substring(0, 2) == Tokens.DOC)
            {
                labelDocBuffer.Add(line.Substring(2).Trim());
                return(true);
            }

            // line is just a comment
            if (first[0] == Tokens.COMMENT)
            {
                return(true);
            }

            if (first == Tokens.SECTION_DEF)
            {
                // this defines a section
                // 'section' <identifier> ':'
                preprocessor.ParsePreprocessorExp(Tokens.PREPROCESSOR_DEF + line.Split(Tokens.DEF_MARKER)[0]);
                return(true);
            }

            if (first == Tokens.SECTION_END)
            {
                preprocessor.ParsePreprocessorExp(Tokens.PREPROCESSOR_DEF + first);
                return(true);
            }

            if (!isLabelOpen && line.Contains(Tokens.DEF) && Tokens.ValidType(first))
            {
                // this defines a variable
                // <type> <identifier> ':' <arg>
                string[] tokens = line.Split(Tokens.DEF_MARKER);

                if (tokens.Length < 2)
                {
                    throw new SyntaxError("Variable definition", "<nil>");
                }

                // force type unless type is <var>
                string typeCast = first == Tokens.VAR ? "" : first;

                // get variable data
                MetaSymbol arg = ParseArguments(tokens[1].Split(Tokens.SEPARATOR), typeCast, strictType: true)[0];

                string varId = words[1];

                // remove ':' from variable name if it exists
                varId = varId[varId.Length - 1] == Tokens.DEF_MARKER ? varId.Substring(0, varId.Length - 1) : varId;
                Push(varId, arg);
                return(true);
            }

            if (!isLabelOpen && line.Contains(Tokens.DEF))
            {
                // this is a label definition
                if (isLabelOpen)
                {
                    // there was another label opened, close it
                    CloseBlock();
                }

                // parse the label and keep a reference to the result
                isLabelOpen = true;
                openLabel   = ParseLabel();

                if (openLabel.inline)
                {
                    // this is an inline block and should be closed
                    CloseBlock();
                    isLabelOpen = false;
                }

                return(true);
            }

            if (first == Tokens.END)
            {
                // close this label
                CloseBlock();
                isLabelOpen = false;
                return(true);
            }

            if (isLabelOpen)
            {
                // label is currently open, append new lines to its buffer
                //labelBodyBuffer.Add(preprocessor.Compile(words));
                labelBodyBuffer.Add(line);
                return(true);
            }

            // this statement is not a special block
            return(false);
        }
Esempio n. 8
0
 /// <summary>
 /// Adds data to the end of the Symbol Table
 /// </summary>
 internal void Push(MetaSymbol sym)
 {
     symbolTable.Add(sym);
 }
Esempio n. 9
0
 internal void SetAddress(int addr, MetaSymbol value)
 {
     symbolTable[addr] = value;
 }
Esempio n. 10
0
 internal static bool Instruction(MetaSymbol sym)
 {
     return(sym.Type == LBL || sym.Type == FUN);
 }