import { Callable } from "./callable" ; import { Instance } from "./instance" ; import { Function } from "./function" ; import { Interpreter } from "../core/interpreter" ; import { Token , TokenValue } from "../structures/token" ; export class LSClass implements Callable { name : string ; superclass : LSClass | null ; methods : Map < string , Function > ; constructor ( name , superclass , methods ) { this . name = name ; this . methods = methods ; this . superclass = superclass ; } stringify ( ) { return `<class ${ this . name }>` ; } arity ( ) { let init = this . findMethod ( "init" ) ; return init === null ? [ 0 , 0 ] : init . arity ( ) ; } call ( interpreter , token , args ) { let instance = new Instance ( this ) ; let initializer = this . findMethod ( "init" ) ; if ( initializer !== null ) initializer . bind ( token , instance , interpreter . errorhandler ) . call ( interpreter , token , args ) ; return instance ; } findMethod ( name ) { if ( this . methods . has ( name ) ) { let method = this . methods . get ( name ) ; return method === undefined ? null : method ; } if ( this . superclass !== null ) return this . superclass . findMethod ( name ) ; return null ; } }