FormalParameter.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 FormalParameter : Visitable
  8. {
  9. public string name { get; protected set; }
  10. public string type { get; protected set; }
  11. public string default_value { get; protected set; }
  12. public FormalParameter(string name, string type, string default_value = null)
  13. {
  14. this.name = name;
  15. this.type = type;
  16. this.default_value = default_value;
  17. }
  18. public FormalParameter(XElement xml)
  19. {
  20. //Set name
  21. XAttribute name_attribute = xml.Attribute("name");
  22. if (name_attribute == null)
  23. throw new CompilerException("Missing formal parameter name.");
  24. this.name = name_attribute.Value.Trim();
  25. if (this.name == "")
  26. throw new CompilerException("Empty formal parameter name.");
  27. if (Constants.Reserved.Contains(this.name))
  28. throw new CompilerException(string.Format("Reserved word '{0}' used as formal parameter name.", this.name));
  29. //Set type
  30. XAttribute type_attribute = xml.Attribute("type");
  31. if (type_attribute == null)
  32. throw new CompilerException("Missing formal parameter type.");
  33. this.type = type_attribute.Value.Trim();
  34. if (this.type == "")
  35. throw new CompilerException("Empty formal parameter type.");
  36. //Set default value
  37. XAttribute default_attribute = xml.Attribute("default");
  38. if (default_attribute == null || default_attribute.Value.Trim() == "")
  39. this.default_value = null;
  40. else
  41. this.default_value = default_attribute.Value.Trim();
  42. }
  43. public override void accept(Visitor visitor)
  44. {
  45. visitor.visit (this);
  46. }
  47. }
  48. }