=pod

=encoding utf8

=head1 NAME

Lingy - A Clojure Platform for Perl

=head1 SYNOPSIS

Run the Lingy REPL:

    $ lingy
    Lingy 0.1.12 [perl]

    user=> (p<TAB>
    pos?     println  prn      pr-str
    user=> (prn "Hello, world!")
    "Hello, world!"
    nil
    user=>

or a Lingy one-liner:

    $ lingy -e '(println "Hello, world!")'
    Hello, world!

or run a Lingy program file:

    $ echo '(println "Hello, world!")' > hello.ly
    $ lingy hello.ly
    Hello, world!

or run an example Lingy program:

    $ wget -q https://raw.githubusercontent.com/ingydotnet/lingy/main/eg/99-bottles.ly

    $ cat 99-bottles.ly
    (defn main [number]
      (let [
        paragraphs (map paragraph (range number 0 -1)) ]
        (map println paragraphs)))

    (defn paragraph [num]
      (str
        (bottles num) " of beer on the wall,\n"
        (bottles num) " of beer.\n"
        "Take one down, pass it around.\n"
        (bottles (dec num)) " of beer on the wall.\n"))

    (defn bottles [n]
      (cond
        (= n 0) "No more bottles"
        (= n 1) "1 bottle"
        :else (str n " bottles")))

    (main (nth *ARGV* 0 99))

    $ lingy 99-bottles.ly 3
    3 bottles of beer on the wall,
    3 bottles of beer.
    Take one down, pass it around.
    2 bottles of beer on the wall.

    2 bottles of beer on the wall,
    2 bottles of beer.
    Take one down, pass it around.
    1 bottle of beer on the wall.

    1 bottle of beer on the wall,
    1 bottle of beer.
    Take one down, pass it around.
    No more bottles of beer on the wall.

=head1 STATUS

Lingy is in ALPHA status.

=head1 DESCRIPTION

Lingy is an implementation of the Clojure Platform for Perl that is written in Perl and hosted by Perl. Programs and modules written in Lingy have full access to Perl and its CPAN modules.

Perl modules can be written in Lingy and distributed on CPAN. (In the future) Lingy code is compiled to a bytecode and should perform on the same order of magnitude as XS modules.

Since Lingy will be a complete Clojure implementation, it should be able to run programs written in Clojure and make use of libraries written in Clojure.

Clojure is a language that cleanly solves many of the problems of Java including making concurrency simple, and writing functional programs with mostly immutable data types. It is a Lisp dialect that is hosted by Java and compiles to JVM byte code. It has access to any libraries that target the JVM.

Much of the Clojure language is written in Clojure (self hosted) and Lingy actually uses the Clojure source code.

A variant of Clojure called ClojureScript uses the same Clojure source code but is hosted by JavaScript with full access to NPM modules. Lingy also intends to eventually be ported to and hosted by many other programming languages.

Lingy started as a Perl L<implementation|https://github.com/ingydotnet/mal/tree/perl.2/impls/perl.2> of the L<Make a Lisp|https://github.com/kanaka/mal> project. This provided a bare-bones Clojure-inspired Lisp interpreter from which Lingy has grown upon.

=head1 INSTALLATION

    cpanm Lingy

=head1 C<LINGY> CLI USAGE

The Lingy language installs a command-line program called C<lingy>. You can use this command to run Lingy programs, start a Lingy REPL or run Lingy one-liner expressions.

=over

=item * C<lingy --repl> (or just C<lingy>)

Starts a Lingy interactive REPL. The REPL has readline support that includes:

=over

=item * Command history

=item * CTL-R searching

=item * Parentheses match highlighting

=item * CTL-C to abort a command w/o leaving REPL

=back

Use CTL-D to exit the REPL

=item * C<lingy program.ly foo bar>

Run a Lingy program passing in arguments. Arguments are available in Lingy as C<*ARGV*>.

=item * C<cat program.ly | lingy - foo bar>

Run a Lingy program from STDIN and pass in arguments. The C<-> means run from STDIN instead of a file. If there are no arguments you can omit the C<->.

=item * C<lingy -e '(println "Hello" (nth *ARGV* 0))' world>

Run a Lingy one-liner with arguments.

When used with C<--repl>, run the C<-e> code first, then enter the REPL.

=back

=head2 C<LINGY> CLI OPTIONS

=over

=item * C<-e <string>>, C<--eval=<string>>

A Lingy string to evaluate.

=item * C<-r>, C<--repl>

Start a Lingy REPL. Can be used with C<-e>.

=item * C<--ppp>

Print the Lingy compiled AST for a C<-e> expression.

=item * C<--xxx>

YAML dump the Lingy compiled AST for a C<-e> expression.

=back

=head1 LINGY REPL USAGE

If you run C<lingy --repl> (or just C<lingy>) you will start a Lingy interactive REPL. You can run Lingy commands and see the output.

The REPL has command line history to save all your commands. It also has readline history search (ctl-r) and tab completion.

Note: Input lines that match the previous input line will not be saved in history. That way you can run the same command multiple times in a row and not clutter your history.

=head2 MULTILINE INPUT

The Lingy REPL will keep prompting you for more lines until you have completed a well formed expression. Usually that means until you have balanced the parentheses.

The history (up adn down arrow keys) will bring up the entire multiline form for you to edit.

If you want to enter multiple lines as one entry when each line is already well formed, just wrap the entry in a C<do> expression like so:

    user=> (do
      #_=> (prn 1)
      #_=> (prn 2)
      #_=> )
    1
    2
    nil

You can also paste multiline examples into the REPL and they will remain editable as multiline.

If you want to compress a multiline statement in your history to a single line, add a space to the end of it. Then (after you run it) it will be added to your history as a single line.

=head2 USING THE CLOJURE REPL IN THE LINGY REPL

If you have Clojure installed on your system and you run this command in the Lingy REPL: C<(clojure-repl-on)>, then every command you enter will be evaluated both by Lingy and Clojure. Run C<(clojure-repl-off)> to turn it off. Start the Lingy REPL with C<lingy --clj> to turn it on from the start.

Also if you run a command like C<;;;(source first)> it will only run on Clojure. The command is a comment to Lingy but the REPL will remove the C<;;;> and pass it to Clojure.

Using this feature is a great way to compare how Lingy and Clojure work. Eventually they should be very close to identical but currently Lingy is still a baby.

=head1 LINGY / PERL INTEROPERABILITY

Like Clojure and Java, Lingy and Perl are both ways interoperable.

Note: This section currently covers the basics, but more in depth content will be added later.

=head2 USING LINGY FROM PERL CODE

The Lingy.pm module lets you easily evaluate Lingy code from inside your Perl code.

    use Lingy;
    my $lingy = Lingy->new;     # Setup the Lingy environment
    my $result = $lingy->rep("(+ 1 2 3 4)");

=head3 Lingy Methods

The Lingy perl module supports the following methods:

=over

=item * C<my $lingy = Lingy->new();>

Create a new Lingy object.

This method takes no arguments.

The first time C<Lingy->new> is called it initializes the Lingy runtime environment (which is required to process Lingy expressions).

=item * C<my $result_string = $lingy->rep($lingy_source_code_string);>

The C<rep> method stands for C<Read>, C<Evaluate>, C<Print> which is the runtime process for running Lingy (or any Lisp) code. This is likely the most common method you will use.

Note: If you call this in a "Loop" you've created a "REPL" (Read, Evaluate, Print, Loop).

=back

Lingy also exposes each of the Read, Evaluate and Print methods to give you more control:

=over

=item * C<my $form = $lingy->read($lingy_source_code_string);>

Read a string containing a Lingy source expression and return the Lingy AST object (form). In list context, returns all the form objects read if more than one form is parsed.

=item * C<my $result_form = $lingy->eval($form);>

Evaluate a Lingy form object and return the resulting Lingy form object.

Use this in a Perl C<eval> block to catch any Lingy runtime errors.

=item * C<my $result_string = $lingy->print($form);>

Print a Lingy form object to a text string.

Usually this results in a Lingy expression that you can pass to another C<$lingy->read> method call if you want to.

Reading and Printing a Lingy string without Evaluating it in between, often produces the original string (or a semantically equivalent one). That's because Lisp (Lingy) is "homoiconic".

=back

=head2 USING PERL FROM LINGY CODE

Like Clojure, Lingy has a lot of functions/functionality for calling Perl code.

This is how Clojure and Lingy are implemented in that as much of the language as possible is written in itself (Lisp) but many functions need to call to the host language to do the work.

=over

=item * C<(perl "any perl source code string")>

This is the big hammer. Call Perl's C<eval> on any Perl code string and return the result.

The return value likely will not be a Lingy native object. If you save the result in a variable, don't expect that if will work like a native Lingy object. When the Lingy Printer sees a non-Lingy object it prints it as a YAML dump.

=item * C<(import YAML.PP.Perl)>

Load a Perl module. We are just using C<YAML::PP::Perl> as an example here. This is like C<use YAML::PP::Perl;> in Perl.

=item * C<(def y-pp (YAML.PP.Perl. "boolean" "JSON::PP"))>

Create a new C<YAML::PP::Perl> object. The C<.> after the module name is like calling the C<->new> in Perl.

=item * C<(def y-pp (.new YAML.PP.Perl "boolean" "JSON::PP"))>

Call a Perl class method with arguments. This is another way to instantiate an object like the previous example. The C<.xyz word at the start of an expression is like calling >->xyz` on the second word, which may be a class or an object.

=item * C<(println (.dump ypp '(+ 2 2)))>

Call a Perl method with arguments on a Perl instance object.

The example above will print the internal AST form of the Lingy expression C<(+ 2 2)>. This is a very useful way to debug Lingy.

=item * C<WWW>, C<XXX>, C<YYY> and C<ZZZ>

These Lingy functions from the C<lingy.devel> library, work like the C<XXX.pm> CPAN module, but from Lingy.

This can we very useful for seeing what Lingy is doing internally.

    (use 'lingy.devel) (XXX (perl "\\%INC"))  ; Print a YAML dump of Perl's %INC value.

=back

=head1 DIFFERENCES FROM CLOJURE

Lingy intends to be a proper "Clojure Platform"; a complete port of Clojure to Perl and other languages.

That said, differences must exist.

=over

=item * The Lingy platform host language is currently just Perl. It is the intent to extend the language set to Python and others soon.

=item * Lingy currently only supports a subset of Clojure libraries (most notably clojure.core). It is intended that Lingy will eventually use Clojure's libraries directly starting with clojure.core. As of this release Lingy can C<read-string> all of clojure.core and it converts the forms it fully supports into the lingy.core namespace.

=item * Lingy tries to map Clojure namespace and Java class symbols/files to Lingy namespace and Perl module/class symbols/files.

=over

=item * The C<clojure.core> namespace equivalent in Lingy is C<lingy.core> and maps to a file name called C<lib/Lingy/core.ly> in the Lingy source code.

=item * The C<lingy.lang.HashMap> symbol maps to the C<Lingy::HashMap> module (the C<lib/Lingy/HashMap.pm> file).

=item * More mapping details to be added here later.

=back

=item * The C<#?> reader macro uses the C<:lingy/pl> keyword to conditionally use forms that the Lingy/Perl platform supports.

=item * Special global vars (earmuffs) that are unique to Lingy:

=over

=item * C<*lingy-version*> Like C<*clojure-version*> but for Lingy's version.

=item * C<*LANG*> Set to C<"Lingy">.

=item * C<*HOST*> Set to C<"perl">.

=item * C<*clojure-repl*> Boolean (default C<false>) indicates whether Lingy REPL should also send input to a Clojure REPL.

=back

=item * Lingy error messages currently try to be close to the Clojure ones.

=over

=item * This isn't always possible or desirable.

=item * Error messages may be completely overhauled to give better info.

=back

=back

Certainly there are other differences and this section will be improved over time.

=head1 SEE ALSO

=over

=item * L<Clojure|https://clojure.org/>

=item * L<YAMLScript|https://metacpan.org/pod/YAMLScript>

=item * L<Test::More::YAMLScript|https://metacpan.org/pod/Test::More::YAMLScript>

=back

=head1 AUTHORS

=over

=item * Ingy döt Net L<ingy@ingy.net|mailto:ingy@ingy.net>

=back

=head1 COPYRIGHT AND LICENSE

Copyright 2023 by Ingy döt Net

This is free software, licensed under:

The MIT (X11) License

=cut
