-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsingeli
executable file
·78 lines (69 loc) · 3.33 KB
/
singeli
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
#! /usr/bin/env bqn
help_pre ← 1↓"
Compile Singeli program(s).
Argument is a list of input files and options. Supported:"
short‿long‿args‿dup‿desc ← <˘⍉> ⟨
"h" ‿"help" ‿0‿1‿"Print this message and exit"
"o" ‿"out" ‿1‿0‿"Output file (print to stdout by default)"
"oe"‿"errout"‿1‿0‿"Error to: stderr (default), stdout, none, file=path, bqn"
"os"‿"show" ‿1‿0‿"show{} to: stdout (default), stderr, none, file=path, bqn"
# "?" ‿"stdin" ‿0‿0‿"Use stdin as input, after any argument files"
"r" ‿"run" ‿1‿1‿"Use this argument as source code"
"t" ‿"target"‿1‿0‿"Output type: c (default), cpp, ir"
"a" ‿"arch" ‿1‿2‿"Architecture features: list, or none, native (default), all"
"i" ‿"infer" ‿1‿0‿"Type of architecture inference: strict, or loose (default)"
"l" ‿"lib" ‿1‿2‿"Library paths: lib=path to try path/x for include 'lib/x'"
"c" ‿"config"‿1‿2‿"Configuration: name=value to set config name to value"
"p" ‿"pre" ‿1‿0‿"Preamble placed before C output"
"n" ‿"name" ‿1‿0‿"Prefix for names in C output"
"d" ‿"deplog"‿1‿0‿"Output file for log of included dependencies"
⟩
short‿long ∾˜¨⟜<¨↩ "-"‿"--"
args ∾↩ 0 ⋄ dup ∾↩ 1
Spl ← (⊢-˜+`׬)∘=⊔⊢
c ← ≠short
op ← (short⊸⊐ ⌊ long⊸⊐) •args
op ⌈↩ c ׬ <`⊸= op⊏args
opts ← ((1+c)∾˜f/op) ⊔ ((op=c)(1-˜×⟜(+`))○(∾⟜1)f←¬0»op⊏args) ⊔ •args
"Option can't be duplicated" ! ∧´ (1≤dup) ≥ 1<≠¨opts
olist ← (2=dup) (∾','⊸Spl¨)⍟⊣¨ (1⌾(¯1⊸⊑)args) ⊣◶⟨0<≠∘⊢,⊑¨⊢⟩¨ opts
help‿out‿oe‿os‿run‿target‿feats‿inf‿lib‿config‿pre‿namepre‿deplog‿files ← olist
{ help ?
opt_help ← ∾¨´ ⟨desc⟩ ∾˜ (1+·⌈´≠¨)⊸(↑¨)¨ short‿long ∾¨¨ ",:"
•Out ∾∾⟜(@+10)¨ ⟨help_pre,""⟩ ∾ opt_help
•Exit@
;@}
Rel ← •wdpath⊸•file.At
files Rel¨↩
deplog ↩ @ ⊣´ Rel¨deplog
SplitEq ← (»⊸(⊣-<)·∨`'='⊸=)⊸⊔
libpaths ← (Rel⌾(1⊸⊑) ¯2 ↑ SplitEq)¨ lib
configs ← (2 ↑ SplitEq)¨ config
_getShows ← {name _𝕣: ⟨@,⊢⟩ (⊢∾≠⊸↓˜) {
"stderr": •term.ErrRaw•ToUTF8∾(@+10)˙ ; "stdout":•Out ; "none":⊢ ;
e←"" ⋄ Out←{e∾↩𝕩∾@+10} ⋄ write←{
"bqn": {𝕊:e} ;
(p←"file=")(⊣≡≠⊸↑)𝕩? f←Rel p≠⊸↓𝕩 ⋄ {f •file.Chars e ⋄ 𝕩} ;
!"Unknown "∾name∾" output option: "∾𝕩
}𝕩
⟨Out, Write⟩
}}
⟨ShowOut,ShowWrite⟩ ← "show{}" _getShows "stdout" ⊣´ os
ToErrs ← {oe𝕊O‿E: {
"bqn"≡oe ? showWrite{𝕏∘1}↩ ⋄ ⟨O, !E, {𝔽⎊E}⟩ ;
⟨O, •Exit∘1 ⊣ ShowWrite∘E, {𝔽}⟩
}}
⟨ErrOut,ErrExit,_withErr⟩ ← ToErrs⟜("error" _getShows) "stderr" ⊣´ oe
arch ← ⟨feats,⟨"strict"⟩≢inf⟩ •Import "arch.bqn"
outputs ← ShowOut‿ErrOut‿ErrExit
frontend ← arch‿libpaths‿configs‿outputs •Import "singeli.bqn"
backend ← {
⟨"ir"⟩≡target ? ⊢ ;
par ← ⟨⟨"cpp"⟩≡target,arch,"si"⊣´namepre⟩
pre ⊑⊸{𝕨⊸𝕏}⍟(0<≠∘⊣) par •Import "emit_c.bqn"
}
output ← {
≠out ? (Rel⊑out) •file.Chars ⊢ ;
•Out⍟(0<≠) ¯1⊸↓
}
{ShowWrite Output Backend ∾ deplog⊸Frontend¨ 𝕩}_withErr (<¨run) ∾ files