Created
April 6, 2018 20:21
-
-
Save ebb/d6784a47f94f4e0c6b31a09b1c3d95e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Block | |
Let config | |
Let {} | |
Let usage "usage: 84 <program>" | |
In | |
When [OS.argc != 2] | |
(OS.die usage) | |
End | |
In | |
{: root_path (OS.argv 1)} | |
In | |
Let packages | |
Let QUEUE PACKAGE.QUEUE | |
In | |
Iterate {queue packages} From {(QUEUE.new config.root_path) 'nil} | |
Match (QUEUE.pop queue) | |
| 'nothing packages | |
| 'just.{path queue} | |
Let file_name (STRING.append path ".84") | |
In | |
Let text (FILE.read_all file_name) | |
In | |
Match (PARSE.file text) | |
| 'succeed.expr | |
Let imports (PACKAGE.gather_imports expr) | |
In | |
Let package {: path : imports : expr} | |
Let queue (QUEUE.push_all queue imports) | |
In | |
(Continue queue [package & packages]) | |
| 'fail.{message i} | |
Begin | |
(SYNTAX_ERROR.show file_name text message i) | |
(OS.die "Compilation failed.") | |
End | |
; | |
; | |
In | |
Let packages | |
Match (PACKAGE.sort packages) | |
| 'succeed.packages packages | |
| 'fail.component | |
Begin | |
(STDIO.print "Cyclic package dependencies: ") | |
(STDIO.print (STRING.join ", " component)) | |
(STDIO.print_line ".") | |
(OS.die "Compilation failed.") | |
End | |
; | |
In | |
Let program | |
Let exprs (LIST.map packages [Func package package.expr]) | |
In | |
Let exprs | |
(LIST.map exprs | |
[COMPILE.elaborate_recursion >> COMPILE.collect_free_variables]) | |
In | |
Let modules (COMPILE.lift_functions exprs) | |
In | |
Let packages | |
(LIST.map (LIST.zip packages modules) | |
Func {package {init functions}} | |
{ | |
: path package.path | |
: imports package.imports | |
: init | |
: functions | |
}) | |
In | |
Let program (COMPILE.collect_constants packages) | |
In | |
Let program (C.elaborate program) | |
In | |
program | |
In | |
Let {c_emitter d_emitter} (C.make_emitters config.root_path program) | |
In | |
Begin | |
(FILE.with_output_file | |
(STRING.append config.root_path ".c") | |
Func fd (BUFIO.with_writer fd [1024 * 1024] c_emitter)) | |
(FILE.with_output_file | |
(STRING.append config.root_path ".c.d") | |
Func fd (BUFIO.with_writer fd 1024 d_emitter)) | |
End | |
Where | |
Let BUFIO Package "bufio" | |
Let C Package "c" | |
Let COMPILE Package "compile" | |
Let FILE Package "file" | |
Let LIST Package "list" | |
Let OS Package "os" | |
Let PACKAGE Package "package" | |
Let PARSE Package "parse" | |
Let STDIO Package "stdio" | |
Let STRING Package "string" | |
Let SYNTAX_ERROR Package "syntax_error" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment