~eshel/eshellisp

867db67f915f9c1dbf5f12e707a92e431248d09e — Eshel Yaron 2 years ago 4246e72
Formatting
3 files changed, 46 insertions(+), 25 deletions(-)

M Makefile
M prolog/eshellisp/eval.pl
M prolog/eshellisp/read.pl
M Makefile => Makefile +4 -3
@@ 1,11 1,12 @@
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)
SOURCES = $(shell find prolog/ -type f -name '*.pl')


.PHONY: all check install lint pack

all: check
all: $(PACKAGE)

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


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

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

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

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

install:

M prolog/eshellisp/eval.pl => prolog/eshellisp/eval.pl +4 -3
@@ 2,15 2,18 @@
          [ 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]) :-


@@ 46,9 49,7 @@ evaluate_form([Name | Args], File, layout(_, _, [PName | PArgs]), Vars, Value0, 
    ->  true
    ;   Value = exception(no_such_function(Name), File:PName)
    ).
evaluate_form(quote(E), _, _,
              _, _,
              quote(E)) :- !.
evaluate_form(quote(E), _, _, _, _, E) :- !.
evaluate_form(Number, _, _, _, _, Number) :-
    number(Number),
    !.

M prolog/eshellisp/read.pl => prolog/eshellisp/read.pl +38 -19
@@ 2,39 2,56 @@
          [ 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)) -->

token('(')  --> [40], !.
token(')')  --> [41], !.
token('\'') --> [39], !.
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], !.


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)) -->
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)) -->
form(list(List),
     position(StartLine, StartColumn, StartOffset),
     position(EndLine, EndColumn, EndOffset)) -->
    ['('],
    !,
    { C is StartColumn + 1,


@@ 47,7 64,9 @@ form(list(List), position(StartLine, StartColumn, StartOffset), position(EndLine
      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)) -->
form(quote(form(Exp, position(StartLine, C, O), position(EndLine, EndColumn, EndOffset))),
     position(StartLine, StartColumn, StartOffset),
     position(EndLine, EndColumn, EndOffset)) -->
    ['\''],
    !,
    { C is StartColumn + 1,


@@ 77,15 96,15 @@ forms([form(H, P0, P1)|T], P0, P) -->
forms([], P, P) --> [].


form_components(form(Value, P0, P), SExp, layout(P0, P, C)) :-
    value_components(Value, SExp, C).
form_components(form(Form, Start, End), Expression, layout(Start, End, Children)) :-
    form_components_(Form, Expression, Children).


value_components(list(List), SExps, C) :-
form_components_(list(List), Expressions, Children) :-
    !,
    maplist(form_components, List, SExps, C).
value_components(quote(Form), quote(SExp), [C]) :-
    maplist(form_components, List, Expressions, Children).
form_components_(quote(Quoted), quote(Expression), [Layout]) :-
    !,
    form_components(Form, SExp, C).
value_components(symbol(Value), Value, []) :- !.
value_components(number(Value), Value, []) :- !.
    form_components(Quoted, Expression, Layout).
form_components_(symbol(Symbol), Symbol, []) :- !.
form_components_(number(Number), Number, []) :- !.