view compile_old_c.rhope @ 137:daf1ffaf7c2c

Fix selection of default backend in compiler. Small tweak to compile script
author Mike Pavone <pavone@retrodev.com>
date Wed, 17 Nov 2010 02:07:10 +0000
parents fc3815b7462f
children
line wrap: on
line source


Import parser_old_c.rhope
Import cbackend_c.rhope
Import jsbackend.rhope

Other Short[arg,list:out]
{
	If[[arg]=["-"]]
	{
		out <- list
	}{
		rest,last <- [arg]Slice[ [[arg]Length]-[1] ]
		out <- Other Short[rest, [list]Append[last]]
	}
}

Check Short Val[short,args,cur,shortopts,curopts:fname,opts,next,nofname]
{
	If[[cur]<[[[args]Length]-[1]]]
	{
		val <- [args]Index[cur]
		nidx <- [cur]+[1]
	}{
		val <- Yes
		nidx <- Val[cur]
	}
	fname,opts,next,nofname <- Parse Args[args,nidx,shortopts,_Set Short[shortopts,curopts,short,val]]
}

_Set Short[shortopts,dict,short,val:out]
{
	[shortopts]Index[short]
	{
		out <- [dict]Set[~, val]
	}{
		Print[["Unrecognized short option: "]Append[short]]
		out <- dict
	}
}

Parse Args[args,cur,shortopts,curopts:fname,opts,next,nofname]
{
	arg,nofname <- [args]Index[cur]
	{
		If[[~]Starts With["-"]]
		{
			If[[arg]Starts With["--"]]
			{
				,,after <- [arg]Partition["="]
				{
					[~]Slice[2] {}
					{ nextopts <- [curopts]Set[~, after] }
				} {} {} {
					[arg]Slice[2] {}
					{ nextopts <- [curopts]Set[~, Yes] }
				}
				fname,opts,next,nofname <- Parse Args[args,[cur]+[1],shortopts,nextopts]
			}{
				rest,last <- [arg]Slice[ [[arg]Length]-[1] ]
				fname,opts,next,nofname <- Check Short Val[last,args, [cur]+[1], shortopts, 
											Fold[_Set Short[shortopts,?,?,Yes], curopts, Other Short[rest, ()]]]
			}
		}{
			fname <- Val[arg]
			opts <- curopts
			next <- [cur]+[1]
		}
	}
}

Main[args]
{

	fname,options <- Parse Args[args,1,[Dictionary[]]Set["b", "backend"],Dictionary[]]
	{
		backmap <- [[Dictionary[]
			]Set["javascript", JS Program[?]]
			]Set["c", C Program[?]]
		sel <- [options]Index["backend"]
		{
			makeback <- [backmap]Index[~]{}
			{
				Print[[["Unknown backend '"]Append[sel]]Append["' selected. Defaulting to C backend."]]
				makeback <- C Program[?]
			}
		}{ makeback <- Val[C Program[?]] }
		backend <- Call[makeback]
		
		file <- [File[~]]Open["r"]
		text <- String[[file]Read[[file]Length]]
		params <- Parser[]
		Print[["Parsing "]Append[fname]]
		Null[text, params, [backend]Set Stdlib Imports[Parse Program[]], 0]
		{
			Print["Parsing imports"]
			Process Imports[~, params]
			{
				tree <- [~]Workers << [ Map[[~]Workers >>, Check Worker Literals[?, ~]] ]
				{ Print["Compiling"] }
			}

			compiled <- [Tree to Program Native[tree, [backend]Supported Number Types]]Compile Program[backend]
			{ Print["Compiled program to backend"] }
			outfname <- [compiled]Text Filename[fname]
			outfile <- [File[outfname]]Truncate
			[[compiled]Text]Write to File[outfile] 
			{ Print[["Wrote output to "]Append[outfname]] }
		}
	}{}{}{
		Print["You must provide a file name to compile"]
	}
}