Beispiel #1
0
        public ClassDeclaration(SourceLocation location,
                                string name,
                                string doc) : base(location)
        {
            Name          = name;
            Documentation = doc;

            /*
             * Create an empty constructor that will call the class's super constructor
             */

            var emptyCtor = new FunctionDeclaration(
                location,
                name,
                true,
                false,
                false,
                false,
                new List <FunctionParameter> (),
                doc
                );

            /*
             * This is important for insuring that the super class is properly initialized
             */


            var callToSuper = new SuperCallStatement(location, this,
                                                     new ArgumentList(location));

            emptyCtor.Body.AddStatement(callToSuper);

            Constructor = emptyCtor;
        }
		public ClassDeclaration (SourceLocation location, string name, List<string> baseClass)
			: base (location)
		{
			Name = name;
			Base = baseClass;
			FunctionDeclaration dummyCtor = new FunctionDeclaration (location, name, true, false, false, new List<string> ());
			dummyCtor.Add (new Statement (location));
			Add (dummyCtor);
		}
Beispiel #3
0
        public ClassDeclaration(SourceLocation location, string name, List <string> baseClass)
            : base(location)
        {
            Name = name;
            Base = baseClass;
            FunctionDeclaration dummyCtor = new FunctionDeclaration(location, name, true, false, false, new List <string> ());

            dummyCtor.Add(new Statement(location));
            Add(dummyCtor);
        }
		public override void Accept (FunctionDeclaration funcDecl)
		{
			symbolTable.AddSymbol (funcDecl.Name);
			FunctionAnalyser visitor = new FunctionAnalyser (errorLog, symbolTable);
			symbolTable.BeginScope ();

			foreach (string param in funcDecl.Parameters) {
				symbolTable.AddSymbol (param);
			}

			funcDecl.Children [0].Visit (visitor);
			symbolTable.EndScope ();
		}
Beispiel #5
0
        public ClassDeclaration(SourceLocation location,
                                string name,
                                string doc,
                                List <FunctionParameter> parameters)
            : base(location)
        {
            Name          = name;
            Documentation = doc;

            var recordCtor = new FunctionDeclaration(
                location,
                name,
                true,
                false,
                false,
                true,
                parameters,
                doc
                );

            recordCtor.Body.AddStatement(new SuperCallStatement(location, this, new ArgumentList(location)));

            foreach (NamedParameter parameter in parameters)
            {
                recordCtor.Body.AddStatement(
                    new Expression(
                        location,
                        new BinaryExpression(location,
                                             BinaryOperation.Assign,
                                             new MemberExpression(location,
                                                                  new SelfExpression(location),
                                                                  parameter.Name
                                                                  ),
                                             new NameExpression(location, parameter.Name)
                                             )
                        )
                    );
            }

            Constructor = recordCtor;
        }
Beispiel #6
0
        private static AstNode ParseFunction(TokenStream stream, bool prototype = false,
			ClassDeclaration cdecl = null)
        {
            if (stream.Accept (TokenClass.Operator, "@")) {
                /*
                 * Function decorators in the form of
                 * @myDecorator
                 * func foo () {
                 * }
                 * are merely syntatic sugar for
                 * func foo () {
                 * }
                 * foo = myDecorator (foo)
                 */
                AstNode expr = ParseExpression (stream); // Decorator expression
                /* This is the original function which is to be decorated */
                FunctionDeclaration idecl = ParseFunction (stream, prototype, cdecl) as FunctionDeclaration;
                /* We must construct an arglist which will be passed to the decorator */
                ArgumentList args = new ArgumentList (stream.Location);
                args.Add (new NameExpression (stream.Location, idecl.Name));
                /*
                 * Since two values can not be returned, we must return a single node containing both
                 * the function declaration and call to the decorator
                 */
                AstRoot nodes = new AstRoot (stream.Location);
                nodes.Add (idecl);
                nodes.Add (new Expression (stream.Location, new BinaryExpression (stream.Location,
                    BinaryOperation.Assign,
                    new NameExpression (stream.Location, idecl.Name),
                    new CallExpression (stream.Location, expr, args))));
                return nodes;
            }
            stream.Expect (TokenClass.Keyword, "func");
            bool isInstanceMethod;
            bool isVariadic;
            bool hasKeywordArgs;
            Token ident = stream.Expect (TokenClass.Identifier);
            List<string> parameters = ParseFuncParameters (stream,
                out isInstanceMethod,
                out isVariadic,
                out hasKeywordArgs);

            FunctionDeclaration decl = new FunctionDeclaration (stream.Location, ident != null ?
                ident.Value : "",
                isInstanceMethod,
                isVariadic,
                hasKeywordArgs,
                parameters);

            if (!prototype) {

                if (stream.Accept (TokenClass.Operator, "=>")) {
                    decl.Add (new ReturnStatement (stream.Location, ParseExpression (stream)));
                } else {
                    stream.Expect (TokenClass.OpenBrace);
                    CodeBlock scope = new CodeBlock (stream.Location);

                    if (stream.Match (TokenClass.Keyword, "super")) {
                        scope.Add (ParseSuperCall (stream, cdecl));
                    }

                    while (!stream.Match (TokenClass.CloseBrace)) {
                        scope.Add (ParseStatement (stream));
                    }

                    decl.Add (scope);
                    stream.Expect (TokenClass.CloseBrace);
                }
            }
            return decl;
        }
		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 ();
		}
Beispiel #8
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;
 }
Beispiel #9
0
 public void Accept(FunctionDeclaration funcDecl)
 {
     module.AddMethod (compileMethod (funcDecl));
 }
		public override void Accept (FunctionDeclaration funcDecl)
		{
			errorLog.AddError (ErrorType.ParserError, funcDecl.Location,
				"statement can not exist inside pattern!");
		}
 public virtual void Accept(FunctionDeclaration funcDecl)
 {
 }
Beispiel #12
0
		public override void Accept (FunctionDeclaration funcDecl)
		{
			module.AddMethod (CompileMethod (funcDecl));
		}
Beispiel #13
0
 public DecoratedFunction(SourceLocation location, AstNode decorator, FunctionDeclaration decl)
     : base(location)
 {
     Decorator = decorator;
     Function  = decl;
 }