Skip to content

Commit

Permalink
(consoleapp) Remove REPL code
Browse files Browse the repository at this point in the history
As discussed in #406, the
REPL will go away for some time, until we can (at some point)
reimplement it on top of LLVM. At that point, the REPL will be
_dynamically emitting native code_, i.e. still not require a JIT
interpreter of any form. Based on some experiments I've done with LLVM,
this should be doable.

The `-e "<code-to-be-executed>"` will also be removed for now, but will
take that as a separate commit.
  • Loading branch information
perlun committed Mar 31, 2024
1 parent fc3bf3e commit 8922af7
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 1,583 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,6 @@ includes content from the NetBSD project, licensed under the 3-clause BSD
license. Copyright (c) 1980, 1991, 1993 The Regents of the University of
California. All rights reserved.

[src/Perlang.ConsoleApp/dependencies/Mono/Terminal/getline.cs](src/Perlang.ConsoleApp/dependencies/Mono/Terminal/getline.cs)
is based on Miguel de Icaza's [command line
editor](https://github.com/mono/mono/blob/main/mcs/tools/csharp/getline.cs),
dual-licensed under the terms of the MIT X11 license or the Apache License 2.0.
Copyright (c) 2008 Novell, Inc. Copyright (c) 2016 Xamarin Inc.

[src/stdlib/src/bigint.hpp](src/stdlib/src/bigint.hpp) includes content from
Syed Faheel Ahmad's `BigInt` library, available at
https://github.com/faheel/BigInt, licensed under the terms of the MIT license.
Expand Down
3 changes: 2 additions & 1 deletion release-notes/v0.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#### Data types

#### General
- Remove REPL code [[#446][446]]

#### Maintenance

Expand All @@ -17,4 +18,4 @@
### Tests

<!-- Kept as an example; remove this comment the first time you reference a pull request -->
[yyy]: https://github.com/perlang-org/perlang/pull/yyy
[446]: https://github.com/perlang-org/perlang/pull/446
97 changes: 5 additions & 92 deletions src/Perlang.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mono.Terminal;
using Perlang.Compiler;
using Perlang.Internal;
using Perlang.Interpreter;
Expand Down Expand Up @@ -224,13 +223,11 @@ public static int MainWithCustomConsole(string[] args, IPerlangConsole console)
}
else if (parseResult.Tokens.Count == 0)
{
new Program(
replMode: true,
standardOutputHandler: console.WriteStdoutLine,
disabledWarningsAsErrors: disabledWarningsAsErrorsList
).RunPrompt();
return Task.FromResult(0);
// TODO: Tried to fix this using some logic in the rootCommand.AddValidator() lambda, but I
// TODO: couldn't get it working. Since this is going to be rewritten in Perlang at some point
// TODO: anyway, let's not spend too much time thinking about it.
Console.Error.WriteLine("One of the -e <script>, -p <script> or <script-name> arguments must be provided");
return Task.FromResult(1);
}
else
{
Expand Down Expand Up @@ -362,83 +359,6 @@ private int RunFile(string path)
return (int)ExitCodes.SUCCESS;
}

private void RunPrompt()
{
PrintBanner();

// TODO: Should we use different history files for snapshots and release versions?
var lineEditor = new LineEditor("perlang");

lineEditor.AutoCompleteEvent += (a, pos) =>
{
var matchingKeywords = Scanner.ReservedKeywords
.Where(keyword => keyword.Key.StartsWith(a))
.Where(keyword => keyword.Value != TokenType.RESERVED_WORD)
.Select(keyword => keyword.Key)
.ToList();
matchingKeywords.AddRange(
ReplCommands.Where(cmd => cmd.StartsWith(a))
);
string prefix = String.Empty;
if (matchingKeywords.Count == 1)
{
return new LineEditor.Completion(prefix, new[] { matchingKeywords[0].Substring(pos) });
}
else
{
return new LineEditor.Completion(prefix, matchingKeywords.ToArray());
}
};

string command;

while ((command = lineEditor.Edit("> ", String.Empty)) != null)
{
if (command.ToLowerInvariant() == "/quit" || command.ToLowerInvariant() == "/exit")
{
break;
}
else if (command.ToLowerInvariant() == "/help")
{
ShowHelp();
continue;
}

// REPL mode is more relaxed: -Wno-error is enabled for all warnings by default. It simply makes sense
// to be less strict in this mode, since it's often used in an ad-hoc fashion for exploratory
// programming.
Run(command, CompilerWarningAsWarning);
}
}

private void ShowHelp()
{
standardOutputHandlerFromClrString(
"\n" +
"This is the Perlang interactive console (commonly called REPL, short for\n" +
"Read-Evaluate-Print-Loop). Any valid Perlang expression or statement can be\n" +
"entered here, and will be evaluated dynamically. For example, 10 + 5, 2 ** 32,\n" +
"or `print \"Hello, World\"`"
);

standardOutputHandler(Lang.String.Empty);

standardOutputHandlerFromClrString("The following special commands are also available:");
standardOutputHandlerFromClrString("\x1B[36;1m/quit\x1B[0m or \x1B[36;1m/exit\x1B[0m to quit the program.");
standardOutputHandlerFromClrString("\x1B[36;1m/help\x1B[0m to display this help.");
standardOutputHandler(Lang.String.Empty);

standardOutputHandlerFromClrString(
"For more information on the Perlang language, please consult this web page:\n" +
"https://perlang.org/learn. Thank you for your interest in the project! 🙏"
);

standardOutputHandler(Lang.String.Empty);
}

internal int Run(string source, CompilerWarningHandler compilerWarningHandler)
{
object? result = interpreter.Eval(source, ScanError, ParseError, NameResolutionError, ValidationError, ValidationError, compilerWarningHandler);
Expand Down Expand Up @@ -470,13 +390,6 @@ private void ParseAndPrint(string source)
standardOutputHandler(Lang.String.from(result));
}

private void PrintBanner()
{
standardOutputHandlerFromClrString($"Perlang Interactive REPL Console (\x1B[1m{CommonConstants.GetFullVersion()}\x1B[0m, built from git commit {CommonConstants.GitCommit})");
standardOutputHandlerFromClrString("Type \x1B[36;1m/help\x1B[0m for more information or \x1B[36;1m/quit\x1B[0m to quit the program.");
standardOutputHandler(Lang.String.Empty);
}

private void ScanError(ScanError scanError)
{
ReportError(scanError.Line, String.Empty, scanError.Message);
Expand Down

0 comments on commit 8922af7

Please sign in to comment.