1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

色々な言語で簡単なREPLを実装する #dart

Last updated at Posted at 2023-12-07

続きです。

REPLとは

こちらの記事を参照してください。

Dartで実装する

main.dart

import 'dart:io';

void main() {
  while (true) {
    stdout.write('dart> ');
    String? input = stdin.readLineSync();

    if (input == null || input.trim() == 'exit') {
      print('Bye!');
      break;
    }

    try {
      // var prompt = eval(input);
      var prompt = input.trim();
      print(prompt);
    } catch (e) {
      print('Error: $e');
    }
  }
}

dynamic eval(String input) {
  // なにかそれっぽい処理
  return input;
}

実行する

$ dart main.dart
dart> 1 + 1
1 + 1
dart> foobar
foobar
dart> exit
Bye!

evalのような動的評価は、その言語の設計思想次第であるかないかが分かれますね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?