2016-03-22 04:28:08 +03:00
|
|
|
-- ---------------------------------------------------------
|
|
|
|
-- printer.sql
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE SCHEMA printer;
|
|
|
|
|
|
|
|
CREATE FUNCTION printer.pr_str_array(arr integer[],
|
|
|
|
sep varchar, print_readably boolean)
|
2016-03-22 08:53:31 +03:00
|
|
|
RETURNS varchar AS $$
|
|
|
|
DECLARE
|
|
|
|
i integer;
|
|
|
|
res varchar[];
|
|
|
|
BEGIN
|
|
|
|
IF array_length(arr, 1) > 0 THEN
|
|
|
|
FOR i IN array_lower(arr, 1) .. array_upper(arr, 1)
|
|
|
|
LOOP
|
2016-05-03 07:02:29 +03:00
|
|
|
res := array_append(res, printer.pr_str(arr[i], print_readably));
|
2016-03-22 08:53:31 +03:00
|
|
|
END LOOP;
|
|
|
|
RETURN array_to_string(res, sep);
|
|
|
|
ELSE
|
|
|
|
RETURN '';
|
|
|
|
END IF;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION printer.pr_str(ast integer,
|
|
|
|
print_readably boolean DEFAULT true)
|
2016-03-22 08:53:31 +03:00
|
|
|
RETURNS varchar AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
DECLARE
|
|
|
|
type integer;
|
2016-03-31 07:58:03 +03:00
|
|
|
seq integer[];
|
|
|
|
hash hstore;
|
2016-03-22 04:28:08 +03:00
|
|
|
cid integer;
|
|
|
|
vid integer;
|
2016-03-22 08:53:31 +03:00
|
|
|
pid integer;
|
|
|
|
str varchar;
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
|
|
|
-- RAISE NOTICE 'pr_str ast: %', ast;
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT type_id FROM types.value WHERE value_id = ast INTO type;
|
2016-03-22 04:28:08 +03:00
|
|
|
-- RAISE NOTICE 'pr_str type: %', type;
|
|
|
|
CASE
|
|
|
|
WHEN type = 0 THEN RETURN 'nil';
|
|
|
|
WHEN type = 1 THEN RETURN 'false';
|
|
|
|
WHEN type = 2 THEN RETURN 'true';
|
2016-03-22 08:53:31 +03:00
|
|
|
WHEN type = 3 THEN -- integer
|
2016-03-22 04:28:08 +03:00
|
|
|
RETURN CAST((SELECT val_int
|
2016-05-03 07:02:29 +03:00
|
|
|
FROM types.value WHERE value_id = ast) as varchar);
|
2016-03-22 08:53:31 +03:00
|
|
|
WHEN type = 5 THEN -- string
|
2016-05-03 07:02:29 +03:00
|
|
|
str := types._valueToString(ast);
|
2016-03-31 09:04:15 +03:00
|
|
|
IF chr(CAST(x'7f' AS integer)) = substring(str FROM 1 FOR 1) THEN
|
2016-03-25 08:05:54 +03:00
|
|
|
RETURN ':' || substring(str FROM 2 FOR (char_length(str)-1));
|
|
|
|
ELSIF print_readably THEN
|
2016-03-22 08:53:31 +03:00
|
|
|
str := replace(str, E'\\', '\\');
|
|
|
|
str := replace(str, '"', '\"');
|
|
|
|
str := replace(str, E'\n', '\n');
|
|
|
|
RETURN '"' || str || '"';
|
|
|
|
ELSE
|
|
|
|
RETURN str;
|
|
|
|
END IF;
|
|
|
|
WHEN type = 7 THEN -- symbol
|
2016-05-03 07:02:29 +03:00
|
|
|
RETURN types._valueToString(ast);
|
2016-03-22 08:53:31 +03:00
|
|
|
WHEN type = 8 THEN -- list
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT val_seq INTO seq FROM types.value WHERE value_id = ast;
|
2016-03-22 04:28:08 +03:00
|
|
|
RETURN '(' ||
|
|
|
|
array_to_string(array(
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT printer.pr_str(x, print_readably)
|
2016-03-31 07:58:03 +03:00
|
|
|
FROM unnest(seq) AS x), ' ') ||
|
2016-03-22 04:28:08 +03:00
|
|
|
')';
|
|
|
|
END;
|
2016-03-22 08:53:31 +03:00
|
|
|
WHEN type = 9 THEN -- vector
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT val_seq INTO seq FROM types.value WHERE value_id = ast;
|
2016-03-22 04:28:08 +03:00
|
|
|
RETURN '[' ||
|
|
|
|
array_to_string(array(
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT printer.pr_str(x, print_readably)
|
2016-03-31 07:58:03 +03:00
|
|
|
FROM unnest(seq) AS x), ' ') ||
|
2016-03-22 04:28:08 +03:00
|
|
|
']';
|
|
|
|
END;
|
2016-03-25 08:05:54 +03:00
|
|
|
WHEN type = 10 THEN -- hash-map
|
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT val_hash INTO hash FROM types.value WHERE value_id = ast;
|
2016-03-25 08:05:54 +03:00
|
|
|
RETURN '{' ||
|
|
|
|
array_to_string(array(
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT printer.pr_str(CAST(key AS integer),
|
|
|
|
print_readably) || ' ' ||
|
|
|
|
printer.pr_str(CAST(value AS integer),
|
|
|
|
print_readably)
|
2016-03-31 07:58:03 +03:00
|
|
|
FROM each(hash)), ' ') ||
|
2016-03-25 08:05:54 +03:00
|
|
|
'}';
|
|
|
|
END;
|
2016-03-22 08:53:31 +03:00
|
|
|
WHEN type = 11 THEN -- native function
|
|
|
|
RETURN '#<function ' ||
|
2016-05-03 07:02:29 +03:00
|
|
|
(SELECT val_string FROM types.value WHERE value_id = ast) ||
|
2016-03-22 08:53:31 +03:00
|
|
|
'>';
|
|
|
|
WHEN type = 12 THEN -- mal function
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
2016-03-31 07:58:03 +03:00
|
|
|
SELECT ast_id, params_id
|
|
|
|
INTO vid, pid
|
2016-05-03 07:02:29 +03:00
|
|
|
FROM types.value WHERE value_id = ast;
|
|
|
|
RETURN '(fn* ' || printer.pr_str(pid, print_readably) ||
|
|
|
|
' ' || printer.pr_str(vid, print_readably) || ')';
|
2016-03-22 08:53:31 +03:00
|
|
|
END;
|
|
|
|
WHEN type = 13 THEN -- atom
|
|
|
|
BEGIN
|
2016-03-31 07:58:03 +03:00
|
|
|
SELECT val_seq[1] INTO vid
|
2016-05-03 07:02:29 +03:00
|
|
|
FROM types.value WHERE value_id = ast;
|
|
|
|
RETURN '(atom ' || printer.pr_str(vid, print_readably) || ')';
|
2016-03-22 04:28:08 +03:00
|
|
|
END;
|
|
|
|
ELSE
|
|
|
|
RETURN 'unknown';
|
|
|
|
END CASE;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|