[nem-pl] "Dzieło" Pawła

Dawid Duda daveduda at plusnet.pl
Tue Nov 4 19:53:42 CET 2003


W załączeniu najnowsze osiągnięcie z dzisiejszego spotkania :)

dave

-------------- next part --------------




variant Instruction
{
	| MakeInt { name : string; value : int; }
	| MakeChar { name : string; value : char; }
	| NewObject { iVariable : string; iClass : string; 
		      iParams : list (string); }
	| CallMethod { iRetVal : option (string); iVariable : string;
		       iMethod : string; iParams : list (string); }
}


internal class CodeEmitter
{
	private _counter : int;


	public IncreaseCounter () : void
	{
		_counter <- _counter + 1
	}


	public FactorialOfCounter () : int
	{
		def loop (accumulator : int, n : int)
		{
			if (n <= 1)
			  accumulator
			else
			  loop (n * accumulator, n - 1)
		}

		loop (1, _counter);
	}


	static public emitInstruction (instruction : Instruction) : string
	{
		match (instruction)
		{
			| MakeInt (n, v) => 
			    System.String.Format ("int {0} = {1}", n, v);
			| MakeChar (n, v) =>
			    System.String.Format ("char {0} = '{1}'", n, v);

			(* i tak dalej... *)
		}
	}


	static public emitCode (code : list (Instruction)) : string
	{
		def builder = System.Text.StringBuilder (40 * List.Length (code));
			
		def loop (instructionList : list (Instruction)) : void
		{
			match (instructionList)
			{
				| Nil => ""
				| Cons (instruction, rest) =>
				    builder.Append ("\t\t");
				    builder.Append (emitInstruction (instruction));				 
				    builder.Append ("\n\n");
				    loop (rest)
			}
		}

		loop (code);

		builder.ToString();
	}
}



More information about the devel-pl mailing list