Re: Code Generator???

From: Jim Larson <larson_at_menlo.jpl.nasa.gov>
Date: 1997/06/03
Message-ID: <LARSON.97Jun3110846_at_menlo.jpl.nasa.gov>#1/1


In article <5meqfp$89g$3_at_titan.globalserve.net> hender_at_mail.globalserve.net (Scott H.) writes:
>I have in excess of 700 rules such as this. I could manually
>translate the code to SQL or C but I'm sure there must be a better
>approach. I need a program that will read a text file and generate
>the code required to process the file.
>
>Question #1:
>
>Where could I get my hands on a code generator/translator.

We to are working with wide table and many rules to govern their processing. We're trying to do our code generation within PL/SQL. The process is as follows:

  1. Write a function that queries the rule set and generates the code you want as a VARCHAR2 string.
  2. Debug the function by displaying this string (trickier than it should be - brain-damage in DBMS_OUTPUT will haunt you if your string from the function above is too long). Tweak the string generation function until you get the code you want.
  3. Either write out the string to a file and load it, or use DBMS_SQL to parse the string directly.

The code generation function can be as simple as:

	function foo_gen_string(arg argtype) return varchar2 is
		s varchar2(32767);
		cursor rules is
			select appropriate rules
			from rule_table;
	begin
		s :=
/* BEGIN GENERATED TEXT */
'	procedure foo(fooarg1 argtype1, fooarg2 argtype2) is
		somevars sometypes;
	begin
		preamble_statements;
'; /* SUSPEND GENERATED TEXT */
		/* For each rule emit "<somefield>, <otherfield>" */
		for r in rules loop
			s := s || r.somefield || ", "
			|| r.otherfield || ";" || newline_char;
		end_loop;
		s := s ||
/* RESUME GENERATED TEXT */
'		more_statements;
	end foo;
'; /* END GENERATED TEXT */
		return s;
	end foo_gen_string;

If you have higher-level patterns in your code, you can create specialized functions, all returning strings.

Whether this approach is suitable depends on your specific needs, but we've found this to be immensely helpful in avoiding repetitive, error-prone argument marshalling.

Jim

--
Jim Larson                      Jet Propulsion Laboratory
james.s.larson_at_jpl.nasa.gov     #include <disclaimer.h>
Received on Tue Jun 03 1997 - 00:00:00 CEST

Original text of this message