comparison compile_old.rhope @ 184:c6ba9fe45910

Strip _c from filenames of current compiler
author Mike Pavone <pavone@retrodev.com>
date Wed, 27 Jul 2011 21:28:43 -0700
parents compile_old_c.rhope@daf1ffaf7c2c
children
comparison
equal deleted inserted replaced
183:24c6f8767190 184:c6ba9fe45910
1
2 Import parser_old.rhope
3 Import cbackend.rhope
4 Import jsbackend.rhope
5
6 Other Short[arg,list:out]
7 {
8 If[[arg]=["-"]]
9 {
10 out <- list
11 }{
12 rest,last <- [arg]Slice[ [[arg]Length]-[1] ]
13 out <- Other Short[rest, [list]Append[last]]
14 }
15 }
16
17 Check Short Val[short,args,cur,shortopts,curopts:fname,opts,next,nofname]
18 {
19 If[[cur]<[[[args]Length]-[1]]]
20 {
21 val <- [args]Index[cur]
22 nidx <- [cur]+[1]
23 }{
24 val <- Yes
25 nidx <- Val[cur]
26 }
27 fname,opts,next,nofname <- Parse Args[args,nidx,shortopts,_Set Short[shortopts,curopts,short,val]]
28 }
29
30 _Set Short[shortopts,dict,short,val:out]
31 {
32 [shortopts]Index[short]
33 {
34 out <- [dict]Set[~, val]
35 }{
36 Print[["Unrecognized short option: "]Append[short]]
37 out <- dict
38 }
39 }
40
41 Parse Args[args,cur,shortopts,curopts:fname,opts,next,nofname]
42 {
43 arg,nofname <- [args]Index[cur]
44 {
45 If[[~]Starts With["-"]]
46 {
47 If[[arg]Starts With["--"]]
48 {
49 ,,after <- [arg]Partition["="]
50 {
51 [~]Slice[2] {}
52 { nextopts <- [curopts]Set[~, after] }
53 } {} {} {
54 [arg]Slice[2] {}
55 { nextopts <- [curopts]Set[~, Yes] }
56 }
57 fname,opts,next,nofname <- Parse Args[args,[cur]+[1],shortopts,nextopts]
58 }{
59 rest,last <- [arg]Slice[ [[arg]Length]-[1] ]
60 fname,opts,next,nofname <- Check Short Val[last,args, [cur]+[1], shortopts,
61 Fold[_Set Short[shortopts,?,?,Yes], curopts, Other Short[rest, ()]]]
62 }
63 }{
64 fname <- Val[arg]
65 opts <- curopts
66 next <- [cur]+[1]
67 }
68 }
69 }
70
71 Main[args]
72 {
73
74 fname,options <- Parse Args[args,1,[Dictionary[]]Set["b", "backend"],Dictionary[]]
75 {
76 backmap <- [[Dictionary[]
77 ]Set["javascript", JS Program[?]]
78 ]Set["c", C Program[?]]
79 sel <- [options]Index["backend"]
80 {
81 makeback <- [backmap]Index[~]{}
82 {
83 Print[[["Unknown backend '"]Append[sel]]Append["' selected. Defaulting to C backend."]]
84 makeback <- C Program[?]
85 }
86 }{ makeback <- Val[C Program[?]] }
87 backend <- Call[makeback]
88
89 file <- [File[~]]Open["r"]
90 text <- String[[file]Read[[file]Length]]
91 params <- Parser[]
92 Print[["Parsing "]Append[fname]]
93 Null[text, params, [backend]Set Stdlib Imports[Parse Program[]], 0]
94 {
95 Print["Parsing imports"]
96 Process Imports[~, params]
97 {
98 tree <- [~]Workers << [ Map[[~]Workers >>, Check Worker Literals[?, ~]] ]
99 { Print["Compiling"] }
100 }
101
102 compiled <- [Tree to Program Native[tree, [backend]Supported Number Types]]Compile Program[backend]
103 { Print["Compiled program to backend"] }
104 outfname <- [compiled]Text Filename[fname]
105 outfile <- [File[outfname]]Truncate
106 [[compiled]Text]Write to File[outfile]
107 { Print[["Wrote output to "]Append[outfname]] }
108 }
109 }{}{}{
110 Print["You must provide a file name to compile"]
111 }
112 }
113