Beispiel #1
0
        protected void ParseCode(Body parent, StringBuilder code)
        {
            try
            {
                code = new StringBuilder(code.ToString().Trim());

                UnwrapCurlys(code);

                while (code.Length > 0)
                {
                    if (StartsWithIf(code))
                    {
                        StringBuilder condition = GetCondition(code);
                        StringBuilder body      = GetBody(code);
                        IfStatement   ifs       = new IfStatement();
                        ifs.Condition = BuildExprTree(condition);
                        ParseCode(ifs.Body, body);
                        parent.Append(ifs);
                    }
                    else if (StartsWithElseIf(code))
                    {
                        IfStatement   ifs       = new IfStatement();
                        StringBuilder condition = GetCondition(code);
                        StringBuilder body      = GetBody(code);
                        ifs.Condition = BuildExprTree(condition);
                        ParseCode(ifs.Body, body);
                        parent.AppendElseIf(ifs);
                    }
                    else if (StartsWithElse(code))
                    {
                        Body          elseBody = new Body();
                        StringBuilder body     = GetBody(code);
                        ParseCode(elseBody, body);
                        parent.AppendElse(elseBody);
                    }
                    else if (StartsWithWhile(code))
                    {
                        StringBuilder condition = GetCondition(code);
                        StringBuilder body      = GetBody(code);
                        WhileLoop     lp        = new WhileLoop();
                        lp.Condition = BuildExprTree(condition);
                        ParseCode(lp.Body, body);
                        parent.Append(lp);
                    }
                    else
                    { //must be a statement
                        StringBuilder statement = ReadToSemicolon(code);

                        //preprocess statements like +=
                        int operIndex = 0;
                        if (IsMathAssignment(statement.ToString(), ref operIndex))
                        {
                            string rewrite = ReWriteMathAssignment(statement.ToString(), operIndex);
                            statement.Clear();
                            statement.Insert(0, rewrite);
                        }
                        else if (IsIncrementOrDecrement(statement.ToString(), ref operIndex))
                        {
                            string rewrite = ReWriteIncrementDecrement(statement.ToString(), operIndex);
                            statement.Clear();
                            statement.Insert(0, rewrite);
                        }

                        if (IsPropOrAttrAssignment(statement))
                        {
                            string attrName = ExtractAttrName(statement);
                            if (IsAttr(attrName))
                            {
                                parent.Append(CreateAttrAssignment(statement));
                            }
                            else if (IsProp(attrName))
                            {
                                parent.Append(CreatePropAssignment(statement));
                            }
                            else
                            {
                                throw new Exception(attrName + " is not a property or attribute (parsing: " + statement + ")");
                            }
                        }
                        else if (statement.ToString().StartsWith("println"))
                        {
                            parent.Append(new PrintLn(statement));
                        }
                        else if (statement.ToString().StartsWith("newline"))
                        {
                            parent.Append(new NewLn());
                        }
                        else if (statement.ToString().StartsWith("printvar"))
                        {
                            string        inner = ReadToParen(statement);
                            StringBuilder sb    = GetCondition(new StringBuilder(inner));
                            parent.Append(new PrintVar(sb));
                        }
                        else if (statement.ToString().StartsWith("print_name") ||
                                 statement.ToString().StartsWith("printname") ||
                                 statement.ToString().StartsWith("printobjname")
                                 )
                        {
                            string        inner = ReadToParen(statement);
                            StringBuilder sb    = GetCondition(new StringBuilder(inner));
                            parent.Append(new PrintObjectName(ToIIntResult(sb.ToString())));
                        }
                        else if (IsVarAssignment(statement))
                        {
                            parent.Append(CreateVarAssignment(statement));
                        }
                        else if (statement.ToString().StartsWith("look"))
                        {
                            parent.Append(new Look());
                        }
                        else if (statement.ToString().StartsWith("move"))
                        {
                            parent.Append(new Move());
                        }
                        else if (statement.ToString().StartsWith("ask"))
                        {
                            parent.Append(new Ask());
                        }
                        else if (statement.ToString().StartsWith("print"))
                        { //do after all other prints
                            parent.Append(new Print(statement.ToString()));
                        }
                        else if (statement.ToString().StartsWith("call "))
                        {
                            throw new Exception("Don't use 'call'.  Use C calling syntax instead.");
                        }
                        else if (StartsWithFunctionName(statement))
                        {
                            Call c = new Call("call " + GetFunctionName(statement));

                            parent.Append(c);
                        }
                        else if (statement.ToString().StartsWith("//"))
                        {
                            //do nothing, just throw the comment away
                        }
                        else
                        {
                            throw new Exception("Couldn't figure out what to do with: " + statement);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error parsing code near " + code.ToString(), ex);
            }
        }
Beispiel #2
0
 public void Visit(WhileLoop wh)
 {
     throw new Exception("Visit 6809 while loop not implemented.");
 }