Exemple #1
0
 public void Parse(IList<string> raw)
 {
     //Console.Clear();
     program = new BaseInt.Class();
     program.name = "unnamed"+RandomString(5);
     ParseMethods(raw);
     foreach (var method in program.methods)
     {
         try{
         ParseRawLoc(method);
             }
      catch(Exception ex)
      {
      Code c = new Code(typeof(comment));
      c.To<comment>().commentedtext = "Parsing error: " + ex.Message + Environment.NewLine+ex.Source+Environment.NewLine+ex.StackTrace+Environment.NewLine+ex.InnerException;
      c.To<comment>().multiline = true;
      method.code.Add(c);
      }
     }
 }
Exemple #2
0
        public string ConvertCode(Code code)
        {
            Type t = code.code.GetType();
            if (t == typeof(callmethod))
            {
                return code.To<callmethod>().methodname + "();";
            }
            if (t == typeof(Output))
            {
                if (code.To<Output>().outstring.ToString().EndsWith(Environment.NewLine))
                {
                    return @"Console.WriteLine(""" + code.To<Output>().outstring.ToString().Replace(Environment.NewLine, "") + @""");";
                }
                else
                {
                    return @"Console.Write(""" + code.To<Output>().outstring.ToString() + @""");";
                }
            }
            if (t == typeof(comment))
            {
                if (code.To<comment>().multiline)
                {
                    return @"/*" + code.To<comment>().commentedtext + @"*/";
                }
                else
                {
                    return @"//" + code.To<comment>().commentedtext;
                }
            }
            if (t == typeof(DeclareVariable))
            {
                string ret = "";
                DeclareVariable d = code.To<DeclareVariable>();
                if (d.variable.isdeclared)
                {
                    ret += d.variable.type.Name + " ";
                }
                ret += d.variable.name;
                if (!d.setvariabledata)
                {
                    ret += ";";

                }
                else
                {
                    ret += @" = """ + d.variable.data + @""";";
                }
                return ret;
            }
            if (t == typeof(Input))
            {
                if (code.To<Input>().storevar)
                {
                    if (
                    code.To<Input>().var.isdeclared
                        )
                    {
                        return code.To<Input>().var.name + " = Console.ReadLine();";
                    }
                    else
                    {
                        return "//TODO!";
                    }
                }
                else
                {
                    return "Console.ReadLine();";
                }
            }
            if (t == typeof(pause))
            {
                string ret = "";
                ret += @"Console.WriteLine(""Press any key to continue..."");";
                ret+=Environment.NewLine+"\t\t\tConsole.ReadKey();";
                return ret;
            }

            return "//code convert error here, whilst trying to parse code of type: "+t.Name;
        }
Exemple #3
0
        public void ParseRawLoc(Method m)
        {
            int no = 0;
            while (no != m.rawcode.Count)
            {
                if (m.rawcode[no].ToLower().StartsWith("goto"))
                {
                   Code c= new Code(typeof(callmethod));
                   c.To<callmethod>().methodname = m.rawcode[no].Replace("goto ", "");
                    m.code.Add(c);
                }
                if (m.rawcode[no].ToLower().StartsWith("pause"))
                {
                    m.code.Add(new Code(typeof(pause)));
                }
                if (m.rawcode[no].ToLower().StartsWith("echo "))
                {
                    Code c = new Code(typeof(Output));
                    c.To<Output>().outstring = m.rawcode[no].Replace("echo ", "") + Environment.NewLine;
                    if (m.rawcode[no].ToLower().StartsWith("echo."))
                    {
                        c.To<Output>().outstring = Environment.NewLine;
                    }
                    m.code.Add(c);
                }
                if (m.rawcode[no].ToLower().StartsWith("set"))
                {
                    #region set
                    Var v = new Var();
                    v.isdeclared = true;
                    //0  1
                    ///p var=
                    #region getvarname
                    foreach (var item in m.rawcode[no].Split(' '))
                    {
                        if (item.Contains("="))
                        {
                            v.name = item.Substring(0,item.IndexOf('='));
                        }
                    }
                    #endregion

                    // 0   1  2
                    //set /p var=
                    #region getvartype
                    try
                    {
                        if (m.rawcode[no].Split(' ')[1].Contains("/a"))
                        {
                            v.type = typeof(int);
                        }
                        else
                        {
                            v.type = typeof(string);
                        }
                    }
                    catch { v.type = typeof(string); }
                    v.isvardataset = false;
                    #endregion
                    #region declare var
                    Code c = new Code(typeof(DeclareVariable));
                    #region set
                    if (!string.IsNullOrEmpty(m.rawcode[no].Split('=')[1]))
                    {
                        v.data = m.rawcode[no].Split('=')[1].Replace("=", "");
                        c.To<DeclareVariable>().setvariabledata = true;
                    }
                    #endregion
                   // c.To<DeclareVariable>().setvariabledata = false;
                    c.To<DeclareVariable>().variable = v;
                    m.code.Add(c);// string s;
                    #endregion

                    //----------------------------------------------------------
                    #region mathematical var definition mk1
                  //v super fancy comment switch
                    //*
                    if (m.rawcode[no].Split(' ')[1].Contains("/a"))
                    {
                        Code cc = new Code(typeof(MathDeclareVariable));
                        List<string> vars = new List<string>();
                        #region parse all vars involved
                        foreach (var item in m.rawcode[no].Split(' ')[2].Substring(
                            m.rawcode[no].Split(' ')[2].IndexOf('=') + 0, m.rawcode[no].Split(' ')[2].Length))
                        {
                            #region parsing stuff
                            if (item == '%')
                            {
                                string varname = "";
                                vars.Add(varname);
                            }
                            else if (new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'
                                ,'q','r','s','t','u','v','w','x','y','z'}.Contains(item.ToString().ToLower()[0]))
                            {
                                vars[vars.Count] += item;
                            }
                            if (item == '+')
                            {
                                cc.To<MathDeclareVariable>().mathematicaloperators.Add(Operator.add);
                            }
                            if (item == '-')
                            {
                                cc.To<MathDeclareVariable>().mathematicaloperators.Add(Operator.minus);
                            }
                            if (item == '/')
                            {
                                cc.To<MathDeclareVariable>().mathematicaloperators.Add(Operator.divide);
                            }
                            if (item == '*')
                            {
                                cc.To<MathDeclareVariable>().mathematicaloperators.Add(Operator.multiply);
                            }
                            #endregion
                        }
                        #region remove empty values
                        no = 0;
                        while (no != vars.Count)
                        {
                            if (string.IsNullOrEmpty(vars[no]))
                            {
                                vars.RemoveAt(no);
                            }
                            no++;
                        }
                        #endregion
                        #endregion
                        foreach (var va in vars)
                        {
                            Var var = new Var();
                            var.isdeclared = true;
                            var.isvardataset = true;//probably or else batch wont compile
                            var.name = va;
                            var.type = typeof(int);//""
                            cc.To<MathDeclareVariable>().vars.Add(var);

                        }
                        v.isvardataset = true;
                        cc.To<MathDeclareVariable>().outputvar = v;
                        m.code.Add(cc);
                    }
                     /*/

                    //*
                    //
                    #endregion
                    #region mathematical var def mk2
                    if (m.rawcode[no].Split(' ')[1].Contains("/a"))
                    {
                        Code cc = new Code(typeof(MathDeclareVariable));
                        v.isvardataset = true;
                        cc.To<MathDeclareVariable>().outputvar = v;
                        cc.options.Add(m.rawcode[no].Split(' ')[1]
                    }
                    //*/
                    #endregion
                    if (m.rawcode[no].Split(' ')[1].Contains("/p"))
                    {
                        //set /p var=
                        Code cc = new Code(typeof(Input));
                        cc.To<Input>().storevar = true;
                        v.isvardataset = true;
                        cc.To<Input>().var = v;
                        //vvvv
                        //VARNAME = Console.ReadLine();
                        m.code.Add(cc);
                    }
                    #endregion
                }
                no++;
            }
        }