コード例 #1
0
ファイル: EV3MainProgram.cs プロジェクト: gstamac/EV3Basic
 private void ProcessSubroutines(EV3CompilerContext context)
 {
     foreach (SubroutineStatementCompiler compiler in parser.GetStatements <SubroutineStatement>().Select(s => s.Compiler <SubroutineStatementCompiler>()))
     {
         EV3SubDefinition sub = new EV3SubDefinition(compiler.Ev3Name, compiler.Ev3Name, () =>
         {
             StringWriter code = new StringWriter();
             compiler.Compile(code, true);
             return(code.ToString());
         });
         if (threadCompilers.Any(t => t.ThreadName.Equals(compiler.Ev3Name, StringComparison.InvariantCultureIgnoreCase)))
         {
             foreach (SBExpression expression in compiler.ParentStatement.SubroutineBody.GetExpressions())
             {
                 if (expression is IdentifierExpression)
                 {
                     context.FindVariable(((IdentifierExpression)expression).VariableName()).IsConstant = false;
                 }
                 else if (expression is ArrayExpression)
                 {
                     context.FindVariable(((ArrayExpression)expression).VariableName()).IsConstant = false;
                 }
             }
         }
         subroutines.Add(sub);
     }
 }
コード例 #2
0
        public override string Compile(TextWriter writer, EV3CompilerContext context, string[] arguments, string result)
        {
            string inlineCode = Code;

            for (int i = 0; i < arguments.Length; i++)
            {
                inlineCode = inlineCode.Replace($":{i}", arguments[i]);
            }
            if (result != null)
            {
                inlineCode = inlineCode.Replace($":{arguments.Length}", result);
            }
            if (inlineCode.Contains(":#"))
            {
                inlineCode = inlineCode.Replace(":#", context.GetNextLabelNumber().ToString());
            }

            if (Code.Equals(inlineCode))
            {
                inlineCode = inlineCode.TrimEnd(' ', '\t', '\n', '\r') + " " + string.Join(" ", arguments);
                if (result != null)
                {
                    inlineCode += " " + result;
                }
            }

            writer.WriteLine(inlineCode);

            return(result);
        }
コード例 #3
0
        public override string Compile(TextWriter writer, EV3CompilerContext context, string[] arguments, string result)
        {
            writer.WriteLine($"SUB_{Name}:");
            writer.Write(Code);
            writer.WriteLine($"ENDSUB_{Name}:");

            return("");
        }
コード例 #4
0
ファイル: EV3MainProgram.cs プロジェクト: gstamac/EV3Basic
 public void CompileCodeForSubroutines(TextWriter writer, EV3CompilerContext context)
 {
     foreach (EV3SubDefinition sub in subroutines.Where(s => s.IsReferenced))
     {
         writer.WriteLine();
         sub.Compile(writer, context, new string[0], null);
     }
 }
コード例 #5
0
        public EV3Compiler()
        {
            SBErrors = new List <SBError>();
            parser   = new Parser(SBErrors);

            variables   = new EV3Variables(parser);
            library     = new EV3Library();
            mainProgram = new EV3MainProgram(parser);
            context     = new EV3CompilerContext(variables, mainProgram, library);
        }
コード例 #6
0
 public static void AttachCompilers(this Parser parser, EV3CompilerContext context)
 {
     foreach (SBExpression expression in parser.GetExpressions())
     {
         expression.AttachCompiler(context);
     }
     foreach (Statement statement in parser.GetStatements())
     {
         statement.AttachCompiler(context);
     }
 }
コード例 #7
0
        private static void AttachCompiler(this SBExpression expression, EV3CompilerContext context)
        {
            if (expression == null)
            {
                return;
            }

            if (expression is ArrayExpression)
            {
                expression.AttachCompiler(new ArrayExpressionCompiler((ArrayExpression)expression, context));
            }
            else if (expression is BinaryExpression)
            {
                BinaryExpression binaryExpression = (BinaryExpression)expression;
                if (binaryExpression.IsBooleanOperator())
                {
                    expression.AttachCompiler(new BooleanExpressionCompiler(binaryExpression, context));
                }
                else if (binaryExpression.IsBooleanCompareOperator())
                {
                    expression.AttachCompiler(new ComparisonExpressionCompiler(binaryExpression, context));
                }
                else
                {
                    expression.AttachCompiler(new BinaryExpressionCompiler(binaryExpression, context));
                }
            }
            else if (expression is IdentifierExpression)
            {
                expression.AttachCompiler(new IdentifierExpressionCompiler((IdentifierExpression)expression, context));
            }
            else if (expression is LiteralExpression)
            {
                expression.AttachCompiler(new LiteralExpressionCompiler((LiteralExpression)expression, context));
            }
            else if (expression is MethodCallExpression)
            {
                expression.AttachCompiler(new MethodCallExpressionCompiler((MethodCallExpression)expression, context));
            }
            else if (expression is NegativeExpression)
            {
                expression.AttachCompiler(new NegativeExpressionCompiler((NegativeExpression)expression, context));
            }
            else if (expression is PropertyExpression)
            {
                expression.AttachCompiler(new PropertyExpressionCompiler((PropertyExpression)expression, context));
            }
            else
            {
                throw new Exception("Unknown expression " + expression.GetType().Name);
            }
        }
コード例 #8
0
        public override string Compile(TextWriter writer, EV3CompilerContext context, string[] arguments, string result)
        {
            string argumentsString = string.Join("", arguments.Select(a => " " + a));

            if (result == null)
            {
                writer.WriteLine($"    CALL {Name}{argumentsString}");
            }
            else
            {
                writer.WriteLine($"    CALL {Name}{argumentsString} {result}");
            }

            return(result);
        }
コード例 #9
0
ファイル: EV3MainProgram.cs プロジェクト: gstamac/EV3Basic
 public void Process(EV3CompilerContext context)
 {
     ProcessThreads();
     ProcessSubroutines(context);
 }
コード例 #10
0
 private void ProcessCode(EV3CompilerContext context)
 {
     mainProgram.Process(context);
 }
コード例 #11
0
 public abstract string Compile(TextWriter writer, EV3CompilerContext context, string[] arguments, string result);
コード例 #12
0
 public DummyStatementCompiler(Statement statement, EV3CompilerContext context) : base(statement, context)
 {
 }
コード例 #13
0
        private static void AttachCompiler(this Statement statement, EV3CompilerContext context)
        {
            if (statement is AssignmentStatement)
            {
                AssignmentStatement assignmentStatement = (AssignmentStatement)statement;

                PropertyExpressionCompiler propertyExpressionCompiler = assignmentStatement.LeftValue.Compiler <PropertyExpressionCompiler>();
                if (propertyExpressionCompiler != null && propertyExpressionCompiler.Value.Equals("Thread.Run", StringComparison.InvariantCultureIgnoreCase))
                {
                    statement.AttachCompiler(new ThreadStatementCompiler(assignmentStatement, context));
                }
                else
                {
                    statement.AttachCompiler(new AssignmentStatementCompiler(assignmentStatement, context));
                }
            }
            else if (statement is ForStatement)
            {
                statement.AttachCompiler(new ForStatementCompiler((ForStatement)statement, context));
            }
            else if (statement is WhileStatement)
            {
                statement.AttachCompiler(new WhileStatementCompiler((WhileStatement)statement, context));
            }
            else if (statement is GotoStatement)
            {
                statement.AttachCompiler(new DummyStatementCompiler((GotoStatement)statement, context));
            }
            else if (statement is IfStatement)
            {
                statement.AttachCompiler(new IfStatementCompiler((IfStatement)statement, context));
            }
            else if (statement is ElseIfStatement)
            {
                statement.AttachCompiler(new ElseIfStatementCompiler((ElseIfStatement)statement, context));
            }
            else if (statement is MethodCallStatement)
            {
                statement.AttachCompiler(new MethodCallStatementCompiler((MethodCallStatement)statement, context));
            }
            else if (statement is SubroutineCallStatement)
            {
                statement.AttachCompiler(new SubroutineCallStatementCompiler((SubroutineCallStatement)statement, context));
            }
            else if (statement is SubroutineStatement)
            {
                statement.AttachCompiler(new SubroutineStatementCompiler((SubroutineStatement)statement, context));
            }
            else if (statement is EmptyStatement)
            {
                statement.AttachCompiler(new DummyStatementCompiler((EmptyStatement)statement, context));
            }
            else if (statement is IllegalStatement)
            {
                statement.AttachCompiler(new DummyStatementCompiler((IllegalStatement)statement, context));
            }
            else
            {
                throw new Exception("Unknown statement " + statement.GetType().Name);
            }
        }