Esempio n. 1
0
 public ModuleCompiler(ErrorLog errorLog, SymbolTable symbolTable, IodineModule module)
 {
     this.errorLog = errorLog;
     this.symbolTable = symbolTable;
     this.module = module;
     functionCompiler = new FunctionCompiler (errorLog, symbolTable, module.Initializer);
 }
Esempio n. 2
0
        private IodineMethod CompileMethod(FunctionDeclaration funcDecl)
        {
            symbolTable.NextScope();
            IodineMethod methodBuilder = new IodineMethod(module, funcDecl.Name, funcDecl.InstanceMethod,
                                                          funcDecl.Parameters.Count,
                                                          symbolTable.CurrentScope.SymbolCount);
            FunctionCompiler compiler = new FunctionCompiler(symbolTable,
                                                             methodBuilder);

            methodBuilder.Variadic           = funcDecl.Variadic;
            methodBuilder.AcceptsKeywordArgs = funcDecl.AcceptsKeywordArgs;
            for (int i = 0; i < funcDecl.Parameters.Count; i++)
            {
                methodBuilder.Parameters [funcDecl.Parameters [i]] = symbolTable.GetSymbol
                                                                         (funcDecl.Parameters [i]).Index;
            }
            funcDecl.Children [0].Visit(compiler);
            AstNode lastNode = funcDecl.Children [0].LastOrDefault();

            if (lastNode != null)
            {
                methodBuilder.EmitInstruction(lastNode.Location, Opcode.LoadNull);
            }
            else
            {
                methodBuilder.EmitInstruction(funcDecl.Location, Opcode.LoadNull);
            }
            methodBuilder.FinalizeLabels();
            symbolTable.LeaveScope();
            return(methodBuilder);
        }
Esempio n. 3
0
        public override void Accept(LambdaExpression lambda)
        {
            symbolTable.NextScope();

            int          locals     = methodBuilder.LocalCount > 0 ? methodBuilder.LocalCount : symbolTable.CurrentScope.SymbolCount;
            IodineMethod anonMethod = new IodineMethod(methodBuilder, methodBuilder.Module, null, lambda.InstanceMethod,
                                                       lambda.Parameters.Count, locals);
            FunctionCompiler compiler = new FunctionCompiler(symbolTable, anonMethod);

            for (int i = 0; i < lambda.Parameters.Count; i++)
            {
                anonMethod.Parameters [lambda.Parameters [i]] = symbolTable.GetSymbol
                                                                    (lambda.Parameters [i]).Index;
            }
            lambda.Children [0].Visit(compiler);
            anonMethod.EmitInstruction(lambda.Location, Opcode.LoadNull);
            anonMethod.Variadic = lambda.Variadic;
            anonMethod.FinalizeLabels();
            methodBuilder.EmitInstruction(lambda.Location, Opcode.LoadConst,
                                          methodBuilder.Module.DefineConstant(anonMethod));
            if (methodBuilder.LocalCount > 0)
            {
                methodBuilder.EmitInstruction(lambda.Location, Opcode.BuildClosure);
            }
            symbolTable.LeaveScope();
        }
Esempio n. 4
0
        public override void Accept(CodeBlock scope)
        {
            symbolTable.NextScope();

            FunctionCompiler scopeCompiler = new FunctionCompiler(symbolTable, methodBuilder,
                                                                  breakLabels, continueLabels);

            foreach (AstNode node in scope)
            {
                node.Visit(scopeCompiler);
            }
            symbolTable.LeaveScope();
        }
Esempio n. 5
0
        public override void Accept(FunctionDeclaration funcDecl)
        {
            symbolTable.NextScope();
            IodineMethod anonMethod = new IodineMethod(methodBuilder, methodBuilder.Module, null, funcDecl.InstanceMethod,
                                                       funcDecl.Parameters.Count, methodBuilder.LocalCount);
            FunctionCompiler compiler = new FunctionCompiler(symbolTable, anonMethod);

            for (int i = 0; i < funcDecl.Parameters.Count; i++)
            {
                anonMethod.Parameters [funcDecl.Parameters [i]] = symbolTable.GetSymbol
                                                                      (funcDecl.Parameters [i]).Index;
            }
            funcDecl.Children [0].Visit(compiler);
            anonMethod.EmitInstruction(funcDecl.Location, Opcode.LoadNull);
            anonMethod.Variadic           = funcDecl.Variadic;
            anonMethod.AcceptsKeywordArgs = funcDecl.AcceptsKeywordArgs;
            anonMethod.FinalizeLabels();
            methodBuilder.EmitInstruction(funcDecl.Location, Opcode.LoadConst,
                                          methodBuilder.Module.DefineConstant(anonMethod));
            methodBuilder.EmitInstruction(funcDecl.Location, Opcode.BuildClosure);
            methodBuilder.EmitInstruction(funcDecl.Location, Opcode.StoreLocal, symbolTable.GetSymbol(funcDecl.Name).Index);
            symbolTable.LeaveScope();
        }
Esempio n. 6
0
		public override void Accept (LambdaExpression lambda)
		{
			symbolTable.NextScope ();

			int locals = methodBuilder.LocalCount > 0 ? methodBuilder.LocalCount : symbolTable.CurrentScope.SymbolCount;
			IodineMethod anonMethod = new IodineMethod (methodBuilder, methodBuilder.Module, null, lambda.InstanceMethod, 
				                          lambda.Parameters.Count, locals);
			FunctionCompiler compiler = new FunctionCompiler (symbolTable, anonMethod);
			for (int i = 0; i < lambda.Parameters.Count; i++) {
				anonMethod.Parameters [lambda.Parameters [i]] = symbolTable.GetSymbol
					(lambda.Parameters [i]).Index;
			}
			lambda.Children [0].Visit (compiler);
			anonMethod.EmitInstruction (lambda.Location, Opcode.LoadNull);
			anonMethod.Variadic = lambda.Variadic;
			anonMethod.FinalizeLabels ();
			methodBuilder.EmitInstruction (lambda.Location, Opcode.LoadConst,
				methodBuilder.Module.DefineConstant (anonMethod));
			if (methodBuilder.LocalCount > 0) {
				methodBuilder.EmitInstruction (lambda.Location, Opcode.BuildClosure);
			}
			symbolTable.LeaveScope ();
		}
Esempio n. 7
0
		public override void Accept (CodeBlock scope)
		{
			symbolTable.NextScope ();

			FunctionCompiler scopeCompiler = new FunctionCompiler (symbolTable, methodBuilder,
				                                 breakLabels, continueLabels);
			foreach (AstNode node in scope) {
				node.Visit (scopeCompiler);
			}
			symbolTable.LeaveScope ();
		}
Esempio n. 8
0
		public override void Accept (FunctionDeclaration funcDecl)
		{
			symbolTable.NextScope ();
			IodineMethod anonMethod = new IodineMethod (methodBuilder, methodBuilder.Module, null, funcDecl.InstanceMethod, 
				                          funcDecl.Parameters.Count, methodBuilder.LocalCount);
			FunctionCompiler compiler = new FunctionCompiler (symbolTable, anonMethod);
			for (int i = 0; i < funcDecl.Parameters.Count; i++) {
				anonMethod.Parameters [funcDecl.Parameters [i]] = symbolTable.GetSymbol
					(funcDecl.Parameters [i]).Index;
			}
			funcDecl.Children [0].Visit (compiler);
			anonMethod.EmitInstruction (funcDecl.Location, Opcode.LoadNull);
			anonMethod.Variadic = funcDecl.Variadic;
			anonMethod.AcceptsKeywordArgs = funcDecl.AcceptsKeywordArgs;
			anonMethod.FinalizeLabels ();
			methodBuilder.EmitInstruction (funcDecl.Location, Opcode.LoadConst,
				methodBuilder.Module.DefineConstant (anonMethod));
			methodBuilder.EmitInstruction (funcDecl.Location, Opcode.BuildClosure);
			methodBuilder.EmitInstruction (funcDecl.Location, Opcode.StoreLocal, symbolTable.GetSymbol (funcDecl.Name).Index);
			symbolTable.LeaveScope ();
		}
Esempio n. 9
0
 private IodineMethod compileMethod(FunctionDeclaration funcDecl)
 {
     symbolTable.NextScope ();
     IodineMethod methodBuilder = new IodineMethod (module, funcDecl.Name, funcDecl.InstanceMethod,
                                      funcDecl.Parameters.Count,
                                      symbolTable.CurrentScope.SymbolCount);
     FunctionCompiler compiler = new FunctionCompiler (errorLog, symbolTable,
                                     methodBuilder);
     methodBuilder.Variadic = funcDecl.Variadic;
     methodBuilder.AcceptsKeywordArgs = funcDecl.AcceptsKeywordArgs;
     for (int i = 0; i < funcDecl.Parameters.Count; i++) {
         methodBuilder.Parameters [funcDecl.Parameters [i]] = symbolTable.GetSymbol
             (funcDecl.Parameters [i]).Index;
     }
     funcDecl.Children [0].Visit (compiler);
     methodBuilder.EmitInstruction (Opcode.LoadNull);
     methodBuilder.FinalizeLabels ();
     symbolTable.LeaveScope ();
     return methodBuilder;
 }
Esempio n. 10
0
        public IodineClass CompileClass(ClassDeclaration classDecl)
        {
            IodineMethod constructor = compileMethod (classDecl.Constructor);
            if (classDecl.Constructor.Children [0].Children.Count == 0 ||
                !(classDecl.Constructor.Children [0].Children [0] is SuperCallExpression)) {
                if (classDecl.Base.Count > 0) {
                    foreach (string subclass in classDecl.Base) {
                        string[] contract = subclass.Split ('.');
                        constructor.EmitInstruction (classDecl.Location, Opcode.LoadGlobal,
                            constructor.Module.DefineConstant (new IodineName (contract [0])));
                        for (int j = 1; j < contract.Length; j++) {
                            constructor.EmitInstruction (classDecl.Location, Opcode.LoadAttribute,
                                constructor.Module.DefineConstant (new IodineName (contract [0])));
                        }
                        constructor.EmitInstruction (classDecl.Location, Opcode.InvokeSuper, 0);
                    }
                }
            }
            IodineMethod initializer = new IodineMethod (module, "__init__", false, 0, 0);
            IodineClass clazz = new IodineClass (classDecl.Name, initializer, constructor);
            FunctionCompiler compiler = new FunctionCompiler (errorLog, symbolTable,
                clazz.Initializer);

            for (int i = 1; i < classDecl.Children.Count; i++) {
                if (classDecl.Children [i] is FunctionDeclaration) {
                    FunctionDeclaration func = classDecl.Children [i] as FunctionDeclaration;
                    if (func.InstanceMethod)
                        clazz.AddInstanceMethod (compileMethod (func));
                    else {
                        clazz.SetAttribute (func.Name, compileMethod (func));
                    }
                } else if (classDecl.Children [i] is ClassDeclaration) {
                    ClassDeclaration subclass = classDecl.Children [i] as ClassDeclaration;
                    clazz.SetAttribute (subclass.Name, CompileClass (subclass));
                } else if (classDecl.Children [i] is EnumDeclaration) {
                    EnumDeclaration enumeration = classDecl.Children [i] as EnumDeclaration;
                    clazz.SetAttribute (enumeration.Name, CompileEnum (enumeration));
                } else if (classDecl.Children [i] is BinaryExpression) {
                    BinaryExpression expr = classDecl.Children [i] as BinaryExpression;
                    NameExpression name = expr.Left as NameExpression;
                    expr.Right.Visit (compiler);
                    initializer.EmitInstruction (Opcode.LoadGlobal, module.DefineConstant (new
                        IodineName (classDecl.Name)));
                    initializer.EmitInstruction (Opcode.StoreAttribute, module.DefineConstant (new
                        IodineName (name.Value)));
                } else {
                    classDecl.Children [i].Visit (compiler);
                }
            }
            clazz.Initializer.FinalizeLabels ();
            return clazz;
        }
Esempio n. 11
0
 public ModuleCompiler(SymbolTable symbolTable, IodineModule module)
 {
     this.symbolTable = symbolTable;
     this.module      = module;
     functionCompiler = new FunctionCompiler(symbolTable, module.Initializer);
 }
Esempio n. 12
0
        public IodineClass CompileClass(ClassDeclaration classDecl)
        {
            IodineMethod constructor = CompileMethod(classDecl.Constructor);

            if (classDecl.Constructor.Children [0].Children.Count == 0 ||
                !(classDecl.Constructor.Children [0].Children [0] is SuperCallExpression))
            {
                if (classDecl.Base.Count > 0)
                {
                    foreach (string subclass in classDecl.Base)
                    {
                        string[] contract = subclass.Split('.');
                        constructor.EmitInstruction(classDecl.Location, Opcode.LoadGlobal,
                                                    constructor.Module.DefineConstant(new IodineName(contract [0])));
                        for (int j = 1; j < contract.Length; j++)
                        {
                            constructor.EmitInstruction(classDecl.Location, Opcode.LoadAttribute,
                                                        constructor.Module.DefineConstant(new IodineName(contract [0])));
                        }
                        constructor.EmitInstruction(classDecl.Location, Opcode.InvokeSuper, 0);
                    }
                }
            }
            IodineMethod     initializer = new IodineMethod(module, "__init__", false, 0, 0);
            IodineClass      clazz       = new IodineClass(classDecl.Name, initializer, constructor);
            FunctionCompiler compiler    = new FunctionCompiler(symbolTable,
                                                                clazz.Initializer);

            for (int i = 1; i < classDecl.Children.Count; i++)
            {
                if (classDecl.Children [i] is FunctionDeclaration)
                {
                    FunctionDeclaration func = classDecl.Children [i] as FunctionDeclaration;
                    if (func.InstanceMethod)
                    {
                        clazz.AddInstanceMethod(CompileMethod(func));
                    }
                    else
                    {
                        clazz.SetAttribute(func.Name, CompileMethod(func));
                    }
                }
                else if (classDecl.Children [i] is ClassDeclaration)
                {
                    ClassDeclaration subclass = classDecl.Children [i] as ClassDeclaration;
                    clazz.SetAttribute(subclass.Name, CompileClass(subclass));
                }
                else if (classDecl.Children [i] is EnumDeclaration)
                {
                    EnumDeclaration enumeration = classDecl.Children [i] as EnumDeclaration;
                    clazz.SetAttribute(enumeration.Name, CompileEnum(enumeration));
                }
                else if (classDecl.Children [i] is BinaryExpression)
                {
                    BinaryExpression expr = classDecl.Children [i] as BinaryExpression;
                    NameExpression   name = expr.Left as NameExpression;
                    expr.Right.Visit(compiler);
                    initializer.EmitInstruction(classDecl.Location, Opcode.LoadGlobal,
                                                module.DefineConstant(new
                                                                      IodineName(classDecl.Name)));
                    initializer.EmitInstruction(classDecl.Location, Opcode.StoreAttribute,
                                                module.DefineConstant(new
                                                                      IodineName(name.Value)));
                }
                else
                {
                    classDecl.Children [i].Visit(compiler);
                }
            }
            clazz.Initializer.FinalizeLabels();
            return(clazz);
        }
Esempio n. 13
0
		public ModuleCompiler (SymbolTable symbolTable, IodineModule module)
		{
			this.symbolTable = symbolTable;
			this.module = module;
			functionCompiler = new FunctionCompiler (symbolTable, module.Initializer);
		}