2016-05-03 07:02:29 +03:00
|
|
|
-- ---------------------------------------------------------
|
|
|
|
-- step2_eval.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
|
|
|
\i types.sql
|
|
|
|
\i reader.sql
|
|
|
|
\i printer.sql
|
|
|
|
|
|
|
|
-- ---------------------------------------------------------
|
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 integer AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
RETURN reader.read_str(line);
|
2016-03-22 04:28:08 +03:00
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
-- eval
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.eval_ast(ast integer, env hstore) RETURNS integer AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
DECLARE
|
|
|
|
type integer;
|
|
|
|
symkey varchar;
|
2016-03-31 07:58:03 +03:00
|
|
|
seq integer[];
|
|
|
|
eseq integer[];
|
|
|
|
hash hstore;
|
|
|
|
ehash hstore;
|
|
|
|
kv RECORD;
|
2016-03-22 04:28:08 +03:00
|
|
|
e integer;
|
|
|
|
result integer;
|
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT type_id INTO type FROM types.value WHERE value_id = ast;
|
2016-03-22 04:28:08 +03:00
|
|
|
CASE
|
|
|
|
WHEN type = 7 THEN
|
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
symkey := types._valueToString(ast);
|
2016-03-31 07:58:03 +03:00
|
|
|
IF env ? symkey THEN
|
|
|
|
result := env -> symkey;
|
|
|
|
ELSE
|
2016-03-22 04:28:08 +03:00
|
|
|
RAISE EXCEPTION '''%'' not found', symkey;
|
|
|
|
END IF;
|
|
|
|
END;
|
2016-03-31 07:58:03 +03:00
|
|
|
WHEN type IN (8, 9) THEN
|
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-31 07:58:03 +03:00
|
|
|
-- Evaluate each entry creating a new sequence
|
|
|
|
FOR i IN 1 .. COALESCE(array_length(seq, 1), 0) LOOP
|
2016-05-03 07:02:29 +03:00
|
|
|
eseq[i] := mal.EVAL(seq[i], env);
|
2016-03-31 07:58:03 +03:00
|
|
|
END LOOP;
|
2016-05-03 07:02:29 +03:00
|
|
|
INSERT INTO types.value (type_id, val_seq) VALUES (type, eseq)
|
2016-03-26 07:11:40 +03:00
|
|
|
RETURNING value_id INTO result;
|
2016-03-31 07:58:03 +03:00
|
|
|
END;
|
|
|
|
WHEN type = 10 THEN
|
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT val_hash INTO hash FROM types.value WHERE value_id = ast;
|
2016-03-31 07:58:03 +03:00
|
|
|
-- Evaluate each value for every key/value
|
|
|
|
FOR kv IN SELECT * FROM each(hash) LOOP
|
2016-05-03 07:02:29 +03:00
|
|
|
e := mal.EVAL(CAST(kv.value AS integer), env);
|
2016-03-31 07:58:03 +03:00
|
|
|
IF ehash IS NULL THEN
|
|
|
|
ehash := hstore(kv.key, CAST(e AS varchar));
|
|
|
|
ELSE
|
|
|
|
ehash := ehash || hstore(kv.key, CAST(e AS varchar));
|
|
|
|
END IF;
|
2016-03-22 04:28:08 +03:00
|
|
|
END LOOP;
|
2016-05-03 07:02:29 +03:00
|
|
|
INSERT INTO types.value (type_id, val_hash) VALUES (type, ehash)
|
2016-03-31 07:58:03 +03:00
|
|
|
RETURNING value_id INTO result;
|
2016-03-22 04:28:08 +03:00
|
|
|
END;
|
|
|
|
ELSE
|
|
|
|
result := ast;
|
|
|
|
END CASE;
|
|
|
|
|
|
|
|
RETURN result;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.EVAL(ast integer, env hstore) RETURNS integer AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
DECLARE
|
2016-05-03 07:02:29 +03:00
|
|
|
type integer;
|
|
|
|
el integer;
|
|
|
|
fname varchar;
|
|
|
|
args integer[];
|
|
|
|
result integer;
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT type_id INTO type FROM types.value WHERE value_id = ast;
|
2016-03-22 04:28:08 +03:00
|
|
|
IF type <> 8 THEN
|
2016-05-03 07:02:29 +03:00
|
|
|
RETURN mal.eval_ast(ast, env);
|
|
|
|
END IF;
|
|
|
|
IF types._count(ast) = 0 THEN
|
|
|
|
RETURN ast;
|
2016-03-22 04:28:08 +03:00
|
|
|
END IF;
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
el := mal.eval_ast(ast, env);
|
|
|
|
SELECT val_string INTO fname FROM types.value
|
|
|
|
WHERE value_id = types._first(el);
|
|
|
|
args := types._restArray(el);
|
2016-03-31 07:58:03 +03:00
|
|
|
EXECUTE format('SELECT %s($1);', fname) INTO result USING args;
|
2016-03-22 04:28:08 +03:00
|
|
|
RETURN result;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
-- print
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.PRINT(exp integer) RETURNS varchar AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
RETURN printer.pr_str(exp);
|
2016-03-22 04:28:08 +03:00
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
|
|
|
|
-- repl
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.intop(op varchar, args integer[]) RETURNS integer AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
DECLARE a integer; b integer; result integer;
|
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
SELECT val_int INTO a FROM types.value WHERE value_id = args[1];
|
|
|
|
SELECT val_int INTO b FROM types.value WHERE value_id = args[2];
|
|
|
|
EXECUTE format('INSERT INTO types.value (type_id, val_int)
|
|
|
|
VALUES (3, $1 %s $2)
|
2016-03-22 04:28:08 +03:00
|
|
|
RETURNING value_id;', op) INTO result USING a, b;
|
|
|
|
RETURN result;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.add(args integer[]) RETURNS integer AS $$
|
|
|
|
BEGIN RETURN mal.intop('+', args); END; $$ LANGUAGE plpgsql;
|
|
|
|
CREATE FUNCTION mal.subtract(args integer[]) RETURNS integer AS $$
|
|
|
|
BEGIN RETURN mal.intop('-', args); END; $$ LANGUAGE plpgsql;
|
|
|
|
CREATE FUNCTION mal.multiply(args integer[]) RETURNS integer AS $$
|
|
|
|
BEGIN RETURN mal.intop('*', args); END; $$ LANGUAGE plpgsql;
|
|
|
|
CREATE FUNCTION mal.divide(args integer[]) RETURNS integer AS $$
|
|
|
|
BEGIN RETURN mal.intop('/', args); END; $$ LANGUAGE plpgsql;
|
2016-03-22 04:28:08 +03:00
|
|
|
|
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.REP(env hstore, line varchar) RETURNS varchar AS $$
|
2016-03-22 04:28:08 +03:00
|
|
|
BEGIN
|
2016-05-03 07:02:29 +03:00
|
|
|
RETURN mal.PRINT(mal.EVAL(mal.READ(line), env));
|
2016-03-22 04:28:08 +03:00
|
|
|
END; $$ LANGUAGE plpgsql;
|
2016-03-25 08:05:54 +03:00
|
|
|
|
2016-05-03 07:02:29 +03:00
|
|
|
CREATE FUNCTION mal.MAIN(pwd varchar) RETURNS integer AS $$
|
2016-03-25 08:05:54 +03:00
|
|
|
DECLARE
|
2016-03-31 07:58:03 +03:00
|
|
|
repl_env hstore;
|
|
|
|
line varchar;
|
|
|
|
output varchar;
|
2016-03-25 08:05:54 +03:00
|
|
|
BEGIN
|
2016-03-31 07:58:03 +03:00
|
|
|
repl_env := hstore(ARRAY[
|
2016-05-03 07:02:29 +03:00
|
|
|
'+', types._function('mal.add'),
|
|
|
|
'-', types._function('mal.subtract'),
|
|
|
|
'*', types._function('mal.multiply'),
|
|
|
|
'/', types._function('mal.divide')]);
|
2016-03-31 07:58:03 +03:00
|
|
|
WHILE true LOOP
|
2016-03-25 08:05:54 +03:00
|
|
|
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(repl_env, 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;
|
|
|
|
END; $$ LANGUAGE plpgsql;
|