plsql: step4 basics.
[jackhill/mal.git] / plsql / core.sql
CommitLineData
9fc524f1
JM
1PROMPT 'core.sql start';
2
3CREATE OR REPLACE TYPE core_ns_type IS TABLE OF varchar2(100);
4/
5
6
7CREATE OR REPLACE PACKAGE core_pkg IS
8
9FUNCTION do_core_func(fn mal_type, args mal_seq_items_type) RETURN mal_type;
10
11FUNCTION get_core_ns RETURN core_ns_type;
12
13END core_pkg;
14/
15
16
17CREATE OR REPLACE PACKAGE BODY core_pkg AS
18
19-- general functions
20FUNCTION equal_Q(args mal_seq_items_type) RETURN mal_type IS
21BEGIN
22 RETURN types_pkg.wraptf(types_pkg.equal_Q(args(1), args(2)));
23END;
24
25-- string functions
26FUNCTION pr_str(args mal_seq_items_type) RETURN mal_type IS
27BEGIN
28 RETURN mal_str_type(5, printer_pkg.pr_str_seq(args, ' ', TRUE));
29END;
30
31FUNCTION str(args mal_seq_items_type) RETURN mal_type IS
32BEGIN
33 RETURN mal_str_type(5, printer_pkg.pr_str_seq(args, '', FALSE));
34END;
35
36FUNCTION prn(args mal_seq_items_type) RETURN mal_type IS
37BEGIN
38 stream_writeline(printer_pkg.pr_str_seq(args, ' ', TRUE));
39 RETURN mal_type(0);
40END;
41
42FUNCTION println(args mal_seq_items_type) RETURN mal_type IS
43BEGIN
44 stream_writeline(printer_pkg.pr_str_seq(args, ' ', FALSE));
45 RETURN mal_type(0);
46END;
47
48
49-- numeric functions
50FUNCTION lt(args mal_seq_items_type) RETURN mal_type IS
51BEGIN
52 RETURN types_pkg.wraptf(TREAT(args(1) AS mal_int_type).val_int <
53 TREAT(args(2) AS mal_int_type).val_int);
54END;
55
56FUNCTION lte(args mal_seq_items_type) RETURN mal_type IS
57BEGIN
58 RETURN types_pkg.wraptf(TREAT(args(1) AS mal_int_type).val_int <=
59 TREAT(args(2) AS mal_int_type).val_int);
60END;
61
62FUNCTION gt(args mal_seq_items_type) RETURN mal_type IS
63BEGIN
64 RETURN types_pkg.wraptf(TREAT(args(1) AS mal_int_type).val_int >
65 TREAT(args(2) AS mal_int_type).val_int);
66END;
67
68FUNCTION gte(args mal_seq_items_type) RETURN mal_type IS
69BEGIN
70 RETURN types_pkg.wraptf(TREAT(args(1) AS mal_int_type).val_int >=
71 TREAT(args(2) AS mal_int_type).val_int);
72END;
73
74FUNCTION add(args mal_seq_items_type) RETURN mal_type IS
75BEGIN
76 RETURN mal_int_type(3, TREAT(args(1) AS mal_int_type).val_int +
77 TREAT(args(2) AS mal_int_type).val_int);
78END;
79
80FUNCTION subtract(args mal_seq_items_type) RETURN mal_type IS
81BEGIN
82 RETURN mal_int_type(3, TREAT(args(1) AS mal_int_type).val_int -
83 TREAT(args(2) AS mal_int_type).val_int);
84END;
85
86FUNCTION multiply(args mal_seq_items_type) RETURN mal_type IS
87BEGIN
88 RETURN mal_int_type(3, TREAT(args(1) AS mal_int_type).val_int *
89 TREAT(args(2) AS mal_int_type).val_int);
90END;
91
92FUNCTION divide(args mal_seq_items_type) RETURN mal_type IS
93BEGIN
94 RETURN mal_int_type(3, TREAT(args(1) AS mal_int_type).val_int /
95 TREAT(args(2) AS mal_int_type).val_int);
96END;
97
98-- general native function case/switch
99FUNCTION do_core_func(fn mal_type, args mal_seq_items_type) RETURN mal_type IS
100 fname varchar(100);
101BEGIN
102 IF fn.type_id <> 11 THEN
103 raise_application_error(-20004,
104 'Invalid function call', TRUE);
105 END IF;
106
107 fname := TREAT(fn AS mal_str_type).val_str;
108
109 CASE
110 WHEN fname = '=' THEN RETURN equal_Q(args);
111
112 WHEN fname = 'pr-str' THEN RETURN pr_str(args);
113 WHEN fname = 'str' THEN RETURN str(args);
114 WHEN fname = 'prn' THEN RETURN prn(args);
115 WHEN fname = 'println' THEN RETURN println(args);
116
117 WHEN fname = '<' THEN RETURN lt(args);
118 WHEN fname = '<=' THEN RETURN lte(args);
119 WHEN fname = '>' THEN RETURN gt(args);
120 WHEN fname = '>=' THEN RETURN gte(args);
121 WHEN fname = '+' THEN RETURN add(args);
122 WHEN fname = '-' THEN RETURN subtract(args);
123 WHEN fname = '*' THEN RETURN multiply(args);
124 WHEN fname = '/' THEN RETURN divide(args);
125
126 WHEN fname = 'list' THEN RETURN types_pkg.list(args);
127 WHEN fname = 'list?' THEN RETURN types_pkg.wraptf(args(1).type_id = 8);
128
129 WHEN fname = 'empty?' THEN
130 RETURN types_pkg.wraptf(0 = types_pkg.count(args(1)));
131 WHEN fname = 'count' THEN
132 IF args(1).type_id = 0 THEN
133 RETURN mal_int_type(3, 0);
134 ELSE
135 RETURN mal_int_type(3, types_pkg.count(args(1)));
136 END IF;
137
138 ELSE raise_application_error(-20004,
139 'Invalid function call', TRUE);
140 END CASE;
141END;
142
143FUNCTION get_core_ns RETURN core_ns_type IS
144BEGIN
145 RETURN core_ns_type(
146 '=',
147
148 'pr-str',
149 'str',
150 'prn',
151 'println',
152
153 '<',
154 '<=',
155 '>',
156 '>=',
157 '+',
158 '-',
159 '*',
160 '/',
161
162 'list',
163 'list?',
164
165 'empty?',
166 'count');
167END;
168
169END core_pkg;
170/
171show errors;
172
173PROMPT 'core.sql finished';