diff compile_old_c.rhope @ 135:18a4403fe576

Javascript backend can now produce broken output. Needs fixes plus port of standard lib
author Mike Pavone <pavone@retrodev.com>
date Sun, 14 Nov 2010 03:09:49 -0500
parents 25a205094f9b
children fc3815b7462f
line wrap: on
line diff
--- a/compile_old_c.rhope	Wed Nov 10 22:29:49 2010 -0500
+++ b/compile_old_c.rhope	Sun Nov 14 03:09:49 2010 -0500
@@ -1,9 +1,77 @@
 
 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 <- [args]Index[1]
+
+        fname,options <- Parse Args[args,1,[Dictionary[]]Set["b", "backend"],Dictionary[]]
         {
                 file <- [File[~]]Open["r"]
                 text <- String[[file]Read[[file]Length]]
@@ -17,13 +85,25 @@
                                 tree <- [~]Workers << [ Map[[~]Workers >>, Check Worker Literals[?, ~]] ]
                                 { Print["Compiling"] }
                         }
-                        compiled <- [Tree to Program Native[tree]]Compile Program[C Program[]]
+                        backmap <- [[Dictionary[]
+                        	]Set["javascript", JS Program[?]]
+                        	]Set["c", C Program[?]]
+                        sel <- [options]Index["backend"]
+                        {
+	                    	backend <- [backmap]Index[~]{}
+	                    	{
+	                    		Print[[["Unknown backend '"]Append[sel]]Append["' selected. Defaulting to C backend."]]
+	                    		backend <- C Program[?]
+	                    	}
+                        }{ backend <- C Program[?] }
+                        compiled <- [Tree to Program Native[tree]]Compile Program[Call[backend]]
                         { Print["Compiled program to backend"] }
-                        outfile <- [File[ [fname]Append[".c"] ]]Truncate
+                        outfname <- [compiled]Text Filename[fname]
+                        outfile <- [File[outfname]]Truncate
                         [[compiled]Text]Write to File[outfile] 
-                        { Print[["Wrote output to "]Append[ [fname]Append[".c"] ]] }
+                        { Print[["Wrote output to "]Append[outfname]] }
                 }
-        }{
+        }{}{}{
                 Print["You must provide a file name to compile"]
         }
 }