Method.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. namespace csharp_sccd_compiler
  6. {
  7. public class Method : Visitable
  8. {
  9. public string name { get; protected set; }
  10. public string return_type { get; protected set; }
  11. public string access { get; protected set; }
  12. public string body { get; protected set; }
  13. public List<FormalParameter> parameters { get; protected set; }
  14. protected Method()
  15. {
  16. }
  17. public Method(XElement xml)
  18. {
  19. //Set name
  20. XAttribute name_attribute = xml.Attribute("name");
  21. if (name_attribute == null)
  22. throw new CompilerException("Missing method name.");
  23. this.name = name_attribute.Value.Trim();
  24. if (this.name == "")
  25. throw new CompilerException("Empty method name.");
  26. if (Constants.Reserved.Contains(this.name))
  27. throw new CompilerException(string.Format("Reserved word '{0}' used as method name.", this.name));
  28. //Set return type
  29. XAttribute return_attribute = xml.Attribute("type");
  30. if (return_attribute == null)
  31. throw new CompilerException("Missing method type.");
  32. this.return_type = return_attribute.Value.Trim();
  33. if (this.return_type == "")
  34. throw new CompilerException("Empty method type.");
  35. //Set access level
  36. XAttribute access_attribute = xml.Attribute("access");
  37. if (access_attribute == null || access_attribute.Value.Trim() == "")
  38. this.access = "public"; //default value
  39. else
  40. this.access = access_attribute.Value.Trim();
  41. //Set body
  42. XAttribute body_attribute = xml.Attribute("body");
  43. if (body_attribute == null)
  44. this.body = "";
  45. else
  46. this.body = body_attribute.Value;
  47. //Set parameters
  48. this.parameters = new List<FormalParameter>();
  49. foreach (XElement parameter_xml in xml.Elements("parameter"))
  50. {
  51. this.parameters.Add(new FormalParameter(parameter_xml));
  52. }
  53. }
  54. public override void accept(Visitor visitor)
  55. {
  56. visitor.visit (this);
  57. }
  58. }
  59. }