示例#1
0
        public static bool TryScan(CodeReader reader, out ForCommand command)
        {
            command = null;
            string line = reader.NextLine();

            if (!Regex.IsMatch(line, RegEx))
            {
                return(false);
            }
            string         variable  = line.Split(':')[0];
            int            min       = int.Parse(line.Split('(', ' ')[2]);
            int            max       = int.Parse(Regex.Match(line, " to " + RegExHelper.Integer + " in ").Value.Split(new string[] { " to ", " in " }, StringSplitOptions.RemoveEmptyEntries)[0]);
            int            increment = 1;  //TODO
            List <Command> commands  = new List <Command>();

            line = reader.NextLine();
            while (!reader.Done && line != "endfor")
            {
                if (TryScan(line, out Command cmd))
                {
                    commands.Add(cmd);
                }
                else
                {
                    throw new ArgumentException();
                }
                line = reader.NextLine();
            }
            command = new ForCommand(variable, new CommandCollection(commands.ToArray()), min, max, increment);
            return(true);
        }
示例#2
0
文件: Command.cs 项目: wert007/Slides
        public static bool TryScan(CodeReader reader, out Command scanned)
        {
            scanned = null;
            string line = reader.NextLine().Trim(';');

            if (Regex.IsMatch(line, LoopCommand.RegEx))
            {
                string loop   = line.Split(':')[0];
                string fpsStr = Regex.Match(loop, RegExHelper.Integer + "fps").Value;
                if (!int.TryParse(fpsStr.Trim('f', 'p', 's'), out int fps))
                {
                    fps = 30;
                }
                int    times    = -1;
                string timesStr = Regex.Match(loop, RegExHelper.Integer + "(" + RegExHelper.Time + ")?").Value;
                if (!int.TryParse(Regex.Match(timesStr, RegExHelper.Integer).Value, out times))
                {
                    times = -1;
                }
                if (TryScan(line.Substring(line.IndexOf(':') + 1), out Command cmd))
                {
                    scanned = new LoopCommand(cmd, fps, times);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, FunctionCallCommand.RegEx))
            {
                string obj        = line.Split('.')[0];
                string method     = line.Split('.', '(')[1];
                string parameters = line.Substring(line.IndexOf(method) + method.Length);
                if (TryScan(parameters, out Command cmd))
                {
                    scanned = new FunctionCallCommand(obj, method, (ParameterCommand)cmd);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(SetPropertyCommand.RegEx)))
            {
                string obj      = line.Split('.')[0];
                string property = line.Split('.', ':')[1];
                string value    = line.Substring(line.IndexOf(property) + property.Length + 1);
                if (TryScan(value, out Command cmd))
                {
                    scanned = new SetPropertyCommand(obj, property, cmd);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, Command.RegExWholeLine(AssignmentCommand.RegEx)))
            {
                if (AssignmentCommand.TryScan(reader.Copy(), out AssignmentCommand cmd))
                {
                    scanned = cmd;
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(SetArrayCommand.RegEx)))
            {
                string variable = line.Split('[')[0];
                string number   = line.Split('[', ']')[1];
                string command  = string.Join(":", line.Split(':').Skip(1)).Trim();
                if (Command.TryScan(command, out Command val))
                {
                    if (int.TryParse(number, out int index))
                    {
                        scanned = new SetArrayCommand(variable, index, val);
                        return(true);
                    }
                }
            }
            if (Regex.IsMatch(line, ForCommand.RegEx))
            {
                if (ForCommand.TryScan(reader.Copy(), out ForCommand cmd))
                {
                    scanned = cmd;
                    return(true);
                }
            }
            if (Regex.IsMatch(line, GotoCommand.RegEx))
            {
                throw new NotImplementedException();
            }
            if (Regex.IsMatch(line, IfCommand.Regex))
            {
                if (TryScan(line.Split('(', ')')[1], out Command condition))
                {
                    List <Command> body = new List <Command>();
                    while (line != "endif" && !reader.Done)
                    {
                        line = reader.NextLine();
                        if (TryScan(reader.Copy(), out Command cmd))
                        {
                            body.Add(cmd);
                        }
                        else
                        {
                            throw new Exception("Unknown Command at Line " + reader.TextLine + ".");
                        }
                        line = reader.NextLine().Trim();
                    }
                    scanned = new IfCommand(condition, new CommandCollection(body.ToArray()));
                    return(true);
                }
            }
            if (Regex.IsMatch(line, SwitchCommand.Regex))
            {
                if (TryScan(line.Split('(', ')')[1], out Command condition))
                {
                    List <CaseCommand> body = new List <CaseCommand>();
                    while (line != "endswitch" && !reader.Done)
                    {
                        if (Regex.IsMatch(line, CaseCommand.RegEx))
                        {
                            if (CaseCommand.TryScan(reader.Copy(), out CaseCommand cmd))
                            {
                                body.Add(cmd);
                            }
                            else
                            {
                                throw new Exception("Unknown Command at Line " + reader.TextLine + ".");
                            }
                        }
                        line = reader.NextLine().Trim();
                    }
                    scanned = new SwitchCommand(condition, body.ToArray());
                    return(true);
                }
            }
            if (Regex.IsMatch(line, WhileCommand.Regex))
            {
                throw new NotImplementedException();
            }
            if (Regex.IsMatch(line, RegExWholeLine(StyleCommand.RegEx)))
            {
                if (StyleValue.TryScan(line, out StyleValue styleValue))
                {
                    scanned = new StyleCommand(styleValue);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, ApplyStyleCommand.RegEx))
            {
                if (line.Count(c => c == '.') > 0)
                {
                    Console.WriteLine("ApplyStyleCommand: " + line);
                    string lastHalf  = line.Split('.').Last();
                    string firstHalf = line.Remove(line.Length - lastHalf.Length - 1);
                    string name      = lastHalf.Trim('(', ')');
                    if (TryScan(firstHalf, out Command cmd))
                    {
                        scanned = new ApplyStyleCommand(Style.GetByName(name), cmd);
                        return(true);
                    }
                }
                else
                {
                    Console.WriteLine("ApplyStyleCommand: " + line);
                }
            }
            //if (Regex.IsMatch(line, ValueCommand.RegEx))
            //	throw new NotImplementedException();
            //if (Regex.IsMatch(line, CommandCollection.Regex))
            //	throw new NotImplementedException();
            return(false);
        }