public override void visit(slice_expr sl)
        {
            expression_list el = null;

            if (sl.slices == null)
            {
                el = construct_expression_list_for_slice_expr(sl);
            }
            else
            {
                el = construct_expression_list_for_slice_expr_multi(sl);  // то это многомерный массив
            }
            // надо как-то запретить многомерные слайсы в левой части присваивания
            if (sl.Parent is assign parent_assign && parent_assign.to == sl)
            {
                // если это многомерный слайс - кинуть ошибку
                if (sl.slices != null)
                {
                    // запретим пока или вовсе
                    throw new SyntaxVisitorError("MULTIDIMENSIONAL_SLICES_FORBIDDEN_IN_LEFT_SIDE_OF_ASSIGNMENT", sl.source_context);
                }
                el.Insert(0, parent_assign.from);
                var mc = method_call.NewP(
                    dot_node.NewP(
                        sl.v,
                        new ident("SystemSliceAssignment", sl.v.source_context),
                        sl.v.source_context),
                    el, sl.source_context);
                var systemSliceAssignmentCall   = new procedure_call(mc, sl.source_context);
                var typeCompatibilityCheck      = GetAssignmentTypeCompatibilityCheck(sl, parent_assign.from, mc);
                var checkAndDesugaredSliceBlock = new statement_list(typeCompatibilityCheck, systemSliceAssignmentCall);
                checkAndDesugaredSliceBlock.source_context = sl.source_context;
                ReplaceUsingParent(parent_assign, checkAndDesugaredSliceBlock);
                visit(systemSliceAssignmentCall); // обойти заменённое на предмет наличия такого же синтаксического сахара
            }
Esempio n. 2
0
        public override void visit(for_node fn)
        {
            ProcessNode(fn.statements);
            var b = HasStatementVisitor <yield_node> .Has(fn);

            if (!b)
            {
                return;
            }

            var gt1 = goto_statement.New;
            var gt2 = goto_statement.New;

            var endtemp = new ident(newVarName());
            var ass1    = new var_statement(fn.loop_variable, fn.type_name, fn.initial_value);
            var ass2    = new var_statement(endtemp, fn.type_name, fn.finish_value);


            var if0 = new if_node(bin_expr.Greater(fn.loop_variable, fn.finish_value), gt1);
            var lb2 = new labeled_statement(gt2.label, if0);
            var lb1 = new labeled_statement(gt1.label);
            var Inc = new procedure_call(new method_call(new ident("Inc"), new expression_list(fn.loop_variable)));

            ReplaceStatement(fn, SeqStatements(ass1, ass2, lb2, fn.statements, Inc, gt2, lb1));

            // в declarations ближайшего блока добавить описание labels
            block bl = listNodes.FindLast(x => x is block) as block;

            bl.defs.Add(new label_definitions(gt1.label, gt2.label));
        }
 public override void visit(procedure_call pc)
 {
     if (pc.func_name is method_call mc && mc.dereferencing_value is ident id && (id.name.ToLower() == "read" || id.name.ToLower() == "readln"))
     {
         syntax_tree_node n = pc;
         do
         {
             n = n.Parent;
         } while (n != null && !(n is class_members));
         if (n != null)
         {
             //ReadProc = false
             ;
         }
         else
         {
             ReadProc += 1;
         }
     }
     if (pc.func_name is method_call mc1 && mc1.dereferencing_value is ident id1 && (id1.name.ToLower() == "write" || id1.name.ToLower() == "writeln") &&
         mc1.parameters != null && mc1.parameters.expressions.Any(ex => ex is char_const cc && cc.cconst == ' '))
     {
         WriteProcWithSpace += 1;
     }
     base.visit(pc);
 }
        private DeconstructionDesugaringResult DesugarPattern(deconstructor_pattern pattern, expression matchingExpression)
        {
            Debug.Assert(!pattern.IsRecursive, "All recursive patterns should be desugared into simple patterns at this point");

            var desugarResult    = new DeconstructionDesugaringResult();
            var castVariableName = NewGeneralName();

            desugarResult.CastVariableDefinition = new var_statement(castVariableName, pattern.type);

            var successVariableName = NewSuccessName();

            desugarResult.SuccessVariableDefinition = new var_statement(successVariableName, new ident("false"));

            // делегирование проверки паттерна функции IsTest
            desugarResult.TypeCastCheck = SubtreeCreator.CreateSystemFunctionCall(IsTestMethodName, matchingExpression, castVariableName);

            var parameters = pattern.parameters.Cast <var_deconstructor_parameter>();

            foreach (var deconstructedVariable in parameters)
            {
                desugarResult.DeconstructionVariables.Add(
                    new var_def_statement(deconstructedVariable.identifier, deconstructedVariable.type));
            }

            var deconstructCall = new procedure_call();

            deconstructCall.func_name     = SubtreeCreator.CreateMethodCall(DeconstructMethodName, castVariableName.name, parameters.Select(x => x.identifier).ToArray());
            desugarResult.DeconstructCall = deconstructCall;

            return(desugarResult);
        }
        public override void visit(procedure_call pc)
        {
            var pcIdent = pc.func_name as ident;

            if (pcIdent != null)
            {
                var pcName = pcIdent.name.ToLower();
                if (pcName == "break")
                {
                    ReplaceUsingParent(pc, this.GotoBreak);
                }
                else if (pcName == "continue")
                {
                    ReplaceUsingParent(pc, this.GotoContinue);
                }
            }
        }
Esempio n. 6
0
 //public override void visit(method_call _method_call)
 public override void visit(procedure_call _proc_call)
 {
     if (isForNode)
     {
         if ((_proc_call.func_name != null) &&
             (_proc_call.func_name is ident))
         {
             if ((_proc_call.func_name as ident).name.ToLower() == "continue")
             {
                 (_proc_call.func_name as ident).name = "exit";
             }
             if ((_proc_call.func_name as ident).name.ToLower() == "break")
             {
                 throw new OpenMPException("Нельзя использовать break в распараллеливаемом цикле", _proc_call.source_context);
             }
         }
     }
     base.visit(_proc_call);
 }
Esempio n. 7
0
        public override void visit(slice_expr sl)
        {
            var el = construct_expression_list_for_slice_expr(sl);

            if (sl.Parent is assign parent_assign && parent_assign.to == sl)
            {
                el.Insert(0, parent_assign.from);
                var mc = method_call.NewP(
                    dot_node.NewP(
                        sl.v,
                        new ident("SystemSliceAssignment", sl.v.source_context),
                        sl.v.source_context),
                    el, sl.source_context);
                var systemSliceAssignmentCall   = new procedure_call(mc, sl.source_context);
                var typeCompatibilityCheck      = GetAssignmentTypeCompatibilityCheck(sl, parent_assign.from, mc);
                var checkAndDesugaredSliceBlock = new statement_list(typeCompatibilityCheck, systemSliceAssignmentCall);
                checkAndDesugaredSliceBlock.source_context = sl.source_context;
                ReplaceUsingParent(parent_assign, checkAndDesugaredSliceBlock);
                visit(systemSliceAssignmentCall); // обойти заменённое на предмет наличия такого же синтаксического сахара
            }
Esempio n. 8
0
 public override void visit(procedure_call _procedure_call)
 {
     prepare_node(_procedure_call.func_name, "func_name");
 }
Esempio n. 9
0
 public virtual void visit(procedure_call _procedure_call)
 {
     DefaultVisit(_procedure_call);
 }
Esempio n. 10
0
		public override void visit(procedure_call _procedure_call)
		{
			DefaultVisit(_procedure_call);
			pre_do_visit(_procedure_call);
			visit(procedure_call.func_name);
			post_do_visit(_procedure_call);
		}
Esempio n. 11
0
		public virtual void post_do_visit(procedure_call _procedure_call)
		{
		}
Esempio n. 12
0
 public virtual void visit(procedure_call _procedure_call)
 {
 }
		public virtual void visit(procedure_call _procedure_call)
		{
			DefaultVisit(_procedure_call);
		}
        bool VisitTypeclassDeclaration(type_declaration typeclassDeclaration)
        {
            var typeclassDefinition = typeclassDeclaration.type_def as typeclass_definition;

            if (typeclassDefinition == null)
            {
                return(false);
            }

            var typeclassName = typeclassDeclaration.type_name as typeclass_restriction;

            // TODO: typeclassDefinition.additional_restrictions - translate to usual classes

            // Creating interface

            // Get members for typeclass interface
            var interfaceMembers = new List <class_members>();

            foreach (var cm in typeclassDefinition.body.class_def_blocks)
            {
                var cmNew = (class_members)cm.Clone();

                for (int i = 0; i < cmNew.members.Count; i++)
                {
                    var member = cmNew.members[i];
                    if (member is function_header || member is procedure_header)
                    {
                        cmNew.members[i] = member;
                    }
                    else if (member is procedure_definition procDef)
                    {
                        cmNew.members[i] = procDef.proc_header;
                    }
                    AddAttribute(cmNew.members[i], "__TypeclassMemberAttribute");
                    if (cmNew.members[i] is procedure_header ph)
                    {
                        ConvertOperatorNameIdent(ph);
                    }
                }

                interfaceMembers.Add(cmNew);
            }
            var interfaceInheritance = (named_type_reference_list)typeclassDefinition.additional_restrictions?.Clone();

            if (interfaceInheritance != null)
            {
                interfaceInheritance.source_context = null;
                for (int i = 0; i < interfaceInheritance.types.Count; i++)
                {
                    if (interfaceInheritance.types[i] is typeclass_reference tr)
                    {
                        interfaceInheritance.types[i] = TypeclassReferenceToInterfaceName(tr.names[0].name, tr.restriction_args);
                    }
                    else
                    {
                        throw new NotImplementedException("Syntactic Error");
                    }
                }
            }
            var typeclassInterfaceDef =
                SyntaxTreeBuilder.BuildClassDefinition(
                    interfaceInheritance,
                    null, interfaceMembers.ToArray());

            typeclassInterfaceDef.keyword = class_keyword.Interface;
            var typeclassInterfaceName = new template_type_name("I" + typeclassName.name, RestrictionsToIdentList(typeclassName.restriction_args));
            var typeclassInterfaceDecl = new type_declaration(typeclassInterfaceName, typeclassInterfaceDef);

            typeclassInterfaceDecl.attributes = typeclassDeclaration.attributes;
            AddAttribute(
                typeclassInterfaceDecl, "__TypeclassAttribute",
                new expression_list(new string_const(TypeclassRestrictionToString(typeclassName))));


            // Creating class

            var typeclassDefTranslated =
                SyntaxTreeBuilder.BuildClassDefinition(
                    new named_type_reference_list(new template_type_reference(typeclassInterfaceName.name, typeclassName.restriction_args)),
                    null, typeclassDefinition.body.class_def_blocks.ToArray());

            typeclassDefTranslated.attribute = class_attribute.Abstract;
            for (int i = 0; i < typeclassDefTranslated.body.class_def_blocks.Count; i++)
            {
                var cm = typeclassDefTranslated.body.class_def_blocks[i].members;

                for (int j = 0; j < cm.Count; j++)
                {
                    procedure_header header = null;
                    if (cm[j] is procedure_header ph)
                    {
                        header = ph;
                        header.proc_attributes.Add(new procedure_attribute("abstract", proc_attribute.attr_abstract));
                    }
                    else if (cm[j] is procedure_definition pd)
                    {
                        header = pd.proc_header;
                        header.proc_attributes.Add(new procedure_attribute("virtual", proc_attribute.attr_virtual));
                    }

                    ConvertOperatorNameIdent(header);
                }
            }

            /*
             * {
             *  // Add constructor
             *  var cm = typeclassDefTranslated.body.class_def_blocks[0];
             *  var def = new procedure_definition(
             *      new constructor(),
             *      new statement_list(new empty_statement()));
             *  def.proc_body.Parent = def;
             *  def.proc_header.proc_attributes = new procedure_attributes_list();
             *
             *  cm.Add(def);
             * }
             */
            // Add template parameters for typeclass class(derived typeclasses)
            ident_list templates = RestrictionsToIdentList(typeclassName.restriction_args);

            if (typeclassDefinition.additional_restrictions != null)
            {
                for (int i = 0; i < typeclassDefinition.additional_restrictions.types.Count; i++)
                {
                    string name;
                    string templateName;
                    if (typeclassDefinition.additional_restrictions.types[i] is typeclass_reference tr)
                    {
                        name         = tr.names[0].name;
                        templateName = TypeclassRestrctionToTemplateName(name, tr.restriction_args).name;
                    }
                    else
                    {
                        throw new NotImplementedException("SyntaxError");
                    }

                    // Add template parameter
                    templates.Add(templateName);

                    // Add where restriction
                    if (typeclassDefTranslated.where_section == null)
                    {
                        typeclassDefTranslated.where_section = new where_definition_list();
                    }
                    typeclassDefTranslated.where_section.Add(GetWhereRestriction(
                                                                 interfaceInheritance.types[i],
                                                                 templateName));

                    // Add methods from derived typeclasses
                    var body = (instancesAndRestrictedFunctions.typeclasses[name].type_def as typeclass_definition).body;
                    foreach (var cdb in body.class_def_blocks)
                    {
                        var cdbNew = new class_members(cdb.access_mod == null ? access_modifer.none : cdb.access_mod.access_level);
                        foreach (var member in cdb.members)
                        {
                            procedure_header memberHeaderNew;

                            if (member is procedure_header || member is function_header)
                            {
                                memberHeaderNew = (procedure_header)member.Clone();
                                memberHeaderNew.source_context = null;
                            }
                            else if (member is procedure_definition procDefinition)
                            {
                                memberHeaderNew                = (procedure_header)procDefinition.proc_header.Clone();
                                memberHeaderNew.Parent         = null;
                                memberHeaderNew.source_context = null;
                            }
                            else
                            {
                                continue;
                            }

                            var variableName = templateName + "Instance";
                            var parameters   = memberHeaderNew.parameters.params_list.Aggregate(new expression_list(), (x, y) => new expression_list(x.expressions.Concat(y.idents.idents).ToList()));

                            expression methodCall = null;
                            if (memberHeaderNew.name.meth_name is operator_name_ident oni)
                            {
                                ConvertOperatorNameIdent(memberHeaderNew);
                                Debug.Assert(parameters.expressions.Count == 2, "Parameters count for operation should be equal to 2");
                                //methodCall = new bin_expr(parameters.expressions[0], parameters.expressions[1], oni.operator_type);
                            }
                            var callName = new dot_node(variableName, memberHeaderNew.name.meth_name.name);
                            methodCall = new method_call(callName, parameters);
                            statement exec = null;
                            if (memberHeaderNew is function_header)
                            {
                                exec = new assign("Result", methodCall);
                            }
                            else if (memberHeaderNew is procedure_header)
                            {
                                exec = new procedure_call(methodCall as method_call);
                            }
                            var procDef = new procedure_definition(
                                memberHeaderNew,
                                new statement_list(
                                    GetInstanceSingletonVarStatement(templateName),
                                    exec));
                            cdbNew.Add(procDef);
                        }
                        typeclassDefTranslated.body.class_def_blocks.Add(cdbNew);
                    }
                }
            }

            var typeclassNameTanslated = new template_type_name(typeclassName.name, templates, typeclassName.source_context);

            var typeclassDeclTranslated = new type_declaration(typeclassNameTanslated, typeclassDefTranslated, typeclassDeclaration.source_context);

            typeclassDeclTranslated.attributes = typeclassDeclaration.attributes;
            AddAttribute(
                typeclassDeclTranslated, "__TypeclassAttribute",
                new expression_list(new string_const(TypeclassRestrictionToString(typeclassName))));

            Replace(typeclassDeclaration, typeclassDeclTranslated);
            UpperNodeAs <type_declarations>().InsertBefore(typeclassDeclTranslated, typeclassInterfaceDecl);
            visit(typeclassInterfaceDecl);
            visit(typeclassDeclTranslated);

            return(true);
        }
Esempio n. 15
0
        public override void visit(for_node fn)
        {
            var b = HasStatementVisitor <yield_node> .Has(fn);

            if (!b)
            {
                return;
            }

            var gotoContinue = goto_statement.New;
            var gotoBreak    = goto_statement.New;
            var gotoStart    = goto_statement.New;

            ReplaceBreakContinueWithGotoLabelVisitor replaceBreakContinueVis = new ReplaceBreakContinueWithGotoLabelVisitor(gotoContinue, gotoBreak);

            fn.statements.visit(replaceBreakContinueVis);

            ProcessNode(fn.statements);

            var newNames = this.NewVarNames(fn.loop_variable);

            var newLoopVar = fn.create_loop_variable ? new ident(newNames.VarName) : fn.loop_variable;

            // Нужно заменить fn.loop_variable -> newLoopVar в теле цикла
            var replacerVis = new ReplaceVariableNameVisitor(fn.loop_variable, newLoopVar);

            fn.visit(replacerVis);

            fn.loop_variable = newLoopVar;

            var endtemp = new ident(newNames.VarEndName); //new ident(newVarName());

            //var ass1 = new var_statement(fn.loop_variable, fn.type_name, fn.initial_value);
            //var ass1 = new var_statement(fn.loop_variable, fn.type_name, fn.initial_value);
            //var ass2 = new var_statement(endtemp, fn.type_name, fn.finish_value);

            // Исправления в связи с #1254 (новый алгоритм)
            // цикл for i:=a to b do
            //          i := a
            //          if i > b then goto break
            //Start:    stmts
            //          if i >= b then goto break
            //Continue: Inc(i)
            //          goto Start
            //Break:

            // цикл for i:=a downto b do
            //          i := a
            //          if i < b then goto break
            //Start:    stmts
            //          if i <= b then goto break
            //Continue: Dec(i)
            //          goto Start
            //Break:

            // frninja 05/06/16 - фиксим для !fn.create_variable
            var ass1 = fn.create_loop_variable
                ? new var_statement(fn.loop_variable, fn.type_name, fn.initial_value) as statement
                : new assign(fn.loop_variable, fn.initial_value) as statement;

            var if0 = new if_node((fn.cycle_type == for_cycle_type.to) ?
                                  bin_expr.Greater(fn.loop_variable, fn.finish_value) :
                                  bin_expr.Less(fn.loop_variable, fn.finish_value), gotoBreak);

            var if1 = new if_node((fn.cycle_type == for_cycle_type.to) ?
                                  bin_expr.GreaterEqual(fn.loop_variable, fn.finish_value) :
                                  bin_expr.LessEqual(fn.loop_variable, fn.finish_value), gotoBreak);

            var lb1 = new labeled_statement(gotoStart.label);
            var lb2 = new labeled_statement(gotoBreak.label); // пустой оператор
            var Inc = new procedure_call(new method_call((fn.cycle_type == for_cycle_type.to) ?
                                                         new ident("Inc") :
                                                         new ident("Dec"), new expression_list(fn.loop_variable)));

            var lbInc = new labeled_statement(gotoContinue.label, Inc);

            ReplaceStatement(fn, SeqStatements(ass1, if0, lb1, fn.statements, if1, lbInc, gotoStart, lb2));

            /*var if0 = new if_node((fn.cycle_type == for_cycle_type.to) ?
             *  bin_expr.Greater(fn.loop_variable, fn.finish_value) :
             *  bin_expr.Less(fn.loop_variable, fn.finish_value), gotoBreak);
             *
             * var lb2 = new labeled_statement(gotoStart.label, if0);
             * var lb1 = new labeled_statement(gotoBreak.label); // пустой оператор
             * var Inc = new procedure_call(new method_call((fn.cycle_type == for_cycle_type.to) ?
             *  new ident("Inc") :
             *  new ident("Dec"), new expression_list(fn.loop_variable)));
             *
             * var lbInc = new labeled_statement(gotoContinue.label, Inc);
             *
             * ReplaceStatement(fn, SeqStatements(ass1, lb2, fn.statements, lbInc, gotoStart, lb1));*/

            // в declarations ближайшего блока добавить описание labels
            block bl = listNodes.FindLast(x => x is block) as block;

            bl.defs.Add(new label_definitions(gotoContinue.label, gotoBreak.label, gotoStart.label));
        }
Esempio n. 16
0
 public override void visit(procedure_call _procedure_call)
 {
 }
Esempio n. 17
0
        public override void visit(for_node fn)
        {
            var b = HasStatementVisitor <yield_node> .Has(fn);

            if (!b)
            {
                return;
            }

            var gotoContinue = goto_statement.New;
            var gotoBreak    = goto_statement.New;
            var gotoStart    = goto_statement.New;

            ReplaceBreakContinueWithGotoLabelVisitor replaceBreakContinueVis = new ReplaceBreakContinueWithGotoLabelVisitor(gotoContinue, gotoBreak);

            fn.statements.visit(replaceBreakContinueVis);

            ProcessNode(fn.statements);

            var newNames = this.NewVarNames(fn.loop_variable);

            var newLoopVar = fn.create_loop_variable ? new ident(newNames.VarName) : fn.loop_variable;

            // Нужно заменить fn.loop_variable -> newLoopVar в теле цикла
            var replacerVis = new ReplaceVariableNameVisitor(fn.loop_variable, newLoopVar);

            fn.visit(replacerVis);

            fn.loop_variable = newLoopVar;

            var endtemp = new ident(newNames.VarEndName); //new ident(newVarName());

            //var ass1 = new var_statement(fn.loop_variable, fn.type_name, fn.initial_value);
            //var ass1 = new var_statement(fn.loop_variable, fn.type_name, fn.initial_value);
            //var ass2 = new var_statement(endtemp, fn.type_name, fn.finish_value);

            // frninja 05/06/16 - фиксим для !fn.create_variable
            var ass1 = fn.create_loop_variable
                ? new var_statement(fn.loop_variable, fn.type_name, fn.initial_value) as statement
                : new assign(fn.loop_variable, fn.initial_value) as statement;


            var if0 = new if_node((fn.cycle_type == for_cycle_type.to) ?
                                  bin_expr.Greater(fn.loop_variable, fn.finish_value) :
                                  bin_expr.Less(fn.loop_variable, fn.finish_value), gotoBreak);

            var lb2 = new labeled_statement(gotoStart.label, if0);
            var lb1 = new labeled_statement(gotoBreak.label);
            var Inc = new procedure_call(new method_call((fn.cycle_type == for_cycle_type.to) ?
                                                         new ident("Inc") :
                                                         new ident("Dec"), new expression_list(fn.loop_variable)));

            var lbInc = new labeled_statement(gotoContinue.label, Inc);

            ReplaceStatement(fn, SeqStatements(ass1, lb2, fn.statements, lbInc, gotoStart, lb1));

            // в declarations ближайшего блока добавить описание labels
            block bl = listNodes.FindLast(x => x is block) as block;

            bl.defs.Add(new label_definitions(gotoContinue.label, gotoBreak.label, gotoStart.label));
        }
Esempio n. 18
0
		public virtual void visit(procedure_call _procedure_call)
		{
		}
Esempio n. 19
0
 public override void visit(procedure_call _procedure_call)
 {
     _procedure_call.func_name.visit(this);
 }
Esempio n. 20
0
		public override void visit(procedure_call _procedure_call)
		{
			executer.visit(_procedure_call);
			if (_procedure_call.func_name != null)
				this.visit((dynamic)_procedure_call.func_name);
			if (_procedure_call.attributes != null)
				this.visit((dynamic)_procedure_call.attributes);
		}