js.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ByteBuffer.h>
  27. #include <AK/NonnullOwnPtr.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/File.h>
  31. #include <LibJS/AST.h>
  32. #include <LibJS/Interpreter.h>
  33. #include <LibJS/Parser.h>
  34. #include <LibJS/Runtime/Object.h>
  35. #include <LibJS/Runtime/PrimitiveString.h>
  36. #include <LibJS/Runtime/Value.h>
  37. #include <stdio.h>
  38. bool dump_ast = false;
  39. String read_next_piece()
  40. {
  41. StringBuilder piece;
  42. int level = 0;
  43. do {
  44. if (level == 0)
  45. fprintf(stderr, "> ");
  46. else
  47. fprintf(stderr, ".%*c", 4 * level + 1, ' ');
  48. char* line = nullptr;
  49. size_t allocated_size = 0;
  50. ssize_t nread = getline(&line, &allocated_size, stdin);
  51. if (nread < 0) {
  52. if (errno == ESUCCESS) {
  53. // Explicit EOF; stop reading. Print a newline though, to make
  54. // the next prompt (or the shell prompt) appear on the next
  55. // line.
  56. fprintf(stderr, "\n");
  57. break;
  58. } else {
  59. perror("getline");
  60. exit(1);
  61. }
  62. }
  63. piece.append(line);
  64. auto lexer = JS::Lexer(line);
  65. for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
  66. switch (token.type()) {
  67. case JS::TokenType::BracketOpen:
  68. case JS::TokenType::CurlyOpen:
  69. case JS::TokenType::ParenOpen:
  70. level++;
  71. break;
  72. case JS::TokenType::BracketClose:
  73. case JS::TokenType::CurlyClose:
  74. case JS::TokenType::ParenClose:
  75. level--;
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. free(line);
  82. } while (level > 0);
  83. return piece.to_string();
  84. }
  85. void repl(JS::Interpreter& interpreter)
  86. {
  87. while (true) {
  88. String piece = read_next_piece();
  89. if (piece.is_empty())
  90. break;
  91. auto program = JS::Parser(JS::Lexer(piece)).parse_program();
  92. if (dump_ast)
  93. program->dump(0);
  94. auto result = interpreter.run(*program);
  95. printf("%s\n", result.to_string().characters());
  96. }
  97. }
  98. int main(int argc, char** argv)
  99. {
  100. bool gc_on_every_allocation = false;
  101. bool print_last_result = false;
  102. const char* script_path = nullptr;
  103. Core::ArgsParser args_parser;
  104. args_parser.add_option(dump_ast, "Dump the AST", "ast-dump", 'A');
  105. args_parser.add_option(print_last_result, "Print last result", "print-last-result", 'l');
  106. args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
  107. args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No);
  108. args_parser.parse(argc, argv);
  109. JS::Interpreter interpreter;
  110. interpreter.heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
  111. if (script_path == nullptr) {
  112. repl(interpreter);
  113. } else {
  114. auto file = Core::File::construct(script_path);
  115. if (!file->open(Core::IODevice::ReadOnly)) {
  116. fprintf(stderr, "Failed to open %s: %s\n", script_path, file->error_string());
  117. return 1;
  118. }
  119. auto file_contents = file->read_all();
  120. StringView source;
  121. if (file_contents.size() >= 2 && file_contents[0] == '#' && file_contents[1] == '!') {
  122. size_t i = 0;
  123. for (i = 2; i < file_contents.size(); ++i) {
  124. if (file_contents[i] == '\n')
  125. break;
  126. }
  127. source = StringView((const char*)file_contents.data() + i, file_contents.size() - i);
  128. } else {
  129. source = file_contents;
  130. }
  131. auto program = JS::Parser(JS::Lexer(source)).parse_program();
  132. if (dump_ast)
  133. program->dump(0);
  134. auto result = interpreter.run(*program);
  135. if (print_last_result)
  136. printf("%s\n", result.to_string().characters());
  137. }
  138. return 0;
  139. }