2016-05-03 07:02:29 +03:00
|
|
|
-- ---------------------------------------------------------
|
|
|
|
-- step0_repl.sql
|
|
|
|
|
2016-03-22 04:28:08 +03:00
|
|
|
\i init.sql
|
2016-03-25 08:05:54 +03:00
|
|
|
\i io.sql
|
2016-03-22 04:28:08 +03:00
|
|
|
|
|
|
|
-- ---------------------------------------------------------
|
2016-05-03 07:02:29 +03:00
|
|
|
|
|
|
|
CREATE SCHEMA mal;
|
2016-03-22 04:28:08 +03:00
|
|
|
|
|
|
|
-- read
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.READ(line varchar) RETURNS varchar AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
|
|
|
RETURN line;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
-- eval
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.EVAL(ast varchar, env varchar) RETURNS varchar AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
|
|
|
RETURN ast;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
-- print
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.PRINT(exp varchar) RETURNS varchar AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
|
|
|
RETURN exp;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
|
|
|
|
-- repl
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.REP(line varchar) RETURNS varchar AS $$
|
2016-03-25 08:05:54 +03:00
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
RETURN mal.PRINT(mal.EVAL(mal.READ(line), ''));
|
2016-03-25 08:05:54 +03:00
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.MAIN(pwd varchar) RETURNS integer AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
DECLARE
|
2016-05-03 07:02:29 +03:00
|
|
|
line varchar;
|
|
|
|
output varchar;
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
2016-03-25 08:05:54 +03:00
|
|
|
WHILE true
|
|
|
|
LOOP
|
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
line := io.readline('user> ', 0);
|
|
|
|
IF line IS NULL THEN
|
|
|
|
PERFORM io.close(1);
|
|
|
|
RETURN 0;
|
|
|
|
END IF;
|
|
|
|
IF line NOT IN ('', E'\n') THEN
|
|
|
|
output := mal.REP(line);
|
|
|
|
PERFORM io.writeline(output);
|
2016-03-25 08:05:54 +03:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
EXCEPTION WHEN OTHERS THEN
|
2016-05-03 07:02:29 +03:00
|
|
|
PERFORM io.writeline('Error: ' || SQLERRM);
|
2016-03-25 08:05:54 +03:00
|
|
|
END;
|
|
|
|
END LOOP;
|
2016-03-22 04:28:08 +03:00
|
|
|
END; $$ LANGUAGE plpgsql;
|