| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | // Copyright 2009 The Go Authors.  All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package Compilation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import Globals "globals" | 
					
						
							|  |  |  | import Object "object" | 
					
						
							|  |  |  | import Type "type" | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | import Universe "universe" | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | import Scanner "scanner" | 
					
						
							| 
									
										
										
										
											2008-07-18 17:18:29 -07:00
										 |  |  | import AST "ast" | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | import Parser "parser" | 
					
						
							| 
									
										
										
										
											2008-07-16 17:00:48 -07:00
										 |  |  | import Export "export" | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-16 17:00:48 -07:00
										 |  |  | func BaseName(s string) string { | 
					
						
							|  |  |  | 	// TODO this is not correct for non-ASCII strings! | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 	i := len(s) - 1; | 
					
						
							| 
									
										
										
										
											2008-07-16 17:00:48 -07:00
										 |  |  | 	for i >= 0 && s[i] != '/' { | 
					
						
							|  |  |  | 		if s[i] > 128 { | 
					
						
							|  |  |  | 			panic "non-ASCII string" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		i--; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s[i + 1 : len(s)]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func FixExt(s string) string { | 
					
						
							|  |  |  | 	i := len(s) - 3;  // 3 == len(".go"); | 
					
						
							|  |  |  | 	if s[i : len(s)] == ".go" { | 
					
						
							|  |  |  | 		s = s[0 : i]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 	return s + ".7"; | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export Compile | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | func Compile(file_name string, verbose int) { | 
					
						
							|  |  |  | 	src, ok := sys.readfile(file_name); | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 		print "cannot open ", file_name, "\n" | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 	Universe.Init();  // TODO eventually this should be only needed once | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	comp := Globals.NewCompilation(); | 
					
						
							|  |  |  | 	pkg := Globals.NewPackage(file_name); | 
					
						
							|  |  |  | 	comp.Insert(pkg); | 
					
						
							|  |  |  | 	if comp.npkgs != 1 { | 
					
						
							|  |  |  | 		panic "should have exactly one package now"; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 	scanner := new(Scanner.Scanner); | 
					
						
							|  |  |  | 	scanner.Open(file_name, src); | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 	parser := new(Parser.Parser); | 
					
						
							|  |  |  | 	parser.Open(comp, scanner, verbose); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	print "parsing ", file_name, "\n"; | 
					
						
							|  |  |  | 	parser.ParseProgram(); | 
					
						
							|  |  |  | 	if parser.S.nerrors > 0 { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 	// export | 
					
						
							| 
									
										
										
										
											2008-07-18 14:04:21 -07:00
										 |  |  | 	/* | 
					
						
							|  |  |  | 	exp := new(Export.Exporter); | 
					
						
							|  |  |  | 	exp.Export(comp, FixExt(BaseName(file_name))); | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | } |