public int Arity() { LoxFunction initilizer = FindMethod("init"); if (initilizer == null) { return(0); } return(initilizer.Arity()); }
public object Call(Interpreter interpreter, List <object> arguments) { LoxInstance instance = new LoxInstance(this); LoxFunction initilizer = FindMethod("init"); if (initilizer != null) { initilizer.Bind(instance).Call(interpreter, arguments); } return(instance); }
internal object Get(Token name) { if (fields.ContainsKey(name.lexeme)) { return(fields[name.lexeme]); } LoxFunction method = klass.FindMethod(name.lexeme); if (method != null) { return(method.Bind(this)); } throw new RuntimeError(name, "Undefined property '" + name.lexeme + "'."); }
public object VisitClassStmt(Class stmt) { environment.Define(stmt.name.lexeme, null); Dictionary <string, LoxFunction> methods = new Dictionary <string, LoxFunction>(); foreach (Function method in stmt.methods) { LoxFunction function = new LoxFunction(method, environment, method.name.lexeme.Equals("init")); methods.Add(method.name.lexeme, function); } LoxClass klass = new LoxClass(stmt.name.lexeme, methods); environment.Assign(stmt.name, klass); return(null); }