Example #1
0
            public override object VisitInteger_expr([NotNull] scheme_langParser.Integer_exprContext context)
            {
                int result_reg = PopLastParamReg();

                CommandSetVariable cmd = new CommandSetVariable(GetRegName(result_reg), context.integer().GetText());

                currentFunc.AddCommand(cmd, expression_index);

                return(base.VisitInteger_expr(context));
            }
Example #2
0
            public override object VisitLogical_const_expr([NotNull] scheme_langParser.Logical_const_exprContext context)
            {
                int param = PopLastParamReg();

                string logical_const = context.logical_const().GetText();

                CommandSetVariable cmd = new CommandSetVariable(GetRegName(param), logical_const);

                currentFunc.AddCommand(cmd, expression_index);

                return(base.VisitLogical_const_expr(context));
            }
Example #3
0
            public override object VisitObject_atom([NotNull] scheme_langParser.Object_atomContext context)
            {
                int result_reg = PopLastParamReg();

                string objName = context.object_name().GetText();

                VariableManager.CheckValueValidity(objName, this, context.object_name());

                CommandSetVariable cmd = new CommandSetVariable(GetRegName(result_reg), objName);

                currentFunc.AddCommand(cmd, expression_index);

                return(base.VisitObject_atom(context));
            }
Example #4
0
            public override object VisitVariable_expr([NotNull] scheme_langParser.Variable_exprContext context)
            {
                //variable_expr can also be 'of' expression, in that case we do nothing here
                if (context.variable_name() != null)
                {
                    int result_reg = PopLastParamReg();

                    string varName = context.variable_name().GetText();

                    VariableManager.CheckValueValidity(varName, this, context.variable_name());

                    CommandSetVariable cmd = new CommandSetVariable(GetRegName(result_reg), varName);
                    currentFunc.AddCommand(cmd, expression_index);
                }

                return(base.VisitVariable_expr(context));
            }
Example #5
0
            /* String const expression */

            public override object VisitString_const_expr([NotNull] scheme_langParser.String_const_exprContext context)
            {
                int result_reg = PopLastParamReg();

                string string_const_name = context.string_const().GetText();

                if (Config.GetStringConstByName(string_const_name) == null)
                {
                    Errors.Add(new ErrorDescriptor($"String const '{string_const_name}' does not exist.", context));
                }

                CommandSetVariable cmd = new CommandSetVariable(GetRegName(result_reg), string_const_name);

                currentFunc.AddCommand(cmd, expression_index);

                return(base.VisitString_const_expr(context));
            }
Example #6
0
            public override object VisitCmd_set_var([NotNull] scheme_langParser.Cmd_set_varContext context)
            {
                string varName = context.variable_name().GetText();

                if (!VariableManager.IsVariable(varName, Scheme))
                {
                    Errors.Add(new ErrorDescriptor($"Variable '{varName}' is not recognized.", context.variable_name()));
                }

                CommandSetVariable cmd = new CommandSetVariable(varName, GetRegName(0));

                currentFunc.AddCommand(cmd);

                SetNewExpression(1);

                return(base.VisitCmd_set_var(context));
            }
Example #7
0
            public override object VisitBody_variable_definition([NotNull] scheme_langParser.Body_variable_definitionContext context)
            {
                //Add predefined variable to scheme
                var variable_definition = context.variable_definition();

                string type = variable_definition?.variable_type()?.GetText();
                string name = variable_definition?.variable_name()?.GetText();

                if (type == null || name == null)
                {
                    return(base.VisitBody_variable_definition(context));
                }

                if (!VariableManager.IsTypeValid(type, Config))
                {
                    Errors.Add(new ErrorDescriptor($"Type '{type}' is not recognized.", variable_definition.variable_type()));
                }

                if (CompiledScheme.GetVariableByName(name) != null || CompiledScheme.GetParameterByName(name) != null)
                {
                    Errors.Add(new ErrorDescriptor($"Variable or parameter '{name}' already exists.", variable_definition.variable_name()));
                    if (variable_definition.EQUALS() != null)
                    {
                        SetNewExpression(1);
                    }
                    return(base.VisitBody_variable_definition(context));
                }

                ObjectVariable variable = new ObjectVariable(type, name, null);

                if (variable != null)
                {
                    CompiledScheme.AddVariable(variable);

                    CommandSetVariable cmd = new CommandSetVariable(variable_definition.variable_name().GetText(), GetRegName(0));
                    currentFunc.AddCommand(cmd);
                    SetNewExpression(1);
                }

                return(base.VisitBody_variable_definition(context));
            }
Example #8
0
        public static void SolveSimpleValueRegs(SchemeFunction function)
        {
            //We iterate commands
            //If we find a SET(<reg>, <simple value>) command, we
            //    //remove this SET command
            //    //Iterate over the subsequent commands in the base block
            //    //If we find any command with input <reg> we change it to <simple value>
            //    //If we find any command with output <reg> we stop and goto 1. (continuing from the SET command we found)

            var           commands         = function.Commands;
            HashSet <int> removableIndexes = new HashSet <int>();

            for (int i = 0; i < commands.Count; ++i)
            {
                ISchemeCommand cmd = commands[i];

                if (cmd is CommandSetVariable)
                {
                    CommandSetVariable cmdSet    = (CommandSetVariable)cmd;
                    string             targetVar = cmdSet.GetVariableName();
                    string             value     = cmdSet.GetValue();

                    if (SchemeExecutor.IsRegister(targetVar))
                    {
                        removableIndexes.Add(i);
                        for (int i2 = i + 1; i2 < commands.Count; ++i2)
                        {
                            ISchemeCommand cmd2 = commands[i2];
                            cmd2.ChangeInputs(targetVar, value);
                            if (cmd2 is CommandJumpBase || cmd2.HasOutput(targetVar))
                            {
                                break;
                            }
                        }
                    }
                }
            }

            RemoveCommandsAt(function, removableIndexes);
        }
Example #9
0
            public override object VisitCmd_create_var([NotNull] scheme_langParser.Cmd_create_varContext context)
            {
                var var_def = context.variable_definition();

                string var_type = var_def.variable_type()?.GetText();
                string var_name = var_def.variable_name()?.GetText();

                if (var_type == null || var_name == null)
                {
                    if (var_def.EQUALS() != null)
                    {
                        SetNewExpression(1);
                    }
                    return(base.VisitCmd_create_var(context));
                }

                if (!VariableManager.IsTypeValid(var_type, Config))
                {
                    Errors.Add(new ErrorDescriptor($"Type '{var_type}' is not recognized.", var_def.variable_type()));
                }

                VariableManager.CheckNewVariable(var_name, this, var_def.variable_name());

                CommandCreateVariable create_cmd = new CommandCreateVariable(var_type, var_name);

                currentFunc.AddCommand(create_cmd);

                if (var_def.EQUALS() != null)
                {
                    CommandSetVariable cmd = new CommandSetVariable(var_name, GetRegName(0));
                    currentFunc.AddCommand(cmd);
                    SetNewExpression(1);
                }

                return(base.VisitCmd_create_var(context));
            }
Example #10
0
        public static void SolveRegisterBuffers(SchemeFunction function)
        {
            // if an operation stores its result in a register <reg>, and it is followed by a SET(<var>, <reg>) command
            //      we delete the SET command and set the opperation's output to <var>
            //      we iterate over the following commands and change any <reg> to <var>
            //      we stop if <reg> is set again or we leave the block

            var           commands         = function.Commands;
            HashSet <int> removableIndexes = new HashSet <int>();

            for (int i = 0; i < commands.Count; ++i)
            {
                ISchemeCommand cmd = commands[i];

                var outputs = cmd.GetOutputs();

                if (outputs == null)
                {
                    continue;
                }

                // check if any output is a register
                foreach (var reg in outputs)
                {
                    if (SchemeExecutor.IsRegister(reg))
                    {
                        string variable = null;
                        for (int j = i + 1; j < commands.Count; ++j)
                        {
                            ISchemeCommand cmd2 = commands[j];

                            if (cmd2 is CommandJumpBase)
                            {
                                break;                          // break if we leave the block
                            }
                            if (cmd2.HasOutput(reg))
                            {
                                break;                       // break if the register is set again
                            }
                            if (cmd2 is CommandSetVariable && cmd2.HasInput(reg))
                            {
                                CommandSetVariable setCmd = (CommandSetVariable)cmd2;
                                variable = setCmd.GetVariableName();
                                if (SchemeExecutor.IsRegister(variable))
                                {
                                    variable = null;
                                }
                                else
                                {
                                    cmd.ChangeOutput(reg, variable);
                                    removableIndexes.Add(j);
                                }
                            }

                            if (variable != null && cmd2.HasInput(reg))
                            {
                                cmd2.ChangeInputs(reg, variable);
                            }
                        }
                    }
                }
            }

            RemoveCommandsAt(function, removableIndexes);
        }