private static AstNode ParseClass (TokenStream stream) { stream.Expect (TokenClass.Keyword, "class"); string name = stream.Expect (TokenClass.Identifier).Value; List<string> baseClass = new List<string> (); if (stream.Accept (TokenClass.Colon)) { do { baseClass.Add (ParseClassName (stream)); } while (stream.Accept (TokenClass.Comma)); } ClassDeclaration clazz = new ClassDeclaration (stream.Location, name, baseClass); stream.Expect (TokenClass.OpenBrace); while (!stream.Match (TokenClass.CloseBrace)) { if (stream.Match (TokenClass.Keyword, "func") || stream.Match (TokenClass.Operator, "@")) { FunctionDeclaration func = ParseFunction (stream, false, clazz) as FunctionDeclaration; if (func.Name == name) { clazz.Constructor = func; } else { clazz.Add (func); } } else { clazz.Add (ParseStatement (stream)); } } stream.Expect (TokenClass.CloseBrace); return clazz; }
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 static SuperCallExpression ParseSuperCall(TokenStream stream, ClassDeclaration parent) { SuperCallExpression ret = new SuperCallExpression (stream.Location); stream.Expect (TokenClass.Keyword, "super"); ret.Parent = parent; ret.Add (ParseArgumentList (stream)); while (stream.Accept (TokenClass.SemiColon)) ; return ret; }
public override void Accept (ClassDeclaration classDecl) { ModuleCompiler compiler = new ModuleCompiler (symbolTable, methodBuilder.Module); IodineClass clazz = compiler.CompileClass (classDecl); methodBuilder.EmitInstruction (classDecl.Location, Opcode.LoadConst, methodBuilder.Module.DefineConstant (clazz)); methodBuilder.EmitInstruction (classDecl.Location, Opcode.StoreLocal, symbolTable.GetSymbol (classDecl.Name).Index); }
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; }
public void Accept(ClassDeclaration classDecl) { module.SetAttribute (classDecl.Name, CompileClass (classDecl)); }
public override void Accept (ClassDeclaration classDecl) { errorLog.AddError (ErrorType.ParserError, classDecl.Location, "statement can not exist inside pattern!"); }
public virtual void Accept(ClassDeclaration classDecl) { }
public void Accept(ClassDeclaration classDecl) { symbolTable.AddSymbol (classDecl.Name); RootAnalyser visitor = new RootAnalyser (errorLog, symbolTable); classDecl.Visit (visitor); }
public void Accept(ClassDeclaration classDecl) { classDecl.VisitChildren (this); }
public void Accept(ClassDeclaration classDecl) { }
public SuperCallStatement(SourceLocation location, ClassDeclaration parent, ArgumentList argumentList) : base(location) { Parent = parent; Arguments = argumentList; }