private void VisitNode(MethodDeclaration node) { // catch errors prior to entering method declaration ErrorDescriptor err = node.TypeDescriptor as ErrorDescriptor; if (err != null) { PrintError(err); return; } _localVariables.OpenScope(); MethodTypeDescriptor desc = node.TypeDescriptor as MethodTypeDescriptor; if (desc != null) { AbstractNode modifiers = node.Child; AbstractNode typeSpecifier = modifiers.Sib; AbstractNode methodDeclarator = typeSpecifier.Sib; AbstractNode methodBody = methodDeclarator.Sib; AbstractNode methodDeclaratorName = methodDeclarator.Child; AbstractNode parameterList = methodDeclaratorName.Sib; // may be null string name = ((Identifier)methodDeclaratorName).ID; if (!desc.Modifiers.Contains(ModifiersEnums.STATIC) /*&& * name.ToLower().Equals("main")*/) { desc.Modifiers.Add(ModifiersEnums.STATIC); } string mods = String.Join(" ", desc.Modifiers).ToLower(); string typeSpec = GetIlType(desc.ReturnType, typeSpecifier); string argList = GetIlTypeParams(desc.Signature.ParameterTypes); string begin = name.ToLower().Equals("main") ? "\n{\n.entrypoint\n.maxstack 42\n" : "\n{\n.maxstack 42"; string end = "ret\n}"; File.WriteLine($".method {mods} {typeSpec} {name}" + $"({argList}) {begin}"); methodBody.Accept(this); File.WriteLine(end); _localVariables.CloseScope(); } }
private void VisitNode(MethodCall node) { // catch errors prior to entering method call ErrorDescriptor err = node.TypeDescriptor as ErrorDescriptor; if (err != null) { PrintError(err); return; } MethodTypeDescriptor desc = node.TypeDescriptor as MethodTypeDescriptor; string retType = GetIlType(desc.ReturnType, node); AbstractNode methodReference = node.Child; AbstractNode argumentList = methodReference.Sib; // may be null QualifiedName qualifiedName = methodReference as QualifiedName; string methodName = qualifiedName.GetStringName(); // add arguments argumentList?.Accept(this); // special calls for Write or WriteLine methods if (methodName.ToLower().Equals("write") || methodName.ToLower().Equals("writeline")) { CallWrites(methodName.ToLower(), GetIlTypeParams(desc.Signature.ParameterTypes)); } // call for declared methods w/in program else { string param = (argumentList == null) ? "" : GetIlTypeParams(argumentList); File.WriteLine("call {0} {1}::{2}({3})", retType, ClassName, methodName, param); } }
private void VisitNode(MethodCall node) { AbstractNode methodReference = node.Child; AbstractNode argumentList = methodReference.Sib; // may be null TypeDescriptor descriptor; QualifiedName qualifiedName = methodReference as QualifiedName; if (qualifiedName == null) { descriptor = new ErrorDescriptor ("Only Qualified Name supported for Method Call reference"); } else { // get parameters from method call List <TypeDescriptor> argListTypes = GetParameterTypes(argumentList as ArgumentList); // get parameters (signature) from declared method Attributes attr = Table.lookup(qualifiedName.GetStringName()); MethodTypeDescriptor methodDescriptor = attr.TypeDescriptor as MethodTypeDescriptor; if (methodDescriptor != null) { SignatureDescriptor methodSignature = methodDescriptor.Signature; if (methodSignature != null && ParametersMatch(methodSignature, argListTypes)) { // method descriptor for only current signature MethodTypeDescriptor temp = new MethodTypeDescriptor(); temp.ReturnType = methodDescriptor.ReturnType; temp.Signature.ParameterTypes = argListTypes; temp.Signature.Next = null; descriptor = temp; } else if (methodSignature == null) { descriptor = new ErrorDescriptor ("No signature found for method: " + qualifiedName.GetStringName()); } else { descriptor = new ErrorDescriptor ("No method signature found matching: (" + String.Join(", ", argListTypes) + ")"); } } else { descriptor = new ErrorDescriptor("Method not declared: " + qualifiedName.GetStringName()); } node.TypeDescriptor = descriptor; Attributes methodCallAttr = new Attr(descriptor); methodCallAttr.Kind = Kind.MethodType; node.AttributesRef = methodCallAttr; } }
private void setBaseline() { ScopeTable currScopeTable = symbolTable.Pop(); // Until given additional info, Java objects are placeholders Attr javaAttr = new Attr(new JavaObjectDescriptor()); javaAttr.Kind = Kind.TypeAttributes; currScopeTable.Add("java", javaAttr); currScopeTable.Add("io", javaAttr); currScopeTable.Add("lang", javaAttr); currScopeTable.Add("System", javaAttr); currScopeTable.Add("out", javaAttr); currScopeTable.Add("print", javaAttr); currScopeTable.Add("outint", javaAttr); currScopeTable.Add("PrintStream", javaAttr); currScopeTable.Add("TestClasses", javaAttr); // primitive types PrimitiveAttributes primVoidAttrs = new PrimitiveAttributes(new PrimitiveTypeVoidDescriptor()); currScopeTable.Add("VOID", primVoidAttrs); PrimitiveAttributes primIntAttrs = new PrimitiveAttributes(new PrimitiveTypeIntDescriptor()); currScopeTable.Add("INT", primIntAttrs); PrimitiveAttributes primBooleanAttrs = new PrimitiveAttributes(new PrimitiveTypeBooleanDescriptor()); currScopeTable.Add("BOOLEAN", primBooleanAttrs); // special names SpecialNameAttributes spNameThis = new SpecialNameAttributes (SpecialNameEnums.THIS); currScopeTable.Add(spNameThis.Name, spNameThis); SpecialNameAttributes spNameNull = new SpecialNameAttributes( SpecialNameEnums.NULL); currScopeTable.Add(spNameNull.Name, spNameNull); // Write & WriteLine PrimitiveTypeVoidDescriptor returnType = new PrimitiveTypeVoidDescriptor(); // no parameters SignatureDescriptor sigDescNone = new SignatureDescriptor(); // integer parameter SignatureDescriptor sigDescInt = new SignatureDescriptor(); sigDescInt.AddParameter(new PrimitiveTypeIntDescriptor()); // boolean parameter SignatureDescriptor sigDescBoolean = new SignatureDescriptor(); sigDescBoolean.AddParameter(new PrimitiveTypeBooleanDescriptor()); // literal parameter (string) SignatureDescriptor sigDescLiteral = new SignatureDescriptor(); sigDescLiteral.AddParameter(new LiteralTypeDescriptor()); // chain signature type descriptors together (sigDescNode = first) sigDescNone.Next = sigDescInt; sigDescInt.Next = sigDescBoolean; sigDescBoolean.Next = sigDescLiteral; MethodTypeDescriptor methodTypeDescriptor = new MethodTypeDescriptor(); methodTypeDescriptor.ReturnType = returnType; methodTypeDescriptor.Signature = sigDescNone; Attr methodAttr = new Attr(methodTypeDescriptor); currScopeTable.Add("Write", methodAttr); currScopeTable.Add("WriteLine", methodAttr); // TODO: add additional baseline information symbolTable.Push(currScopeTable); }
private void VisitNode(MethodDeclaration node) { AbstractNode modifiers = node.Child; AbstractNode typeSpecifier = modifiers.Sib; AbstractNode methodDeclarator = typeSpecifier.Sib; AbstractNode methodBody = methodDeclarator.Sib; AbstractNode retType = typeSpecifier.Child; if (retType is PrimitiveTypeVoid) { ((PrimitiveTypeVoid)retType).Accept(TypeVisitor); } else if (retType is PrimitiveTypeBoolean) { ((PrimitiveTypeBoolean)retType).Accept(TypeVisitor); } else if (retType is PrimitiveTypeInt) { ((PrimitiveTypeInt)retType).Accept(TypeVisitor); } else if (retType is QualifiedName) { ((QualifiedName)retType).Accept(TypeVisitor); } else { string msg = "Return type of a method must be a " + "PrimitiveType or QualifiedName (found: " + GetSimpleName(retType.TypeDescriptor); retType.TypeDescriptor = new ErrorDescriptor(msg); } AbstractNode methodDeclaratorName = methodDeclarator.Child; AbstractNode parameterList = methodDeclaratorName.Sib; // may be null MethodTypeDescriptor methDesc = new MethodTypeDescriptor(); methDesc.ReturnType = retType.TypeDescriptor; methDesc.Modifiers = ((Modifiers)modifiers).ModifierTokens; methDesc.Locals = new ScopeTable(); methDesc.IsDefinedIn = CurrentClass; Attributes attr = new Attr(methDesc); attr.Kind = Kind.MethodType; string name = ((Identifier)methodDeclaratorName).ID; Table.enter(name, attr); node.TypeDescriptor = attr.TypeDescriptor; node.AttributesRef = attr; Table.openScope(methDesc.Locals); MethodTypeDescriptor oldCurrentMethod = CurrentMethod; CurrentMethod = methDesc; if (parameterList != null) { parameterList.Accept(this); methDesc.Signature.ParameterTypes = ((ParameterListTypeDescriptor) parameterList.TypeDescriptor).ParamTypeDescriptors; attr.TypeDescriptor = methDesc; Table.updateValue(name, attr); node.TypeDescriptor = methDesc; node.AttributesRef = Table.lookup(name); } methodBody.Accept(this); CurrentMethod = oldCurrentMethod; Table.closeScope(); }