Skip to content

Commit b1df6a7

Browse files
authored
Merge pull request #74 from fremling/master
Added empryo of W2Julia : converter from MathLink to julia code.
2 parents 3051156 + 45da031 commit b1df6a7

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/MathLink.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ include("extras.jl")
2929
include("eval.jl")
3030
include("display.jl")
3131
include("operators.jl")
32-
32+
include("W2Julia.jl")
3333
end

src/W2Julia.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
3+
export W2Julia
4+
5+
W2Julia(X::Number) = X
6+
W2Julia(X::MathLink.WSymbol) = X
7+
function W2Julia(X::MathLink.WExpr)
8+
if X.head == W"List"
9+
return [W2Julia(x) for x in X.args]
10+
elseif X.head == W"Association"
11+
W2JuliaAccociation(X)
12+
else
13+
return X
14+
end
15+
end
16+
17+
18+
function W2JuliaAccociation(Asso::MathLink.WExpr)
19+
if Asso.head != W"Association"
20+
error("Not an Association")
21+
end
22+
##Wprint(Asso)
23+
if Asso.head != W"Association"
24+
error("Not an association")
25+
end
26+
D=Dict()
27+
for Rule in Asso.args
28+
if Rule.head != W"Rule"
29+
error("not a rule")
30+
end
31+
if length(Rule.args) != 2
32+
error("Bad rule")
33+
end
34+
D[Rule.args[1]]=Rule.args[2]
35+
end
36+
return D
37+
end

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ using Test
44
import MathLink: WExpr, WSymbol
55

66

7+
@testset "W2Julia" begin
8+
###Test of a simple MathLink to Julia converter. It converts MathLink expressions to the correcsponding Julia constructions
9+
10+
@testset "Lists to Lists" begin
11+
@test W2Julia(W`{1,2,3}`) == [1,2,3]
12+
@test W2Julia(W`{1,a,3}`) == [1,W"a",3]
13+
@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
14+
end
15+
@testset "Association to Dict" begin
16+
@test W2Julia(W`Association["team" -> "HOU", "lastName" -> "Ching"]`) == Dict( "team" => "HOU" , "lastName" => "Ching")
17+
end
18+
end
19+
720

821
@testset "W2Mstr" begin
922
###Test of a naive MathLink to Mathematica converter function (the results can be copied into mathematica directly)

0 commit comments

Comments
 (0)