@@ -3,37 +3,57 @@ package main
3
3
import (
4
4
"flag"
5
5
"fmt"
6
+ "io/ioutil"
6
7
"log"
7
8
"os"
8
9
10
+ "github.com/ncw/gpython/compile"
9
11
"github.com/ncw/gpython/parser"
10
12
)
11
13
12
14
var (
13
- lex = flag .Bool ("l" , false , "Lex the file only" )
14
- debugLevel = flag .Int ("d" , 0 , "Debug level 0-4" )
15
+ lexFile = flag .Bool ("l" , false , "Lex the file only" )
16
+ compileFile = flag .Bool ("c" , false , "Lex, Parse and compile the file" )
17
+ debugLevel = flag .Int ("d" , 0 , "Debug level 0-4" )
15
18
)
16
19
17
20
func main () {
18
21
flag .Parse ()
19
22
parser .SetDebug (* debugLevel )
23
+ if len (flag .Args ()) == 0 {
24
+ log .Printf ("Need files to parse" )
25
+ os .Exit (1 )
26
+ }
20
27
for _ , path := range flag .Args () {
21
- if * lex {
28
+ if * lexFile {
22
29
fmt .Printf ("Lexing %q\n " , path )
30
+ } else if * compileFile {
31
+ fmt .Printf ("Compiling %q\n " , path )
23
32
} else {
24
33
fmt .Printf ("Parsing %q\n " , path )
25
34
}
26
35
in , err := os .Open (path )
27
36
if err != nil {
28
37
log .Fatal (err )
29
38
}
30
- fmt .Printf ("-----------------\n " )
31
- if * lex {
32
- _ , err = parser .Lex (in , "exec" )
39
+ if * debugLevel > 0 {
40
+ fmt .Printf ("-----------------\n " )
41
+ }
42
+ if * lexFile {
43
+ _ , err = parser .Lex (in , path , "exec" )
44
+ } else if * compileFile {
45
+ var input []byte
46
+ input , err = ioutil .ReadAll (in )
47
+ if err != nil {
48
+ log .Fatalf ("Failed to read %q: %v" , path , err )
49
+ }
50
+ _ , err = compile .Compile (string (input ), path , "exec" , 0 , false )
33
51
} else {
34
- _ , err = parser .Parse (in , "exec" )
52
+ _ , err = parser .Parse (in , path , "exec" )
53
+ }
54
+ if * debugLevel > 0 {
55
+ fmt .Printf ("-----------------\n " )
35
56
}
36
- fmt .Printf ("-----------------\n " )
37
57
closeErr := in .Close ()
38
58
if err != nil {
39
59
log .Fatalf ("Failed on %q: %v" , path , err )
0 commit comments