added block_iterator and hash functions to default imports
[antimony.git] / transmute / transmute.sb
blob003fff7f86af213e6d4e7606d737cd80c3e75e23
1 # Antimony compiler.
3 section functions
5 export main
6 import sb.args sb.blob_from_cstring sb.compile_executable \
7   sb.compile_executable_to_voodoo sb.compile_module \
8   sb.compile_module_to_voodoo sb.init_voodoo_translator sb.transmute_stream
10 function namespace_from_path path base_path {
11   var namespace = -1
12   if (ne base_path -1) {
13     set base_path concatenate_blobs base_path "/"
14     if (blob_starts_with path base_path) {
15       let prefix_len blob_length base_path
16       set path copy_blob_part path prefix_len (sub (blob_length path) prefix_len)
17     }
18   }
19   loop {
20     let path_len blob_length path
21     var start = 0
22     var dot = 0
23     var end = 0
24     var got_part = false
25     do
26     set got_part false
27     if (eq end path_len) {
28       set got_part true
29     } else {
30       let c blob_nth path end
31       if (eq c 47) { # /
32         set got_part true
33       } else if (eq c 46) { # .
34         if (eq dot 0) {
35           set dot end
36         }
37       }
38     }
39     if (eq got_part true) {
40       if (eq dot 0) {
41         set dot end
42       }
43       var len = sub dot start
44       if (gt len 0) {
45         let part copy_blob_part path start len
46         if (eq namespace -1) {
47           set namespace get_namespace_absolute part
48         } else {
49           set namespace get_namespace (namespace_intern namespace part)
50         }
51       }
52       set start add end 1
53       set dot 0
54     }
55     while (lt end path_len)
56     set end add end 1
57   }
58   return namespace
61 function main args {
62   var base_path = -1
63   var format = -1
64   var namespace = -1
65   var source = -1
66   var target = -1
67   var type = "module"
68   var library_path = dynarray 0 0
69   loop {
70     var i = 1
71     var arg = 0
72     var len = array_length args
73     do
74     while (lt i len)
75     set arg array_nth args i
76     if (gt (blob_length arg) 0) {
77       var c = blob_nth arg 0
78       if (blobs_equal arg "-L") {
79         set i add i 1
80         dynarray_add library_path (array_nth args i)
81       } else if (blobs_equal arg "-b") {
82         set i add i 1
83         set base_path (array_nth args i)
84       } else if (blobs_equal arg "-f") {
85         set i add i 1
86         set format (array_nth args i)
87       } else if (blobs_equal arg "-o") {
88         set i add i 1
89         set target (array_nth args i)
90       } else if (blobs_equal arg "-t") {
91         set i add i 1
92         set type (array_nth args i)
93       } else if (eq c 45) {             # -
94         error (concatenate_blobs "Flag not recognized: " arg)
95       } else {
96         set source arg
97       }
98     }
99     set i add i 1
100   }
102   if (eq source -1) {
103     if (eq target -1) {
104       # Old behavior, used to bootstrap Antimony.
105       set namespace symbol_namespace #`true
106       sb.transmute_stream namespace standard_input standard_output
107     } else {
108       error "Target specified, but no source"
109     }
110   } else if (eq target -1) {
111     error "Source specified, but no target"
112   } else if (blobs_equal type "exe") {
113     set namespace (namespace_from_path source base_path)
114     if (eq format -1) {
115       sb.compile_executable namespace source target (dynarray_to_array library_path)
116     } else if (blobs_equal format "voodoo") {
117       mkdir target 511
118       sb.compile_executable_to_voodoo namespace source target
119     } else {
120       error (concatenate_blobs "Format not supported: " format)
121     }
122   } else if (blobs_equal type "module") {
123     set namespace (namespace_from_path source base_path)
124     if (eq format -1) {
125       sb.compile_module namespace source target
126     } else if (blobs_equal format "voodoo") {
127       mkdir target 511
128       sb.compile_module_to_voodoo namespace source target
129     } else {
130       error (concatenate_blobs "Format not supported: " format)
131     }
132   } else {
133     error (concatenate_blobs "Format not supported: " format)
134   }
136   return 0
139 sb.init_voodoo_translator
140 main (sb.args)