Skip to content

Commit 78f5b56

Browse files
committed
futamura support code
1 parent 2751548 commit 78f5b56

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

examples/futamura/Manifest.toml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
[[Base64]]
4+
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
5+
6+
[[BenchmarkTools]]
7+
deps = ["JSON", "Logging", "Printf", "Statistics", "UUIDs"]
8+
git-tree-sha1 = "9e62e66db34540a0c919d72172cc2f642ac71260"
9+
uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
10+
version = "0.5.0"
11+
12+
[[Dates]]
13+
deps = ["Printf"]
14+
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
15+
16+
[[Distributed]]
17+
deps = ["Random", "Serialization", "Sockets"]
18+
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
19+
20+
[[IRTools]]
21+
deps = ["InteractiveUtils", "MacroTools", "Test"]
22+
git-tree-sha1 = "5b2ef472bd7001923ea0d703583865ade78c80a3"
23+
repo-rev = "master"
24+
repo-url = "https://github.com/MikeInnes/IRTools.jl.git"
25+
uuid = "7869d1d1-7146-5819-86e3-90919afe41df"
26+
version = "0.4.0"
27+
28+
[[InteractiveUtils]]
29+
deps = ["Markdown"]
30+
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
31+
32+
[[JSON]]
33+
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
34+
git-tree-sha1 = "b34d7cef7b337321e97d22242c3c2b91f476748e"
35+
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
36+
version = "0.21.0"
37+
38+
[[Libdl]]
39+
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
40+
41+
[[LinearAlgebra]]
42+
deps = ["Libdl"]
43+
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
44+
45+
[[Logging]]
46+
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
47+
48+
[[MacroTools]]
49+
deps = ["Markdown", "Random"]
50+
git-tree-sha1 = "f7d2e3f654af75f01ec49be82c231c382214223a"
51+
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
52+
version = "0.5.5"
53+
54+
[[Markdown]]
55+
deps = ["Base64"]
56+
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
57+
58+
[[Mjolnir]]
59+
deps = ["IRTools", "MacroTools"]
60+
git-tree-sha1 = "0a21c7c39ee65b885b3e7dff4a7586532f6985a6"
61+
repo-rev = "master"
62+
repo-url = "https://github.com/MikeInnes/Mjolnir.jl.git"
63+
uuid = "1154507a-7ac2-44fe-9f15-34617bca9db5"
64+
version = "0.1.0"
65+
66+
[[Mmap]]
67+
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
68+
69+
[[Parsers]]
70+
deps = ["Dates", "Test"]
71+
git-tree-sha1 = "eb3e09940c0d7ae01b01d9291ebad7b081c844d3"
72+
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
73+
version = "1.0.5"
74+
75+
[[Printf]]
76+
deps = ["Unicode"]
77+
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
78+
79+
[[Random]]
80+
deps = ["Serialization"]
81+
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
82+
83+
[[SHA]]
84+
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
85+
86+
[[Serialization]]
87+
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
88+
89+
[[Sockets]]
90+
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
91+
92+
[[SparseArrays]]
93+
deps = ["LinearAlgebra", "Random"]
94+
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
95+
96+
[[Statistics]]
97+
deps = ["LinearAlgebra", "SparseArrays"]
98+
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
99+
100+
[[Test]]
101+
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
102+
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
103+
104+
[[UUIDs]]
105+
deps = ["Random", "SHA"]
106+
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
107+
108+
[[Unicode]]
109+
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

examples/futamura/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3+
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df"
4+
Mjolnir = "1154507a-7ac2-44fe-9f15-34617bca9db5"

examples/futamura/brainfuck.jl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
@enum BFInstruction inc dec left right accept output debug
2+
3+
struct Loop
4+
body::Vector{Union{Loop,BFInstruction}}
5+
end
6+
7+
function parsechar(s, i)
8+
ch = s[i]
9+
ch == '+' ? (i+1, inc) :
10+
ch == '-' ? (i+1, dec) :
11+
ch == '>' ? (i+1, right) :
12+
ch == '<' ? (i+1, left) :
13+
ch == '.' ? (i+1, output) :
14+
ch == ',' ? (i+1, accept) :
15+
ch == '#' ? (i+1, debug) :
16+
ch == '[' ? parseloop(s, i) :
17+
(i+1, nothing)
18+
end
19+
20+
function parseloop(s, i)
21+
body = Union{Loop,BFInstruction}[]
22+
i += 1
23+
while true
24+
s[i] == ']' && break
25+
i, op = parsechar(s, i)
26+
op === nothing || push!(body, op)
27+
end
28+
return i+1, Loop(body)
29+
end
30+
31+
function bfparse(s, i = 1)
32+
is = Union{Loop,BFInstruction}[]
33+
while i <= length(s)
34+
i, op = parsechar(s, i)
35+
op === nothing || push!(is, op)
36+
end
37+
return is
38+
end
39+
40+
function debugtape(tape, ptr)
41+
for i = 1:length(tape)
42+
print(tape[i])
43+
i == ptr && print(*)
44+
print(" ")
45+
end
46+
println()
47+
end
48+
49+
function interploop(tape, loop, ptr)
50+
while tape[ptr] != 0
51+
ptr = interp(tape, loop.body, ptr)
52+
end
53+
return ptr
54+
end
55+
56+
function interp(tape, ops, ptr)
57+
for op in ops
58+
if op === inc
59+
tape[ptr] += 1
60+
elseif op === dec
61+
tape[ptr] -= 1
62+
elseif op === left
63+
ptr -= 1
64+
elseif op === right
65+
ptr += 1
66+
elseif op === debug
67+
debugtape(tape, ptr)
68+
elseif op isa Loop
69+
ptr = interploop(tape, op, ptr)
70+
end
71+
end
72+
return ptr
73+
end
74+
75+
function interpret(ops, tape = zeros(UInt32, 2^16))
76+
interp(tape, ops, 1)
77+
return tape
78+
end
79+
80+
interpret(ops::String, args...) = interpret(bfparse(ops, 1), args...)
81+
82+
mutable struct NVector{T,S} <: AbstractVector{T}
83+
data::NTuple{S,T}
84+
end
85+
86+
Base.size(xs::NVector) = (length(xs.data),)
87+
Base.getindex(xs::NVector, i::Integer) = xs.data[i]
88+
Base.setindex!(xs::NVector, v, i::Integer) =
89+
xs.data = ntuple(j -> j == i ? v : xs.data[j], length(xs.data))

0 commit comments

Comments
 (0)