~eshel/eshellisp

baf84c3c349f2f93f5bc091902dc691129b49904 — Eshel Yaron 2 years ago
Initial commit
A  => .build.yml +15 -0
@@ 1,15 @@
image: debian/stable
packages:
  - swi-prolog
  - scdoc
  - build-essential
environment:
  PACK_NAME: eshellisp
sources:
  - git@git.sr.ht:~eshel/eshellisp
tasks:
  - getdeps: |
      swipl -q -g "pack_install(diagnostics, [interactive(false)])" -t halt
  - check: |
      cd $PACK_NAME
      make check

A  => .gitignore +2 -0
@@ 1,2 @@
/eshellisp
/eshellisp-0.1.0.zip

A  => LICENSE +20 -0
@@ 1,20 @@
Copyright 2022 Eshel Yaron

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

A  => Makefile +25 -0
@@ 1,25 @@
PROLOG = swipl
PACKAGE = $(shell $(PROLOG) -s pack.pl -g "name(N)   , writeln(N)" -t halt)
VERSION = $(shell $(PROLOG) -s pack.pl -g "version(V), writeln(V)" -t halt)


.PHONY: all check install lint pack

all: check

lint:
	$(PROLOG) -q -g "use_module(library(diagnostics))" -t halt -- prolog/$(PACKAGE)/*.pl

check: lint $(PACKAGE)
	./$(PACKAGE) lisp/giga.eshellisp 2>&1 | grep -q 1073741824

$(PACKAGE):
	$(PROLOG) --no-pce --undefined=error -O -o eshellisp -c prolog/$(PACKAGE)/main.pl

pack: $(PACKAGE)-$(VERSION).zip

$(PACKAGE)-$(VERSION).zip:
	zip -r $@ pack.pl prolog README.md LICENSE

install:
	true

A  => README.md +10 -0
@@ 1,10 @@
# eshellisp
[![builds.sr.ht status](https://builds.sr.ht/~eshel/eshellisp.svg)](https://builds.sr.ht/~eshel/eshellisp?)

Lisp interpreter implemented as a SWI-Prolog `pack`.

## Contributing

For questions, requests, patches, please write to
[~eshel/dev@lists.sr.ht](mailto:~eshel/dev@lists.sr.ht) specifying the name of this repository in
mail subject, e.g. `[PATCH eshellisp] fix it`.

A  => lisp/giga.eshellisp +14 -0
@@ 1,14 @@
(define -- (n)
  (+ n (- 1)))

(define * (n m)
  (if (<= 2 n)
      (+ m (* (-- n) m))
    m))

(define ^ (m n)
  (if (<= 2 n)
      (* m (^ m (-- n)))
    m))

(^ 2 30)

A  => pack.pl +5 -0
@@ 1,5 @@
name(eshellisp).
version('0.1.0').
title('simple Lisp interpreter').
author('Eshel Yaron', 'eshelshay.yaron@gmail.com').
home('https://git.sr.ht/~eshel/eshellisp').

A  => prolog/eshellisp/eval.pl +67 -0
@@ 1,67 @@
:- module(eshellisp_eval,
          [ evaluate_program/6   % +Program, +File, +Layout, +Vars, +Value0, -Value
          ]).

:- use_module(read).
:- use_module(write).

:- dynamic defined/5.


evaluate_program(Forms, File, Layout, Vars, Value0, Value) :-
    evaluate_forms(Forms, File, Layout, Vars, Value0, Value, _).

evaluate_forms(_, _, _, _, exception(What, Where), exception(What, Where), []) :-
    !.
evaluate_forms([H|T], File, [PH|PT], Vars, Value0, Value, [Value1|RT]) :-
    !,
    evaluate_form(H, File, PH, Vars, Value0, Value1),
    evaluate_forms(T, File, PT, Vars, Value1, Value, RT).
evaluate_forms([], _, _, _, Value, Value, []).


evaluate_form(_, _, _, _, exception(What, Where), exception(What, Where)) :- !.
evaluate_form([define, Name, Args | Body], File, layout(_, _, [_, _, _ | PBody]), _, _, quote(Name)) :-
    !,
    retractall(defined(Name, _, _, _, _)),
    asserta(defined(Name, Args, Body, File, PBody)).
evaluate_form([if, Cond, Then, Else], File, layout(_, _, [_, PCond, PThen, PElse]), Vars, R0, R) :-
    !,
    evaluate_form(Cond, File, PCond, Vars, R0, R1),
    (   R1 == '#f'
    ->  Next = Else, PNext = PElse
    ;   Next = Then, PNext = PThen
    ),
    evaluate_form(Next, File, PNext, Vars, R1, R).
evaluate_form([Name | Args], File, layout(_, _, [PName | PArgs]), Vars, Value0, Value) :-
    !,
    evaluate_forms(Args, File, PArgs, Vars, Value0, Value1, Vals),
    (   defined(Name, A, Body, F, P)
    ->  (   maplist([Arg,Val,Arg=Val]>>true, A, Vals, Vars1)
        ->  append(Vars1, Vars, Vars2),
            evaluate_program(Body, F, P, Vars2, Value1, Value)
        ;   Value = Value1
        )
    ;   primitive(Name, Vals, Value)
    ->  true
    ;   Value = exception(no_such_function(Name), File:PName)
    ).
evaluate_form(quote(E), _, _,
              _, _,
              quote(E)) :- !.
evaluate_form(Number, _, _, _, _, Number) :-
    number(Number),
    !.
evaluate_form(Symbol, File, Layout, Vars, _, Value) :-
    atom(Symbol),
    !,
    (   memberchk(Symbol=Value, Vars)
    ->  true
    ;   Value = exception(no_such_symbol(Symbol), File:Layout)
    ).


primitive('-',  [A]   , R   ) :- !, R is -A.
primitive('+',  [A, B], R   ) :- !, R is A + B.
primitive('<=', [A, B], '#t') :- A =< B, !.
primitive('<=', [_, _], '#f') :- !.

A  => prolog/eshellisp/main.pl +34 -0
@@ 1,34 @@
#!/usr/bin/env swipl

:- use_module(read).
:- use_module(eval).
:- use_module(write).

:- initialization(main, main).

main(Argv) :-
    OptsSpec =
    [ [opt(help),
       default(false),
       type(boolean),
       shortflags([h]),
       longflags([help]),
       help('Print usage information')
      ]
    ],

    (   opt_parse(OptsSpec, Argv, Opts, [Path|_])
    ->  (   option(help(true), Opts)
        ->  show_help(OptsSpec)
        ;   setup_call_cleanup(open(Path, read, R),
                               read_program(R, Program, [positions(Positions)]),
                               close(R)),
            evaluate_program(Program, Path, Positions, [], '#f', Value),
            write_form(Value)
        )
    ;   show_help(OptsSpec)
    ).

show_help(OptsSpec) :-
    writeln('eshellisp [-h] PATH'),
    opt_help(OptsSpec, Help), write(Help).

A  => prolog/eshellisp/read.pl +94 -0
@@ 1,94 @@
:- module(eshellisp_read,
          [ read_program/3   % +Stream, -Program, +Options
          ]).

:- use_module(library(dcg/basics)).

read_program(Stream, SExps, Options) :-
    phrase_from_stream(tokens(Ts), Stream),
    phrase(forms(Ps, position(1, 0, 0), _), Ts),
    maplist(form_components, Ps, SExps, Positions),
    option(positions(Positions), Options, Positions).

token('(') --> `(`, !.
token(')') --> `)`, !.
token('\'') --> `'`, !. %'
token(number(Number, Len)) --> digits([H|T]), !, { length([H|T], Len), number_codes(Number, [H|T]) }.
token(symbol(Name)) --> string_without([10, 32, 39, 40, 41], [H|T]), !, { atom_codes(Name, [H|T]) }.
token(nl) --> [10], !.
token(ws) --> [32], !.

tokens([H|T]) --> token(H), !, tokens(T).
tokens([]) --> [].

form(symbol(Name), position(StartLine, StartColumn, StartOffset), position(StartLine, EndColumn, EndOffset)) -->
    [symbol(Name)],
    !,
    { atom_length(Name, Length),
      EndColumn is StartColumn + Length,
      EndOffset is StartOffset + Length
    }.
form(number(Number), position(StartLine, StartColumn, StartOffset), position(StartLine, EndColumn, EndOffset)) -->
    [number(Number, Length)],
    !,
    { EndColumn is StartColumn + Length,
      EndOffset is StartOffset + Length
    }.
form(list(List), position(StartLine, StartColumn, StartOffset), position(EndLine, EndColumn, EndOffset)) -->
    ['('],
    !,
    { C is StartColumn + 1,
      O is StartOffset + 1
    },
    forms(List, position(StartLine, C, O), position(L1, C1, O1)),
    [')'],
    !,
    { EndLine is L1,
      EndColumn is C1 + 1,
      EndOffset is O1 + 1
    }.
form(quote(form(Exp, position(StartLine, C, O), position(EndLine, EndColumn, EndOffset))), position(StartLine, StartColumn, StartOffset), position(EndLine, EndColumn, EndOffset)) -->
    ['\''],
    !,
    { C is StartColumn + 1,
      O is StartOffset + 1
    },
    form(Exp, position(StartLine, C, O), position(EndLine, EndColumn, EndOffset)).


forms(List, position(L0, C0, O0), position(L, C, O)) -->
    [ws],
    !,
    { C1 is C0 + 1,
      O1 is O0 + 1
    },
    forms(List, position(L0, C1, O1), position(L, C, O)).
forms(List, position(L0, _C0, O0), position(L, C, O)) -->
    [nl],
    !,
    { L1 is L0 + 1,
      O1 is O0 + 1
    },
    forms(List, position(L1, 0, O1), position(L, C, O)).
forms([form(H, P0, P1)|T], P0, P) -->
    form(H, P0, P1),
    !,
    forms(T, P1, P).
forms([], P, P) --> [].


:- det(form_components/3).

form_components(form(Value, P0, P), SExp, layout(P0, P, C)) :-
    value_components(Value, SExp, C).

:- det(value_components/3).

value_components(list(List), SExps, C) :-
    !,
    maplist(form_components, List, SExps, C).
value_components(quote(Form), quote(SExp), [C]) :-
    !,
    form_components(Form, SExp, C).
value_components(symbol(Value), Value, []) :- !.
value_components(number(Value), Value, []) :- !.

A  => prolog/eshellisp/write.pl +47 -0
@@ 1,47 @@
:- module(eshellisp_write,
          [ write_form/1   % +Value
          ]).

write_form(Value) :-
    print_message(information, eshellisp(Value)).

:- multifile prolog:message//1.

prolog:message(eshellisp(Value)) -->
    !,
    form(Value).

form(exception(What, Path:layout(position(Line, Column, _), _, _))) -->
    !,
    { absolute_file_name(Path, Abs) },
    [ 'Exception ~w at ~w:~w:~w'-[What, Abs, Line, Column] ].
form(quote(Value)) -->
    !,
    [ '\''-[] ],
    form(Value).
form(Symbol) -->
    { atom(Symbol) },
    !,
    [ Symbol-[] ].
form(Number) -->
    { number(Number) },
    !,
    [ '~w'-[Number] ].
form(List) -->
    { is_list(List) },
    !,
    [ '('-[] ],
    forms(List),
    [ ')'-[] ].

forms([A,B|T]) -->
    !,
    form(A),
    [ ' '-[] ],
    forms([B|T]).
forms([H]) -->
    !,
    form(H).
forms([]) -->
    !,
    [].