-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZLIB_LISP.abap
1838 lines (1699 loc) · 74.1 KB
/
ZLIB_LISP.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*&---------------------------------------------------------------------*
*& Include ZLIB_LISP
*& https://github.com/mydoghasworms/abap-lisp
*& Lisp interpreter written in ABAP
*& Copy and paste this code into a type I (include) program
*&---------------------------------------------------------------------*
*& Martin Ceronio, [email protected]
*& June 2015
*& MIT License (see below)
*&---------------------------------------------------------------------*
* The MIT License (MIT)
*
* Copyright (c) 2015 Martin Ceronio
*
* 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.
* Macro to simplify the definition of a native procedure
define _proc_meth.
methods &1
importing list type ref to lcl_lisp_element
returning value(result) type ref to lcl_lisp_element
raising lcx_lisp_eval_err.
end-of-definition.
* Macro that implements the logic for the comparison native
* procedures, where only the comparison operator differs
define _comparison.
data: cell type ref to lcl_lisp_element.
result = true.
data: carry type decfloat34.
if list->car->type ne lcl_lisp_element=>type_number.
eval_err( |{ list->car->value } is not a number| ).
endif.
cell = list->cdr.
carry = list->car->number.
while cell ne nil.
if cell->car->type ne lcl_lisp_element=>type_number.
eval_err( |{ list->car->value } is not a number| ).
endif.
if carry &1 cell->car->number.
result = nil.
exit.
endif.
carry = cell->car->number.
cell = cell->cdr.
endwhile.
end-of-definition.
*--------------------------------------------------------------------*
* EXCEPTIONS
*--------------------------------------------------------------------*
*----------------------------------------------------------------------*
* CLASS lcx_lisp_exception DEFINITION
*----------------------------------------------------------------------*
* General Lisp exception
*----------------------------------------------------------------------*
class lcx_lisp_exception definition inheriting from cx_dynamic_check.
public section.
data: message type string read-only.
methods: constructor importing message type string optional.
endclass. "lcx_lisp_exception DEFINITION
*----------------------------------------------------------------------*
* CLASS lcx_lisp_exception IMPLEMENTATION
*----------------------------------------------------------------------*
class lcx_lisp_exception implementation.
method constructor.
super->constructor( ).
me->message = message.
endmethod. "constructor
endclass. "lcx_lisp_exception IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcx_lisp_parse_err DEFINITION
*----------------------------------------------------------------------*
* Parse exception
*----------------------------------------------------------------------*
class lcx_lisp_parse_err definition inheriting from lcx_lisp_exception.
endclass. "lcx_lisp_parse_err DEFINITION
*----------------------------------------------------------------------*
* CLASS lcx_lisp_eval_err DEFINITION
*----------------------------------------------------------------------*
* Evaluation exception
*----------------------------------------------------------------------*
class lcx_lisp_eval_err definition inheriting from lcx_lisp_exception.
endclass. "lcx_lisp_eval_err DEFINITION
class lcl_lisp_environment definition deferred.
* Single element that will capture cons cells, atoms etc.
*----------------------------------------------------------------------*
* CLASS lcl_lisp_element DEFINITION
*----------------------------------------------------------------------*
class lcl_lisp_element definition.
public section.
* Type definitions for the various elements
types: tv_type type char1.
class-data: type_symbol type tv_type value 'S'.
class-data: type_number type tv_type value 'N'.
class-data: type_string type tv_type value '"'.
class-data: type_conscell type tv_type value 'C'.
class-data: type_lambda type tv_type value 'λ'.
class-data: type_native type tv_type value 'P'.
class-data: type_hash type tv_type value 'H'.
* Types for ABAP integration:
class-data: type_abap_data type tv_type value 'D'.
class-data: type_abap_table type tv_type value 'T'.
class-data: type_abap_function type tv_type value 'F'.
class-data: type_abap_class type tv_type value 'R'.
data: type type char1.
data: value type string.
data: number type decfloat34.
* For ABAP integration
data: data type ref to data.
* Specifically for cons cells:
data: car type ref to lcl_lisp_element. "Contents of Address portion of Register
data: cdr type ref to lcl_lisp_element. "Contents of Decrement portion of Register
* Specifically for lambdas:
data: environment type ref to lcl_lisp_environment.
methods: constructor importing type type tv_type.
endclass. "lcl_lisp_element DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_lisp_hash DEFINITION
*----------------------------------------------------------------------*
* Hash is a specialized ABAP Lisp type for quick lookup of elements
* using a symbol or string key (backed by an ABAP hash table)
*----------------------------------------------------------------------*
class lcl_lisp_hash definition inheriting from lcl_lisp_element.
public section.
types: begin of ts_hash,
key type string,
element type ref to lcl_lisp_element,
end of ts_hash.
types: tt_hash type hashed table of ts_hash with unique key key.
data: hash type tt_hash.
endclass. "lcl_lisp_abapfunction DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_lisp_abapfunction DEFINITION
*----------------------------------------------------------------------*
* Specialized element representing an ABAP function module that can
* be called
*----------------------------------------------------------------------*
class lcl_lisp_abapfunction definition inheriting from lcl_lisp_element.
public section.
data: parameters type abap_func_parmbind_tab.
data: exceptions type abap_func_excpbind_tab.
* List of actual parameters passed
data: paramact type abap_func_parmbind_tab.
endclass. "lcl_lisp_abapfunction DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_lisp_element IMPLEMENTATION
*----------------------------------------------------------------------*
class lcl_lisp_element implementation.
method constructor.
me->type = type.
endmethod. "constructor
endclass. "lcl_lisp_element IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_lisp_environment DEFINITION
*----------------------------------------------------------------------*
class lcl_lisp_environment definition.
public section.
types: begin of ts_map,
symbol type string,
value type ref to lcl_lisp_element,
end of ts_map.
types: tt_map type hashed table of ts_map with unique key symbol.
data: map type tt_map.
* Reference to outer (parent) environment:
data: outer type ref to lcl_lisp_environment.
methods:
* Find a value in the environment
find importing symbol type any
returning value(cell) type ref to lcl_lisp_element
raising lcx_lisp_eval_err,
* Add a value to the (local) environment
define
importing symbol type string
element type ref to lcl_lisp_element,
* Convenience method to add a value and create the cell
define_value
importing symbol type string
type type lcl_lisp_element=>tv_type
value type any optional
returning value(element) type ref to lcl_lisp_element.
endclass. "lcl_lisp_environment DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_lisp_environment IMPLEMENTATION
*----------------------------------------------------------------------*
class lcl_lisp_environment implementation.
method find.
data: ls_map type ts_map.
read table map into ls_map with key symbol = symbol.
if sy-subrc = 0.
cell = ls_map-value.
else.
* Try locate the symbol in the parent (outer) environment
if outer is bound.
cell = outer->find( symbol ).
endif.
endif.
if cell is not bound.
raise exception type lcx_lisp_eval_err
exporting
message = |Symbol { symbol } is undefined|.
endif.
endmethod. "find
method define_value.
data: ls_map type ts_map.
ls_map-symbol = symbol.
create object ls_map-value
exporting
type = type.
* ls_map-value->type = type.
if type = lcl_lisp_element=>type_number.
ls_map-value->number = value.
else.
ls_map-value->value = value.
endif.
* TODO: reuse DEFINE() here
insert ls_map into table map.
* To comply with Scheme define, overwrite existing defined values
if sy-subrc = 4.
modify table map from ls_map.
endif.
element = ls_map-value.
endmethod. "define_cell
method define.
data: ls_map type ts_map.
ls_map-symbol = symbol.
ls_map-value = element.
insert ls_map into table map.
* To comply with Scheme define, overwrite existing defined values
if sy-subrc = 4.
modify table map from ls_map.
endif.
endmethod. "define
endclass. "lcl_lisp_environment IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_lisp_interpreter DEFINITION
*----------------------------------------------------------------------*
class lcl_lisp_interpreter definition.
public section.
data: code type string read-only.
data: length type i.
data: index type i.
data: char type char1.
data: env type ref to lcl_lisp_environment. "Global environment
types: tt_element type standard table of ref to lcl_lisp_element with default key.
methods:
constructor,
next_char raising lcx_lisp_parse_err,
skip_whitespace raising lcx_lisp_parse_err,
parse_list
returning value(list) type ref to lcl_lisp_element
raising lcx_lisp_parse_err,
parse_token
returning value(element) type ref to lcl_lisp_element
raising lcx_lisp_parse_err,
parse
importing code type clike
returning value(elements) type tt_element
raising lcx_lisp_parse_err.
* Methods for evaluation
methods:
eval
importing element type ref to lcl_lisp_element
environment type ref to lcl_lisp_environment
returning value(result) type ref to lcl_lisp_element
raising lcx_lisp_eval_err,
* To enable a REPL, the following convenience method wraps parsing and evaluating
* and stringifies the response/error
eval_source
importing code type clike
returning value(response) type string
raising lcx_lisp_eval_err,
to_string
importing element type ref to lcl_lisp_element
returning value(str) type string
raising lcx_lisp_eval_err.
* Native functions:
_proc_meth:
proc_append, ##called
proc_car, ##called
proc_cdr, ##called
proc_cons, ##called
proc_length, ##called
proc_list, ##called
proc_nilp, ##called
proc_add, ##called
proc_subtract, ##called
proc_multiply, ##called
proc_divide, ##called
proc_gt, ##called
proc_gte, ##called
proc_lt, ##called
proc_lte, ##called
proc_eql, ##called
* Not in the spec: Just adding it anyway
proc_equal. ##called
* Functions for dealing with hashes:
_proc_meth:
proc_make_hash, ##called "Create new hash
proc_hash_get, ##called "Get an element from a hash
proc_hash_insert, ##called "Insert a new element into a hash
proc_hash_remove, ##called "Delete an item from a hash
proc_hash_keys. ##called "Delete an item from a hash
* Built-in functions for ABAP integration:
_proc_meth:
proc_abap_data, ##called
proc_abap_function, ##called
* proc_abap_function_param,##called
proc_abap_table, ##called
proc_abap_append_row, ##called
proc_abap_delete_row, ##called
proc_abap_get_row, ##called
proc_abap_get_value, ##called
proc_abap_set_value, ##called
proc_abap_set, ##called
proc_abap_get, ##called
* Called internally only:
proc_abap_function_call. ##called
* true, false and nil
class-data: nil type ref to lcl_lisp_element read-only.
class-data: true type ref to lcl_lisp_element read-only.
* data: false type ref to lcl_lisp_element read-only.
protected section.
* TODO: Could incorporate this logic directly before evaluation a proc/lambda
* and do away with this method
class-methods: eval_err importing message type string
raising lcx_lisp_eval_err.
*---- ABAP Integration support functions; mapping -----
class-methods:
* Convert ABAP data to Lisp element
data_to_element importing value(data) type any
returning value(element) type ref to lcl_lisp_element
raising lcx_lisp_eval_err,
* Convert Lisp element to ABAP Data
element_to_data importing value(element) type ref to lcl_lisp_element
changing value(data) type any "ref to data
raising lcx_lisp_eval_err.
* Determine an ABAP data component from an element and an identifier
class-methods:
get_element importing value(element) type ref to lcl_lisp_element
value(identifier) type ref to lcl_lisp_element
returning value(rdata) type ref to data
raising lcx_lisp_eval_err.
endclass. "lcl_lisp_interpreter DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_lisp_interpreter IMPLEMENTATION
*----------------------------------------------------------------------*
class lcl_lisp_interpreter implementation.
method constructor.
create object env.
* Create nil, true and false values
nil = env->define_value( symbol = 'nil' type = lcl_lisp_element=>type_symbol value = 'nil' ).
true = env->define_value( symbol = 'true' type = lcl_lisp_element=>type_symbol value = 'true' ).
* Add native functions to environment
env->define_value( symbol = '+' type = lcl_lisp_element=>type_native value = 'PROC_ADD' ).
env->define_value( symbol = '-' type = lcl_lisp_element=>type_native value = 'PROC_SUBTRACT' ).
env->define_value( symbol = '*' type = lcl_lisp_element=>type_native value = 'PROC_MULTIPLY' ).
env->define_value( symbol = '/' type = lcl_lisp_element=>type_native value = 'PROC_DIVIDE' ).
env->define_value( symbol = 'append' type = lcl_lisp_element=>type_native value = 'PROC_APPEND' ).
env->define_value( symbol = 'list' type = lcl_lisp_element=>type_native value = 'PROC_LIST' ).
env->define_value( symbol = 'length' type = lcl_lisp_element=>type_native value = 'PROC_LENGTH' ).
env->define_value( symbol = 'car' type = lcl_lisp_element=>type_native value = 'PROC_CAR' ).
env->define_value( symbol = 'cdr' type = lcl_lisp_element=>type_native value = 'PROC_CDR' ).
env->define_value( symbol = 'cons' type = lcl_lisp_element=>type_native value = 'PROC_CONS' ).
env->define_value( symbol = 'nil?' type = lcl_lisp_element=>type_native value = 'PROC_NILP' ).
env->define_value( symbol = 'null?' type = lcl_lisp_element=>type_native value = 'PROC_NILP' ).
env->define_value( symbol = '>' type = lcl_lisp_element=>type_native value = 'PROC_GT' ).
env->define_value( symbol = '>=' type = lcl_lisp_element=>type_native value = 'PROC_GTE' ).
env->define_value( symbol = '<' type = lcl_lisp_element=>type_native value = 'PROC_LT' ).
env->define_value( symbol = '<=' type = lcl_lisp_element=>type_native value = 'PROC_LTE' ).
env->define_value( symbol = '=' type = lcl_lisp_element=>type_native value = 'PROC_EQL' ). "Math equal
env->define_value( symbol = 'equal?' type = lcl_lisp_element=>type_native value = 'PROC_EQUAL' ).
* Hash-related functions
env->define_value( symbol = 'make-hash' type = lcl_lisp_element=>type_native value = 'PROC_MAKE_HASH' ).
env->define_value( symbol = 'hash-get' type = lcl_lisp_element=>type_native value = 'PROC_HASH_GET' ).
env->define_value( symbol = 'hash-insert' type = lcl_lisp_element=>type_native value = 'PROC_HASH_INSERT' ).
env->define_value( symbol = 'hash-remove' type = lcl_lisp_element=>type_native value = 'PROC_HASH_REMOVE' ).
env->define_value( symbol = 'hash-keys' type = lcl_lisp_element=>type_native value = 'PROC_HASH_KEYS' ).
* Functions for type:
env->define_value( symbol = 'string?' type = lcl_lisp_element=>type_native value = 'PROC_IS_STRING' ).
env->define_value( symbol = 'hash?' type = lcl_lisp_element=>type_native value = 'PROC_IS_HASH' ).
env->define_value( symbol = 'number?' type = lcl_lisp_element=>type_native value = 'PROC_IS_NUMBER' ).
env->define_value( symbol = 'symbol?' type = lcl_lisp_element=>type_native value = 'PROC_IS_SYMBOL' ).
env->define_value( symbol = 'type' type = lcl_lisp_element=>type_native value = 'PROC_IS_TYPE' ).
* Native functions for ABAP integration
env->define_value( symbol = 'ab-data' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_DATA' ).
env->define_value( symbol = 'ab-function' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_FUNCTION' ).
env->define_value( symbol = 'ab-func-param' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_FUNCTION_PARAM' ).
env->define_value( symbol = 'ab-table' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_TABLE' ).
env->define_value( symbol = 'ab-append-row' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_APPEND_ROW' ).
env->define_value( symbol = 'ab-delete-row' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_DELETE_ROW' ).
env->define_value( symbol = 'ab-get-row' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_GET_ROW' ).
env->define_value( symbol = 'ab-get-value' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_GET_VALUE' ).
env->define_value( symbol = 'ab-set-value' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_SET_VALUE' ).
env->define_value( symbol = 'ab-get' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_GET' ).
env->define_value( symbol = 'ab-set' type = lcl_lisp_element=>type_native value = 'PROC_ABAP_SET' ).
* Define a value in the environment for SYST
data: lr_sy type ref to lcl_lisp_element.
create object lr_sy exporting type = lcl_lisp_element=>type_abap_data.
get reference of syst into lr_sy->data.
env->define( symbol = 'ab-sy' element = lr_sy ).
endmethod. "constructor
*--------------------------------------------------------------------*
* PARSING FUNCTIONS
*--------------------------------------------------------------------*
method next_char.
index = index + 1.
if index < length.
char = code+index(1).
elseif index = length.
char = space.
elseif index > length.
* Unexpected end reached; exception
raise exception type lcx_lisp_parse_err.
endif.
endmethod. "next_char
method skip_whitespace.
while char = ' ' or char = cl_abap_char_utilities=>newline or
char = cl_abap_char_utilities=>cr_lf(1).
next_char( ).
endwhile.
endmethod. "skip_whitespace
method parse_list.
data: lr_cell type ref to lcl_lisp_element.
data: lv_empty_list type boole_d value abap_true.
create object list
exporting
type = lcl_lisp_element=>type_conscell.
* list->type = lcl_lisp_element=>type_conscell.
lr_cell = list. "Set pointer to start of list
* list = lr_cell. "Pointer to start of list
next_char( ). "Skip past opening paren
while index < length.
skip_whitespace( ).
if char = ')'.
if lv_empty_list = abap_true.
list = nil. "Result = empty list = NIL
else.
lr_cell->cdr = nil. "Terminate list
endif.
next_char( ). "Skip past closing paren
return.
endif.
if lv_empty_list = abap_false.
* On at least the second item; add new cell and move pointer
create object lr_cell->cdr
exporting
type = lcl_lisp_element=>type_conscell.
* list->type = lcl_lisp_element=>type_conscell.
lr_cell = lr_cell->cdr.
endif.
lv_empty_list = abap_false. "Next char was not closing paren
lr_cell->car = parse_token( ).
endwhile.
endmethod. "parse_list
method parse_token.
skip_whitespace( ).
data: sval type string.
"create object cell.
if char = '('.
element = parse_list( ).
elseif char = ''''. "Quoted element
* ' is just a shortcut for QUOTE, so we wrap the consecutive element in a list starting with the quote symbol
* so that when it is evaluated later, it returns the quote elements unmodified
create object element
exporting
type = lcl_lisp_element=>type_conscell.
create object element->car
exporting
type = lcl_lisp_element=>type_symbol.
element->car->value = 'quote'.
create object element->cdr
exporting
type = lcl_lisp_element=>type_conscell.
element->cdr->cdr = nil.
next_char( ). "Skip past single quote
element->cdr->car = parse_token( ).
elseif char = '"'.
data: pchar type char1.
next_char( ). "Skip past opening quote
while index < length.
if char = '"' and pchar ne '\'.
exit.
endif.
concatenate sval char into sval respecting blanks.
* sval = |{ sval }{ char }|.
pchar = char.
next_char( ).
endwhile.
next_char( ). "Skip past closing quote
create object element
exporting
type = lcl_lisp_element=>type_string.
element->value = sval.
return.
else.
* Run to delimiter
while index < length.
sval = |{ sval }{ char }|.
next_char( ).
if char = ')' or char = ' ' or char = '('
or char = cl_abap_char_utilities=>newline
or char = cl_abap_char_utilities=>cr_lf(1).
exit.
endif.
endwhile.
* Return atom value:
* Check whether the token can be converted to a float, to cover all
* manner of number formats, including scientific, otherwise treat it
* as a symbol (but we still store it as a string to preserve the original value
* and let the ABAP kernel do the heavy lifting later on)
data: lv_num type decfloat34.
condense sval.
try.
lv_num = sval. "If this passes, it's a number
create object element
exporting
type = lcl_lisp_element=>type_number.
element->number = sval.
catch cx_sy_conversion_no_number.
create object element
exporting
type = lcl_lisp_element=>type_symbol.
element->value = sval.
endtry.
endif.
endmethod. "parse_token
* Entry point for parsing code. This is not thread-safe, but as an ABAP
* process does not have the concept of threads, we are safe :-)
method parse.
data: element type ref to lcl_lisp_element.
me->code = code.
length = strlen( code ).
if length = 0.
append nil to elements.
return.
endif.
index = 0.
char = code+index(1). "Kick off things by reading first char
while index < length.
skip_whitespace( ).
if char = '('.
element = parse_list( ).
else.
element = parse_token( ).
endif.
append element to elements.
endwhile.
endmethod. "parse
**********************************************************************
*
*------------------------------- EVAL( ) ----------------------------
*
**********************************************************************
method eval.
* Return predefined symbols as themselves to save having to look them
* up in the environment
if element = nil. result = nil. return. endif.
if element = true. result = true. return. endif.
case element->type.
when lcl_lisp_element=>type_number or lcl_lisp_element=>type_string.
result = element. "Number or string evaluates to itself
when lcl_lisp_element=>type_symbol. "Symbol
result = environment->find( element->value ). "Look up symbol in environment
*---### EVAL LIST
when lcl_lisp_element=>type_conscell. "Cons Cell = List
* To evaluate list, we must first evaluate head value
data: lr_head type ref to lcl_lisp_element.
data: lr_tail type ref to lcl_lisp_element.
* Evaluate first element of list to determine if it is a native procedure or lambda
lr_head = element->car. "Unevaluated value
lr_tail = element->cdr.
*--- QUOTE
if lr_head->value = 'quote'. "Return the argument to quote unevaluated
* Ensure QUOTE takes a single argument
if lr_tail->cdr ne nil.
eval_err( |QUOTE can only take a single argument| ).
endif.
result = lr_tail->car.
*--- IF
elseif lr_head->value = 'if'.
if eval( element = lr_tail->car environment = environment ) ne nil.
result = eval( element = lr_tail->cdr->car environment = environment ).
else.
if lr_tail->cdr->cdr = nil.
result = nil.
else.
result = eval( element = lr_tail->cdr->cdr->car environment = environment ).
endif.
endif.
*--- DEFINE
elseif lr_head->value = 'define'.
* Assign symbol
if lr_tail->car->type = lcl_lisp_element=>type_symbol.
environment->define( symbol = lr_tail->car->value
element = eval( element = lr_tail->cdr->car environment = environment ) ).
create object result
exporting
type = lcl_lisp_element=>type_symbol.
result->value = lr_tail->car->value.
* Function shorthand (define (id arg ... ) body ...+)
elseif lr_tail->car->type = lcl_lisp_element=>type_conscell.
* define's function shorthand allows us to define a function by specifying a list as the
* first argument where the first element is a symbol and consecutive elements are arguments
create object result
exporting
type = lcl_lisp_element=>type_lambda.
result->car = lr_tail->car->cdr. "List of params following function symbol
result->cdr = lr_tail->cdr.
result->environment = environment.
* Add function to the environment with symbol
environment->define( symbol = lr_tail->car->car->value
element = result ).
* TODO: Here and above: Scheme does not return a value for define; should we?
create object result
exporting
type = lcl_lisp_element=>type_symbol.
result->value = lr_tail->car->car->value.
else.
eval_err( |{ to_string( lr_tail->car ) } cannot be a variable identifier| ).
endif.
*--- LAMBDA
elseif lr_head->value = 'lambda'.
* The lambda is a special cell that stores a pointer to a list of parameters and a pointer
* to a list which is the body to be evaluated later on
create object result
exporting
type = lcl_lisp_element=>type_lambda.
result->car = lr_tail->car. "List of parameters
result->cdr = lr_tail->cdr. "Body
* Store the reference to the environment in which the lambda was created
* This is necessary, because if the lambda is created inside another lambda, we want that environment
* to be present when we evaluate the new lambda
result->environment = environment.
*--- BEGIN
elseif lr_head->value = 'begin'.
data: lr_ptr type ref to lcl_lisp_element.
lr_ptr = lr_tail.
do.
result = eval( element = lr_ptr->car environment = environment ).
if lr_ptr->cdr = nil.
exit.
endif.
lr_ptr = lr_ptr->cdr.
enddo.
*--- NATIVE PROCEDURES AND LAMBDAS
else.
* Other symbols at the start of the list must be evaluated first
* The evaluated head must be either a native procedure or lambda
lr_head = eval( element = element->car environment = environment ).
* Before execution of the procedure or lambda, all parameters must be evaluated
data: lr_args type ref to lcl_lisp_element. "Argument
data: lr_arg_source type ref to lcl_lisp_element.
data: lr_arg_target type ref to lcl_lisp_element.
* TODO: Need to review whether the way I am evaluating the tail is correct...
if lr_tail = nil or lr_tail->car = nil.
lr_args = nil.
else.
create object lr_args
exporting
type = lcl_lisp_element=>type_conscell.
lr_arg_source = lr_tail.
lr_arg_target = lr_args.
do.
lr_arg_target->car = eval( element = lr_arg_source->car environment = environment ).
if lr_arg_source->cdr = nil.
lr_arg_target->cdr = nil.
exit.
endif.
lr_arg_source = lr_arg_source->cdr.
create object lr_arg_target->cdr
exporting
type = lcl_lisp_element=>type_conscell.
lr_arg_target = lr_arg_target->cdr.
enddo.
endif.
***--- NATIVE FUNCTION
if lr_head->type = lcl_lisp_element=>type_native.
* Evaluate native function:
call method (lr_head->value)
exporting
list = lr_args
receiving
result = result.
***--- LAMBDA FUNCTION
elseif lr_head->type = lcl_lisp_element=>type_lambda.
* The lambda receives its own local environment in which to execute,
* where parameters become symbols that are mapped to the corresponding arguments
data: lr_env type ref to lcl_lisp_environment.
create object lr_env.
lr_env->outer = lr_head->environment.
* Assign each argument to its corresponding symbol in the newly created environment
data: lr_par type ref to lcl_lisp_element. "Parameter
data: lr_arg type ref to lcl_lisp_element. "Argument
lr_par = lr_head->car. "Pointer to formal parameters
lr_arg = lr_args. "Pointer to arguments
if lr_par ne nil. "Nil would mean no parameters to map
do.
* NOTE: Each element of the argument list is evaluated before being defined in the environment
lr_env->define( symbol = lr_par->car->value element = lr_arg->car ).
if lr_par->cdr = nil.
exit.
endif.
lr_par = lr_par->cdr.
lr_arg = lr_arg->cdr.
if lr_arg = nil. "Premature end of arguments
eval_err( |Parameter mismatch| ). "TODO: Details on parameter mismatch
endif.
enddo.
endif.
* Evaluate lambda
lr_ptr = lr_head->cdr.
do.
result = eval( element = lr_ptr->car environment = lr_env ).
if lr_ptr->cdr = nil.
exit.
endif.
lr_ptr = lr_ptr->cdr.
enddo.
* >>> TEST: Support evaluation of ABAP function directly
elseif lr_head->type = lcl_lisp_element=>type_abap_function.
* Recompose as if calling a PROC (which we are). This is part of the test. If we make an ABAP function
* call first-class, then we would need to revisit evaluating the whole of ELEMENT in one shot
data: lr_funcinput type ref to lcl_lisp_element.
create object lr_funcinput
exporting
type = lcl_lisp_element=>type_conscell.
lr_funcinput->car = lr_head.
lr_funcinput->cdr = lr_tail.
result = proc_abap_function_call( lr_funcinput ).
* <<< TEST
else.
eval_err( |Cannot evaluate { to_string( lr_head ) } - not a function| ).
endif.
endif.
endcase.
if result is not bound.
* In theory this should never happen!
eval_err( 'EVAL( ) came up empty-handed' ).
endif.
endmethod. "eval
method to_string.
case element->type.
when lcl_lisp_element=>type_lambda.
str = '<lambda>'.
* TODO: Other Lisp REPLs give back the string as a quoted string
when lcl_lisp_element=>type_symbol or lcl_lisp_element=>type_string.
str = element->value.
when lcl_lisp_element=>type_number.
str = element->number.
when lcl_lisp_element=>type_native.
str = '<native>'.
when lcl_lisp_element=>type_conscell.
data: lr_elem type ref to lcl_lisp_element.
str = '( '. "Opening paren
lr_elem = element.
do.
* If the next item is not a cons cell, indicate with dot notation
if lr_elem->type ne lcl_lisp_element=>type_conscell.
str = |{ str } . { to_string( lr_elem ) }|.
else.
str = |{ str } { to_string( lr_elem->car ) }|.
endif.
if lr_elem->cdr is not bound or lr_elem->cdr = nil.
exit.
endif.
lr_elem = lr_elem->cdr. "Next element in list
enddo.
str = |{ str } )|. "Closing paren
when lcl_lisp_element=>type_hash.
str = '<hash>'.
*--------------------------------------------------------------------*
* Additions for ABAP Types:
when lcl_lisp_element=>type_abap_function.
str = |<ABAP function module { element->value }>|.
when lcl_lisp_element=>type_abap_data.
str = |<ABAP Data>|.
when lcl_lisp_element=>type_abap_table.
str = |<ABAP Table>|.
when lcl_lisp_element=>type_abap_class.
str = |<ABAP Class>|.
endcase.
endmethod. "to_string
method eval_source.
data: lr_err type ref to lcx_lisp_exception.
data: lr_element type ref to lcl_lisp_element.
data: lt_element type lcl_lisp_interpreter=>tt_element.
data: lx_root type ref to cx_root.
try.
lt_element = parse( code ).
loop at lt_element into lr_element.
lr_element = eval( element = lr_element environment = env ).
response = |{ response }{ to_string( lr_element ) } |.
endloop.
catch lcx_lisp_parse_err into lr_err.
response = |Parse: { lr_err->message }|.
catch lcx_lisp_eval_err into lr_err.
response = |Eval: { lr_err->message }|.
catch lcx_lisp_exception into lr_err.
response = lr_err->message.
if response is initial.
response = 'Error in processing'.
endif.
catch cx_root into lx_root.
response = lx_root->get_text( ).
endtry.
endmethod. "eval_source
method eval_err.
raise exception type lcx_lisp_eval_err
exporting
message = message.
endmethod. "eval_err
**********************************************************************
* NATIVE PROCEDURES
**********************************************************************
method proc_append.
* Takes two parameters: the first must be a list, and the second can
* be of any type. Appends the second param to the first. But if the
* last element in the list is not a cons cell, we cannot append
if list->car->type ne lcl_lisp_element=>type_conscell.
eval_err( |{ to_string( list->car ) } is not a list| ).
endif.
* Get to last element in list - this can make APPEND expensive, like LENGTH
data: lr_cell type ref to lcl_lisp_element.
lr_cell = list->car.
while lr_cell->cdr is bound and lr_cell->cdr ne nil.
lr_cell = lr_cell->cdr.
endwhile.
* If the last item is not a cons cell, we cannot append
if lr_cell->type ne lcl_lisp_element=>type_conscell.
eval_err( |A proper list must not end with { to_string( list->car ) }| ).
endif.
* Last item is a cons cell; tack on the new value
lr_cell->cdr = list->cdr->car.
result = list->car.
endmethod. "proc_append
**********************************************************************
method proc_car.
if list->car = nil.
result = nil. return.
endif.
result = list->car->car.
* result = list->car.
if result is not bound.
break-point.
endif.
endmethod. "proc_car
**********************************************************************
method proc_cdr.
if list->cdr = nil and list->car = nil.
result = nil. return.
endif.
* TODO: Check for incorrect number of arguments
* if list->cdr->type ne lcl_lisp_element=>type_conscell.
* eval_err( |Argument to CDR must be a pair| ).
* endif.
result = list->car->cdr.
* result = list->cdr.
* FIXME: test (cdr 23); should result in error!
if result is not bound.
break-point.
endif.
endmethod. "proc_cdr
**********************************************************************
method proc_cons.
* Create new cell and prepend it to second parameter
create object result
exporting
type = lcl_lisp_element=>type_conscell.
result->car = list->car.
result->cdr = list->cdr->car.
endmethod. "proc_cons
**********************************************************************
method proc_length.
if list->car->type ne lcl_lisp_element=>type_conscell.
eval_err( |{ to_string( list->car ) } is not a list| ).
endif.
if list->cdr ne nil.
eval_err( 'LIST takes only one argument' ).
endif.
create object result
exporting
type = lcl_lisp_element=>type_number.
result->number = 0.
if list = nil.
return.
endif.
* Iterate over list to count the number of items
result->number = 1.
data: lr_cell type ref to lcl_lisp_element.
lr_cell = list->car.
while lr_cell->cdr is bound and lr_cell->cdr ne nil.
add 1 to result->number.
lr_cell = lr_cell->cdr.
endwhile.
* If the last item is not a cons cell, give an error
if lr_cell->type ne lcl_lisp_element=>type_conscell.
eval_err( |A proper list must not end with { to_string( list->car ) }| ).
endif.
endmethod. "proc_length
**********************************************************************
method proc_list.
* The items given to us are already in a list and evaluated; we just need to return the head
result = list.
endmethod. "proc_list
**********************************************************************
method proc_nilp.
if list->car = nil. " or
result = true.
else.