Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


No Format
//By DougHolton
This script visits the AST structure after each step in the compile process
and converts it to XML or back to boo syntax.
If there are visible differences since the previous step, it saves the output
to a file in a folder named "compilersteps".

How to run:

/path/to/booi.exe path/to/ [-xml] [-types] path/to/your/

You can also use the "-r:assembly.dll" flag to add assembly references.

It will generate a folder in the current directory named "compilersteps",
and put copies of the script as it looks after each compiler step in that folder.

import System
import System.IO
import System.Xml.Serialization from System.Xml
import Boo.Lang.Compiler from Boo.Lang.Compiler
import Boo.Lang.Compiler.IO
import Boo.Lang.Compiler.Pipelines
import Boo.Lang.Compiler.Ast
import Boo.Lang.Compiler.Ast.Visitors
import Boo.Lang.Compiler.TypeSystem
import Boo.Lang.Compiler.Steps
import System.Reflection

class Globals:
	static format = "boo" //or "xml" //format for output
	static foldername = "compilersteps" //folder where files are saved
	static showtypes = false  //whether to print typesystem information
	static showexp = false //show expression types as well
	//used internally:
	static savefolder as string
	static n = 0
	static laststep as string
class BooTypePrinterVisitor(BooPrinterVisitor):
	_showexp = false
	def constructor(writer as TextWriter, show_expressions as bool):
		_showexp = show_expressions
	override def Visit(node as Node) as bool:
		tagname = ""
		if node is not null:
			entity = TypeSystemServices.GetOptionalEntity(node)
			if entity is not null:
				tagname = entity.GetType().ToString(). \
				s = "<"
				s += tagname
				if _showexp:
					exp = node as Expression
					if exp is not null and exp.ExpressionType is not null:
						s += " ExpType="+exp.ExpressionType.ToString()
				s += ">"
		result = super(node)
		if tagname != "":
		return result
def PrintAST([required]result as CompilerContext, [required]outo as TextWriter):
	astobject = result.CompileUnit
		s = XmlSerializer( astobject.GetType() )
	except e:
		print e.Message
		s.Serialize( outo, astobject )
	except e:
		print "\n", e.ToString()

def AfterStep(sender, e as CompilerStepEventArgs):
	stepname = e.Step.ToString().Replace("Boo.Lang.Parser.","").Replace("Boo.Lang.Compiler.Steps.","")
	tempfile = Path.GetTempFileName()
	using temp = StreamWriter(tempfile):
		if format == "xml":
			PrintAST(e.Context, temp)
			printer as BooPrinterVisitor
			if showtypes:
				printer = BooTypePrinterVisitor(temp, showexp)
				printer = BooPrinterVisitor(temp)
	using r = StreamReader(tempfile):
		thisstep = r.ReadToEnd()
	filename = string.Format("STEP{0:D2}-{1}.{2}", n, stepname, format)
	if thisstep != laststep:
		File.Move(tempfile, Path.Combine(savefolder, filename))
		laststep = thisstep
		print string.Format("STEP{0:D2}-{1}: SAVED TO {2} FILE.", n, stepname, format.ToUpper())
		print string.Format("STEP{0:D2}-{1}: NO CHANGE TO AST.", n, stepname)
def LoadAssembly(assemblyName as string) as Assembly:
	reference as Assembly
	if File.Exists(Path.GetFullPath(assemblyName)):
		reference = Assembly.LoadWithPartialNameLoadFrom(Path.GetFullPath(assemblyName))
	if reference is null:
		reference = Assembly.LoadFromLoadWithPartialName(Path.GetFullPath(assemblyName))
		if reference is null:
			raise ApplicationException(ResourceManager.Format("BooC.UnableToLoadAssembly", assemblyName))
	return reference


if len(argv) == 0:
	print "Please specify at least one boo file as a parameter"

compiler = BooCompiler()

//delete old folder if running more than once:
if Directory.Exists(foldername):
	Directory.Delete(foldername, true)
savedir = Directory.CreateDirectory(foldername)
if savedir is null or not Directory.Exists(foldername):
	print "The directory '${foldername}' could not be created."

savefolder = savedir.FullName

compiler.Parameters.Pipeline = Compile()
compiler.Parameters.Pipeline.AfterStep += AfterStep

for arg in argv:
	if arg[0:3] == "-r:":
	elif arg == "-xml":
		format = "xml"
	elif arg == "-types":
		showtypes = true
	elif arg == "-exp":
		showtypes = true
		showexp = true
	elif arg == "-ducky":
		compiler.Parameters.Ducky = true

	print "\nSeeSee boo/src/Boo.Lang.Compiler/Steps/ for the source code for these steps:\n."
	result = compiler.Run()
	if len(result.Errors) > 0:
		print "\nThere were ${len(result.Errors)} errors compiling the boo file(s)"
		print result.Errors.ToString(true)
		print "\nSuccessful: See the files under: '${savefolder}'"
except e:
	print e.GetType(), ":", e.Message