Example #1
0
        public override PsudoInstruction CopyTo(PsudoMethod newMethod)
        {
            CallMethodInstruction clone = new CallMethodInstruction(this.Line, newMethod, this.methodName);

            clone.Arguments = this.Arguments;
            return(clone);
        }
Example #2
0
        private static PsudoInstruction RealCompileInstruction(string line, int lineNum, PsudoMethod method)
        {
            string args          = "";
            string variableRegex = VariableRegexString;

            if (CheckSimpleCommand(line, "read", out args))
            {
                string[] vars = SplitVariableLiteralList(args);

                return(new ReadInstruction(lineNum, method, vars));
            }
            else if (CheckSimpleCommand(line, "get", out args))
            {
                if (lastInstruction is PromptInstruction)
                {
                    return(new NoOpInstruction(lineNum, method));
                }
                throw new Exception("Cannot use get without a prompt command");
            }
            else if (CheckSimpleCommand(line, "print", out args) ||
                     CheckSimpleCommand(line, "write", out args) ||
                     CheckSimpleCommand(line, "output", out args) ||
                     CheckSimpleCommand(line, "put", out args) ||
                     CheckSimpleCommand(line, "display", out args)
                     )
            {
                string[] vars = SplitVariableLiteralList(args);

                return(new PrintInstruction(lineNum, method, vars));
            }
            else if (CheckSimpleCommand(line, "prompt", out args) ||
                     CheckSimpleCommand(line, "input", out args))
            {
                string withoutFor = args.Trim();
                if (args.Trim().StartsWith("for"))
                {
                    withoutFor = withoutFor.Substring(3).Trim();
                }

                string[] vars = SplitVariableLiteralList(withoutFor);

                return(new PromptInstruction(lineNum, method, vars));
            }
            else if (CheckSimpleCommand(line, "add", out args))
            {
                string oper = "+";
                // This should be in the format "add number to total"
                Match number = Regex.Match(args,
                                           @"^[\s]*\b" + variableRegex + @"\b[\s]+(?=([\s]*\b(to)\b[\s]*))",
                                           RegexOptions.IgnoreCase);
                Match total = Regex.Match(args,
                                          @"(?<=([\s]*\b(to)\b[\s]*))\b" + variableRegex + @"\b[\s]*",
                                          RegexOptions.IgnoreCase);

                if (!number.Success || !total.Success)
                {
                    throw new Exception("Invalid Operation Arguments on " + lineNum);
                }

                string expression = total.Value + oper + number.Value;
                return(new MathmaticAssignmentInstruction(lineNum, method, total.Value.Trim(), expression));
            }
            else if (CheckSimpleCommand(line, "subtract", out args))
            {
                string oper = "-";
                // This should be in the format "add number to total"
                Match number = Regex.Match(args,
                                           @"^[\s]*\b" + variableRegex + @"\b[\s](?=([\s]*\b(from)\b[\s]*))",
                                           RegexOptions.IgnoreCase);
                Match total = Regex.Match(args,
                                          @"(?<=([\s]*\b(from)\b[\s]*))\b" + variableRegex + @"\b[\s]*",
                                          RegexOptions.IgnoreCase);

                if (!number.Success || !total.Success)
                {
                    throw new Exception("Invalid Operation Arguments on " + lineNum);
                }

                string expression = total.Value + oper + number.Value;
                return(new MathmaticAssignmentInstruction(lineNum, method, total.Value.Trim(), expression));
            }
            else if (CheckSimpleCommand(line, "multiply", out args))
            {
                string oper = "*";
                // This should be in the format "add number to total"
                Match total = Regex.Match(args,
                                          @"^[\s]*\b" + variableRegex + @"\b[\s](?=([\s]*\b(by)\b[\s]*))",
                                          RegexOptions.IgnoreCase);
                Match number = Regex.Match(args,
                                           @"(?<=([\s]*\b(by)\b[\s]*))\b" + variableRegex + @"\b[\s]*",
                                           RegexOptions.IgnoreCase);

                if (!number.Success || !total.Success)
                {
                    throw new Exception("Invalid Operation Arguments on " + lineNum);
                }

                string expression = total.Value + oper + number.Value;
                return(new MathmaticAssignmentInstruction(lineNum, method, total.Value.Trim(), expression));
            }
            else if (CheckSimpleCommand(line, "divide", out args))
            {
                string oper = "/";
                // This should be in the format "add number to total"
                Match total = Regex.Match(args,
                                          @"^[\s]*\b" + variableRegex + @"\b[\s](?=([\s]*\b(by)\b[\s]*))",
                                          RegexOptions.IgnoreCase);
                Match number = Regex.Match(args,
                                           @"(?<=([\s]*\b(by)\b[\s]*))\b" + variableRegex + @"\b[\s]*",
                                           RegexOptions.IgnoreCase);

                if (!number.Success || !total.Success)
                {
                    throw new Exception("Invalid Operation Arguments on " + lineNum);
                }

                string expression = total.Value + oper + number.Value;
                return(new MathmaticAssignmentInstruction(lineNum, method, total.Value.Trim(), expression));
            }
            else if (CheckSimpleCommand(line, "initialize", out args) ||
                     CheckSimpleCommand(line, "set", out args))
            {
                // This should be in the format "set number to total"
                Match name = Regex.Match(args,
                                         @"^[\s]*\b" + variableRegex + @"\b[\s](?=([\s]*\b(to)\b[\s]*))",
                                         RegexOptions.IgnoreCase);
                Match value = Regex.Match(args,
                                          @"(?<=([\s]*to[\s]*))\b" + variableRegex + @"\b[\s]*",
                                          RegexOptions.IgnoreCase);

                if (!value.Success || !name.Success)
                {
                    throw new Exception("Invalid Operation Arguments on " + lineNum);
                }

                return(new AssignVariable(lineNum, method, name.Value.Trim(), value.Value.Trim()));
            }
            // Simple Assignment Instructions
            else if (
                // check for normal assignment
                Regex.IsMatch(line, @"^[\s]*" + variableRegex + @"[\s]*=[\s]*[$]*[a-zA-Z0-9._ ,]*[\s]*$", RegexOptions.IgnoreCase)
                // check for string literals
                || Regex.IsMatch(line, @"^[\s]*" + variableRegex + @"[\s]*=[\s]*" + "[\\'\"]" + @"[a-zA-Z0-9_' -]*" + "[\\'\"]" + @"[\s]*$", RegexOptions.IgnoreCase)
                )
            {
                Match name = Regex.Match(line,
                                         @"^[\s]*" + variableRegex + @"[\s]*=",
                                         RegexOptions.IgnoreCase);
                Match value = Regex.Match(line,
                                          @"(?<=(=[\s]))*[$]*[a-zA-Z0-9._," + '"' + @"' -]*[\s]*$",
                                          RegexOptions.IgnoreCase);
                // This is a simple assignment to either a variable or literal
                return(new AssignVariable(lineNum, method, name.Value.Trim('=').Trim(), value.Value.Trim()));
            }
            // Mathmatic assignment
            else if (Regex.IsMatch(line, @"^[\s]*" + variableRegex + @"[\s]*=[\s]*[a-zA-Z0-9.()\s" + "\"" + @"_+-/*,]*[\s]*$", RegexOptions.IgnoreCase))
            {
                Match name = Regex.Match(line,
                                         @"^[\s]*" + variableRegex + @"[\s]",
                                         RegexOptions.IgnoreCase);
                Match value = Regex.Match(line,
                                          @"(?<=(=[\s]))*[a-zA-Z0-9._ ()" + "\"" + @"+-/*,]*[\s]*$",
                                          RegexOptions.IgnoreCase);

                // This is a simple assignment to either a variable or literal
                return(new MathmaticAssignmentInstruction(lineNum, method, name.Value.Trim(), value.Value.Trim()));
            }
            // Simple Call Method
            else if (Regex.IsMatch(line, @"^[\s]*[\s]*[a-zA-Z0-9_]*[\s]*$", RegexOptions.IgnoreCase))
            {
                string name = line.Trim();
                CallMethodInstruction ins = new CallMethodInstruction(lineNum, method, name.Trim());
                return(ins);
            }

            throw new Exception(string.Format(
                                    "Invalid Instruction in Method \"{0}\"",
                                    method.Name));
        }