Пример #1
0
        public override void Compile(CompilationModel model)
        {
            var methodModel = new MethodModel(Name, Type.Name, Block); // block will be compiled later
            model.Classes.Latest().Methods.Add(methodModel);
            methodModel.Locals.AddRange(Block.GetLocals());

            foreach (var arg in Arguments)
            {
                methodModel.Arguments.Add(new ArgumentModel(arg.Name, arg.Type.Name));
            }

            methodModel.Locals.Add(new LocalModel(LocalModel.TempVariable, BuiltinTypes.Any));
        }
Пример #2
0
        public void AddDefaultLibraries()
        {
            var stringClass = new ClassModel("String", this, true);
            stringClass.Methods.Add(new MethodModel("length", "Integer"));
            var appendStringMethod = new MethodModel("append", "Integer");
            appendStringMethod.Arguments.Add(new ArgumentModel("appendString", "String"));
            var appendIntegerMethod = new MethodModel("append", "Integer");
            appendIntegerMethod.Arguments.Add(new ArgumentModel("appendInteger", "Integer"));
            stringClass.Methods.Add(appendIntegerMethod);
            stringClass.Methods.Add(appendStringMethod);
            stringClass.Methods.Add(new MethodModel("split", "String"));
            stringClass.Methods.Add(new MethodModel("toInteger", "Integer"));

            Classes.Add(stringClass);
            Classes.Add(new ClassModel("Integer", this, true));
            Classes.Add(new ClassModel("Any", this, true));
            Classes.Add(new ClassModel("Bool", this, true));
            Classes.Add(new ClassModel("Float", this, true));
            Classes.Add(new ClassModel("Unit", this, true));

            var fileReaderClass = new ClassModel("FileReader", this, true);
            fileReaderClass.Fields.Add(new FieldModel("readerReference", "Integer"));
            var openMethod = new MethodModel("open", BuiltinTypes.Unit);
            openMethod.Arguments.Add(new ArgumentModel("s", "String"));
            fileReaderClass.Methods.Add(openMethod);
            fileReaderClass.Methods.Add(new MethodModel("readLine", "String"));
            fileReaderClass.Methods.Add(new MethodModel("close", "Unit"));

            Classes.Add(fileReaderClass);

            var fileWriterClass = new ClassModel("FileWriter", this, true);
            fileWriterClass.Fields.Add(new FieldModel("writerReference", "Integer"));
            var openWriterMethod = new MethodModel("open", BuiltinTypes.Unit);
            openWriterMethod.Arguments.Add(new ArgumentModel("s", "String"));
            fileWriterClass.Methods.Add(openWriterMethod);
            var writeLineMethod = new MethodModel("writeLine", BuiltinTypes.Unit);
            writeLineMethod.Arguments.Add(new ArgumentModel("s", "String"));
            fileWriterClass.Methods.Add(writeLineMethod);
            fileWriterClass.Methods.Add(new MethodModel("close", "Unit"));

            Classes.Add(fileWriterClass);

            var arrayClass = new ClassModel("Array", this, true);
            arrayClass.Methods.Add(new MethodModel("size", "Integer"));

            Classes.Add(arrayClass);
        }
Пример #3
0
        public MethodModel GetMethodWithArgs(string name, Sequence <Expression> args, CompilationModel model)
        {
            var         argsList  = args.ToList();
            MethodModel tmpMethod = null;
            MethodModel method    = null;

            foreach (var m in Methods.Where(m => m.Name == name && m.Arguments.Count == argsList.Count))
            {
                if (method == null)                 // to handle methods without parameters
                {
                    method = m;
                }

                for (int i = 0; i < m.Arguments.Count; i++)
                {
                    if (m.Arguments[i].Type == argsList[i].GetExpressionType(model) || m.Arguments[i].Type == BuiltinTypes.Any)
                    {
                        tmpMethod = m;
                    }
                    else
                    {
                        tmpMethod = null;
                    }
                }

                if (tmpMethod != null)
                {
                    method = tmpMethod;
                }
            }

            if (method != null)
            {
                return(method);
            }
            throw new Exception(string.Format("Method with with name {0} and {1} args not found!", name, argsList.Count));
        }
Пример #4
0
        public string ToXml()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("\t<class name=\"{0}\" parentName=\"{1}\">", Name, ParentName);
            builder.AppendLine();

            builder.AppendLine("\t\t<fields>");
            foreach (var field in Fields)
            {
                builder.AppendLine(field.ToXml());
            }
            builder.AppendLine("\t\t</fields>");

            builder.AppendLine("\t\t<methods>");
            if (Methods.Count(m => m.Name == "New" && m.Arguments.Count == 0) == 0)
            {
                var method = new MethodModel("New", Name, new Block(new Sequence<Statement>()));
                method.Compile(CompilationModel);
                builder.Append(method.ToXml());
            }
            foreach (var method in Methods)
            {
                builder.Append(method.ToXml());
            }
            builder.AppendLine("\t\t</methods>");

            builder.AppendLine("\t</class>");
            return builder.ToString();
        }