etng-r2/metaeval.tng
author Tony Garnock-Jones <tonygarnockjones@gmail.com>
Wed, 16 Jan 2019 17:15:58 +0000
changeset 438 1fe179d53161
parent 234 10e62e160cb0
permissions -rw-r--r--
Add missing primitive implementation for the plain interpreter.
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     1
namespace s = "http://eighty-twenty.org/etng/r1/ns/stream#";
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     2
namespace core = "http://eighty-twenty.org/etng/r1/ns/abstract-syntax/core#";
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     3
namespace reflection = "http://eighty-twenty.org/etng/r1/ns/interpreter/meta-representation#";
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     4
namespace interpreter = "http://eighty-twenty.org/etng/r1/ns/interpreter#";
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     5
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     6
---------------------------------------------------------------------------
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     7
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
     8
define :TExtendable = rec {
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     9
  .:with extension ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    10
    reflection:reflect(self).reflection:extendWith(reflection:reflect(extension)).reflection:reify;
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    11
};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    12
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    13
---------------------------------------------------------------------------
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    14
namespace "http://eighty-twenty.org/etng/r1/ns/interpreter/meta-representation#";
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    15
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    16
define makeQName uri localName -> {
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    17
  .uri = uri;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    18
  .localName = localName;
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    19
};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    20
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    21
define emptyEnvironment = {};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    22
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    23
define searchEnvironment env name ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    24
  1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    25
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    26
define extendEnvironment env1 env2 ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    27
  1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    28
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    29
define match pattern value env ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    30
  1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    31
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    32
define Object = {
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    33
  .lookup message k ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    34
    1;
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    35
};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    36
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    37
define makeObject methods ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    38
  1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    39
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    40
define Function = {
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    41
  .lookup message k ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    42
    1;
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    43
};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    44
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    45
define makeFunction methods ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    46
  1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    47
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    48
define makeMessage parts ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    49
  1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    50
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    51
define Tuple = {
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    52
};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    53
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    54
define makeTuple values ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    55
  1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    56
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    57
---------------------------------------------------------------------------
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    58
namespace "http://eighty-twenty.org/etng/r1/ns/interpreter#";
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    59
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    60
define InterpreterState = :TExtendable.:with {
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    61
  .env = reflection:emptyEnvironment;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    62
  .k = < >;
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    63
  .self_ = {};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    64
  .nextMethod_ = {exit(<.internalError>);} -- ??
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    65
};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    66
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    67
define Interpreter = rec {
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    68
  .globals = 1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    69
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    70
  .unboundVariable state name ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    71
    exit(<.unboundVariable state name>);
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    72
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    73
  .doesNotUnderstand state receiver message ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    74
    exit(<.doesNotUnderstand state receiver message>);
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    75
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    76
  .noNextMethod state ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    77
    exit(<.noNextMethod state>);
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    78
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    79
  .lookup state name k ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    80
    reflection:searchEnvironment (state.env) name {
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    81
      .:ok(v) -> k v;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    82
      _ -> self.globals name {
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    83
	    .:ok(v)-> k v;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    84
	    _ -> self.unboundVariable state name;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    85
	  };
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    86
    };
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    87
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    88
  .apply state (closure, bindings, newSelf, newNextMethod) k ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    89
    let env = reflection:extendEnvironment (closure.reflection:env) bindings;
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
    90
    self.eval (state.:with {.env = env; .nextMethod_ = newNextMethod; .self_ = newSelf;})
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    91
	      (closure.reflection:exp)
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    92
	      k;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    93
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    94
  .send state receiver message k ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    95
    -- need to handle <...> messages here
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    96
    receiver.reflection:lookup message {
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    97
      .:ok result -> self.apply state result k;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    98
      _ -> self.doesNotUnderstand receiver message;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    99
    };
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   100
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   101
  .evlis state elements k ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   102
    let e = {(element, acc) continue -> self.eval state element {v -> continue [v | acc]}};
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
   103
    s:foldlK elements [] e {acc -> k (s:reverse acc)};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   104
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   105
  .eval state exp k ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   106
    (<exp.core:kind>) {
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   107
      .core:Namespace -> self.eval state (exp.core:value) k;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   108
      .core:Send ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   109
	self.eval state (exp.core:receiver) {r ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   110
	  self.eval state (exp.core:message) {m ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   111
	    self.send r m k}};
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   112
      .core:Object -> reflection:makeObject state (exp.core:methods);
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   113
      .core:Function -> reflection:makeFunction state (exp.core:methods);
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   114
      .core:Message -> self.evlis state (exp.core:parts) reflection:makeMessage;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   115
      .core:Do ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   116
	self.eval state (exp.core:head) {v ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   117
	  self.eval state (exp.core:tail) k};
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   118
      .core:Let ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   119
	self.eval state (exp.core:value) {v ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   120
	  let newEnv = reflection:match (exp.core:pattern) v (state.env);
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
   121
	  self.eval (state.:with {.env = newEnv}) (exp.core:body) k};
184
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   122
      .core:Ref -> self.lookup state (exp.core:name) k;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   123
      .core:Tuple -> self.evlis state (exp.core:elements) {vals -> k (reflection:makeTuple vals)};
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   124
      .core:Lit -> exp.core:value;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   125
      .core:Self -> state.self_;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   126
      .core:NextMethod ->
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   127
	state.nextMethod_ {
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   128
	  .:ok result -> self.apply state result k;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   129
	  _ -> self.noNextMethod state;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   130
	};
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   131
      .core:Meta -> 1;
5d95518da947 Interim commit
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   132
    };
234
10e62e160cb0 Switch from "[]" to "rec {}", and add "rec selfid {}" form
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents: 198
diff changeset
   133
};