static void ProcessFields(ParseNode x, ClassDefn defn) { Dictionary <string, Expression> initializers = new Dictionary <string, Expression>(); ParseNode fields = x.GetChild("fields"); if (fields != null) { foreach (ParseNode node in fields.Children) { FieldDefn fd = CreateField(node); fd.annotations = CreateAnnotations(node); defn.AddField(fd); ParseNode exprNode = node.GetChild("expr"); if (exprNode != null) { Expression expr = CreateExpr(exprNode); initializers.Add(fd.name, expr); } } } foreach (string s in initializers.Keys) { Assignment ass = new Assignment(new Name(s), initializers[s]); ExpressionStatement st = new ExpressionStatement(ass); defn.GetAutoConstructor().body.statements.Add(st); } }
static public ClassDefn ParseClass(ModuleDefn m, string s) { ParseNode node = ParserState.Parse(HeronGrammar.Class, s); if (node == null) { return(null); } ClassDefn r = CodeModelBuilder.CreateClass(m, node); return(r); }
public bool InheritsFrom(ClassDefn c) { string s = c.name; if (s == name) { return(true); } if (GetBaseClass() != null) { return(GetBaseClass().InheritsFrom(c)); } return(false); }
public static ClassDefn CreateClass(ModuleDefn m, ParseNode x) { string name = x.GetChild("name").ToString(); ClassDefn r = new ClassDefn(m, name); ParseNode inherits = x.GetChild("inherits"); if (inherits != null) { if (inherits.GetNumChildren() != 1) { throw new Exception("A class can only inherit from exactly one other class"); } ParseNode type = inherits.GetChild(0); r.SetBaseClass(CreateTypeRef(type)); } ParseNode implements = x.GetChild("implements"); if (implements != null) { foreach (ParseNode node in implements.Children) { r.AddImplementedInterface(CreateTypeRef(node)); } } ParseNode methods = x.GetChild("methods"); if (methods != null) { foreach (ParseNode node in methods.Children) { FunctionDefn f = CreateMethod(node, r); r.AddMethod(f); } } ProcessFields(x, r); m.AddClass(r); return(r); }
/// <summary> /// Used to cast the class instance to its base class or an interface. /// </summary> /// <param name="t"></param> /// <returns></returns> public override HeronValue As(HeronType t) { if (t is ClassDefn) { ClassDefn c1 = cls; ClassDefn c2 = t as ClassDefn; if (c2.name == c1.name) { return(this); } if (GetBase() == null) { return(null); } return(GetBase().As(t)); } else if (t is InterfaceDefn) { if (cls.Implements(t as InterfaceDefn)) { return(new InterfaceInstance(this, t as InterfaceDefn)); } return(null); } else if (t == PrimitiveTypes.AnyType) { return(new AnyValue(this)); } else if (t == PrimitiveTypes.UnknownType) { return(this); } return(null); }
public ClassInstance(ClassDefn c, ModuleInstance m) { cls = c; module = m; }
public void AddClass(ClassDefn x) { types.Add(x.name, x); classes.Add(x); }