Skip to content

Commit 3b91ea4

Browse files
committed
Adding the backbone of juilia expresisons.
1 parent 1575dbb commit 3b91ea4

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/MathLink.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ include("eval.jl")
3030
include("display.jl")
3131
include("operators.jl")
3232
include("W2JuliaStruct.jl")
33+
include("W2JuliaExpr.jl")
3334
end

src/W2JuliaExpr.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
export W2JuliaExpr
4+
5+
"""
6+
Converts MathLink `WExpr`essions to Julia `Expr`essions
7+
"""
8+
function W2JuliaExpr(wexpr::WExpr)
9+
####Check if the operator has an alias
10+
if haskey(funcDict, wexpr.head.name)
11+
Operator=funcDict[wexpr.head.name]
12+
else
13+
Operator=Symbol(wexpr.head.name)
14+
end
15+
return Expr(:call,Operator,[W2JuliaExpr(arg) for arg in wexpr.args]...)
16+
end
17+
W2JuliaExpr(wexpr::WSymbol) = Symbol(wexpr.name)
18+
W2JuliaExpr(wexpr::Number) = wexpr
19+
20+
21+
###Dictionary with known funcitons and their translations
22+
funcDict=Dict("Plus"=>:+,
23+
"Minus"=>:-,
24+
"Power"=>:^,
25+
"Times"=>:*,
26+
"Sin"=>:sin,
27+
"Cos"=>:cos,
28+
)

test/runtests.jl

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

66

7+
@testset "W2JuliaExpr" begin
8+
###Test of a simple MathLink to Julia converter. It converts MathLink expressions to the correcsponding Julia constructions
9+
@testset "Variables" begin
10+
@test W2JuliaExpr(W"a") == :a
11+
@test W2JuliaExpr(W`a`) == :a
12+
@test W2JuliaExpr(W"a"+W"b") == :(a+b)
13+
@test W2JuliaExpr(W`sin`) == :sin
14+
@test W2JuliaExpr(W`a+b`) == :(a+b)
15+
@test W2JuliaExpr(W`a*b`) == :(a*b)
16+
@test W2JuliaExpr(W`Sin[a]`) == :(sin(a))
17+
@test W2JuliaExpr(W`Sin[a+b]`) == :(sin(a+b))
18+
@test W2JuliaExpr(W`Cos[a^b]`) == :(cos(a^b))
19+
@test W2JuliaExpr(W`a/b`) == :(a*(b^-1))
20+
@test W2JuliaExpr(W`a^b`) == :(a^b)
21+
end
22+
end
23+
24+
25+
726
@testset "W2JuliaStruct" begin
827
###Test of a simple MathLink to Julia converter. It converts MathLink expressions to the correcsponding Julia constructions
928
@testset "Lists to Lists" begin

0 commit comments

Comments
 (0)