//Tokenise using the lexer in F#
    public void script(string line)
    {
        #region Lexing

        StringBuilder str      = new StringBuilder();
        var           hookList = new List <string> {
            TARGET.HOOK
        };
        var trigList = new List <bool> {
            TARGET.active
        };
        foreach (Entity ent in ENT_LIST)
        {
            hookList.Add(ent.HOOK);
            trigList.Add(ent.active);
        }

        var hooks     = Inter.interpretStr(line, hookList, trigList);
        var tokenList = new List <Token>();
        // Add tokens to a C# list manually because there are type conflicts
        foreach (Token t in hooks)
        {
            tokenList.Add(t);
            str.Append(Lexer.printToken(t));
        }

        //CONSOLE.displayMessage(str.ToString());
        CONSOLE.appendConsoleLogLine(str.ToString());

        #endregion

        #region Parsing and executing

        if (tokenList[tokenList.Count - 1].IsOP) // Multiple hooks and one operation
        {
            var entList = new List <Entity>();
            for (int t = 0; t + 1 < tokenList.Count; t += 2)
            {
                //Syntax checking
                if (!(tokenList[t + 1].IsEX || tokenList[t + 1].IsOP))
                {
                    CONSOLE.appendConsoleLogLine(string.Format(
                                                     "SYNTAX ERROR: {0} can only be proceeded by an expression " +
                                                     "(i.e. AND) or an operator (i.e. +/-).",
                                                     Lexer.printToken(tokenList[t])));
                    return;
                }
                var ent = getEntity(Parser.getVA(tokenList[t]));
                if (ent == null)
                {
                    CONSOLE.appendConsoleLogLine(string.Format(
                                                     "SYNTAX ERROR: {0} does not exist.",
                                                     Lexer.printToken(tokenList[t])));
                    return;
                }

                else
                {
                    entList.Add(ent);
                }
            }
            bool op = Parser.getOP(tokenList[tokenList.Count - 1]);

            // If the hooks are triggers
            int trigCount = 0;
            foreach (var ent in entList)
            {
                if (TRIG_LIST.Contains(ent) && ent.checkActive())
                {
                    trigCount++;
                }
            }
            if (trigCount == TRIG_LIST.Count)
            {
                TARGET.trigger(new string[] { op.ToString() });
                return;
            }

            // If they're not triggers
            foreach (var ent in entList)
            {
                if (ent.active)
                {
                    ent.trigger(new string[] { op.ToString() });
                }
                else
                {
                    CONSOLE.appendConsoleLogLine(string.Format(
                                                     "SYNTAX ERROR: {0} is not active/doesn't exist, try one of the gears!", ent.HOOK));
                }
            }
        }
        else
        {
            CONSOLE.appendConsoleLogLine(string.Format(
                                             "SYNTAX ERROR: Script must be finished with an operator " +
                                             "(i.e. +/-)."));
            return;
        }

        #endregion
    }
Beispiel #2
0
 public Parser(Lexer lexer)
 {
     this.lexer   = lexer;
     currentToken = lexer.GetNextToken();
 }
Beispiel #3
0
 protected internal override bool Match(Lexer lexer)
 {
     return(choose(lexer) != null);
 }
Beispiel #4
0
 public ASTree Parse(Lexer lexer)
 {
     return(program.Parse(lexer));
 }
Beispiel #5
0
        static void Main(string[] args)
        {
            Lexer lexer = new Lexer("if x > y then a-b");

            lexer.tokenise();
        }