1 /*******************************************************************
3 * A Lisp compiler
, written
in Prolog
5 * (builtin_lisp_functions
.pl
)
9 * This program provides some built
-in functionality
for the
10 * Lisp compiler
. It requires that the file lisp_compiler
.pl has
11 * already been successfully compiled
.
13 *******************************************************************/
17 :- ensure_loaded
(library
(writef
)).
18 :- use_module
(library
(lists
)).
22 once
(( List
= [Result
|_
]
23 ; lisp_error_description
(first_not_cons
, ErrMsg
),
24 throw
(error
(first_not_cons
(List
), context
(first
, ErrMsg
))) )).
27 once
(( List
= [_
|Result
]
28 ; lisp_error_description
(rest_not_cons
, ErrMsg
),
29 throw
(error
(first_not_cons
(List
), context
(first
, ErrMsg
))) )).
31 cons
(Item
, List
, Result
):-
39 eq(Item1
, Item2
, Result
):-
44 equalp
(Item1
, Item2
, Result
):-
50 plus
(Num1
, Num2
, Result
):-
51 Result is Num1
+ Num2
.
52 minus
(Num1
, Num2
, Result
):-
53 Result is Num1
- Num2
.
54 times(Num1
, Num2
, Result
):-
55 Result is Num1
* Num2
.
56 divide
(Num1
, Num2
, Result
):-
57 Result is Num1
/ Num2
.
60 lisp_not
(Boolean
, Result
):-
65 or(Bool1
, Bool2
, Result
):-
66 once
(Bool1 \
= [] ; Bool2 \
= [])
70 and(Bool1
, Bool2
, Result
):-
71 (Bool1 \
= [] , Bool2 \
= [])
76 lisp_apply
(FunctionObject
, Arguments
, Result
):-
77 % FunctionObject
= closure
(FormalArgs
, Body
, Environment
)
78 FunctionObject
= closure
(FormalArgs
, Body
, ClosureEnvironment
, Environment
)
79 -> % zip_with
(FormalArgs
, Arguments
, [Arg
, Val
, binding
(Arg
, [Val
|_
])]^true
, Bindings
),
80 maplist
(make_binding
, FormalArgs
, Arguments
, Bindings
),
81 append
(ClosureEnvironment
, Environment
, ActualEnvironment
),
82 % apply
(Body
, [[Bindings
|Environment
], Result
])
83 apply
(Body
, [[Bindings
|ActualEnvironment
], Result
])
84 ; FunctionObject
= function
(FunctionName
),
85 append
(Arguments
, [Result
], ArgumentsResult
),
86 Function
=.. [FunctionName
|ArgumentsResult
],
90 extract_variable_value
([Val
|Vals
], FoundVal
, Hole
):-
94 ; extract_variable_value
(Vals
, FoundVal
, Hole
).
97 lisp_call
(Function
, Result
):-
98 apply
(Function
, [Result
]).
104 setof
(sv
(Var
, Value
), special_var
(Var
, Value
), SVs
)
105 -> format
('Variable ~tValue~n~n'),
106 maplist
((sv
(Var2
, Value2
), format
('~w :~t~w~n', [Var2
, Value2
])), SVs
)
107 ; format
('No special variables~n').
111 setof
(sv
(Var
, Value
), special_var
(Var
, Value
), SVs
),
113 format
('Variable ~tValue~20|~n~n'),
114 % maplist
((sv
(Var2
, Value2
), format
('~w :~t~w~n', [Var2
, Value2
])), SVs
).
115 maplist
(show_one_special
, SVs
).
117 format
('No special variables~n').
119 show_one_special
(sv
(Var
, Value
)):-
120 format
('~w :~t~w~20|~n', [Var
, Value
]).