1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-11 00:52:44 +03:00
mal/plpgsql/step0_repl.sql
Joel Martin 494792abe4 plpgsql: schema namespaces. refactor IO.
- move each file to a schema namespace. Don't recreate DB, just the
  schemas. This is much faster and avoids the error about user still
  using the DB when recreating the DB.
- combine RUN and MAIN_LOOP into MAIN routine.
- add open column to streams table
- close stream 1 in MAIN
2016-05-02 23:37:16 -05:00

60 lines
1.3 KiB
PL/PgSQL

-- ---------------------------------------------------------
-- step0_repl.sql
\i init.sql
\i io.sql
-- ---------------------------------------------------------
CREATE SCHEMA mal;
-- read
CREATE FUNCTION mal.READ(line varchar) RETURNS varchar AS $$
BEGIN
RETURN line;
END; $$ LANGUAGE plpgsql;
-- eval
CREATE FUNCTION mal.EVAL(ast varchar, env varchar) RETURNS varchar AS $$
BEGIN
RETURN ast;
END; $$ LANGUAGE plpgsql;
-- print
CREATE FUNCTION mal.PRINT(exp varchar) RETURNS varchar AS $$
BEGIN
RETURN exp;
END; $$ LANGUAGE plpgsql;
-- repl
CREATE FUNCTION mal.REP(line varchar) RETURNS varchar AS $$
BEGIN
RETURN mal.PRINT(mal.EVAL(mal.READ(line), ''));
END; $$ LANGUAGE plpgsql;
CREATE FUNCTION mal.MAIN(pwd varchar) RETURNS integer AS $$
DECLARE
line varchar;
output varchar;
BEGIN
WHILE true
LOOP
BEGIN
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);
END IF;
EXCEPTION WHEN OTHERS THEN
PERFORM io.writeline('Error: ' || SQLERRM);
END;
END LOOP;
END; $$ LANGUAGE plpgsql;