summaryrefslogtreecommitdiff
path: root/usr/gri/gosrc/compilation.go
blob: a186966f2bbfd09920570301ac5bed59a82a9bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// 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 Platform "platform"
import Utils "utils"
import Globals "globals"
import Object "object"
import Type "type"
import Universe "universe"
import Scanner "scanner"
import AST "ast"
import Parser "parser"
import Importer "import"
import Exporter "export"
import Printer "printer"
import Verifier "verifier"


// Compute (line, column) information for a given source position.
func LineCol(src string, pos int) (line, col int) {
	line = 1;
	lpos := 0;

	if pos > len(src) {
		pos = len(src);
	}

	for i := 0; i < pos; i++ {
		if src[i] == '\n' {
			line++;
			lpos = i;
		}
	}

	return line, pos - lpos;
}


export func Error(comp *Globals.Compilation, pos int, msg string) {
	const errdist = 10;
	delta := pos - comp.errpos;  // may be negative!
	if delta < 0 {
		delta = -delta;
	}
	if delta > errdist || comp.nerrors == 0 /* always report first error */ {
		print(comp.src_file);
		if pos >= 0 {
			// print position
			line, col := LineCol(comp.src, pos);
			if Platform.USER == "gri" {
				print(":", line, ":", col);
			} else {
				print(":", line);
			}
		}
		print(": ", msg, "\n");
		comp.nerrors++;
		comp.errpos = pos;
	}

	if comp.nerrors >= 10 {
		sys.Exit(1);
	}
}


func ReadImport(comp* Globals.Compilation, filename string, update bool) (data string, ok bool) {
	if filename == "" {
		panic("illegal package file name");
	}

	// see if it just works
	data, ok = Platform.ReadObjectFile(filename);
	if ok {
		return data, ok;
	}

	if filename[0] == '/' {
		// absolute path
		panic(`don't know how to handle absolute import file path "` + filename + `"`);
	}

	// relative path
	// try relative to the $GOROOT/pkg directory
	std_filename := Platform.GOROOT + "/pkg/" + filename;
	data, ok = Platform.ReadObjectFile(std_filename);
	if ok {
		return data, ok;
	}

	if !update {
		return "", false;
	}

	// TODO BIG HACK - fix this!
	// look for a src file
	// see if it just works
	data, ok = Platform.ReadSourceFile(filename);
	if ok {
		comp.env.Compile(comp, filename + Platform.src_file_ext);
		data, ok = ReadImport(comp, filename, false);
		if ok {
			return data, ok;
		}
	}

	return "", false;
}


export func Import(comp *Globals.Compilation, pkg_file string) *Globals.Package {
	data, ok := ReadImport(comp, pkg_file, comp.flags.update_packages);
	var pkg *Globals.Package;
	if ok {
		pkg = Importer.Import(comp, data);
	}
	return pkg;
}


export func Export(comp *Globals.Compilation, pkg_file string) {
	data := Exporter.Export(comp);
	ok := Platform.WriteObjectFile(pkg_file, data);
	if !ok {
		panic("export failed");
	}
}


export func Compile(comp *Globals.Compilation, src_file string) {
	// TODO This is incorrect: When compiling with the -r flag, we are
	// calling this function recursively w/o setting up a new comp - this
	// is broken and leads to an assertion error (more then one package
	// upon parsing of the package header).

	src, ok := Platform.ReadSourceFile(src_file);
	if !ok {
		print("cannot open ", src_file, "\n");
		return;
	}

	comp.src_file = src_file;
	comp.src = src;

	if comp.flags.verbosity > 0 {
		print(src_file, "\n");
	}

	scanner := new(Scanner.Scanner);
	scanner.Open(src_file, src);

	var tstream chan *Scanner.Token;
	if comp.flags.token_chan {
		tstream = make(chan *Scanner.Token, 100);
		go scanner.Server(tstream);
	}

	parser := new(Parser.Parser);
	parser.Open(comp, scanner, tstream);

	parser.ParseProgram();
	if parser.scanner.nerrors > 0 {
		return;
	}

	Verifier.Verify(comp);

	if comp.flags.print_interface {
		Printer.PrintObject(comp, comp.pkg_list[0].obj, false);
	}

	Export(comp, src_file);
}