summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/public/repl.js
blob: 22068f19086370659a7c1a8d193336142dcd1f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var websocket = undefined;

function generateRandomToken() {
  const array = new Uint8Array(16);
  window.crypto.getRandomValues(array);
  return Array.from(array, dec => ('0' + dec.toString(16)).substr(-2)).join('');
}

function connect() {
  websocket = (function() {
    const host = window.location.host;
    const params = new URLSearchParams(window.location.search);
    let token = generateRandomToken();
    if (params.has("token")) {
      token = params.get("token");
    }
    const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
    const ws = new WebSocket(protocol + "//" + host + "/repl/" + token);
    ws.binaryType = "arraybuffer";
    return ws;
  })();

  websocket.onopen = function(e) {
    document.getElementById("command").classList.remove("disconnected");
    document.getElementById("command").classList.add("connected");
    document.getElementById("connect").src = "images/connected.png";
    document.getElementById("connect").removeEventListener("click", connect);
  };

  websocket.onclose = function(e) {
    document.getElementById("command").classList.remove("connected");
    document.getElementById("command").classList.add("disconnected");
    document.getElementById("connect").src = "images/connect.png";
    document.getElementById("connect").addEventListener("click", connect);
  };

  websocket.onmessage = function(e) {
    const reader = new FileReader();
    reader.addEventListener('loadend', readMessageAndAppend(reader));

    if(e.data instanceof ArrayBuffer) {
      reader.readAsText(new Blob([e.data]));
    } else {
      reader.readAsText(e.data);
    }
  };
}

var commandHistory = [];

function appendOutput(text) {
  const root = document.getElementById("minilang");
  const sub = document.createElement("div");
  sub.classList.add("output");
  sub.appendChild(text);
  if(root.firstChild) {
    root.insertBefore(sub, root.firstChild);
  } else {
    root.appendChild(sub);
  }
};

function prettyAST(term) {
  if(term.tag === "Sym") {
    return term.contents;
  } else if(term.tag === "Abs") {
    return "(lam (" + term.contents[0].join(" ") + ") " + prettyAST(term.contents[1]) + ")";
  } else if(term.tag === "App") {
    const pref = "(" + prettyAST(term.contents[0]) + " " + prettyAST(term.contents[1]);
    if(term.contents[2]) {
      return pref + " " + term.contents[2].map(prettyAST).join(" ") + ")";
    } else {
      return pref + ")";
    }
  }
  return JSON.stringify(term);
}

function prettyType(type) {
  if(type.tag === "TyVar") {
    return type.contents;
  } else if(type.tag === ":->") {
    return "(" + prettyType(type.contents[0]) + " -> " + prettyType(type.contents[1]) + ")";
  }
  return JSON.stringify(type);
}

function formatResult(text) {
  const result = JSON.parse(text);
  if (result.tag === "Evaluated") {
    return prettyAST(result.contents[0]) + ' : ' + prettyType(result.contents[2]);
  } else if (result.tag === "Msg" ) {
    return result.contents;
  }
  return text;
}

function readMessageAndAppend(reader) {
  return () => {
    appendOutput(document.createTextNode(formatResult(reader.result)));
  };
};

function makeCommand(val) {
  const words = val.split(/ +/);
  if(words[0] === ":env") {
    return {tag:"Com",contents:{tag:"DumpEnv"}};
  } else if(words[0] === ":clear") {
    return {tag:"Com",contents:{tag:"ClearEnv"}};
  } else if(words[0] === ":help") {
    return {tag:"Com",contents:{tag:"Help"}};
  } else if(words[0] === ":lang") {
    return {tag:"Com",contents:{tag:"Set", contents:{tag: "SelectLanguage", contents:words[1]}}};
  } else {
    return {tag:"In",contents:val};
  }
};

function sendCommand(e) {
  const input = document.getElementById("command");
  if (e.keyCode == 13) {
    const command = makeCommand(input.value);
    appendOutput(document.createTextNode("> "+ input.value));
    commandHistory.push(input.value);
    websocket.send(JSON.stringify(command));
    input.value = "";
    return false;
  } else if(e.keyCode == 38) {
    input.value = commandHistory.pop() || "";
    return false;
  }

  return true;
};

function loadText(e) {
  const text = document.getElementById("program").value;
  const command = {tag:"Com",
                   contents:{ tag:"Load",
                              contents:{ tag:"Data",
                                         contents:text}}};
  websocket.send(JSON.stringify(command));
  return false;
};

function toggleHelp(e) {
  const help = document.getElementById("help");
  if(help.style.display === "block") {
    help.style.display = "none";
  } else {
    help.style.display = "block";
  }
}


document.getElementById("command").addEventListener("keyup", sendCommand);
document.getElementById("connect").addEventListener("click", connect);
document.getElementById("send").addEventListener("click", loadText);
document.getElementById("helptip").addEventListener("click", toggleHelp);
document.getElementById("help").addEventListener("click", toggleHelp);

connect();