Skip to content

Commit

Permalink
change parsing to make tensor macros more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Dec 19, 2019
1 parent c61b503 commit 4cc4a06
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/indexnotation/analyzers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function getinputtensorobjects(ex)
end
append!(list, list2)
end
elseif isa(ex, Expr) && ex.head in (:for, :function)
elseif isa(ex, Expr) && ex.head in (:for, :while)
append!(list, getinputtensorobjects(ex.args[2]))
elseif isa(ex, Expr) && ex.head == :call && ex.args[1] == :scalar
append!(list, gettensorobjects(ex.args[2]))
Expand All @@ -142,7 +142,7 @@ function getoutputtensorobjects(ex)
end
append!(list, list2)
end
elseif isa(ex, Expr) && ex.head in (:for, :function)
elseif isa(ex, Expr) && ex.head in (:for, :while)
append!(list, getoutputtensorobjects(ex.args[2]))
end
return unique!(list)
Expand All @@ -159,7 +159,7 @@ function getnewtensorobjects(ex)
for e in ex.args
append!(list, getnewtensorobjects(e))
end
elseif isa(ex, Expr) && ex.head in (:for, :function)
elseif isa(ex, Expr) && ex.head in (:for, :while)
append!(list, getnewtensorobjects(ex.args[2]))
end
return unique!(list)
Expand Down
3 changes: 3 additions & 0 deletions src/indexnotation/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ mutable struct TensorParser
end

function (parser::TensorParser)(ex)
if ex isa Expr && ex.head == :function
return Expr(:function, ex.args[1], parser(ex.args[2]))
end
for p in parser.preprocessors
ex = p(ex)
end
Expand Down
5 changes: 4 additions & 1 deletion src/indexnotation/preprocessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ end

# replace all tensor objects by a function of that object
function replacetensorobjects(f, ex::Expr)
# first try to replace ex completely
ex2 = f(ex, nothing, nothing)
ex2 !== ex && return ex2
if istensor(ex)
obj, leftind, rightind = decomposetensor(ex)
return Expr(ex.head, f(obj, leftind, rightind), ex.args[2:end]...)
else
return Expr(ex.head, (replacetensorobjects(f, e) for e in ex.args)...)
end
end
replacetensorobjects(f, ex) = ex
replacetensorobjects(f, ex) = f(ex, nothing, nothing)

# expandconj: conjugate individual terms or factors instead of a whole expression
function expandconj(ex::Expr)
Expand Down

2 comments on commit 4cc4a06

@Jutho
Copy link
Owner Author

@Jutho Jutho commented on 4cc4a06 Dec 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/7034

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v2.1.1 -m "<description of version>" 4cc4a066cc623ab5342f2076fe09de4bea2324b1
git push origin v2.1.1

Please sign in to comment.