#lang shplait ~untyped // On object is a tuple containing // - a list for fields // - a list for methods, where each method // is a function that takes `this` and one argument type Object = Mapof(Symbol, ....) * Mapof(Symbol, (...., ....) -> ....) macro 'object ($f_id = $f_expr, ...): method $m_id($arg): $m_expr ...': 'values([values(#' $f_id, $f_expr), ...], [values(#' $m_id, method ($arg): $m_expr), ...])' macro 'method ($arg): $expr': 'fun ($(replace_scopes('this', arg)), $arg): $expr' macro | '$o_expr . $(id :: Identifier) ($arg)': // Extract a method form the second hash table 'let o = $o_expr: find(snd(o), #' $id)(o, $arg)' | '$o_expr . $(id :: Identifier)': // Extract a field form the first list table 'find(fst($o_expr), #' $id)' fun find(l :: Listof(Symbol * ?a), name :: Symbol) :: ?a: match l | []: error(#'find, "not found: " +& name) | cons(p, rst_l): if name == fst(p) | snd(p) | find(rst_l, name) // ---------------------------------------- macro 'rectangle($init_w, $init_h)': 'object (w = $init_w, h = $init_h): method area(arg): this.w * this.h method is_bigger_than(arg): this.area(0) > arg' def r = rectangle(10, 15) r.area(0) r.is_bigger_than(100)