added test for converting AST to postfix
Some checks are pending
CI / Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} (x64, ubuntu-latest, 1.10) (push) Waiting to run
CI / Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} (x64, ubuntu-latest, 1.6) (push) Waiting to run
CI / Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} (x64, ubuntu-latest, pre) (push) Waiting to run

This commit is contained in:
Daniel 2024-07-12 15:27:13 +02:00
parent 26741adec7
commit 59b1d77ad6
2 changed files with 22 additions and 10 deletions

View File

@ -7,20 +7,27 @@ export SymbolTable64
const SymbolTable64 = Dict{Tuple{Expr, Symbol},Float64}
# Maybe switch from Array{String} to Array{Union{Float64, String}}?
function expr_to_postfix(expr::Expr)::Array{String}
postfix = Array{String,1}()
# Maybe switch from Array{String} to Array{Union{Float64, Symbol}}?
function expr_to_postfix(expr::Expr)::Array{Union{Float64,String}}
postfix = Array{Union{Float64,String},1}()
operator = String(expr.args[1])
push!(postfix, expr.args[2], expr.args[3], operator)
for i in 4:length(expr.args)
# push!(postfix, expr.args[2], expr.args[3], operator)
for i in 2:length(expr.args)
arg = expr.args[i]
if typeof(arg) === Expr
push!(postfix, expr_to_postfix(ex))
append!(postfix, expr_to_postfix(arg))
elseif typeof(arg) === Symbol
push!(postfix, String(arg))
else
push!(postfix, string(arg), operator)
push!(postfix, convert(Float64, arg))
end
if length(postfix) >= 2
push!(postfix, operator)
end
end
return postfix
end
"Replaces all the variables and parameters of the given expression with their corresponding value stored in the symtable
@ -34,7 +41,6 @@ function replace_variables!(ex::Expr, symtable::SymbolTable64, originalExpr::Exp
if typeof(arg) === Expr
replace_variables!(arg, symtable, originalExpr)
elseif haskey(symtable, (originalExpr,arg)) # We found a variable/parameter and can replace it with the actual value
println("Replacing")
ex.args[i] = symtable[(originalExpr,arg)]
end
end

View File

@ -22,10 +22,16 @@ end
@testset "Test variable replacement in expression" begin
symtable = ExpressionExecutorCuda.ExpressionProcessing.construct_symtable(expressions, variables, parameters)
println(keys(symtable))
exprCopy = deepcopy(expressions[1])
ExpressionExecutorCuda.ExpressionProcessing.replace_variables!(exprCopy, symtable, expressions[1])
dump(exprCopy)
@test eval(exprCopy) == 10 # If it can be evaluated, it means all variables have been replaced, as there are no globally defined variables
end
@testset "Test conversion to postfix" begin
reference = Array{Union{Float64,String},1}()
append!(reference, ["x1", 1.0, "x2", "*", "+", "p1", "+"])
postfix = ExpressionExecutorCuda.ExpressionProcessing.expr_to_postfix(expressions[1])
@test isequal(reference, postfix)
end