@@ -4,6 +4,30 @@ static const char* replModuleSource =
44" import \" io\"  for Stdin, Stdout\n " 
55" import \" os\"  for Platform\n " 
66" \n " 
7+ " class CodePointList {\n " 
8+ "   construct new(s) {\n " 
9+ "     _string = s\n " 
10+ "   }\n " 
11+ "   [x] {\n " 
12+ "     if (x is Num) {\n " 
13+ "       return _string.codePoints.skip(x).take(1).map { |x| String.fromCodePoint(x) }.join()\n " 
14+ "     } else {\n " 
15+ "       if (x.to == -1) {\n " 
16+ "         return _string.codePoints.skip(x.from).map { |x| String.fromCodePoint(x) }.join()\n " 
17+ "       } else {\n " 
18+ "         var n = x.to - x.from\n " 
19+ "         return _string.codePoints.skip(x.from).take(n).map { |x| String.fromCodePoint(x) }.join()\n " 
20+ "       }\n " 
21+ "     }\n " 
22+ "   }\n " 
23+ "   // +(other) {\n " 
24+ "   //   _string = _string + other\n " 
25+ "   // }\n " 
26+ "   // toString {\n " 
27+ "   //   return _string\n " 
28+ "   // }\n " 
29+ " }\n " 
30+ " \n " 
731" /// Abstract base class for the REPL. Manages the input line and history, but\n " 
832" /// does not render.\n " 
933" class Repl {\n " 
@@ -25,13 +49,15 @@ static const char* replModuleSource =
2549"     refreshLine(false)\n " 
2650" \n " 
2751"     while (true) {\n " 
28- "       var byte  = Stdin.readByte ()\n " 
29- "       if (handleChar(byte )) break\n " 
52+ "       var codepoint  = Stdin.readCodePoint ()\n " 
53+ "       if (handleChar(codepoint )) break\n " 
3054"       refreshLine(true)\n " 
3155"     }\n " 
3256"   }\n " 
3357" \n " 
34- "   handleChar(byte) {\n " 
58+ "   handleChar(codepoint) {\n " 
59+ "     var byte = codepoint.bytes[0]\n " 
60+ "     var codepoints\n " 
3561"     if (byte == Chars.ctrlC) {\n " 
3662"       System.print()\n " 
3763"       return true\n " 
@@ -72,14 +98,18 @@ static const char* replModuleSource =
7298"     } else if (byte == Chars.delete) {\n " 
7399"       deleteLeft()\n " 
74100"     } else if (byte >= Chars.space && byte <= Chars.tilde) {\n " 
75- "       insertChar(byte)\n " 
101+ "       insertCodePoint(codepoint)\n " 
102+ "     } else if (byte > 127) {\n " 
103+ "       insertCodePoint(codepoint)\n " 
76104"     } else if (byte == Chars.ctrlW) { // Handle Ctrl+w\n " 
77105"       // Delete trailing spaces\n " 
78- "       while (_cursor != 0 && _line[_cursor - 1] == \"  \" ) {\n " 
106+ "       codepoints = CodePointList.new(_line)\n " 
107+ "       while (_cursor != 0 && codepoints[_cursor - 1] == \"  \" ) {\n " 
79108"         deleteLeft()\n " 
80109"       }\n " 
110+ "       codepoints = CodePointList.new(_line)\n " 
81111"       // Delete until the next space\n " 
82- "       while (_cursor != 0 && _line [_cursor - 1] != \"  \" ) {\n " 
112+ "       while (_cursor != 0 && codepoints [_cursor - 1] != \"  \" ) {\n " 
83113"         deleteLeft()\n " 
84114"       }\n " 
85115"     } else {\n " 
@@ -90,6 +120,12 @@ static const char* replModuleSource =
90120"     return false\n " 
91121"   }\n " 
92122" \n " 
123+ "   insertCodePoint(cp) {\n " 
124+ "     var codePoints = CodePointList.new(_line)\n " 
125+ "     _line = codePoints[0..._cursor] + cp  + codePoints[_cursor..-1]\n " 
126+ "     _cursor = _cursor + 1\n " 
127+ "   }\n " 
128+ " \n " 
93129"   /// Inserts the character with [byte] value at the current cursor position.\n " 
94130"   insertChar(byte) {\n " 
95131"     var char = String.fromCodePoint(byte)\n " 
@@ -102,7 +138,8 @@ static const char* replModuleSource =
102138"     if (_cursor == 0) return\n " 
103139" \n " 
104140"     // Delete the character before the cursor.\n " 
105- "     _line = _line[0...(_cursor - 1)] + _line[_cursor..-1]\n " 
141+ "     var codePoints = CodePointList.new(_line)\n " 
142+ "     _line = codePoints[0...(_cursor - 1)] + codePoints[_cursor..-1]\n " 
106143"     _cursor = _cursor - 1\n " 
107144"   }\n " 
108145" \n " 
@@ -111,7 +148,8 @@ static const char* replModuleSource =
111148"     if (_cursor == _line.count) return\n " 
112149" \n " 
113150"     // Delete the character after the cursor.\n " 
114- "     _line = _line[0..._cursor] + _line[(_cursor + 1)..-1]\n " 
151+ "     var codePoints = CodePointList.new(_line)\n " 
152+ "     _line = codePoints[0..._cursor] + codePoints[(_cursor + 1)..-1]\n " 
115153"   }\n " 
116154" \n " 
117155"   handleEscapeBracket(byte) {\n " 
@@ -297,7 +335,8 @@ static const char* replModuleSource =
297335"     super()\n " 
298336"   }\n " 
299337" \n " 
300- "   handleChar(byte) {\n " 
338+ "   handleChar(codepoint) {\n " 
339+ "     var byte = codepoint.bytes[0]\n " 
301340"     if (byte == Chars.ctrlA) {\n " 
302341"       cursor = 0\n " 
303342"     } else if (byte == Chars.ctrlB) {\n " 
@@ -319,7 +358,7 @@ static const char* replModuleSource =
319358"       // TODO: ESC H and F to move to beginning and end of line. (Both ESC\n " 
320359"       // [ and ESC 0 sequences?)\n " 
321360"       // TODO: Ctrl-W delete previous word.\n " 
322- "       return super.handleChar(byte )\n " 
361+ "       return super.handleChar(codepoint )\n " 
323362"     }\n " 
324363" \n " 
325364"     return false\n " 
0 commit comments